From 16d281c315ccdef80930883dbb5fc276a8e3a391 Mon Sep 17 00:00:00 2001 From: rambohe-ch Date: Tue, 11 Oct 2022 23:15:54 +0800 Subject: [PATCH] bugfix: even no endpoints left after filter, an empty object should be returned to clients --- pkg/yurthub/filter/servicetopology/handler.go | 99 +- .../filter/servicetopology/handler_test.go | 2550 ++++++++++------- 2 files changed, 1618 insertions(+), 1031 deletions(-) diff --git a/pkg/yurthub/filter/servicetopology/handler.go b/pkg/yurthub/filter/servicetopology/handler.go index 93357d82741..fab41251add 100644 --- a/pkg/yurthub/filter/servicetopology/handler.go +++ b/pkg/yurthub/filter/servicetopology/handler.go @@ -81,33 +81,24 @@ func (fh *serviceTopologyFilterHandler) ObjectResponseFilter(b []byte) ([]byte, // filter endpointSlice before k8s 1.21 var items []discoveryV1beta1.EndpointSlice for i := range v.Items { - isNil, item := fh.serviceTopologyHandler(&v.Items[i]) - if !isNil { - eps := item.(*discoveryV1beta1.EndpointSlice) - items = append(items, *eps) - } + eps := fh.serviceTopologyHandler(&v.Items[i]).(*discoveryV1beta1.EndpointSlice) + items = append(items, *eps) } v.Items = items return fh.serializer.Encode(v) case *discovery.EndpointSliceList: var items []discovery.EndpointSlice for i := range v.Items { - isNil, item := fh.serviceTopologyHandler(&v.Items[i]) - if !isNil { - eps := item.(*discovery.EndpointSlice) - items = append(items, *eps) - } + eps := fh.serviceTopologyHandler(&v.Items[i]).(*discovery.EndpointSlice) + items = append(items, *eps) } v.Items = items return fh.serializer.Encode(v) case *v1.EndpointsList: var items []v1.Endpoints for i := range v.Items { - isNil, item := fh.serviceTopologyHandler(&v.Items[i]) - if !isNil { - ep := item.(*v1.Endpoints) - items = append(items, *ep) - } + ep := fh.serviceTopologyHandler(&v.Items[i]).(*v1.Endpoints) + items = append(items, *ep) } v.Items = items return fh.serializer.Encode(v) @@ -134,20 +125,17 @@ func (fh *serviceTopologyFilterHandler) StreamResponseFilter(rc io.ReadCloser, c return err } - isNil, filteredObj := fh.serviceTopologyHandler(obj) - if !isNil { - ch <- watch.Event{ - Type: watchType, - Object: filteredObj, - } + ch <- watch.Event{ + Type: watchType, + Object: fh.serviceTopologyHandler(obj), } } } -func (fh *serviceTopologyFilterHandler) serviceTopologyHandler(obj runtime.Object) (bool, runtime.Object) { +func (fh *serviceTopologyFilterHandler) serviceTopologyHandler(obj runtime.Object) runtime.Object { needHandle, serviceTopologyType := fh.resolveServiceTopologyType(obj) if !needHandle || len(serviceTopologyType) == 0 { - return false, obj + return obj } switch serviceTopologyType { @@ -158,7 +146,7 @@ func (fh *serviceTopologyFilterHandler) serviceTopologyHandler(obj runtime.Objec // close traffic on the same node pool return fh.nodePoolTopologyHandler(obj) default: - return false, obj + return obj } } @@ -190,36 +178,24 @@ func (fh *serviceTopologyFilterHandler) resolveServiceTopologyType(obj runtime.O return false, "" } -func (fh *serviceTopologyFilterHandler) nodeTopologyHandler(obj runtime.Object) (bool, runtime.Object) { +func (fh *serviceTopologyFilterHandler) nodeTopologyHandler(obj runtime.Object) runtime.Object { switch v := obj.(type) { case *discoveryV1beta1.EndpointSlice: - newObj := reassembleV1beta1EndpointSlice(v, fh.nodeName, nil) - if newObj == nil { - return true, obj - } - return false, newObj + return reassembleV1beta1EndpointSlice(v, fh.nodeName, nil) case *discovery.EndpointSlice: - newObj := reassembleEndpointSlice(v, fh.nodeName, nil) - if newObj == nil { - return true, obj - } - return false, newObj + return reassembleEndpointSlice(v, fh.nodeName, nil) case *v1.Endpoints: - newObj := reassembleEndpoints(v, fh.nodeName, nil) - if newObj == nil { - return true, obj - } - return false, newObj + return reassembleEndpoints(v, fh.nodeName, nil) default: - return false, obj + return obj } } -func (fh *serviceTopologyFilterHandler) nodePoolTopologyHandler(obj runtime.Object) (bool, runtime.Object) { +func (fh *serviceTopologyFilterHandler) nodePoolTopologyHandler(obj runtime.Object) runtime.Object { currentNode, err := fh.nodeGetter(fh.nodeName) if err != nil { klog.Warningf("skip serviceTopologyFilterHandler, failed to get current node %s, err: %v", fh.nodeName, err) - return false, obj + return obj } nodePoolName, ok := currentNode.Labels[nodepoolv1alpha1.LabelCurrentNodePool] @@ -231,30 +207,18 @@ func (fh *serviceTopologyFilterHandler) nodePoolTopologyHandler(obj runtime.Obje nodePool, err := fh.nodePoolLister.Get(nodePoolName) if err != nil { klog.Warningf("serviceTopologyFilterHandler: failed to get nodepool %s, err: %v", nodePoolName, err) - return false, obj + return obj } switch v := obj.(type) { case *discoveryV1beta1.EndpointSlice: - newObj := reassembleV1beta1EndpointSlice(v, "", nodePool) - if newObj == nil { - return true, obj - } - return false, newObj + return reassembleV1beta1EndpointSlice(v, "", nodePool) case *discovery.EndpointSlice: - newObj := reassembleEndpointSlice(v, "", nodePool) - if newObj == nil { - return true, obj - } - return false, newObj + return reassembleEndpointSlice(v, "", nodePool) case *v1.Endpoints: - newObj := reassembleEndpoints(v, "", nodePool) - if newObj == nil { - return true, obj - } - return false, newObj + return reassembleEndpoints(v, "", nodePool) default: - return false, obj + return obj } } @@ -279,9 +243,8 @@ func reassembleV1beta1EndpointSlice(endpointSlice *discoveryV1beta1.EndpointSlic } } } - if len(newEps) == 0 { - return nil - } + + // even no endpoints left, empty endpoints slice should be returned endpointSlice.Endpoints = newEps return endpointSlice } @@ -308,9 +271,7 @@ func reassembleEndpointSlice(endpointSlice *discovery.EndpointSlice, nodeName st } } - if len(newEps) == 0 { - return nil - } + // even no endpoints left, empty endpoints slice should be returned endpointSlice.Endpoints = newEps return endpointSlice } @@ -338,10 +299,8 @@ func reassembleEndpoints(endpoints *v1.Endpoints, nodeName string, nodePool *nod newEpSubsets = append(newEpSubsets, endpoints.Subsets[i]) } } - if len(newEpSubsets) == 0 { - // this endpoints has no valid addresses for ingress controller, return nil to ignore it - return nil - } + + // even no subsets left, empty subset slice should be returned endpoints.Subsets = newEpSubsets return endpoints } diff --git a/pkg/yurthub/filter/servicetopology/handler_test.go b/pkg/yurthub/filter/servicetopology/handler_test.go index 5e875a9e6cb..a3cc2fddeba 100644 --- a/pkg/yurthub/filter/servicetopology/handler_test.go +++ b/pkg/yurthub/filter/servicetopology/handler_test.go @@ -18,6 +18,8 @@ package servicetopology import ( "context" + "net/http" + "net/http/httptest" "reflect" "testing" "time" @@ -27,65 +29,85 @@ import ( discoveryV1beta1 "k8s.io/api/discovery/v1beta1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/util/sets" + "k8s.io/apiserver/pkg/endpoints/filters" + "k8s.io/apiserver/pkg/endpoints/request" "k8s.io/client-go/informers" k8sfake "k8s.io/client-go/kubernetes/fake" + filterutil "github.com/openyurtio/openyurt/pkg/yurthub/filter/util" + "github.com/openyurtio/openyurt/pkg/yurthub/kubernetes/serializer" + "github.com/openyurtio/openyurt/pkg/yurthub/proxy/util" + hubutil "github.com/openyurtio/openyurt/pkg/yurthub/util" nodepoolv1alpha1 "github.com/openyurtio/yurt-app-manager-api/pkg/yurtappmanager/apis/apps/v1alpha1" yurtfake "github.com/openyurtio/yurt-app-manager-api/pkg/yurtappmanager/client/clientset/versioned/fake" yurtinformers "github.com/openyurtio/yurt-app-manager-api/pkg/yurtappmanager/client/informers/externalversions" ) +func newTestRequestInfoResolver() *request.RequestInfoFactory { + return &request.RequestInfoFactory{ + APIPrefixes: sets.NewString("api", "apis"), + GrouplessAPIPrefixes: sets.NewString("api"), + } +} + func TestServiceTopologyHandler(t *testing.T) { currentNodeName := "node1" nodeName2 := "node2" nodeName3 := "node3" testcases := map[string]struct { + path string object runtime.Object kubeClient *k8sfake.Clientset yurtClient *yurtfake.Clientset expectResult runtime.Object }{ - "v1beta1.EndpointSlice: topologyKeys is kubernetes.io/hostname": { - object: &discoveryV1beta1.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, - Endpoints: []discoveryV1beta1.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", - }, - Topology: map[string]string{ - corev1.LabelHostname: currentNodeName, - }, - }, - { - Addresses: []string{ - "10.244.1.3", - }, - Topology: map[string]string{ - corev1.LabelHostname: "node2", - }, - }, - { - Addresses: []string{ - "10.244.1.4", - }, - Topology: map[string]string{ - corev1.LabelHostname: currentNodeName, - }, - }, - { - Addresses: []string{ - "10.244.1.5", + "v1beta1.EndpointSliceList: topologyKeys is kubernetes.io/hostname": { + path: "/apis/discovery.k8s.io/v1beta1/endpointslices", + object: &discoveryV1beta1.EndpointSliceList{ + Items: []discoveryV1beta1.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discoveryV1beta1.LabelServiceName: "svc1", + }, }, - Topology: map[string]string{ - corev1.LabelHostname: "node3", + Endpoints: []discoveryV1beta1.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.3", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node2", + }, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node3", + }, + }, }, }, }, @@ -154,74 +176,87 @@ func TestServiceTopologyHandler(t *testing.T) { }, }, ), - expectResult: &discoveryV1beta1.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, - Endpoints: []discoveryV1beta1.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", - }, - Topology: map[string]string{ - corev1.LabelHostname: currentNodeName, - }, - }, - { - Addresses: []string{ - "10.244.1.4", + expectResult: &discoveryV1beta1.EndpointSliceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointSliceList", + APIVersion: "discovery.k8s.io/v1beta1", + }, + Items: []discoveryV1beta1.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discoveryV1beta1.LabelServiceName: "svc1", + }, }, - Topology: map[string]string{ - corev1.LabelHostname: currentNodeName, + Endpoints: []discoveryV1beta1.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, }, }, }, }, }, - "v1beta1.EndpointSlice: topologyKeys is openyurt.io/nodepool": { - object: &discoveryV1beta1.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, - Endpoints: []discoveryV1beta1.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", - }, - Topology: map[string]string{ - corev1.LabelHostname: currentNodeName, - }, - }, - { - Addresses: []string{ - "10.244.1.3", - }, - Topology: map[string]string{ - corev1.LabelHostname: "node2", - }, - }, - { - Addresses: []string{ - "10.244.1.4", - }, - Topology: map[string]string{ - corev1.LabelHostname: currentNodeName, - }, - }, - { - Addresses: []string{ - "10.244.1.5", + "v1beta1.EndpointSliceList: topologyKeys is openyurt.io/nodepool": { + path: "/apis/discovery.k8s.io/v1beta1/endpointslices", + object: &discoveryV1beta1.EndpointSliceList{ + Items: []discoveryV1beta1.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discoveryV1beta1.LabelServiceName: "svc1", + }, }, - Topology: map[string]string{ - corev1.LabelHostname: "node3", + Endpoints: []discoveryV1beta1.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.3", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node2", + }, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node3", + }, + }, }, }, }, @@ -290,82 +325,95 @@ func TestServiceTopologyHandler(t *testing.T) { }, }, ), - expectResult: &discoveryV1beta1.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, - Endpoints: []discoveryV1beta1.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", - }, - Topology: map[string]string{ - corev1.LabelHostname: currentNodeName, - }, - }, - { - Addresses: []string{ - "10.244.1.4", - }, - Topology: map[string]string{ - corev1.LabelHostname: currentNodeName, - }, - }, - { - Addresses: []string{ - "10.244.1.5", + expectResult: &discoveryV1beta1.EndpointSliceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointSliceList", + APIVersion: "discovery.k8s.io/v1beta1", + }, + Items: []discoveryV1beta1.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discoveryV1beta1.LabelServiceName: "svc1", + }, }, - Topology: map[string]string{ - corev1.LabelHostname: "node3", + Endpoints: []discoveryV1beta1.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node3", + }, + }, }, }, }, }, }, - "v1beta1.EndpointSlice: topologyKeys is kubernetes.io/zone": { - object: &discoveryV1beta1.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, - Endpoints: []discoveryV1beta1.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", - }, - Topology: map[string]string{ - corev1.LabelHostname: currentNodeName, - }, - }, - { - Addresses: []string{ - "10.244.1.3", - }, - Topology: map[string]string{ - corev1.LabelHostname: "node2", - }, - }, - { - Addresses: []string{ - "10.244.1.4", - }, - Topology: map[string]string{ - corev1.LabelHostname: currentNodeName, - }, - }, - { - Addresses: []string{ - "10.244.1.5", + "v1beta1.EndpointSliceList: topologyKeys is kubernetes.io/zone": { + path: "/apis/discovery.k8s.io/v1beta1/endpointslices", + object: &discoveryV1beta1.EndpointSliceList{ + Items: []discoveryV1beta1.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discoveryV1beta1.LabelServiceName: "svc1", + }, }, - Topology: map[string]string{ - corev1.LabelHostname: "node3", + Endpoints: []discoveryV1beta1.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.3", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node2", + }, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node3", + }, + }, }, }, }, @@ -434,82 +482,95 @@ func TestServiceTopologyHandler(t *testing.T) { }, }, ), - expectResult: &discoveryV1beta1.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, - Endpoints: []discoveryV1beta1.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", - }, - Topology: map[string]string{ - corev1.LabelHostname: currentNodeName, - }, - }, - { - Addresses: []string{ - "10.244.1.4", - }, - Topology: map[string]string{ - corev1.LabelHostname: currentNodeName, - }, - }, - { - Addresses: []string{ - "10.244.1.5", + expectResult: &discoveryV1beta1.EndpointSliceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointSliceList", + APIVersion: "discovery.k8s.io/v1beta1", + }, + Items: []discoveryV1beta1.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discoveryV1beta1.LabelServiceName: "svc1", + }, }, - Topology: map[string]string{ - corev1.LabelHostname: "node3", + Endpoints: []discoveryV1beta1.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node3", + }, + }, }, }, }, }, }, - "v1beta1.EndpointSlice: without openyurt.io/topologyKeys": { - object: &discoveryV1beta1.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, - Endpoints: []discoveryV1beta1.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", - }, - Topology: map[string]string{ - corev1.LabelHostname: currentNodeName, - }, - }, - { - Addresses: []string{ - "10.244.1.3", - }, - Topology: map[string]string{ - corev1.LabelHostname: "node2", - }, - }, - { - Addresses: []string{ - "10.244.1.4", - }, - Topology: map[string]string{ - corev1.LabelHostname: currentNodeName, - }, - }, - { - Addresses: []string{ - "10.244.1.5", + "v1beta1.EndpointSliceList: without openyurt.io/topologyKeys": { + path: "/apis/discovery.k8s.io/v1beta1/endpointslices", + object: &discoveryV1beta1.EndpointSliceList{ + Items: []discoveryV1beta1.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discoveryV1beta1.LabelServiceName: "svc1", + }, }, - Topology: map[string]string{ - corev1.LabelHostname: "node3", + Endpoints: []discoveryV1beta1.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.3", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node2", + }, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node3", + }, + }, }, }, }, @@ -576,90 +637,103 @@ func TestServiceTopologyHandler(t *testing.T) { }, }, ), - expectResult: &discoveryV1beta1.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, - Endpoints: []discoveryV1beta1.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", - }, - Topology: map[string]string{ - corev1.LabelHostname: currentNodeName, - }, - }, - { - Addresses: []string{ - "10.244.1.3", - }, - Topology: map[string]string{ - corev1.LabelHostname: "node2", - }, - }, - { - Addresses: []string{ - "10.244.1.4", - }, - Topology: map[string]string{ - corev1.LabelHostname: currentNodeName, - }, - }, - { - Addresses: []string{ - "10.244.1.5", + expectResult: &discoveryV1beta1.EndpointSliceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointSliceList", + APIVersion: "discovery.k8s.io/v1beta1", + }, + Items: []discoveryV1beta1.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discoveryV1beta1.LabelServiceName: "svc1", + }, }, - Topology: map[string]string{ - corev1.LabelHostname: "node3", + Endpoints: []discoveryV1beta1.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.3", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node2", + }, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node3", + }, + }, }, }, }, }, }, - "v1beta1.EndpointSlice: currentNode is not in any nodepool": { - object: &discoveryV1beta1.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, - Endpoints: []discoveryV1beta1.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", - }, - Topology: map[string]string{ - corev1.LabelHostname: currentNodeName, - }, - }, - { - Addresses: []string{ - "10.244.1.3", - }, - Topology: map[string]string{ - corev1.LabelHostname: "node2", - }, - }, - { - Addresses: []string{ - "10.244.1.4", - }, - Topology: map[string]string{ - corev1.LabelHostname: currentNodeName, - }, - }, - { - Addresses: []string{ - "10.244.1.5", + "v1beta1.EndpointSliceList: currentNode is not in any nodepool": { + path: "/apis/discovery.k8s.io/v1beta1/endpointslices", + object: &discoveryV1beta1.EndpointSliceList{ + Items: []discoveryV1beta1.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discoveryV1beta1.LabelServiceName: "svc1", + }, }, - Topology: map[string]string{ - corev1.LabelHostname: "node3", + Endpoints: []discoveryV1beta1.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.3", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node2", + }, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node3", + }, + }, }, }, }, @@ -725,74 +799,218 @@ func TestServiceTopologyHandler(t *testing.T) { }, }, ), - expectResult: &discoveryV1beta1.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, - Endpoints: []discoveryV1beta1.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", + expectResult: &discoveryV1beta1.EndpointSliceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointSliceList", + APIVersion: "discovery.k8s.io/v1beta1", + }, + Items: []discoveryV1beta1.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discoveryV1beta1.LabelServiceName: "svc1", + }, }, - Topology: map[string]string{ - corev1.LabelHostname: currentNodeName, + Endpoints: []discoveryV1beta1.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, }, }, - { - Addresses: []string{ - "10.244.1.4", + }, + }, + }, + "v1beta1.EndpointSliceList: currentNode has no endpoints on node": { + path: "/apis/discovery.k8s.io/v1beta1/endpointslices", + object: &discoveryV1beta1.EndpointSliceList{ + Items: []discoveryV1beta1.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discoveryV1beta1.LabelServiceName: "svc1", + }, }, - Topology: map[string]string{ - corev1.LabelHostname: currentNodeName, + Endpoints: []discoveryV1beta1.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + Topology: map[string]string{ + corev1.LabelHostname: nodeName2, + }, + }, + { + Addresses: []string{ + "10.244.1.3", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node2", + }, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + Topology: map[string]string{ + corev1.LabelHostname: nodeName3, + }, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node3", + }, + }, }, }, }, }, - }, - "v1beta1.EndpointSlice: currentNode has no endpoints on node": { - object: &discoveryV1beta1.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", + kubeClient: k8sfake.NewSimpleClientset( + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: currentNodeName, + Labels: map[string]string{ + nodepoolv1alpha1.LabelCurrentNodePool: "shanghai", + }, }, }, - Endpoints: []discoveryV1beta1.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node2", + Labels: map[string]string{ + nodepoolv1alpha1.LabelCurrentNodePool: "hangzhou", }, - Topology: map[string]string{ - corev1.LabelHostname: nodeName2, + }, + }, + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node3", + Labels: map[string]string{ + nodepoolv1alpha1.LabelCurrentNodePool: "hangzhou", }, }, - { - Addresses: []string{ - "10.244.1.3", + }, + &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + AnnotationServiceTopologyKey: AnnotationServiceTopologyValueNode, }, - Topology: map[string]string{ - corev1.LabelHostname: "node2", + }, + }, + ), + yurtClient: yurtfake.NewSimpleClientset( + &nodepoolv1alpha1.NodePool{ + ObjectMeta: metav1.ObjectMeta{ + Name: "hangzhou", + }, + Spec: nodepoolv1alpha1.NodePoolSpec{ + Type: nodepoolv1alpha1.Edge, + }, + Status: nodepoolv1alpha1.NodePoolStatus{ + Nodes: []string{ + currentNodeName, + "node3", }, }, - { - Addresses: []string{ - "10.244.1.4", + }, + &nodepoolv1alpha1.NodePool{ + ObjectMeta: metav1.ObjectMeta{ + Name: "shanghai", + }, + Spec: nodepoolv1alpha1.NodePoolSpec{ + Type: nodepoolv1alpha1.Edge, + }, + Status: nodepoolv1alpha1.NodePoolStatus{ + Nodes: []string{ + "node2", }, - Topology: map[string]string{ - corev1.LabelHostname: nodeName3, + }, + }, + ), + expectResult: &discoveryV1beta1.EndpointSliceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointSliceList", + APIVersion: "discovery.k8s.io/v1beta1", + }, + Items: []discoveryV1beta1.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discoveryV1beta1.LabelServiceName: "svc1", + }, }, }, - { - Addresses: []string{ - "10.244.1.5", + }, + }, + }, + "v1beta1.EndpointSliceList: currentNode has no endpoints in nodepool": { + path: "/apis/discovery.k8s.io/v1beta1/endpointslices", + object: &discoveryV1beta1.EndpointSliceList{ + Items: []discoveryV1beta1.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discoveryV1beta1.LabelServiceName: "svc1", + }, }, - Topology: map[string]string{ - corev1.LabelHostname: "node3", + Endpoints: []discoveryV1beta1.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + Topology: map[string]string{ + corev1.LabelHostname: nodeName2, + }, + }, + { + Addresses: []string{ + "10.244.1.3", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node2", + }, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + Topology: map[string]string{ + corev1.LabelHostname: nodeName3, + }, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node3", + }, + }, }, }, }, @@ -827,7 +1045,7 @@ func TestServiceTopologyHandler(t *testing.T) { Name: "svc1", Namespace: "default", Annotations: map[string]string{ - AnnotationServiceTopologyKey: AnnotationServiceTopologyValueNode, + AnnotationServiceTopologyKey: AnnotationServiceTopologyValueNodePool, }, }, }, @@ -860,49 +1078,67 @@ func TestServiceTopologyHandler(t *testing.T) { }, }, }, - ), - expectResult: nil, - }, - "v1beta1.EndpointSlice: currentNode has no endpoints in nodepool": { - object: &discoveryV1beta1.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, - Endpoints: []discoveryV1beta1.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", - }, - Topology: map[string]string{ - corev1.LabelHostname: nodeName2, - }, - }, - { - Addresses: []string{ - "10.244.1.3", - }, - Topology: map[string]string{ - corev1.LabelHostname: "node2", - }, - }, - { - Addresses: []string{ - "10.244.1.4", - }, - Topology: map[string]string{ - corev1.LabelHostname: nodeName3, + ), + expectResult: &discoveryV1beta1.EndpointSliceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointSliceList", + APIVersion: "discovery.k8s.io/v1beta1", + }, + Items: []discoveryV1beta1.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discoveryV1beta1.LabelServiceName: "svc1", + }, }, }, + }, + }, + }, + "v1beta1.EndpointSliceList: no service info in endpointslice": { + path: "/apis/discovery.k8s.io/v1beta1/endpointslices", + object: &discoveryV1beta1.EndpointSliceList{ + Items: []discoveryV1beta1.EndpointSlice{ { - Addresses: []string{ - "10.244.1.5", + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", }, - Topology: map[string]string{ - corev1.LabelHostname: "node3", + Endpoints: []discoveryV1beta1.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.3", + }, + Topology: map[string]string{ + corev1.LabelHostname: nodeName2, + }, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + Topology: map[string]string{ + corev1.LabelHostname: nodeName3, + }, + }, }, }, }, @@ -971,41 +1207,93 @@ func TestServiceTopologyHandler(t *testing.T) { }, }, ), - expectResult: nil, - }, - "v1.EndpointSlice: topologyKeys is kubernetes.io/hostname": { - object: &discovery.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, + expectResult: &discoveryV1beta1.EndpointSliceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointSliceList", + APIVersion: "discovery.k8s.io/v1beta1", }, - Endpoints: []discovery.Endpoint{ + Items: []discoveryV1beta1.EndpointSlice{ { - Addresses: []string{ - "10.244.1.2", + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.3", + Endpoints: []discoveryV1beta1.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.3", + }, + Topology: map[string]string{ + corev1.LabelHostname: nodeName2, + }, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + Topology: map[string]string{ + corev1.LabelHostname: nodeName3, + }, + }, }, - NodeName: &nodeName2, }, - { - Addresses: []string{ - "10.244.1.4", + }, + }, + }, + "v1.EndpointSliceList: topologyKeys is kubernetes.io/hostname": { + path: "/apis/discovery.k8s.io/v1/endpointslices", + object: &discovery.EndpointSliceList{ + Items: []discovery.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discovery.LabelServiceName: "svc1", + }, }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.5", + Endpoints: []discovery.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.3", + }, + NodeName: &nodeName2, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + NodeName: &nodeName3, + }, }, - NodeName: &nodeName3, }, }, }, @@ -1073,63 +1361,76 @@ func TestServiceTopologyHandler(t *testing.T) { }, }, ), - expectResult: &discovery.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, - Endpoints: []discovery.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", + expectResult: &discovery.EndpointSliceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointSliceList", + APIVersion: "discovery.k8s.io/v1", + }, + Items: []discovery.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discovery.LabelServiceName: "svc1", + }, }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.4", + Endpoints: []discovery.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + NodeName: ¤tNodeName, + }, }, - NodeName: ¤tNodeName, }, }, }, }, - "v1.EndpointSlice: topologyKeys is openyurt.io/nodepool": { - object: &discovery.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, - Endpoints: []discovery.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", - }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.3", - }, - NodeName: &nodeName2, - }, - { - Addresses: []string{ - "10.244.1.4", + "v1.EndpointSliceList: topologyKeys is openyurt.io/nodepool": { + path: "/apis/discovery.k8s.io/v1/endpointslices", + object: &discovery.EndpointSliceList{ + Items: []discovery.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discovery.LabelServiceName: "svc1", + }, }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.5", + Endpoints: []discovery.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.3", + }, + NodeName: &nodeName2, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + NodeName: &nodeName3, + }, }, - NodeName: &nodeName3, }, }, }, @@ -1197,69 +1498,82 @@ func TestServiceTopologyHandler(t *testing.T) { }, }, ), - expectResult: &discovery.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, - Endpoints: []discovery.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", - }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.4", + expectResult: &discovery.EndpointSliceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointSliceList", + APIVersion: "discovery.k8s.io/v1", + }, + Items: []discovery.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discovery.LabelServiceName: "svc1", + }, }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.5", + Endpoints: []discovery.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + NodeName: &nodeName3, + }, }, - NodeName: &nodeName3, }, }, }, }, - "v1.EndpointSlice: topologyKeys is kubernetes.io/zone": { - object: &discovery.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, - Endpoints: []discovery.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", - }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.3", - }, - NodeName: &nodeName2, - }, - { - Addresses: []string{ - "10.244.1.4", + "v1.EndpointSliceList: topologyKeys is kubernetes.io/zone": { + path: "/apis/discovery.k8s.io/v1/endpointslices", + object: &discovery.EndpointSliceList{ + Items: []discovery.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discovery.LabelServiceName: "svc1", + }, }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.5", + Endpoints: []discovery.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.3", + }, + NodeName: &nodeName2, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + NodeName: &nodeName3, + }, }, - NodeName: &nodeName3, }, }, }, @@ -1327,69 +1641,82 @@ func TestServiceTopologyHandler(t *testing.T) { }, }, ), - expectResult: &discovery.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, - Endpoints: []discovery.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", - }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.4", + expectResult: &discovery.EndpointSliceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointSliceList", + APIVersion: "discovery.k8s.io/v1", + }, + Items: []discovery.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discovery.LabelServiceName: "svc1", + }, }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.5", + Endpoints: []discovery.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + NodeName: &nodeName3, + }, }, - NodeName: &nodeName3, }, }, }, }, - "v1.EndpointSlice: without openyurt.io/topologyKeys": { - object: &discovery.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, - Endpoints: []discovery.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", - }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.3", - }, - NodeName: &nodeName2, - }, - { - Addresses: []string{ - "10.244.1.4", + "v1.EndpointSliceList: without openyurt.io/topologyKeys": { + path: "/apis/discovery.k8s.io/v1/endpointslices", + object: &discovery.EndpointSliceList{ + Items: []discovery.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discovery.LabelServiceName: "svc1", + }, }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.5", + Endpoints: []discovery.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.3", + }, + NodeName: &nodeName2, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + NodeName: &nodeName3, + }, }, - NodeName: &nodeName3, }, }, }, @@ -1455,75 +1782,88 @@ func TestServiceTopologyHandler(t *testing.T) { }, }, ), - expectResult: &discovery.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, - Endpoints: []discovery.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", - }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.3", - }, - NodeName: &nodeName2, - }, - { - Addresses: []string{ - "10.244.1.4", + expectResult: &discovery.EndpointSliceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointSliceList", + APIVersion: "discovery.k8s.io/v1", + }, + Items: []discovery.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discovery.LabelServiceName: "svc1", + }, }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.5", + Endpoints: []discovery.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.3", + }, + NodeName: &nodeName2, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + NodeName: &nodeName3, + }, }, - NodeName: &nodeName3, }, }, }, }, - "v1.EndpointSlice: currentNode is not in any nodepool": { - object: &discovery.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, - Endpoints: []discovery.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", - }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.3", - }, - NodeName: &nodeName2, - }, - { - Addresses: []string{ - "10.244.1.4", + "v1.EndpointSliceList: currentNode is not in any nodepool": { + path: "/apis/discovery.k8s.io/v1/endpointslices", + object: &discovery.EndpointSliceList{ + Items: []discovery.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discovery.LabelServiceName: "svc1", + }, }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.5", + Endpoints: []discovery.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.3", + }, + NodeName: &nodeName2, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + NodeName: &nodeName3, + }, }, - NodeName: &nodeName3, }, }, }, @@ -1588,51 +1928,64 @@ func TestServiceTopologyHandler(t *testing.T) { }, }, ), - expectResult: &discovery.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, - Endpoints: []discovery.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", + expectResult: &discovery.EndpointSliceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointSliceList", + APIVersion: "discovery.k8s.io/v1", + }, + Items: []discovery.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discovery.LabelServiceName: "svc1", + }, }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.4", + Endpoints: []discovery.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + NodeName: ¤tNodeName, + }, }, - NodeName: ¤tNodeName, }, }, }, }, - "v1.EndpointSlice: currentNode has no endpoints on node": { - object: &discovery.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, - Endpoints: []discovery.Endpoint{ - { - Addresses: []string{ - "10.244.1.3", + "v1.EndpointSliceList: currentNode has no endpoints on node": { + path: "/apis/discovery.k8s.io/v1/endpointslices", + object: &discovery.EndpointSliceList{ + Items: []discovery.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discovery.LabelServiceName: "svc1", + }, }, - NodeName: &nodeName2, - }, - { - Addresses: []string{ - "10.244.1.5", + Endpoints: []discovery.Endpoint{ + { + Addresses: []string{ + "10.244.1.3", + }, + NodeName: &nodeName2, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + NodeName: &nodeName3, + }, }, - NodeName: &nodeName3, }, }, }, @@ -1700,29 +2053,50 @@ func TestServiceTopologyHandler(t *testing.T) { }, }, ), - expectResult: nil, - }, - "v1.EndpointSlice: currentNode has no endpoints in nodePool": { - object: &discovery.EndpointSlice{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", + expectResult: &discovery.EndpointSliceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointSliceList", + APIVersion: "discovery.k8s.io/v1", + }, + Items: []discovery.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discovery.LabelServiceName: "svc1", + }, + }, }, }, - Endpoints: []discovery.Endpoint{ - { - Addresses: []string{ - "10.244.1.3", + }, + }, + "v1.EndpointSliceList: currentNode has no endpoints in nodePool": { + path: "/apis/discovery.k8s.io/v1/endpointslices", + object: &discovery.EndpointSliceList{ + Items: []discovery.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discovery.LabelServiceName: "svc1", + }, }, - NodeName: &nodeName2, - }, - { - Addresses: []string{ - "10.244.1.5", + Endpoints: []discovery.Endpoint{ + { + Addresses: []string{ + "10.244.1.3", + }, + NodeName: &nodeName2, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + NodeName: &nodeName3, + }, }, - NodeName: &nodeName3, }, }, }, @@ -1790,32 +2164,57 @@ func TestServiceTopologyHandler(t *testing.T) { }, }, ), - expectResult: nil, + expectResult: &discovery.EndpointSliceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointSliceList", + APIVersion: "discovery.k8s.io/v1", + }, + Items: []discovery.EndpointSlice{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discovery.LabelServiceName: "svc1", + }, + }, + }, + }, + }, }, - "v1.Endpoints: topologyKeys is kubernetes.io/hostname": { - object: &corev1.Endpoints{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", + "v1.EndpointsList: topologyKeys is kubernetes.io/hostname": { + path: "/api/v1/endpoints", + object: &corev1.EndpointsList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointsList", + APIVersion: "v1", }, - Subsets: []corev1.EndpointSubset{ + Items: []corev1.Endpoints{ { - Addresses: []corev1.EndpointAddress{ - { - IP: "10.244.1.2", - NodeName: ¤tNodeName, - }, - { - IP: "10.244.1.3", - NodeName: &nodeName2, - }, - { - IP: "10.244.1.4", - NodeName: ¤tNodeName, - }, + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + }, + Subsets: []corev1.EndpointSubset{ { - IP: "10.244.1.5", - NodeName: &nodeName3, + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.2", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.3", + NodeName: &nodeName2, + }, + { + IP: "10.244.1.4", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.5", + NodeName: &nodeName3, + }, + }, }, }, }, @@ -1885,54 +2284,68 @@ func TestServiceTopologyHandler(t *testing.T) { }, }, ), - expectResult: &corev1.Endpoints{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", + expectResult: &corev1.EndpointsList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointsList", + APIVersion: "v1", }, - Subsets: []corev1.EndpointSubset{ + Items: []corev1.Endpoints{ { - Addresses: []corev1.EndpointAddress{ - { - IP: "10.244.1.2", - NodeName: ¤tNodeName, - }, + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + }, + Subsets: []corev1.EndpointSubset{ { - IP: "10.244.1.4", - NodeName: ¤tNodeName, + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.2", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.4", + NodeName: ¤tNodeName, + }, + }, }, }, }, }, }, }, - "v1.Endpoints: topologyKeys is openyurt.io/nodepool": { - object: &corev1.Endpoints{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", + "v1.EndpointsList: topologyKeys is openyurt.io/nodepool": { + path: "/api/v1/endpoints", + object: &corev1.EndpointsList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointsList", + APIVersion: "v1", }, - Subsets: []corev1.EndpointSubset{ + Items: []corev1.Endpoints{ { - Addresses: []corev1.EndpointAddress{ - { - IP: "10.244.1.2", - NodeName: ¤tNodeName, - }, - { - IP: "10.244.1.3", - NodeName: &nodeName2, - }, - { - IP: "10.244.1.4", - NodeName: ¤tNodeName, - }, - { - IP: "10.244.1.5", - NodeName: &nodeName3, - }, + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + }, + Subsets: []corev1.EndpointSubset{ { - IP: "10.244.1.6", + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.2", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.3", + NodeName: &nodeName2, + }, + { + IP: "10.244.1.4", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.5", + NodeName: &nodeName3, + }, + }, }, }, }, @@ -2002,55 +2415,72 @@ func TestServiceTopologyHandler(t *testing.T) { }, }, ), - expectResult: &corev1.Endpoints{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", + expectResult: &corev1.EndpointsList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointsList", + APIVersion: "v1", }, - Subsets: []corev1.EndpointSubset{ + Items: []corev1.Endpoints{ { - Addresses: []corev1.EndpointAddress{ - { - IP: "10.244.1.2", - NodeName: ¤tNodeName, - }, - { - IP: "10.244.1.4", - NodeName: ¤tNodeName, - }, + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + }, + Subsets: []corev1.EndpointSubset{ { - IP: "10.244.1.5", - NodeName: &nodeName3, + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.2", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.4", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.5", + NodeName: &nodeName3, + }, + }, }, }, }, }, }, }, - "v1.Endpoints: topologyKeys is kubernetes.io/zone": { - object: &corev1.Endpoints{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", + "v1.EndpointsList: topologyKeys is kubernetes.io/zone": { + path: "/api/v1/endpoints", + object: &corev1.EndpointsList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointsList", + APIVersion: "v1", }, - Subsets: []corev1.EndpointSubset{ + Items: []corev1.Endpoints{ { - Addresses: []corev1.EndpointAddress{ - { - IP: "10.244.1.2", - NodeName: ¤tNodeName, - }, - { - IP: "10.244.1.3", - NodeName: &nodeName2, - }, - { - IP: "10.244.1.4", - NodeName: ¤tNodeName, - }, + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + }, + Subsets: []corev1.EndpointSubset{ { - IP: "10.244.1.5", - NodeName: &nodeName3, + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.2", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.3", + NodeName: &nodeName2, + }, + { + IP: "10.244.1.4", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.5", + NodeName: &nodeName3, + }, + }, }, }, }, @@ -2120,55 +2550,72 @@ func TestServiceTopologyHandler(t *testing.T) { }, }, ), - expectResult: &corev1.Endpoints{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", + expectResult: &corev1.EndpointsList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointsList", + APIVersion: "v1", }, - Subsets: []corev1.EndpointSubset{ + Items: []corev1.Endpoints{ { - Addresses: []corev1.EndpointAddress{ - { - IP: "10.244.1.2", - NodeName: ¤tNodeName, - }, - { - IP: "10.244.1.4", - NodeName: ¤tNodeName, - }, + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + }, + Subsets: []corev1.EndpointSubset{ { - IP: "10.244.1.5", - NodeName: &nodeName3, + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.2", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.4", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.5", + NodeName: &nodeName3, + }, + }, }, }, }, }, }, }, - "v1.Endpoints: without openyurt.io/topologyKeys": { - object: &corev1.Endpoints{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", + "v1.EndpointsList: without openyurt.io/topologyKeys": { + path: "/api/v1/endpoints", + object: &corev1.EndpointsList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointsList", + APIVersion: "v1", }, - Subsets: []corev1.EndpointSubset{ + Items: []corev1.Endpoints{ { - Addresses: []corev1.EndpointAddress{ - { - IP: "10.244.1.2", - NodeName: ¤tNodeName, - }, - { - IP: "10.244.1.3", - NodeName: &nodeName2, - }, - { - IP: "10.244.1.4", - NodeName: ¤tNodeName, - }, + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + }, + Subsets: []corev1.EndpointSubset{ { - IP: "10.244.1.5", - NodeName: &nodeName3, + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.2", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.3", + NodeName: &nodeName2, + }, + { + IP: "10.244.1.4", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.5", + NodeName: &nodeName3, + }, + }, }, }, }, @@ -2236,59 +2683,76 @@ func TestServiceTopologyHandler(t *testing.T) { }, }, ), - expectResult: &corev1.Endpoints{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", + expectResult: &corev1.EndpointsList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointsList", + APIVersion: "v1", }, - Subsets: []corev1.EndpointSubset{ + Items: []corev1.Endpoints{ { - Addresses: []corev1.EndpointAddress{ - { - IP: "10.244.1.2", - NodeName: ¤tNodeName, - }, - { - IP: "10.244.1.3", - NodeName: &nodeName2, - }, - { - IP: "10.244.1.4", - NodeName: ¤tNodeName, - }, + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + }, + Subsets: []corev1.EndpointSubset{ { - IP: "10.244.1.5", - NodeName: &nodeName3, + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.2", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.3", + NodeName: &nodeName2, + }, + { + IP: "10.244.1.4", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.5", + NodeName: &nodeName3, + }, + }, }, }, }, }, }, }, - "v1.Endpoints: currentNode is not in any nodepool": { - object: &corev1.Endpoints{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", + "v1.EndpointsList: currentNode is not in any nodepool": { + path: "/api/v1/endpoints", + object: &corev1.EndpointsList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointsList", + APIVersion: "v1", }, - Subsets: []corev1.EndpointSubset{ + Items: []corev1.Endpoints{ { - Addresses: []corev1.EndpointAddress{ - { - IP: "10.244.1.2", - NodeName: ¤tNodeName, - }, - { - IP: "10.244.1.3", - NodeName: &nodeName2, - }, - { - IP: "10.244.1.4", - NodeName: ¤tNodeName, - }, + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + }, + Subsets: []corev1.EndpointSubset{ { - IP: "10.244.1.5", - NodeName: &nodeName3, + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.2", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.3", + NodeName: &nodeName2, + }, + { + IP: "10.244.1.4", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.5", + NodeName: &nodeName3, + }, + }, }, }, }, @@ -2355,43 +2819,60 @@ func TestServiceTopologyHandler(t *testing.T) { }, }, ), - expectResult: &corev1.Endpoints{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", + expectResult: &corev1.EndpointsList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointsList", + APIVersion: "v1", }, - Subsets: []corev1.EndpointSubset{ + Items: []corev1.Endpoints{ { - Addresses: []corev1.EndpointAddress{ - { - IP: "10.244.1.2", - NodeName: ¤tNodeName, - }, + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + }, + Subsets: []corev1.EndpointSubset{ { - IP: "10.244.1.4", - NodeName: ¤tNodeName, + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.2", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.4", + NodeName: ¤tNodeName, + }, + }, }, }, }, }, }, }, - "v1.Endpoints: currentNode has no endpoints on node": { - object: &corev1.Endpoints{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", + "v1.EndpointsList: currentNode has no endpoints on node": { + path: "/api/v1/endpoints", + object: &corev1.EndpointsList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointsList", + APIVersion: "v1", }, - Subsets: []corev1.EndpointSubset{ + Items: []corev1.Endpoints{ { - Addresses: []corev1.EndpointAddress{ - { - IP: "10.244.1.3", - NodeName: &nodeName2, - }, + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + }, + Subsets: []corev1.EndpointSubset{ { - IP: "10.244.1.4", - NodeName: &nodeName3, + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.3", + NodeName: &nodeName2, + }, + { + IP: "10.244.1.5", + NodeName: &nodeName3, + }, + }, }, }, }, @@ -2461,24 +2942,46 @@ func TestServiceTopologyHandler(t *testing.T) { }, }, ), - expectResult: nil, + expectResult: &corev1.EndpointsList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointsList", + APIVersion: "v1", + }, + Items: []corev1.Endpoints{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + }, + }, + }, + }, }, - "v1.Endpoints: currentNode has no endpoints in nodepool": { - object: &corev1.Endpoints{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", + "v1.EndpointsList: currentNode has no endpoints in nodepool": { + path: "/api/v1/endpoints", + object: &corev1.EndpointsList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointsList", + APIVersion: "v1", }, - Subsets: []corev1.EndpointSubset{ + Items: []corev1.Endpoints{ { - Addresses: []corev1.EndpointAddress{ - { - IP: "10.244.1.3", - NodeName: &nodeName2, - }, + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + }, + Subsets: []corev1.EndpointSubset{ { - IP: "10.244.1.4", - NodeName: &nodeName3, + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.3", + NodeName: &nodeName2, + }, + { + IP: "10.244.1.5", + NodeName: &nodeName3, + }, + }, }, }, }, @@ -2548,24 +3051,46 @@ func TestServiceTopologyHandler(t *testing.T) { }, }, ), - expectResult: nil, + expectResult: &corev1.EndpointsList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointsList", + APIVersion: "v1", + }, + Items: []corev1.Endpoints{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + }, + }, + }, + }, }, - "v1.Endpoints: unknown openyurt.io/topologyKeys": { - object: &corev1.Endpoints{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", + "v1.EndpointsList: unknown openyurt.io/topologyKeys": { + path: "/api/v1/endpoints", + object: &corev1.EndpointsList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointsList", + APIVersion: "v1", }, - Subsets: []corev1.EndpointSubset{ + Items: []corev1.Endpoints{ { - Addresses: []corev1.EndpointAddress{ - { - IP: "10.244.1.3", - NodeName: &nodeName2, - }, + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + }, + Subsets: []corev1.EndpointSubset{ { - IP: "10.244.1.4", - NodeName: &nodeName3, + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.3", + NodeName: &nodeName2, + }, + { + IP: "10.244.1.5", + NodeName: &nodeName3, + }, + }, }, }, }, @@ -2635,29 +3160,122 @@ func TestServiceTopologyHandler(t *testing.T) { }, }, ), - expectResult: &corev1.Endpoints{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", + expectResult: &corev1.EndpointsList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointsList", + APIVersion: "v1", }, - Subsets: []corev1.EndpointSubset{ + Items: []corev1.Endpoints{ { - Addresses: []corev1.EndpointAddress{ - { - IP: "10.244.1.3", - NodeName: &nodeName2, - }, + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + }, + Subsets: []corev1.EndpointSubset{ { - IP: "10.244.1.4", - NodeName: &nodeName3, + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.3", + NodeName: &nodeName2, + }, + { + IP: "10.244.1.5", + NodeName: &nodeName3, + }, + }, }, }, }, }, }, }, + "v1.Pod: un-recognized object for filter": { + path: "/api/v1/pods", + object: &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pod1", + Namespace: "default", + }, + }, + kubeClient: k8sfake.NewSimpleClientset( + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: currentNodeName, + Labels: map[string]string{ + nodepoolv1alpha1.LabelCurrentNodePool: "hangzhou", + }, + }, + }, + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node2", + Labels: map[string]string{ + nodepoolv1alpha1.LabelCurrentNodePool: "shanghai", + }, + }, + }, + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node3", + Labels: map[string]string{ + nodepoolv1alpha1.LabelCurrentNodePool: "shanghai", + }, + }, + }, + &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + AnnotationServiceTopologyKey: "unknown topology", + }, + }, + }, + ), + yurtClient: yurtfake.NewSimpleClientset( + &nodepoolv1alpha1.NodePool{ + ObjectMeta: metav1.ObjectMeta{ + Name: "hangzhou", + }, + Spec: nodepoolv1alpha1.NodePoolSpec{ + Type: nodepoolv1alpha1.Edge, + }, + Status: nodepoolv1alpha1.NodePoolStatus{ + Nodes: []string{ + currentNodeName, + }, + }, + }, + &nodepoolv1alpha1.NodePool{ + ObjectMeta: metav1.ObjectMeta{ + Name: "shanghai", + }, + Spec: nodepoolv1alpha1.NodePoolSpec{ + Type: nodepoolv1alpha1.Edge, + }, + Status: nodepoolv1alpha1.NodePoolStatus{ + Nodes: []string{ + "node2", + "node3", + }, + }, + }, + ), + expectResult: &corev1.Pod{ + TypeMeta: metav1.TypeMeta{ + Kind: "Pod", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "pod1", + Namespace: "default", + }, + }, + }, } + resolver := newTestRequestInfoResolver() + sw := serializer.NewSerializerManager() for k, tt := range testcases { t.Run(k, func(t *testing.T) { //tt.kubeClient.DiscoveryV1beta1().EndpointSlices("default").Create(context.TODO(), tt.endpointSlice, metav1.CreateOptions{}) @@ -2685,26 +3303,36 @@ func TestServiceTopologyHandler(t *testing.T) { return tt.kubeClient.CoreV1().Nodes().Get(context.TODO(), name, metav1.GetOptions{}) } - fh := &serviceTopologyFilterHandler{ - nodeName: currentNodeName, - serviceLister: serviceLister, - nodePoolLister: nodePoolLister, - nodeGetter: nodeGetter, + req, err := http.NewRequest("GET", tt.path, nil) + if err != nil { + t.Errorf("failed to create request, %v", err) } + req.RemoteAddr = "127.0.0.1" + req.Header.Set("Accept", "application/json") - isNil, handledObject := fh.serviceTopologyHandler(tt.object) - if tt.expectResult != nil { - if isNil { - t.Errorf("serviceTopologyHandler expect %v, but got nil", tt.expectResult) + var handledObject runtime.Object + var handler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + ctx := req.Context() + reqContentType, _ := hubutil.ReqContentTypeFrom(ctx) + ctx = hubutil.WithRespContentType(ctx, reqContentType) + req = req.WithContext(ctx) + s := filterutil.CreateSerializer(req, sw) + if s == nil { + t.Fatalf("failed to create serializer, %v", s) } - if !reflect.DeepEqual(handledObject, tt.expectResult) { - t.Errorf("serviceTopologyHandler expect: \n%#+v\nbut got: \n%#+v\n", tt.expectResult, handledObject) - } - } else { - if !isNil { - t.Errorf("serviceTopologyHandler expect nil, but got %v", handledObject) - } + fh := NewServiceTopologyFilterHandler(currentNodeName, s, serviceLister, nodePoolLister, nodeGetter) + inputB, _ := s.Encode(tt.object) + filteredB, _ := fh.ObjectResponseFilter(inputB) + handledObject, _ = s.Decode(filteredB) + }) + + handler = util.WithRequestContentType(handler) + handler = filters.WithRequestInfo(handler, resolver) + handler.ServeHTTP(httptest.NewRecorder(), req) + + if !reflect.DeepEqual(handledObject, tt.expectResult) { + t.Errorf("serviceTopologyHandler expect: \n%#+v\nbut got: \n%#+v\n", tt.expectResult, handledObject) } }) }