Alexander Belanger 4 лет назад
Родитель
Сommit
3b88870b20

+ 74 - 0
api/server/handlers/release/get_controllers.go

@@ -0,0 +1,74 @@
+package release
+
+import (
+	"net/http"
+
+	"github.com/porter-dev/porter/api/server/authz"
+	"github.com/porter-dev/porter/api/server/handlers"
+	"github.com/porter-dev/porter/api/server/shared"
+	"github.com/porter-dev/porter/api/server/shared/apierrors"
+	"github.com/porter-dev/porter/api/server/shared/config"
+	"github.com/porter-dev/porter/api/types"
+	"github.com/porter-dev/porter/internal/helm/grapher"
+	"github.com/porter-dev/porter/internal/models"
+	"helm.sh/helm/v3/pkg/release"
+)
+
+type GetControllersHandler struct {
+	handlers.PorterHandlerReadWriter
+	authz.KubernetesAgentGetter
+}
+
+func NewGetControllersHandler(
+	config *config.Config,
+	writer shared.ResultWriter,
+) *GetControllersHandler {
+	return &GetControllersHandler{
+		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
+		KubernetesAgentGetter:   authz.NewOutOfClusterAgentGetter(config),
+	}
+}
+
+func (c *GetControllersHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	helmRelease, _ := r.Context().Value(types.ReleaseScope).(*release.Release)
+	cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
+
+	agent, err := c.GetAgent(r, cluster)
+
+	if err != nil {
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	yamlArr := grapher.ImportMultiDocYAML([]byte(helmRelease.Manifest))
+	controllers := grapher.ParseControllers(yamlArr)
+	retrievedControllers := []interface{}{}
+
+	// get current status of each controller
+	for _, controller := range controllers {
+		controller.Namespace = helmRelease.Namespace
+		var rc interface{}
+
+		switch controller.Kind {
+		case "Deployment":
+			rc, err = agent.GetDeployment(controller)
+		case "StatefulSet":
+			rc, err = agent.GetStatefulSet(controller)
+		case "DaemonSet":
+			rc, err = agent.GetDaemonSet(controller)
+		case "ReplicaSet":
+			rc, err = agent.GetReplicaSet(controller)
+		case "CronJob":
+			rc, err = agent.GetCronJob(controller)
+		}
+
+		if err != nil {
+			c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+			return
+		}
+
+		retrievedControllers = append(retrievedControllers, rc)
+	}
+
+	c.WriteResult(w, r, retrievedControllers)
+}

+ 30 - 0
api/server/router/release.go

@@ -82,5 +82,35 @@ func getReleaseRoutes(
 		Router:   r,
 	})
 
+	// GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version}/controllers -> release.NewGetControllersHandler
+	getControllersEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbGet,
+			Method: types.HTTPVerbGet,
+			Path: &types.Path{
+				Parent:       basePath,
+				RelativePath: relPath + "/controllers",
+			},
+			Scopes: []types.PermissionScope{
+				types.UserScope,
+				types.ProjectScope,
+				types.ClusterScope,
+				types.NamespaceScope,
+				types.ReleaseScope,
+			},
+		},
+	)
+
+	getControllersHandler := release.NewGetControllersHandler(
+		config,
+		factory.GetResultWriter(),
+	)
+
+	routes = append(routes, &Route{
+		Endpoint: getControllersEndpoint,
+		Handler:  getControllersHandler,
+		Router:   r,
+	})
+
 	return routes, newPath
 }

+ 3 - 5
dashboard/src/main/home/cluster-dashboard/chart/Chart.tsx

@@ -55,15 +55,13 @@ const Chart: React.FunctionComponent<Props> = ({
       const { currentCluster, currentProject } = context;
       const res = await api.getChartControllers(
         "<token>",
+        {},
         {
+          name: chart.name,
           namespace: chart.namespace,
           cluster_id: currentCluster.id,
-          storage: StorageType.Secret,
-        },
-        {
-          id: currentProject.id,
-          name: chart.name,
           revision: chart.version,
+          id: currentProject.id,
         }
       );
 

+ 5 - 9
dashboard/src/main/home/cluster-dashboard/expanded-chart/ExpandedChart.tsx

@@ -99,13 +99,11 @@ const ExpandedChart: React.FC<Props> = (props) => {
     setIsLoadingChartData(true);
     const res = await api.getChart(
       "<token>",
+      {},
       {
+        name: chart.name,
         namespace: chart.namespace,
         cluster_id: currentCluster.id,
-        storage: StorageType.Secret,
-      },
-      {
-        name: chart.name,
         revision: chart.version,
         id: currentProject.id,
       }
@@ -136,15 +134,13 @@ const ExpandedChart: React.FC<Props> = (props) => {
     try {
       const { data: chartControllers } = await api.getChartControllers(
         "<token>",
+        {},
         {
+          name: chart.name,
           namespace: chart.namespace,
           cluster_id: currentCluster.id,
-          storage: StorageType.Secret,
-        },
-        {
-          id: currentProject.id,
-          name: chart.name,
           revision: chart.version,
+          id: currentProject.id,
         }
       );
 

+ 10 - 14
dashboard/src/shared/api.tsx

@@ -405,14 +405,12 @@ const getBranches = baseApi<
 });
 
 const getChart = baseApi<
-  {
-    namespace: string;
-    cluster_id: number;
-    storage: StorageType;
-  },
-  { id: number; name: string; revision: number }
+  {},
+  { id: number; cluster_id: number; namespace: string; name: string; revision: number }
 >("GET", (pathParams) => {
-  return `/api/projects/${pathParams.id}/releases/${pathParams.name}/${pathParams.revision}`;
+  let { id, cluster_id, namespace, name, revision } = pathParams
+
+  return `/api/projects/${id}/clusters/${cluster_id}/namespaces/${namespace}/releases/${name}/${revision}`
 });
 
 const getCharts = baseApi<
@@ -443,14 +441,12 @@ const getChartComponents = baseApi<
 });
 
 const getChartControllers = baseApi<
-  {
-    namespace: string;
-    cluster_id: number;
-    storage: StorageType;
-  },
-  { id: number; name: string; revision: number }
+{},
+{ id: number; cluster_id: number; namespace: string; name: string; revision: number }
 >("GET", (pathParams) => {
-  return `/api/projects/${pathParams.id}/releases/${pathParams.name}/${pathParams.revision}/controllers`;
+  let { id, cluster_id, namespace, name, revision } = pathParams
+  
+  return `/api/projects/${id}/clusters/${cluster_id}/namespaces/${namespace}/releases/${name}/${revision}/controllers`
 });
 
 const getClusterIntegrations = baseApi("GET", "/api/integrations/cluster");