From 5cf4ec972bd157b4e1733b49399570039aadc613 Mon Sep 17 00:00:00 2001 From: Kebe Date: Thu, 23 Jun 2022 10:52:21 +0800 Subject: [PATCH] + add custom api test supported --- api/extend/deploy2service.go | 26 +++++++---- pkg/client/fake/fake_ckube.go | 20 ++++---- pkg/client/fake/fake_client_test.go | 71 +++++++++++++++++++++++++++-- 3 files changed, 96 insertions(+), 21 deletions(-) diff --git a/api/extend/deploy2service.go b/api/extend/deploy2service.go index 669505e..a4543d0 100644 --- a/api/extend/deploy2service.go +++ b/api/extend/deploy2service.go @@ -5,12 +5,15 @@ import ( "github.com/gorilla/mux" v1 "k8s.io/api/core/v1" + v12 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/json" "github.com/DaoCloud/ckube/api" "github.com/DaoCloud/ckube/common" "github.com/DaoCloud/ckube/page" "github.com/DaoCloud/ckube/store" "github.com/DaoCloud/ckube/utils" + "github.com/DaoCloud/ckube/watcher" ) func Deploy2Service(r *api.ReqContext) interface{} { @@ -42,10 +45,10 @@ func Deploy2Service(r *api.ReqContext) interface{} { } var labels map[string]string for _, podIf := range res.Items { - if pod, ok := podIf.(*v1.Pod); ok { + if pod, ok := podIf.(v12.Object); ok { depName := getDeploymentName(pod) if depName != "" { - labels = pod.Labels + labels = pod.GetLabels() break } } @@ -60,20 +63,25 @@ func Deploy2Service(r *api.ReqContext) interface{} { return res.Error } for _, svcIf := range res.Items { - if svc, ok := svcIf.(*v1.Service); ok { - if svc.Spec.Selector != nil && utils.IsSubsetOf(svc.Spec.Selector, labels) { - services = append(services, svc) - } + svc := &v1.Service{} + if s, ok := svcIf.(*watcher.ObjType); ok { + bs, _ := json.Marshal(s) + json.Unmarshal(bs, svc) + } else { + svc = svcIf.(*v1.Service) + } + if svc.Spec.Selector != nil && utils.IsSubsetOf(svc.Spec.Selector, labels) { + services = append(services, svc) } } return services } -func getDeploymentName(pod *v1.Pod) string { - if len(pod.OwnerReferences) == 0 || pod.OwnerReferences[0].Kind != "ReplicaSet" { +func getDeploymentName(pod v12.Object) string { + if len(pod.GetOwnerReferences()) == 0 || pod.GetOwnerReferences()[0].Kind != "ReplicaSet" { return "" } else { - parts := strings.Split(pod.OwnerReferences[0].Name, "-") + parts := strings.Split(pod.GetOwnerReferences()[0].Name, "-") return strings.Join(parts[:len(parts)-1], "-") } } diff --git a/pkg/client/fake/fake_ckube.go b/pkg/client/fake/fake_ckube.go index 26f8f50..88fa970 100644 --- a/pkg/client/fake/fake_ckube.go +++ b/pkg/client/fake/fake_ckube.go @@ -5,6 +5,17 @@ import ( "encoding/base64" "encoding/json" "fmt" + "io" + "net/http" + "os" + "strings" + "sync" + "time" + + "github.com/gorilla/mux" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/rest" + "github.com/DaoCloud/ckube/common" "github.com/DaoCloud/ckube/common/constants" "github.com/DaoCloud/ckube/kube" @@ -14,15 +25,6 @@ import ( "github.com/DaoCloud/ckube/store" "github.com/DaoCloud/ckube/store/memory" "github.com/DaoCloud/ckube/watcher" - "github.com/gorilla/mux" - "io" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/client-go/rest" - "net/http" - "os" - "strings" - "sync" - "time" ) type fakeCkubeServer struct { diff --git a/pkg/client/fake/fake_client_test.go b/pkg/client/fake/fake_client_test.go index 55186c6..c45df50 100644 --- a/pkg/client/fake/fake_client_test.go +++ b/pkg/client/fake/fake_client_test.go @@ -2,13 +2,17 @@ package fake import ( "context" - "github.com/DaoCloud/ckube/page" + "encoding/json" + "fmt" + "sync" + "testing" + "github.com/stretchr/testify/assert" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" - "sync" - "testing" + + "github.com/DaoCloud/ckube/page" ) func TestNewFakeCKubeServer(t *testing.T) { @@ -26,6 +30,18 @@ func TestNewFakeCKubeServer(t *testing.T) { "labels": "{.metadata.labels}", "created_at": "{.metadata.creationTimestamp}" } + }, + { + "group": "", + "version": "v1", + "resource": "services", + "list_kind": "ServiceList", + "index": { + "namespace": "{.metadata.namespace}", + "name": "{.metadata.name}", + "labels": "{.metadata.labels}", + "created_at": "{.metadata.creationTimestamp}" + } } ] } @@ -193,4 +209,53 @@ func TestNewFakeCKubeServer(t *testing.T) { }, }, events) }) + t.Run("custom api", func(t *testing.T) { + cli.CoreV1().Pods("test").Create(context.Background(), &v1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-xxxx-asd", + Namespace: "test", + Labels: map[string]string{ + "app": "test", + }, + OwnerReferences: []metav1.OwnerReference{ + { + Kind: "ReplicaSet", + Name: "test-xxxx", + }, + }, + }, + }, metav1.CreateOptions{}) + cli.CoreV1().Services("test").Create(context.Background(), &v1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-svc", + Namespace: "test", + Labels: map[string]string{ + "app": "test", + }, + }, + Spec: v1.ServiceSpec{ + Ports: []v1.ServicePort{ + { + Port: 20880, + }, + }, + Selector: map[string]string{ + "app": "test", + }, + ClusterIP: "1.1.1.1", + }, + }, metav1.CreateOptions{}) + bs, err := cli.Discovery().RESTClient(). + Get(). + RequestURI(fmt.Sprintf("/custom/v1/namespaces/%s/deployments/%s/services", + // m.Cluster, + "test", + "test")). + DoRaw(context.Background()) + assert.NoError(t, err) + svcs := make([]v1.Service, 0) + err = json.Unmarshal(bs, &svcs) + assert.NoError(t, err) + assert.Len(t, svcs, 1) + }) }