Переглянути джерело

add ListNGINXIngressesHandler

Anukul Sangwan 4 роки тому
батько
коміт
95bad55042

+ 49 - 0
api/server/handlers/cluster/list_nginx_ingresses.go

@@ -0,0 +1,49 @@
+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 ListNGINXIngressesHandler struct {
+	handlers.PorterHandlerWriter
+	KubernetesAgentGetter
+}
+
+func NewListNGINXIngressesHandler(
+	config *shared.Config,
+	writer shared.ResultWriter,
+) *ListNGINXIngressesHandler {
+	return &ListNGINXIngressesHandler{
+		PorterHandlerWriter:   handlers.NewDefaultPorterHandler(config, nil, writer),
+		KubernetesAgentGetter: NewDefaultKubernetesAgentGetter(config),
+	}
+}
+
+func (c *ListNGINXIngressesHandler) 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
+	}
+
+	ingresses, err := prometheus.GetIngressesWithNGINXAnnotation(agent.Clientset)
+
+	if err != nil {
+		c.HandleAPIError(w, apierrors.NewErrInternal(err))
+		return
+	}
+
+	var res types.ListNGINXIngressesResponse = ingresses
+
+	c.WriteResult(w, res)
+}

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

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

+ 6 - 1
api/types/cluster.go

@@ -1,6 +1,9 @@
 package types
 
-import v1 "k8s.io/api/core/v1"
+import (
+	"github.com/porter-dev/porter/internal/kubernetes/prometheus"
+	v1 "k8s.io/api/core/v1"
+)
 
 type Cluster struct {
 	ID uint `json:"id"`
@@ -62,3 +65,5 @@ type DeleteNamespaceRequest struct {
 type GetTemporaryKubeconfigResponse struct {
 	Kubeconfig []byte `json:"kubeconfig"`
 }
+
+type ListNGINXIngressesResponse []prometheus.SimpleIngress

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

@@ -72,11 +72,10 @@ const Metrics: React.FC = () => {
         api
           .getNGINXIngresses(
             "<token>",
-            {
-              cluster_id: currentCluster.id,
-            },
+            {},
             {
               id: currentProject.id,
+              cluster_id: currentCluster.id,
             }
           )
           .then((res) => {

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

@@ -98,11 +98,10 @@ const MetricsSection: React.FunctionComponent<PropsType> = ({
       api
         .getNGINXIngresses(
           "<token>",
-          {
-            cluster_id: currentCluster.id,
-          },
+          {},
           {
             id: currentProject.id,
+            cluster_id: currentCluster.id,
           }
         )
         .then((res) => {

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

@@ -621,12 +621,13 @@ const getNamespaces = baseApi<
 });
 
 const getNGINXIngresses = baseApi<
+  {},
   {
+    id: number;
     cluster_id: number;
-  },
-  { id: number }
+  }
 >("GET", (pathParams) => {
-  return `/api/projects/${pathParams.id}/k8s/prometheus/ingresses`;
+  return `/api/projects/${pathParams.id}/clusters/${pathParams.cluster_id}/prometheus/ingresses`;
 });
 
 const getOAuthIds = baseApi<