Skip to content

Commit

Permalink
Merge pull request #150 from Heylosky/member-cluster-api
Browse files Browse the repository at this point in the history
ADD pod resource & routes to get member-cluster namespace/deployment/pod
  • Loading branch information
karmada-bot authored Dec 6, 2024
2 parents 042ba69 + e16cf8f commit 3afef47
Show file tree
Hide file tree
Showing 11 changed files with 344 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/api/app/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
_ "github.com/karmada-io/dashboard/cmd/api/app/routes/deployment"
_ "github.com/karmada-io/dashboard/cmd/api/app/routes/ingress"
_ "github.com/karmada-io/dashboard/cmd/api/app/routes/job"
_ "github.com/karmada-io/dashboard/cmd/api/app/routes/member/node"
_ "github.com/karmada-io/dashboard/cmd/api/app/routes/member"
_ "github.com/karmada-io/dashboard/cmd/api/app/routes/namespace"
_ "github.com/karmada-io/dashboard/cmd/api/app/routes/overridepolicy"
_ "github.com/karmada-io/dashboard/cmd/api/app/routes/overview"
Expand Down
25 changes: 25 additions & 0 deletions cmd/api/app/router/middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package router

import (
"context"
"github.com/gin-gonic/gin"
"github.com/karmada-io/dashboard/cmd/api/app/types/common"
"github.com/karmada-io/dashboard/pkg/client"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"net/http"
)

func EnsureMemberClusterMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
karmadaClient := client.InClusterKarmadaClient()
_, err := karmadaClient.ClusterV1alpha1().Clusters().Get(context.TODO(), c.Param("clustername"), metav1.GetOptions{})
if err != nil {
c.AbortWithStatusJSON(http.StatusOK, common.BaseResponse{
Code: 500,
Msg: err.Error(),
})
return
}
c.Next()
}
}
7 changes: 7 additions & 0 deletions cmd/api/app/router/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
var (
router *gin.Engine
v1 *gin.RouterGroup
member *gin.RouterGroup
)

