Răsfoiți Sursa

add GetPodMetricsHandler

Anukul Sangwan 4 ani în urmă
părinte
comite
ab3fe2cfe5

+ 66 - 0
api/server/handlers/cluster/get_pod_metrics.go

@@ -0,0 +1,66 @@
+package cluster
+
+import (
+	"github.com/porter-dev/porter/internal/kubernetes/prometheus"
+	"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/models"
+)
+
+type GetPodMetricsHandler struct {
+	handlers.PorterHandlerReadWriter
+	KubernetesAgentGetter
+}
+
+func NewGetPodMetricsHandler(
+	config *shared.Config,
+	decoderValidator shared.RequestDecoderValidator,
+	writer shared.ResultWriter,
+) *GetPodMetricsHandler {
+	return &GetPodMetricsHandler{
+		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
+		KubernetesAgentGetter:   NewDefaultKubernetesAgentGetter(config),
+	}
+}
+
+func (c *GetPodMetricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	request := &types.GetPodMetricsRequest{}
+
+	if ok := c.DecodeAndValidate(w, r, request); !ok {
+		return
+	}
+
+	cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
+
+	agent, err := c.GetAgent(cluster)
+
+	if err != nil {
+		c.HandleAPIError(w, apierrors.NewErrInternal(err))
+		return
+	}
+
+	// get prometheus service
+	promSvc, found, err := prometheus.GetPrometheusService(agent.Clientset)
+
+	if err != nil || !found {
+		c.HandleAPIError(w, apierrors.NewErrInternal(err))
+		return
+	}
+
+	rawQuery, err := prometheus.QueryPrometheus(agent.Clientset, promSvc, request.QueryOpts)
+
+	if err != nil {
+		c.HandleAPIError(w, apierrors.NewErrInternal(err))
+		return
+	}
+
+	s := string(rawQuery)
+
+	var res types.GetPodMetricsResponse = &s
+
+	c.WriteResult(w, res)
+}

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

@@ -245,5 +245,34 @@ func getClusterRoutes(
 		Router:   r,
 	})
 
+	// GET /api/projects/{project_id}/clusters/{cluster_id}/metrics -> cluster.NewGetPodMetricsHandler
+	getPodMetricsEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbGet,
+			Method: types.HTTPVerbGet,
+			Path: &types.Path{
+				Parent:       basePath,
+				RelativePath: relPath + "/metrics",
+			},
+			Scopes: []types.PermissionScope{
+				types.UserScope,
+				types.ProjectScope,
+				types.ClusterScope,
+			},
+		},
+	)
+
+	getPodMetricsHandler := cluster.NewGetPodMetricsHandler(
+		config,
+		factory.GetDecoderValidator(),
+		factory.GetResultWriter(),
+	)
+
+	routes = append(routes, &Route{
+		Endpoint: getPodMetricsEndpoint,
+		Handler:  getPodMetricsHandler,
+		Router:   r,
+	})
+
 	return routes, newPath
 }

+ 6 - 0
api/types/cluster.go

@@ -67,3 +67,9 @@ type GetTemporaryKubeconfigResponse struct {
 }
 
 type ListNGINXIngressesResponse []prometheus.SimpleIngress
+
+type GetPodMetricsRequest struct {
+	*prometheus.QueryOpts
+}
+
+type GetPodMetricsResponse *string

+ 1 - 1
dashboard/src/main/home/cluster-dashboard/dashboard/Metrics.tsx

@@ -216,7 +216,6 @@ const Metrics: React.FC = () => {
       const res = await api.getMetrics(
         "<token>",
         {
-          cluster_id: currentCluster.id,
           metric: selectedMetric,
           shouldsum: false,
           kind: "Ingress",
@@ -233,6 +232,7 @@ const Metrics: React.FC = () => {
         },
         {
           id: currentProject.id,
+          cluster_id: currentCluster.id,
         }
       );
 

+ 2 - 2
dashboard/src/main/home/cluster-dashboard/expanded-chart/metrics/MetricsSection.tsx

@@ -237,7 +237,6 @@ const MetricsSection: React.FunctionComponent<PropsType> = ({
       const res = await api.getMetrics(
         "<token>",
         {
-          cluster_id: currentCluster.id,
           metric: metricType,
           shouldsum: shouldsum,
           kind: selectedController?.kind,
@@ -250,6 +249,7 @@ const MetricsSection: React.FunctionComponent<PropsType> = ({
         },
         {
           id: currentProject.id,
+          cluster_id: currentCluster.id,
         }
       );
 
@@ -297,7 +297,6 @@ const MetricsSection: React.FunctionComponent<PropsType> = ({
       const res = await api.getMetrics(
         "<token>",
         {
-          cluster_id: currentCluster.id,
           metric: selectedMetric,
           shouldsum: shouldsum,
           kind: selectedController?.kind,
@@ -310,6 +309,7 @@ const MetricsSection: React.FunctionComponent<PropsType> = ({
         },
         {
           id: currentProject.id,
+          cluster_id: currentCluster.id,
         }
       );
 

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

@@ -593,7 +593,6 @@ const getMatchingPods = baseApi<
 
 const getMetrics = baseApi<
   {
-    cluster_id: number;
     metric: string;
     shouldsum: boolean;
     pods?: string[];
@@ -605,9 +604,12 @@ const getMetrics = baseApi<
     endrange: number;
     resolution: string;
   },
-  { id: number }
+  {
+    id: number;
+    cluster_id: number;
+  }
 >("GET", (pathParams) => {
-  return `/api/projects/${pathParams.id}/k8s/metrics`;
+  return `/api/projects/${pathParams.id}/clusters/${pathParams.cluster_id}/metrics`;
 });
 
 const getNamespaces = baseApi<