Skip to content
This repository has been archived by the owner on Sep 19, 2022. It is now read-only.

Commit

Permalink
+ add custom api test supported
Browse files Browse the repository at this point in the history
  • Loading branch information
kebe7jun committed Jun 23, 2022
1 parent 0a36c40 commit 5cf4ec9
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 21 deletions.
26 changes: 17 additions & 9 deletions api/extend/deploy2service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{} {
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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], "-")
}
}
20 changes: 11 additions & 9 deletions pkg/client/fake/fake_ckube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
71 changes: 68 additions & 3 deletions pkg/client/fake/fake_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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}"
}
}
]
}
Expand Down Expand Up @@ -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)
})
}

0 comments on commit 5cf4ec9

Please sign in to comment.