func init() {
Expand All @@ -18,6 +19,8 @@ func init() {
router = gin.Default()
_ = router.SetTrustedProxies(nil)
v1 = router.Group("/api/v1")
member = v1.Group("/member/:clustername")
member.Use(EnsureMemberClusterMiddleware())

router.GET("/livez", func(c *gin.Context) {
c.String(200, "livez")
Expand All @@ -34,3 +37,7 @@ func V1() *gin.RouterGroup {
func Router() *gin.Engine {
return router
}

func MemberV1() *gin.RouterGroup {
return member
}
55 changes: 55 additions & 0 deletions cmd/api/app/routes/member/deployment/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package deployment

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/deployment"
"github.com/karmada-io/dashboard/pkg/resource/event"
)

func handleGetMemberDeployments(c *gin.Context) {
memberClient := client.InClusterClientForMemberCluster(c.Param("clustername"))
namespace := common.ParseNamespacePathParameter(c)
dataSelect := common.ParseDataSelectPathParameter(c)
result, err := deployment.GetDeploymentList(memberClient, namespace, dataSelect)
if err != nil {
common.Fail(c, err)
return
}
common.Success(c, result)
}

func handleGetMemberDeploymentDetail(c *gin.Context) {
memberClient := client.InClusterClientForMemberCluster(c.Param("clustername"))
namespace := c.Param("namespace")
name := c.Param("deployment")
result, err := deployment.GetDeploymentDetail(memberClient, namespace, name)
if err != nil {
common.Fail(c, err)
return
}
common.Success(c, result)
}

func handleGetMemberDeploymentEvents(c *gin.Context) {
memberClient := client.InClusterClientForMemberCluster(c.Param("clustername"))
namespace := c.Param("namespace")
name := c.Param("deployment")
dataSelect := common.ParseDataSelectPathParameter(c)
result, err := event.GetResourceEvents(memberClient, dataSelect, namespace, name)
if err != nil {
common.Fail(c, err)
return
}
common.Success(c, result)
}

func init() {
r := router.MemberV1()
r.GET("/deployment", handleGetMemberDeployments)
r.GET("/deployment/:namespace", handleGetMemberDeployments)
r.GET("/deployment/:namespace/:deployment", handleGetMemberDeploymentDetail)
r.GET("/deployment/:namespace/:deployment/event", handleGetMemberDeploymentEvents)
}
9 changes: 9 additions & 0 deletions cmd/api/app/routes/member/member.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package member

// Importing member route packages forces route registration
import (
_ "github.com/karmada-io/dashboard/cmd/api/app/routes/member/deployment"
_ "github.com/karmada-io/dashboard/cmd/api/app/routes/member/namespace"
_ "github.com/karmada-io/dashboard/cmd/api/app/routes/member/node"
_ "github.com/karmada-io/dashboard/cmd/api/app/routes/member/pod"
)
54 changes: 54 additions & 0 deletions cmd/api/app/routes/member/namespace/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package namespace

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/event"
ns "github.com/karmada-io/dashboard/pkg/resource/namespace"
)

func handleGetMemberNamespace(c *gin.Context) {
memberClient := client.InClusterClientForMemberCluster(c.Param("clustername"))

dataSelect := common.ParseDataSelectPathParameter(c)
result, err := ns.GetNamespaceList(memberClient, dataSelect)
if err != nil {
common.Fail(c, err)
return
}
common.Success(c, result)
}

func handleGetMemberNamespaceDetail(c *gin.Context) {
memberClient := client.InClusterClientForMemberCluster(c.Param("clustername"))

name := c.Param("name")
result, err := ns.GetNamespaceDetail(memberClient, name)
if err != nil {
common.Fail(c, err)
return
}
common.Success(c, result)
}

func handleGetMemberNamespaceEvents(c *gin.Context) {
memberClient := client.InClusterClientForMemberCluster(c.Param("clustername"))

name := c.Param("name")
dataSelect := common.ParseDataSelectPathParameter(c)
result, err := event.GetNamespaceEvents(memberClient, dataSelect, name)
if err != nil {
common.Fail(c, err)
return
}
common.Success(c, result)
}

func init() {
r := router.MemberV1()
r.GET("/namespace", handleGetMemberNamespace)
r.GET("/namespace/:name", handleGetMemberNamespaceDetail)
r.GET("/namespace/:name/event", handleGetMemberNamespaceEvents)
}
12 changes: 2 additions & 10 deletions cmd/api/app/routes/member/node/handler.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
package node

import (
"context"
"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/node"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func handleGetClusterNode(c *gin.Context) {
karmadaClient := client.InClusterKarmadaClient()
_, err := karmadaClient.ClusterV1alpha1().Clusters().Get(context.TODO(), c.Param("clustername"), metav1.GetOptions{})
if err != nil {
common.Fail(c, err)
return
}
memberClient := client.InClusterClientForMemberCluster(c.Param("clustername"))
dataSelect := common.ParseDataSelectPathParameter(c)
result, err := node.GetNodeList(memberClient, dataSelect)
Expand All @@ -28,6 +20,6 @@ func handleGetClusterNode(c *gin.Context) {
}

func init() {
r := router.V1()
r.GET("/member/:clustername/node", handleGetClusterNode)
r := router.MemberV1()
r.GET("/node", handleGetClusterNode)
}
42 changes: 42 additions & 0 deletions cmd/api/app/routes/member/pod/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package pod

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/pod"
)

// return a pods list
func handleGetMemberPod(c *gin.Context) {
memberClient := client.InClusterClientForMemberCluster(c.Param("clustername"))
dataSelect := common.ParseDataSelectPathParameter(c)
nsQuery := common.ParseNamespacePathParameter(c)
result, err := pod.GetPodList(memberClient, nsQuery, dataSelect)
if err != nil {
common.Fail(c, err)
return
}
common.Success(c, result)
}

// return a pod detail
func handleGetMemberPodDetail(c *gin.Context) {
memberClient := client.InClusterClientForMemberCluster(c.Param("clustername"))
namespace := c.Param("namespace")
name := c.Param("name")
result, err := pod.GetPodDetail(memberClient, namespace, name)
if err != nil {
common.Fail(c, err)
return
}
common.Success(c, result)
}

func init() {
r := router.MemberV1()
r.GET("/pod", handleGetMemberPod)
r.GET("/pod/:namespace", handleGetMemberPod)
r.GET("/pod/:namespace/:name", handleGetMemberPodDetail)
}
38 changes: 38 additions & 0 deletions pkg/resource/pod/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package pod

import (
"github.com/karmada-io/dashboard/pkg/dataselect"
api "k8s.io/api/core/v1"
)

type PodCell api.Pod

func (self PodCell) GetProperty(name dataselect.PropertyName) dataselect.ComparableValue {
switch name {
case dataselect.NameProperty:
return dataselect.StdComparableString(self.ObjectMeta.Name)
case dataselect.CreationTimestampProperty:
return dataselect.StdComparableTime(self.ObjectMeta.CreationTimestamp.Time)
case dataselect.NamespaceProperty:
return dataselect.StdComparableString(self.ObjectMeta.Namespace)
default:
// if name is not supported then just return a constant dummy value, sort will have no effect.
return nil
}
}

func toCells(std []api.Pod) []dataselect.DataCell {
cells := make([]dataselect.DataCell, len(std))
for i := range std {
cells[i] = PodCell(std[i])
}
return cells
}

func fromCells(cells []dataselect.DataCell) []api.Pod {
std := make([]api.Pod, len(cells))
for i := range std {
std[i] = api.Pod(cells[i].(PodCell))
}
return std
}
25 changes: 25 additions & 0 deletions pkg/resource/pod/detail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package pod

import (
"context"

v1 "k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

type PodDeatil struct {
ObjectMeta metaV1.ObjectMeta `json:"objectMeta"`
TypeMeta metaV1.TypeMeta `json:"typeMeta"`
Spec v1.PodSpec `json:"podSpec"`
Status v1.PodStatus `json:"status"`
}

// GetPodDetail returns a Pod detail
func GetPodDetail(client kubernetes.Interface, namespace, name string) (*v1.Pod, error) {
podData, err := client.CoreV1().Pods(namespace).Get(context.TODO(), name, metaV1.GetOptions{})
if err != nil {
return nil, err
}
return podData, nil
}
Loading

0 comments on commit 3afef47

Please sign in to comment.