2
0
Эх сурвалжийг харах

backend handler (unimpl) and create cluster basic integration

Justin Rhee 3 жил өмнө
parent
commit
e48a80815a

+ 35 - 0
api/server/handlers/project/create_cluster.go

@@ -0,0 +1,35 @@
+package project
+
+import (
+	"fmt"
+	"io"
+	"net/http"
+
+	"github.com/porter-dev/porter/api/server/handlers"
+	"github.com/porter-dev/porter/api/server/shared"
+	"github.com/porter-dev/porter/api/server/shared/config"
+)
+
+type CreateClusterHandler struct {
+	handlers.PorterHandlerReadWriter
+}
+
+func NewProvisionClusterHandler(
+	config *config.Config,
+	decoderValidator shared.RequestDecoderValidator,
+	writer shared.ResultWriter,
+) *CreateClusterHandler {
+	return &CreateClusterHandler{
+		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
+	}
+}
+
+// TODO: implement
+func (c *CreateClusterHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	bytes, err := io.ReadAll(r.Body)
+	if err != nil {
+		return
+	}
+	fmt.Println("Provisioning attempt received:")
+	fmt.Println(string(bytes))
+}

+ 28 - 0
api/server/router/project.go

@@ -1261,5 +1261,33 @@ func getProjectRoutes(
 		Router:   r,
 	})
 
+	// POST /api/project/{project_id}/provision/cluster -> project.NewProvisionClusterHandler
+	provisionClusterEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbCreate,
+			Method: types.HTTPVerbPost,
+			Path: &types.Path{
+				Parent:       basePath,
+				RelativePath: relPath + "/provision/cluster",
+			},
+			Scopes: []types.PermissionScope{
+				types.UserScope,
+				types.ProjectScope,
+			},
+		},
+	)
+
+	provisionClusterHandler := project.NewProvisionClusterHandler(
+		config,
+		factory.GetDecoderValidator(),
+		factory.GetResultWriter(),
+	)
+
+	routes = append(routes, &router.Route{
+		Endpoint: provisionClusterEndpoint,
+		Handler:  provisionClusterHandler,
+		Router:   r,
+	})
+
 	return routes, newPath
 }

+ 60 - 1
dashboard/src/components/ProvisionerForm.tsx

@@ -35,6 +35,12 @@ const regionOptions = [
   { value: "sa-east-1", label: "South America (São Paulo) sa-east-1" },
 ];
 
+const machineTypeOptions = [
+  { value: "t3.medium", label: "t3.medium" },
+  { value: "t3.xlarge", label: "t3.xlarge" },
+  { value: "t3.2xlarge", label: "t3.2xlarge" },
+];
+
 type Props = {
   goBack: () => void;
   credentialId: any;
@@ -48,6 +54,43 @@ const ProvisionerForm: React.FC<Props> = ({
   const [createStatus, setCreateStatus] = useState("");
   const [clusterName, setClusterName] = useState("");
   const [awsRegion, setAwsRegion] = useState("us-east-1");
+  const [machineType, setMachineType] = useState("t3.medium")
+
+  const createCluster = async () => {
+    try {
+      await api.provisionCluster(
+        "<token>",
+        {
+          project_id: currentProject.id,
+          cloud_provider: "aws",
+          cloud_provider_credentials_id: credentialId,
+          cluster_settings: {
+            cluster_name: clusterName,
+            cluster_version: "v1.24.0",
+            cidr_range: "172.0.0.0/16",
+            region: awsRegion,
+            node_groups: [
+              {
+                instance_type: "t3.medium",
+                min_instances: 1,
+                max_instances: 5,
+                node_group_type: 1
+              },
+              {
+                instance_type: machineType,
+                min_instances: 1,
+                max_instances: 10,
+                node_group_type: 3
+              }
+            ]
+          }
+        },
+        { project_id: currentProject.id }
+      );
+    } catch (err) {
+      console.log(err);
+    }
+  }
 
   return (
     <>
@@ -64,6 +107,7 @@ const ProvisionerForm: React.FC<Props> = ({
         Configure settings for your new cluster. 
       </Helper>
       <StyledForm>
+        <Heading isAtTop>EKS configuration</Heading>
         <InputRow
           width="350px"
           isRequired
@@ -82,7 +126,22 @@ const ProvisionerForm: React.FC<Props> = ({
           setActiveValue={setAwsRegion}
           label="📍 AWS Region"
         />
+        <SelectRow
+          options={machineTypeOptions}
+          width="350px"
+          value={machineType}
+          scrollBuffer={true}
+          dropdownMaxHeight="240px"
+          setActiveValue={setMachineType}
+          label="⚙️ Machine type"
+        />
       </StyledForm>
+      <SaveButton
+        disabled={!clusterName && true}
+        onClick={createCluster}
+        clearPosition
+        text="Provision"
+      />
     </>
   );
 };
@@ -128,7 +187,7 @@ const BackButton = styled.div`
 
 const StyledForm = styled.div`
   position: relative;
-  padding: 15px 30px 25px;
+  padding: 30px 30px 25px;
   border-radius: 5px;
   background: #26292e;
   border: 1px solid #494b4f;

+ 2 - 1
dashboard/src/main/home/cluster-dashboard/expanded-chart/ExpandedChart.tsx

@@ -575,7 +575,8 @@ const ExpandedChart: React.FC<Props> = (props) => {
       );
     }
 
-    if (currentChart?.git_action_config?.git_repo && !isStack) {
+    //if (currentChart?.git_action_config?.git_repo && !isStack) {
+    if (true) {
       rightTabOptions.push({
         label: "Build Settings",
         value: "build-settings",

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

@@ -854,6 +854,39 @@ const getInfraTemplate = baseApi<
   return `/api/projects/${project_id}/infras/templates/${name}/${version}`;
 });
 
+const provisionCluster = baseApi<
+  {
+    project_id: number,
+    cloud_provider: string,
+    cloud_provider_credentials_id: string,
+    cluster_settings: {
+      cluster_name: string,
+      cluster_version: string,
+      cidr_range: string,
+      region: string,
+      node_groups: [
+        {
+          instance_type: string,
+          min_instances: number,
+          max_instances: number,
+          node_group_type: number
+        },
+        {
+          instance_type: string,
+          min_instances: number,
+          max_instances: number,
+          node_group_type: number
+        }
+      ]
+    }
+  },
+  {
+    project_id: number;
+  }
+>("POST", ({ project_id }) => {
+  return `/api/projects/${project_id}/provision/cluster`;
+});
+
 const provisionInfra = baseApi<
   {
     kind: string;
@@ -2392,6 +2425,7 @@ export default {
   listInfraTemplates,
   getInfraTemplate,
   getInfra,
+  provisionCluster,
   provisionInfra,
   deleteInfra,
   updateInfra,