From 166891cfd453a081942c466c59dbd45ef142d218 Mon Sep 17 00:00:00 2001 From: warjiang <1096409085@qq.com> Date: Mon, 9 Sep 2024 16:34:33 +0800 Subject: [PATCH] feat: add api for service management Signed-off-by: warjiang <1096409085@qq.com> --- cmd/api/app/api.go | 1 + cmd/api/app/routes/service/handler.go | 54 +++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 cmd/api/app/routes/service/handler.go diff --git a/cmd/api/app/api.go b/cmd/api/app/api.go index 3ea3df9..1213b60 100644 --- a/cmd/api/app/api.go +++ b/cmd/api/app/api.go @@ -27,6 +27,7 @@ import ( _ "github.com/karmada-io/dashboard/cmd/api/app/routes/overview" _ "github.com/karmada-io/dashboard/cmd/api/app/routes/propagationpolicy" _ "github.com/karmada-io/dashboard/cmd/api/app/routes/secret" + _ "github.com/karmada-io/dashboard/cmd/api/app/routes/service" _ "github.com/karmada-io/dashboard/cmd/api/app/routes/statefulset" _ "github.com/karmada-io/dashboard/cmd/api/app/routes/unstructured" ) diff --git a/cmd/api/app/routes/service/handler.go b/cmd/api/app/routes/service/handler.go new file mode 100644 index 0000000..1fe9b84 --- /dev/null +++ b/cmd/api/app/routes/service/handler.go @@ -0,0 +1,54 @@ +package service + +import ( + "github.com/gin-gonic/gin" + "github.com/karmada-io/dashboard/cmd/api/app/router" + "github.com/karmada-io/dashboard/cmd/api/app/types/common" + "github.com/karmada-io/dashboard/pkg/client" + "github.com/karmada-io/dashboard/pkg/resource/service" +) + +func handleGetServices(c *gin.Context) { + k8sClient := client.InClusterClientForKarmadaApiServer() + dataSelect := common.ParseDataSelectPathParameter(c) + nsQuery := common.ParseNamespacePathParameter(c) + result, err := service.GetServiceList(k8sClient, nsQuery, dataSelect) + if err != nil { + common.Fail(c, err) + return + } + common.Success(c, result) +} + +func handleGetServiceDetail(c *gin.Context) { + k8sClient := client.InClusterClientForKarmadaApiServer() + namespace := c.Param("namespace") + name := c.Param("service") + result, err := service.GetServiceDetail(k8sClient, namespace, name) + if err != nil { + common.Fail(c, err) + return + } + common.Success(c, result) +} + +func handleGetServiceEvents(c *gin.Context) { + k8sClient := client.InClusterClientForKarmadaApiServer() + namespace := c.Param("namespace") + name := c.Param("service") + dataSelect := common.ParseDataSelectPathParameter(c) + result, err := service.GetServiceEvents(k8sClient, dataSelect, namespace, name) + if err != nil { + common.Fail(c, err) + return + } + common.Success(c, result) +} + +func init() { + r := router.V1() + r.GET("/service", handleGetServices) + r.GET("/service/:namespace", handleGetServices) + r.GET("/service/:namespace/:service", handleGetServiceDetail) + r.GET("/service/:namespace/:service/event", handleGetServiceEvents) +}