Преглед изворни кода

create namespace integration

sunguroku пре 5 година
родитељ
комит
57bcbc125c

+ 5 - 1
dashboard/src/components/Selector.tsx

@@ -4,6 +4,7 @@ import { Context } from "shared/Context";
 
 type PropsType = {
   activeValue: string;
+  refreshOptions?: () => void;
   options: { value: string; label: string }[];
   addButton?: boolean;
   setActiveValue: (x: string) => void;
@@ -128,7 +129,10 @@ export default class Selector extends Component<PropsType, StateType> {
       <StyledSelector width={this.props.width}>
         <MainSelector
           ref={this.parentRef}
-          onClick={() => this.setState({ expanded: !this.state.expanded })}
+          onClick={() => {
+            this.props.refreshOptions();
+            this.setState({ expanded: !this.state.expanded })
+          }}
           expanded={this.state.expanded}
           width={this.props.width}
           height={this.props.height}

+ 1 - 0
dashboard/src/main/home/launch/launch-flow/SettingsPage.tsx

@@ -253,6 +253,7 @@ export default class SettingsPage extends Component<PropsType, StateType> {
             </NamespaceLabel>
             <Selector
               key={"namespace"}
+              refreshOptions={() => {this.updateNamespaces(this.context.currentCluster.id)}}
               addButton={true}
               activeValue={selectedNamespace}
               setActiveValue={setSelectedNamespace}

+ 26 - 1
dashboard/src/main/home/modals/NamespaceModal.tsx

@@ -9,6 +9,7 @@ import SaveButton from "components/SaveButton";
 import InputRow from "components/values-form/InputRow";
 
 type PropsType = {};
+
 type StateType = {
     namespaceName: string;
     status: string | null; 
@@ -21,6 +22,30 @@ export default class NamespaceModal extends Component<PropsType, StateType> {
     status: null as string | null,
   }
 
+  createNamespace = () => {
+    api
+    .createNamespace(
+      "<token>",
+      {
+        name: this.state.namespaceName,
+      },
+      {
+        id: this.context.currentProject.id,
+        cluster_id: this.context.currentCluster.id,
+      }
+    )
+    .then((res) => {
+      this.setState({ status: "successful" }, () => {
+        setTimeout(() => {     
+          this.context.setCurrentModal(null, null) 
+        }, 1000);
+      });
+    })
+    .catch((err) => {
+      this.setState({ status: "Could not create" });
+    });
+  }
+
   render() {
     return (
       <StyledUpdateProjectModal>
@@ -58,7 +83,7 @@ export default class NamespaceModal extends Component<PropsType, StateType> {
         <SaveButton
           text="Create Namespace"
           color="#616FEEcc"
-          onClick={() => console.log('ok')}
+          onClick={() => this.createNamespace()}
           status={this.state.status}
         />
       </StyledUpdateProjectModal>

+ 23 - 0
dashboard/src/shared/api.tsx

@@ -798,6 +798,27 @@ const deleteConfigMap = baseApi<
   return `/api/projects/${pathParams.id}/k8s/configmap/delete`;
 });
 
+const createNamespace = baseApi<
+  {
+    name: string;
+  },
+  { id: number; cluster_id: number;  }
+>("POST", (pathParams) => {
+  let { id, cluster_id } = pathParams;
+  return `/api/projects/${id}/k8s/namespaces/create?cluster_id=${cluster_id}`;
+});
+
+const deleteNamespace = baseApi<
+  {
+    name: string;
+    cluster_id: number;
+  },
+  { id: number;}
+>("DELETE", (pathParams) => {
+  let { id } = pathParams;
+  return `/api/projects/${id}/k8s/namespaces/delete`;
+});
+
 const stopJob = baseApi<
   {},
   { name: string; namespace: string; id: number; cluster_id: number }
@@ -820,6 +841,7 @@ export default {
   createGHAction,
   createGKE,
   createInvite,
+  createNamespace,
   createPasswordReset,
   createPasswordResetVerify,
   createPasswordResetFinalize,
@@ -829,6 +851,7 @@ export default {
   deleteConfigMap,
   deleteGitRepoIntegration,
   deleteInvite,
+  deleteNamespace,
   deletePod,
   deleteProject,
   deleteRegistryIntegration,

+ 4 - 0
internal/forms/k8s.go

@@ -44,3 +44,7 @@ type ConfigMapForm struct {
 	EnvVariables       map[string]string `json:"variables"`
 	SecretEnvVariables map[string]string `json:"secret_variables"`
 }
+
+type NamespaceForm struct {
+	Name string `json:"name" form:"required"`
+}

+ 17 - 2
server/api/k8s_handler.go

@@ -78,6 +78,7 @@ func (app *App) HandleListNamespaces(w http.ResponseWriter, r *http.Request) {
 // HandleCreateNamespace creates a new namespace given the name.
 func (app *App) HandleCreateNamespace(w http.ResponseWriter, r *http.Request) {
 	vals, err := url.ParseQuery(r.URL.RawQuery)
+	fmt.Println(vals)
 	if err != nil {
 		app.handleErrorFormDecoding(err, ErrReleaseDecode, w)
 		return
@@ -108,7 +109,14 @@ func (app *App) HandleCreateNamespace(w http.ResponseWriter, r *http.Request) {
 		agent, err = kubernetes.GetAgentOutOfClusterConfig(form.OutOfClusterConfig)
 	}
 
-	namespace, err := agent.CreateNamespace(vals["name"][0])
+	ns := &forms.NamespaceForm{}
+
+	if err := json.NewDecoder(r.Body).Decode(ns); err != nil {
+		app.handleErrorFormDecoding(err, ErrUserDecode, w)
+		return
+	}
+
+	namespace, err := agent.CreateNamespace(ns.Name)
 
 	if err != nil {
 		app.handleErrorInternal(err, w)
@@ -162,7 +170,14 @@ func (app *App) HandleDeleteNamespace(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	err = agent.DeleteNamespace(vals["name"][0])
+	namespace := &forms.NamespaceForm{}
+
+	if err := json.NewDecoder(r.Body).Decode(namespace); err != nil {
+		app.handleErrorFormDecoding(err, ErrUserDecode, w)
+		return
+	}
+
+	err = agent.DeleteNamespace(namespace.Name)
 
 	if err != nil {
 		app.handleErrorInternal(err, w)

+ 1 - 1
server/router/router.go

@@ -1086,7 +1086,7 @@ func New(a *api.App) *chi.Mux {
 			)
 
 			r.Method(
-				"POST",
+				"DELETE",
 				"/projects/{project_id}/k8s/namespaces/delete",
 				auth.DoesUserHaveProjectAccess(
 					auth.DoesUserHaveClusterAccess(