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

add DetectPrometheusInstalled handler

Anukul Sangwan 4 жил өмнө
parent
commit
bb40ea2a4e

+ 47 - 0
api/server/handlers/cluster/detect_prometheus_installed.go

@@ -0,0 +1,47 @@
+package cluster
+
+import (
+	"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/apierrors"
+	"github.com/porter-dev/porter/api/types"
+	"github.com/porter-dev/porter/internal/kubernetes/prometheus"
+	"github.com/porter-dev/porter/internal/models"
+)
+
+type DetectPrometheusInstalledHandler struct {
+	handlers.PorterHandler
+	KubernetesAgentGetter
+}
+
+func NewDetectPrometheusInstalledHandler(
+	config *shared.Config,
+) *DetectPrometheusInstalledHandler {
+	return &DetectPrometheusInstalledHandler{
+		PorterHandler:         handlers.NewDefaultPorterHandler(config, nil, nil),
+		KubernetesAgentGetter: NewDefaultKubernetesAgentGetter(config),
+	}
+}
+
+func (c *DetectPrometheusInstalledHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
+
+	agent, err := c.GetAgent(cluster)
+
+	if err != nil {
+		c.HandleAPIError(w, apierrors.NewErrInternal(err))
+		return
+	}
+
+	if _, found, err := prometheus.GetPrometheusService(agent.Clientset); err != nil {
+		c.HandleAPIError(w, apierrors.NewErrInternal(err))
+		return
+	} else if !found {
+		http.NotFound(w, r)
+		return
+	}
+
+	w.WriteHeader(http.StatusOK)
+}

+ 25 - 0
api/server/router/cluster.go

@@ -192,5 +192,30 @@ func getClusterRoutes(
 		Router:   r,
 	})
 
+	// GET /api/projects/{project_id}/clusters/{cluster_id}/prometheus/detect -> cluster.NewDetectPrometheusInstalledHandler
+	detectPrometheusInstalledEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbGet,
+			Method: types.HTTPVerbGet,
+			Path: &types.Path{
+				Parent:       basePath,
+				RelativePath: relPath + "/prometheus/detect",
+			},
+			Scopes: []types.PermissionScope{
+				types.UserScope,
+				types.ProjectScope,
+				types.ClusterScope,
+			},
+		},
+	)
+
+	detectPrometheusInstalledHandler := cluster.NewDetectPrometheusInstalledHandler(config)
+
+	routes = append(routes, &Route{
+		Endpoint: detectPrometheusInstalledEndpoint,
+		Handler:  detectPrometheusInstalledHandler,
+		Router:   r,
+	})
+
 	return routes, newPath
 }

+ 8 - 4
dashboard/src/main/home/cluster-dashboard/ClusterDashboard.tsx

@@ -6,7 +6,12 @@ import { Route, Switch } from "react-router-dom";
 
 import { Context } from "shared/Context";
 import { ChartType, ClusterType } from "shared/types";
-import { getQueryParam, PorterUrl, pushFiltered, pushQueryParams } from "shared/routing";
+import {
+  getQueryParam,
+  PorterUrl,
+  pushFiltered,
+  pushQueryParams,
+} from "shared/routing";
 
 import DashboardHeader from "./DashboardHeader";
 import ChartList from "./chart/ChartList";
@@ -57,11 +62,10 @@ class ClusterDashboard extends Component<PropsType, StateType> {
     api
       .getPrometheusIsInstalled(
         "<token>",
-        {
-          cluster_id: currentCluster.id,
-        },
+        {},
         {
           id: currentProject.id,
+          cluster_id: currentCluster.id,
         }
       )
       .then((res) => {

+ 2 - 3
dashboard/src/main/home/cluster-dashboard/dashboard/Metrics.tsx

@@ -58,11 +58,10 @@ const Metrics: React.FC = () => {
       ),
       api.getPrometheusIsInstalled(
         "<token>",
-        {
-          cluster_id: currentCluster.id,
-        },
+        {},
         {
           id: currentProject.id,
+          cluster_id: currentCluster.id,
         }
       ),
     ])

+ 4 - 3
dashboard/src/shared/api.tsx

@@ -656,12 +656,13 @@ const getProjectRepos = baseApi<{}, { id: number }>("GET", (pathParams) => {
 const getProjects = baseApi("GET", "/api/projects");
 
 const getPrometheusIsInstalled = baseApi<
+  {},
   {
+    id: number;
     cluster_id: number;
-  },
-  { id: number }
+  }
 >("GET", (pathParams) => {
-  return `/api/projects/${pathParams.id}/k8s/prometheus/detect`;
+  return `/api/projects/${pathParams.id}/clusters/${pathParams.cluster_id}/prometheus/detect`;
 });
 
 const getRegistryIntegrations = baseApi("GET", "/api/integrations/registry");