diff --git a/pkg/yurthub/filter/base/base_test.go b/pkg/yurthub/filter/base/base_test.go index 4bab85b5c8b..01d9e98a4dc 100644 --- a/pkg/yurthub/filter/base/base_test.go +++ b/pkg/yurthub/filter/base/base_test.go @@ -17,6 +17,8 @@ limitations under the License. package base import ( + "errors" + "fmt" "testing" "time" @@ -25,6 +27,7 @@ import ( "k8s.io/apimachinery/pkg/util/sets" "k8s.io/client-go/dynamic/dynamicinformer" "k8s.io/client-go/dynamic/fake" + "k8s.io/client-go/tools/cache" "github.com/openyurtio/openyurt/pkg/apis" "github.com/openyurtio/openyurt/pkg/yurthub/filter" @@ -47,16 +50,23 @@ func (noh *nopObjectHandler) Filter(obj runtime.Object, stopCh <-chan struct{}) return obj } -func registerLocalFilters(filters *Filters) { - filters.Register("servicetopology", func() (filter.ObjectFilter, error) { - return &nopObjectHandler{name: "servicetopology"}, nil - }) - filters.Register("discardcloudservice", func() (filter.ObjectFilter, error) { - return &nopObjectHandler{name: "discardcloudservice"}, nil - }) - filters.Register("masterservice", func() (filter.ObjectFilter, error) { - return &nopObjectHandler{name: "masterservice"}, nil - }) +var ( + nodesNameErr = errors.New("nodes name error") +) + +type nopNodesErrHandler struct { + nopObjectHandler + err error +} + +func NewNopNodesErrHandler() filter.ObjectFilter { + return &nopNodesErrHandler{ + err: nodesNameErr, + } +} + +func (nneh *nopNodesErrHandler) SetNodesGetterAndSynced(filter.NodesInPoolGetter, cache.InformerSynced, bool) error { + return nneh.err } type nopInitializer struct{} @@ -65,34 +75,87 @@ func (nopInit *nopInitializer) Initialize(_ filter.ObjectFilter) error { return nil } +type errInitializer struct{} + +func (errInit *errInitializer) Initialize(_ filter.ObjectFilter) error { + return fmt.Errorf("error initialize") +} + func TestNewFromFilters(t *testing.T) { allFilters := []string{"masterservice", "discardcloudservice", "servicetopology"} testcases := map[string]struct { + inputFilters []string disabledFilters []string + initializer filter.Initializer generatedFilters sets.String + expectedErr bool }{ "disable master service filter": { + inputFilters: allFilters, disabledFilters: []string{"masterservice"}, generatedFilters: sets.NewString(allFilters...).Delete("masterservice"), }, "disable service topology filter": { + inputFilters: allFilters, disabledFilters: []string{"servicetopology"}, generatedFilters: sets.NewString(allFilters...).Delete("servicetopology"), }, "disable discard cloud service filter": { + inputFilters: allFilters, disabledFilters: []string{"discardcloudservice"}, generatedFilters: sets.NewString(allFilters...).Delete("discardcloudservice"), }, + "disable all filters": { + inputFilters: allFilters, + disabledFilters: []string{"*"}, + generatedFilters: sets.NewString(), + }, + "register duplicated filters": { + inputFilters: append(allFilters, "servicetopology"), + disabledFilters: []string{}, + generatedFilters: sets.NewString(allFilters...), + }, + "a invalid filter": { + inputFilters: append(allFilters, "invalidFilter"), + disabledFilters: []string{}, + generatedFilters: sets.NewString(), + expectedErr: true, + }, + "initialize error": { + inputFilters: allFilters, + disabledFilters: []string{}, + initializer: &errInitializer{}, + generatedFilters: sets.NewString(), + expectedErr: true, + }, } for k, tt := range testcases { t.Run(k, func(t *testing.T) { filters := NewFilters(tt.disabledFilters) - registerLocalFilters(filters) + for i := range tt.inputFilters { + filterName := tt.inputFilters[i] + filters.Register(filterName, func() (filter.ObjectFilter, error) { + if filterName == "invalidFilter" { + return nil, fmt.Errorf("a invalide filter") + } + return &nopObjectHandler{name: filterName}, nil + }) + } - runners, err := filters.NewFromFilters(&nopInitializer{}) - if err != nil { + initializer := tt.initializer + if initializer == nil { + initializer = &nopInitializer{} + } + runners, err := filters.NewFromFilters(initializer) + if err != nil && tt.expectedErr { + return + } else if err != nil && !tt.expectedErr { t.Errorf("failed to new from filters, %v", err) + return + } else if err == nil && tt.expectedErr { + t.Errorf("expect an error, but got nil") + return } gotRunners := sets.NewString() @@ -120,7 +183,26 @@ func TestInitializers(t *testing.T) { nodeInitializer := initializer.NewNodesInitializer(false, true, yurtFactory) initializers = append(initializers, nodeInitializer) - if err := initializers.Initialize(&nopObjectHandler{}); err != nil { - t.Errorf("initialize error, %v", err) + testcases := map[string]struct { + filter filter.ObjectFilter + resultErr error + }{ + "initialize normally": { + filter: &nopObjectHandler{}, + resultErr: nil, + }, + "initialize error": { + filter: NewNopNodesErrHandler(), + resultErr: nodesNameErr, + }, + } + + for k, tc := range testcases { + t.Run(k, func(t *testing.T) { + err := initializers.Initialize(tc.filter) + if !errors.Is(err, tc.resultErr) { + t.Errorf("initialize expect err %v, but got %v", tc.resultErr, err) + } + }) } } diff --git a/pkg/yurthub/filter/discardcloudservice/filter.go b/pkg/yurthub/filter/discardcloudservice/filter.go index 833144aaac0..fa6d3eb551d 100644 --- a/pkg/yurthub/filter/discardcloudservice/filter.go +++ b/pkg/yurthub/filter/discardcloudservice/filter.go @@ -70,16 +70,6 @@ func (sf *discardCloudServiceFilter) SupportedResourceAndVerbs() map[string]sets func (sf *discardCloudServiceFilter) Filter(obj runtime.Object, _ <-chan struct{}) runtime.Object { switch v := obj.(type) { - case *v1.ServiceList: - var svcNew []v1.Service - for i := range v.Items { - svc := discardCloudService(&v.Items[i]) - if svc != nil { - svcNew = append(svcNew, *svc) - } - } - v.Items = svcNew - return v case *v1.Service: return discardCloudService(v) default: diff --git a/pkg/yurthub/filter/discardcloudservice/filter_test.go b/pkg/yurthub/filter/discardcloudservice/filter_test.go index 931fed6fbf1..d3cbc249449 100644 --- a/pkg/yurthub/filter/discardcloudservice/filter_test.go +++ b/pkg/yurthub/filter/discardcloudservice/filter_test.go @@ -26,8 +26,17 @@ import ( "k8s.io/apimachinery/pkg/util/sets" "github.com/openyurtio/openyurt/pkg/util" + "github.com/openyurtio/openyurt/pkg/yurthub/filter/base" ) +func TestRegister(t *testing.T) { + filters := base.NewFilters([]string{}) + Register(filters) + if !filters.Enabled(FilterName) { + t.Errorf("couldn't register %s filter", FilterName) + } +} + func TestName(t *testing.T) { dcsf, _ := NewDiscardCloudServiceFilter() if dcsf.Name() != FilterName { @@ -58,175 +67,6 @@ func TestFilter(t *testing.T) { responseObj runtime.Object expectObj runtime.Object }{ - "discard lb service for serviceList": { - responseObj: &corev1.ServiceList{ - Items: []corev1.Service{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - Annotations: map[string]string{ - DiscardServiceAnnotation: "true", - }, - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.187", - Type: corev1.ServiceTypeLoadBalancer, - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc2", - Namespace: "default", - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.188", - Type: corev1.ServiceTypeClusterIP, - }, - }, - }, - }, - expectObj: &corev1.ServiceList{ - Items: []corev1.Service{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc2", - Namespace: "default", - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.188", - Type: corev1.ServiceTypeClusterIP, - }, - }, - }, - }, - }, - "discard cloud clusterIP service for serviceList": { - responseObj: &corev1.ServiceList{ - Items: []corev1.Service{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "x-tunnel-server-internal-svc", - Namespace: "kube-system", - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.187", - Type: corev1.ServiceTypeLoadBalancer, - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc2", - Namespace: "default", - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.188", - Type: corev1.ServiceTypeClusterIP, - }, - }, - }, - }, - expectObj: &corev1.ServiceList{ - Items: []corev1.Service{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc2", - Namespace: "default", - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.188", - Type: corev1.ServiceTypeClusterIP, - }, - }, - }, - }, - }, - "doesn't discard service for serviceList": { - responseObj: &corev1.ServiceList{ - Items: []corev1.Service{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - Annotations: map[string]string{ - DiscardServiceAnnotation: "false", - }, - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.187", - Type: corev1.ServiceTypeLoadBalancer, - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc2", - Namespace: "default", - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.188", - Type: corev1.ServiceTypeClusterIP, - }, - }, - }, - }, - expectObj: &corev1.ServiceList{ - Items: []corev1.Service{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - Annotations: map[string]string{ - DiscardServiceAnnotation: "false", - }, - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.187", - Type: corev1.ServiceTypeLoadBalancer, - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc2", - Namespace: "default", - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.188", - Type: corev1.ServiceTypeClusterIP, - }, - }, - }, - }, - }, - "discard all services for serviceList": { - responseObj: &corev1.ServiceList{ - Items: []corev1.Service{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - Annotations: map[string]string{ - DiscardServiceAnnotation: "true", - }, - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.187", - Type: corev1.ServiceTypeLoadBalancer, - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "x-tunnel-server-internal-svc", - Namespace: "kube-system", - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.188", - Type: corev1.ServiceTypeClusterIP, - }, - }, - }, - }, - expectObj: &corev1.ServiceList{}, - }, "discard lb service": { responseObj: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ diff --git a/pkg/yurthub/filter/inclusterconfig/filter.go b/pkg/yurthub/filter/inclusterconfig/filter.go index 74facb3ffed..0647888754f 100644 --- a/pkg/yurthub/filter/inclusterconfig/filter.go +++ b/pkg/yurthub/filter/inclusterconfig/filter.go @@ -65,15 +65,6 @@ func (iccf *inClusterConfigFilter) SupportedResourceAndVerbs() map[string]sets.S func (iccf *inClusterConfigFilter) Filter(obj runtime.Object, _ <-chan struct{}) runtime.Object { switch v := obj.(type) { - case *v1.ConfigMapList: - for i := range v.Items { - newCM, mutated := mutateKubeProxyConfigMap(&v.Items[i]) - if mutated { - v.Items[i] = *newCM - break - } - } - return v case *v1.ConfigMap: cm, _ := mutateKubeProxyConfigMap(v) return cm diff --git a/pkg/yurthub/filter/inclusterconfig/filter_test.go b/pkg/yurthub/filter/inclusterconfig/filter_test.go index 8612788464a..694d422f440 100644 --- a/pkg/yurthub/filter/inclusterconfig/filter_test.go +++ b/pkg/yurthub/filter/inclusterconfig/filter_test.go @@ -26,8 +26,17 @@ import ( "k8s.io/apimachinery/pkg/util/sets" "github.com/openyurtio/openyurt/pkg/util" + "github.com/openyurtio/openyurt/pkg/yurthub/filter/base" ) +func TestRegister(t *testing.T) { + filters := base.NewFilters([]string{}) + Register(filters) + if !filters.Enabled(FilterName) { + t.Errorf("couldn't register %s filter", FilterName) + } +} + func TestName(t *testing.T) { iccf, _ := NewInClusterConfigFilter() if iccf.Name() != FilterName { @@ -100,52 +109,6 @@ func TestRuntimeObjectFilter(t *testing.T) { }, }, }, - "configmapList with kube-proxy configmap": { - responseObject: &v1.ConfigMapList{ - Items: []v1.ConfigMap{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "foo", - Namespace: "default", - }, - Data: map[string]string{ - "foo": "bar", - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: KubeProxyConfigMapName, - Namespace: KubeProxyConfigMapNamespace, - }, - Data: map[string]string{ - "config.conf": " apiVersion: kubeproxy.config.k8s.io/v1alpha1\n bindAddress: 0.0.0.0\n bindAddressHardFail: false\n clientConnection:\n acceptContentTypes: \"\"\n burst: 0\n contentType: \"\"\n kubeconfig: /var/lib/kube-proxy/kubeconfig.conf\n qps: 0\n clusterCIDR: 10.244.0.0/16\n configSyncPeriod: 0s", - }, - }, - }, - }, - expectObject: &v1.ConfigMapList{ - Items: []v1.ConfigMap{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "foo", - Namespace: "default", - }, - Data: map[string]string{ - "foo": "bar", - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: KubeProxyConfigMapName, - Namespace: KubeProxyConfigMapNamespace, - }, - Data: map[string]string{ - "config.conf": " apiVersion: kubeproxy.config.k8s.io/v1alpha1\n bindAddress: 0.0.0.0\n bindAddressHardFail: false\n clientConnection:\n acceptContentTypes: \"\"\n burst: 0\n contentType: \"\"\n #kubeconfig: /var/lib/kube-proxy/kubeconfig.conf\n qps: 0\n clusterCIDR: 10.244.0.0/16\n configSyncPeriod: 0s", - }, - }, - }, - }, - }, "not configmapList": { responseObject: &v1.PodList{ Items: []v1.Pod{ diff --git a/pkg/yurthub/filter/initializer/initializer_test.go b/pkg/yurthub/filter/initializer/initializer_test.go index 4ad13669c28..dbaa2892ae1 100644 --- a/pkg/yurthub/filter/initializer/initializer_test.go +++ b/pkg/yurthub/filter/initializer/initializer_test.go @@ -25,6 +25,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/client-go/informers" + "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/fake" "github.com/openyurtio/openyurt/pkg/yurthub/filter" @@ -76,10 +77,30 @@ func TestInitialize(t *testing.T) { fn: servicetopology.NewServiceTopologyFilter, result: nil, }, - "init errfilter filter": { - fn: NewErrFilter, + "init node err filter": { + fn: NewNodeErrFilter, result: nodeNameErr, }, + "init pool err filter": { + fn: NewPoolErrFilter, + result: poolNameErr, + }, + "init master svc host err filter": { + fn: NewMasterSvcHostErrFilter, + result: masterSvcHostErr, + }, + "init master svc port err filter": { + fn: NewMasterSvcPortErrFilter, + result: masterSvcPortErr, + }, + "init factory err filter": { + fn: NewFactoryErrFilter, + result: factoryErr, + }, + "init kube client err filter": { + fn: NewKubeClientErrFilter, + result: kubeClientErr, + }, } fakeClient := &fake.Clientset{} sharedFactory := informers.NewSharedInformerFactory(fakeClient, 24*time.Hour) @@ -102,32 +123,124 @@ func TestInitialize(t *testing.T) { } } -type errFilter struct { - err error +type baseErrFilter struct { +} + +func (bef *baseErrFilter) Name() string { + return "nop" +} + +func (bef *baseErrFilter) SupportedResourceAndVerbs() map[string]sets.String { + return map[string]sets.String{} +} + +func (bef *baseErrFilter) Filter(obj runtime.Object, _ <-chan struct{}) runtime.Object { + return obj } var ( - nodeNameErr = errors.New("node name error") + nodeNameErr = errors.New("node name error") + poolNameErr = errors.New("pool name error") + masterSvcHostErr = errors.New("master svc host error") + masterSvcPortErr = errors.New("master svc port error") + factoryErr = errors.New("factory error") + kubeClientErr = errors.New("kube client error") ) -func NewErrFilter() (filter.ObjectFilter, error) { - return &errFilter{ +type nodeErrFilter struct { + baseErrFilter + err error +} + +func NewNodeErrFilter() (filter.ObjectFilter, error) { + return &nodeErrFilter{ err: nodeNameErr, }, nil } -func (ef *errFilter) Name() string { - return "nop" +func (nef *nodeErrFilter) SetNodeName(nodeName string) error { + return nef.err } -func (ef *errFilter) SupportedResourceAndVerbs() map[string]sets.String { - return map[string]sets.String{} +type poolErrFilter struct { + baseErrFilter + err error } -func (ef *errFilter) Filter(obj runtime.Object, _ <-chan struct{}) runtime.Object { - return obj +func NewPoolErrFilter() (filter.ObjectFilter, error) { + return &poolErrFilter{ + err: poolNameErr, + }, nil +} + +func (pef *poolErrFilter) SetNodePoolName(poolName string) error { + return pef.err +} + +type masterSvcHostErrFilter struct { + baseErrFilter + err error +} + +func NewMasterSvcHostErrFilter() (filter.ObjectFilter, error) { + return &masterSvcHostErrFilter{ + err: masterSvcHostErr, + }, nil +} + +func (mshef *masterSvcHostErrFilter) SetMasterServiceHost(host string) error { + return mshef.err +} + +func (mshef *masterSvcHostErrFilter) SetMasterServicePort(port string) error { + return nil +} + +type masterSvcPortErrFilter struct { + baseErrFilter + err error +} + +func NewMasterSvcPortErrFilter() (filter.ObjectFilter, error) { + return &masterSvcPortErrFilter{ + err: masterSvcPortErr, + }, nil +} + +func (mvpef *masterSvcPortErrFilter) SetMasterServiceHost(host string) error { + return nil +} + +func (mvpef *masterSvcPortErrFilter) SetMasterServicePort(port string) error { + return mvpef.err +} + +type factoryErrFilter struct { + baseErrFilter + err error +} + +func NewFactoryErrFilter() (filter.ObjectFilter, error) { + return &factoryErrFilter{ + err: factoryErr, + }, nil +} + +func (fef *factoryErrFilter) SetSharedInformerFactory(factory informers.SharedInformerFactory) error { + return fef.err +} + +type kubeClientErrFilter struct { + baseErrFilter + err error +} + +func NewKubeClientErrFilter() (filter.ObjectFilter, error) { + return &kubeClientErrFilter{ + err: kubeClientErr, + }, nil } -func (ef *errFilter) SetNodeName(nodeName string) error { - return ef.err +func (kcef *kubeClientErrFilter) SetKubeClient(client kubernetes.Interface) error { + return kcef.err } diff --git a/pkg/yurthub/filter/interfaces.go b/pkg/yurthub/filter/interfaces.go index 5e9dd86515c..0e10ef943e7 100644 --- a/pkg/yurthub/filter/interfaces.go +++ b/pkg/yurthub/filter/interfaces.go @@ -47,8 +47,7 @@ type ResponseFilter interface { } // ObjectFilter is used for filtering runtime object. -// runtime object includes List object(like ServiceList) that has multiple items and -// Standalone object(like Service). +// runtime object is only a standalone object(like Service). // Every Filter need to implement ObjectFilter interface. type ObjectFilter interface { Name() string diff --git a/pkg/yurthub/filter/masterservice/filter.go b/pkg/yurthub/filter/masterservice/filter.go index 4f374070a81..31c53b4c3f5 100644 --- a/pkg/yurthub/filter/masterservice/filter.go +++ b/pkg/yurthub/filter/masterservice/filter.go @@ -81,15 +81,6 @@ func (msf *masterServiceFilter) SetMasterServicePort(portStr string) error { func (msf *masterServiceFilter) Filter(obj runtime.Object, _ <-chan struct{}) runtime.Object { switch v := obj.(type) { - case *v1.ServiceList: - for i := range v.Items { - mutated := msf.mutateMasterService(&v.Items[i]) - if mutated { - // short-circuit break - break - } - } - return v case *v1.Service: msf.mutateMasterService(v) return v @@ -109,7 +100,7 @@ func (msf *masterServiceFilter) mutateMasterService(svc *v1.Service) bool { break } } - klog.V(2).Infof("mutate master service with ClusterIP:Port=%s:%d", msf.host, msf.port) + klog.Infof("mutate master service with ClusterIP:Port=%s:%d", msf.host, msf.port) } return mutated } diff --git a/pkg/yurthub/filter/masterservice/filter_test.go b/pkg/yurthub/filter/masterservice/filter_test.go index d6c19b53315..e1e390a4368 100644 --- a/pkg/yurthub/filter/masterservice/filter_test.go +++ b/pkg/yurthub/filter/masterservice/filter_test.go @@ -26,8 +26,17 @@ import ( "k8s.io/apimachinery/pkg/util/sets" "github.com/openyurtio/openyurt/pkg/util" + "github.com/openyurtio/openyurt/pkg/yurthub/filter/base" ) +func TestRegister(t *testing.T) { + filters := base.NewFilters([]string{}) + Register(filters) + if !filters.Enabled(FilterName) { + t.Errorf("couldn't register %s filter", FilterName) + } +} + func TestName(t *testing.T) { msf, _ := NewMasterServiceFilter() if msf.Name() != FilterName { @@ -63,112 +72,6 @@ func TestFilter(t *testing.T) { responseObject runtime.Object expectObject runtime.Object }{ - "serviceList contains kubernetes service": { - responseObject: &corev1.ServiceList{ - Items: []corev1.Service{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: MasterServiceName, - Namespace: MasterServiceNamespace, - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.0.1", - Ports: []corev1.ServicePort{ - { - Port: 443, - Name: MasterServicePortName, - }, - }, - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: MasterServiceNamespace, - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.188", - Ports: []corev1.ServicePort{ - { - Port: 80, - }, - }, - }, - }, - }, - }, - expectObject: &corev1.ServiceList{ - Items: []corev1.Service{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: MasterServiceName, - Namespace: MasterServiceNamespace, - }, - Spec: corev1.ServiceSpec{ - ClusterIP: masterServiceHost, - Ports: []corev1.ServicePort{ - { - Port: masterServicePort, - Name: MasterServicePortName, - }, - }, - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: MasterServiceNamespace, - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.188", - Ports: []corev1.ServicePort{ - { - Port: 80, - }, - }, - }, - }, - }, - }, - }, - "serviceList does not contain kubernetes service": { - responseObject: &corev1.ServiceList{ - Items: []corev1.Service{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: MasterServiceNamespace, - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.188", - Ports: []corev1.ServicePort{ - { - Port: 80, - }, - }, - }, - }, - }, - }, - expectObject: &corev1.ServiceList{ - Items: []corev1.Service{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: MasterServiceNamespace, - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.188", - Ports: []corev1.ServicePort{ - { - Port: 80, - }, - }, - }, - }, - }, - }, - }, "it's a kubernetes service": { responseObject: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ diff --git a/pkg/yurthub/filter/nodeportisolation/filter.go b/pkg/yurthub/filter/nodeportisolation/filter.go index 8ad366e106c..99b05ce5c95 100644 --- a/pkg/yurthub/filter/nodeportisolation/filter.go +++ b/pkg/yurthub/filter/nodeportisolation/filter.go @@ -85,16 +85,6 @@ func (nif *nodePortIsolationFilter) SetKubeClient(client kubernetes.Interface) e func (nif *nodePortIsolationFilter) Filter(obj runtime.Object, stopCh <-chan struct{}) runtime.Object { switch v := obj.(type) { - case *v1.ServiceList: - var svcNew []v1.Service - for i := range v.Items { - svc := nif.isolateNodePortService(&v.Items[i]) - if svc != nil { - svcNew = append(svcNew, *svc) - } - } - v.Items = svcNew - return v case *v1.Service: return nif.isolateNodePortService(v) default: diff --git a/pkg/yurthub/filter/nodeportisolation/filter_test.go b/pkg/yurthub/filter/nodeportisolation/filter_test.go index 28d37493057..d90ca675344 100644 --- a/pkg/yurthub/filter/nodeportisolation/filter_test.go +++ b/pkg/yurthub/filter/nodeportisolation/filter_test.go @@ -28,8 +28,17 @@ import ( "github.com/openyurtio/openyurt/pkg/projectinfo" "github.com/openyurtio/openyurt/pkg/util" + "github.com/openyurtio/openyurt/pkg/yurthub/filter/base" ) +func TestRegister(t *testing.T) { + filters := base.NewFilters([]string{}) + Register(filters) + if !filters.Enabled(FilterName) { + t.Errorf("couldn't register %s filter", FilterName) + } +} + func TestName(t *testing.T) { nif, _ := NewNodePortIsolationFilter() if nif.Name() != FilterName { @@ -106,206 +115,107 @@ func TestFilter(t *testing.T) { responseObj runtime.Object expectObj runtime.Object }{ - "enable NodePort service listening on nodes in foo and bar NodePool.": { + "disable nodeport service": { poolName: nodePoolName, - responseObj: &corev1.ServiceList{ - Items: []corev1.Service{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - Annotations: map[string]string{ - ServiceAnnotationNodePortListen: "foo, bar", - }, - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.187", - Type: corev1.ServiceTypeNodePort, - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc2", - Namespace: "default", - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.188", - Type: corev1.ServiceTypeClusterIP, - }, + responseObj: &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + ServiceAnnotationNodePortListen: "-foo", }, }, - }, - expectObj: &corev1.ServiceList{ - Items: []corev1.Service{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - Annotations: map[string]string{ - ServiceAnnotationNodePortListen: "foo, bar", - }, - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.187", - Type: corev1.ServiceTypeNodePort, - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc2", - Namespace: "default", - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.188", - Type: corev1.ServiceTypeClusterIP, - }, - }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.187", + Type: corev1.ServiceTypeNodePort, }, }, + expectObj: nil, }, - "enable NodePort service listening on nodes of all NodePools": { - nodeName: "foo", - responseObj: &corev1.ServiceList{ - Items: []corev1.Service{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - Annotations: map[string]string{ - ServiceAnnotationNodePortListen: "foo, *", - }, - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.187", - Type: corev1.ServiceTypeNodePort, - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc2", - Namespace: "default", - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.188", - Type: corev1.ServiceTypeClusterIP, - }, + "disable NodePort service listening on nodes of foo NodePool": { + poolName: nodePoolName, + responseObj: &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + ServiceAnnotationNodePortListen: "-foo", }, }, - }, - expectObj: &corev1.ServiceList{ - Items: []corev1.Service{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - Annotations: map[string]string{ - ServiceAnnotationNodePortListen: "foo, *", - }, - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.187", - Type: corev1.ServiceTypeNodePort, - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc2", - Namespace: "default", - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.188", - Type: corev1.ServiceTypeClusterIP, - }, - }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.187", + Type: corev1.ServiceTypeNodePort, }, }, + expectObj: nil, }, - "disable NodePort service listening on nodes of all NodePools": { - poolName: nodePoolName, - responseObj: &corev1.ServiceList{ - Items: []corev1.Service{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - Annotations: map[string]string{ - ServiceAnnotationNodePortListen: "-foo,-bar", - }, - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.187", - Type: corev1.ServiceTypeNodePort, - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc2", - Namespace: "default", - Annotations: map[string]string{ - ServiceAnnotationNodePortListen: "-foo", - }, - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.188", - Type: corev1.ServiceTypeLoadBalancer, - }, + "disable NodePort service listening on nodes of bar NodePool": { + poolName: "bar", + responseObj: &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + ServiceAnnotationNodePortListen: "foo", }, }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.187", + Type: corev1.ServiceTypeNodePort, + }, }, - expectObj: &corev1.ServiceList{}, + expectObj: nil, }, - "disable NodePort service listening only on nodes in foo NodePool": { - poolName: nodePoolName, - responseObj: &corev1.ServiceList{ - Items: []corev1.Service{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - Annotations: map[string]string{ - ServiceAnnotationNodePortListen: "-foo,*", - }, - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.187", - Type: corev1.ServiceTypeNodePort, - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc2", - Namespace: "default", - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.188", - Type: corev1.ServiceTypeClusterIP, - }, + "duplicated node pool configuration": { + nodeName: "foo", + responseObj: &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + ServiceAnnotationNodePortListen: "foo,-foo", }, }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.187", + Type: corev1.ServiceTypeNodePort, + }, }, - expectObj: &corev1.ServiceList{ - Items: []corev1.Service{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc2", - Namespace: "default", - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.188", - Type: corev1.ServiceTypeClusterIP, - }, + expectObj: &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + ServiceAnnotationNodePortListen: "foo,-foo", }, }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.187", + Type: corev1.ServiceTypeNodePort, + }, }, }, - "disable nodeport service": { + "enable NodePort service listening on nodes of foo NodePool": { poolName: nodePoolName, responseObj: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: "svc1", Namespace: "default", Annotations: map[string]string{ - ServiceAnnotationNodePortListen: "-foo", + ServiceAnnotationNodePortListen: "foo", + }, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.187", + Type: corev1.ServiceTypeNodePort, + }, + }, + expectObj: &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + ServiceAnnotationNodePortListen: "foo", }, }, Spec: corev1.ServiceSpec{ @@ -313,16 +223,15 @@ func TestFilter(t *testing.T) { Type: corev1.ServiceTypeNodePort, }, }, - expectObj: nil, }, - "duplicated node pool configuration": { - nodeName: "foo", + "enable NodePort service listening on all nodes": { + poolName: nodePoolName, responseObj: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: "svc1", Namespace: "default", Annotations: map[string]string{ - ServiceAnnotationNodePortListen: "foo,-foo", + ServiceAnnotationNodePortListen: "*", }, }, Spec: corev1.ServiceSpec{ @@ -335,7 +244,7 @@ func TestFilter(t *testing.T) { Name: "svc1", Namespace: "default", Annotations: map[string]string{ - ServiceAnnotationNodePortListen: "foo,-foo", + ServiceAnnotationNodePortListen: "*", }, }, Spec: corev1.ServiceSpec{ @@ -344,14 +253,27 @@ func TestFilter(t *testing.T) { }, }, }, - "disable NodePort service listening on nodes of foo NodePool": { - poolName: nodePoolName, + "enable nodeport service on orphan nodes": { + nodeName: "bar", responseObj: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: "svc1", Namespace: "default", Annotations: map[string]string{ - ServiceAnnotationNodePortListen: "-foo", + ServiceAnnotationNodePortListen: "-foo,*", + }, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.187", + Type: corev1.ServiceTypeNodePort, + }, + }, + expectObj: &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + ServiceAnnotationNodePortListen: "-foo,*", }, }, Spec: corev1.ServiceSpec{ @@ -359,10 +281,9 @@ func TestFilter(t *testing.T) { Type: corev1.ServiceTypeNodePort, }, }, - expectObj: nil, }, - "enable nodeport service on orphan nodes": { - nodeName: "bar", + "enable nodeport service on orphan node that can not found": { + nodeName: "notfound", responseObj: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: "svc1", @@ -390,39 +311,28 @@ func TestFilter(t *testing.T) { }, }, }, - "disable NodePort service listening if no value configured": { + "skip ClusterIP service": { poolName: nodePoolName, - responseObj: &corev1.ServiceList{ - Items: []corev1.Service{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - Annotations: map[string]string{ - ServiceAnnotationNodePortListen: "", - }, - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.187", - Type: corev1.ServiceTypeNodePort, - }, - }, - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc2", - Namespace: "default", - Annotations: map[string]string{ - ServiceAnnotationNodePortListen: " ", - }, - }, - Spec: corev1.ServiceSpec{ - ClusterIP: "10.96.105.188", - Type: corev1.ServiceTypeLoadBalancer, - }, - }, + responseObj: &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.187", + Type: corev1.ServiceTypeClusterIP, + }, + }, + expectObj: &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.187", + Type: corev1.ServiceTypeClusterIP, }, }, - expectObj: &corev1.ServiceList{}, }, "skip podList": { poolName: nodePoolName, diff --git a/pkg/yurthub/filter/responsefilter/filter.go b/pkg/yurthub/filter/responsefilter/filter.go index eec8d8e633d..f40d27ab569 100644 --- a/pkg/yurthub/filter/responsefilter/filter.go +++ b/pkg/yurthub/filter/responsefilter/filter.go @@ -24,6 +24,7 @@ import ( "net/http" "strings" + "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/watch" @@ -43,6 +44,7 @@ type filterReadCloser struct { serializer *serializer.Serializer objectFilter filter.ObjectFilter isWatch bool + isList bool ownerName string stopCh <-chan struct{} } @@ -71,6 +73,7 @@ func newFilterReadCloser( serializer: s, objectFilter: objectFilter, isWatch: info.Verb == "watch", + isList: info.Verb == "list", ownerName: ownerName, stopCh: stopCh, } @@ -132,13 +135,32 @@ func (frc *filterReadCloser) objectResponseFilter(rc io.ReadCloser) (*bytes.Buff return &buf, nil } - filteredObj := frc.objectFilter.Filter(obj, frc.stopCh) - if yurtutil.IsNil(filteredObj) { + if frc.isList { + items, err := meta.ExtractList(obj) + if err != nil || len(items) == 0 { + obj = frc.objectFilter.Filter(obj, frc.stopCh) + } else { + list := make([]runtime.Object, 0) + for i := range items { + newObj := frc.objectFilter.Filter(items[i], frc.stopCh) + if !yurtutil.IsNil(newObj) { + list = append(list, newObj) + } + } + if err = meta.SetList(obj, list); err != nil { + klog.Warningf("filter %s doesn't work correctly, couldn't set list, %v.", frc.ownerName, err) + return &buf, nil + } + } + } else { + obj = frc.objectFilter.Filter(obj, frc.stopCh) + } + if yurtutil.IsNil(obj) { klog.Warningf("filter %s doesn't work correctly, response is discarded completely in list request.", frc.ownerName) return &buf, nil } - newData, err := frc.serializer.Encode(filteredObj) + newData, err := frc.serializer.Encode(obj) return bytes.NewBuffer(newData), err } diff --git a/pkg/yurthub/filter/responsefilter/filter_test.go b/pkg/yurthub/filter/responsefilter/filter_test.go index 127ff87bd03..cde7b3de823 100644 --- a/pkg/yurthub/filter/responsefilter/filter_test.go +++ b/pkg/yurthub/filter/responsefilter/filter_test.go @@ -24,25 +24,46 @@ import ( "reflect" "strings" "testing" + "time" corev1 "k8s.io/api/core/v1" + discovery "k8s.io/api/discovery/v1" + discoveryV1beta1 "k8s.io/api/discovery/v1beta1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/watch" "k8s.io/apiserver/pkg/endpoints/filters" apirequest "k8s.io/apiserver/pkg/endpoints/request" + "k8s.io/client-go/dynamic/dynamicinformer" + "k8s.io/client-go/dynamic/fake" + "k8s.io/client-go/informers" + k8sfake "k8s.io/client-go/kubernetes/fake" "k8s.io/klog/v2" + "github.com/openyurtio/openyurt/cmd/yurthub/app/options" + "github.com/openyurtio/openyurt/pkg/apis" + "github.com/openyurtio/openyurt/pkg/apis/apps/v1alpha1" + "github.com/openyurtio/openyurt/pkg/apis/apps/v1beta1" + "github.com/openyurtio/openyurt/pkg/projectinfo" "github.com/openyurtio/openyurt/pkg/yurthub/filter" + "github.com/openyurtio/openyurt/pkg/yurthub/filter/base" "github.com/openyurtio/openyurt/pkg/yurthub/filter/discardcloudservice" + "github.com/openyurtio/openyurt/pkg/yurthub/filter/inclusterconfig" "github.com/openyurtio/openyurt/pkg/yurthub/filter/initializer" + "github.com/openyurtio/openyurt/pkg/yurthub/filter/masterservice" "github.com/openyurtio/openyurt/pkg/yurthub/filter/nodeportisolation" + "github.com/openyurtio/openyurt/pkg/yurthub/filter/servicetopology" "github.com/openyurtio/openyurt/pkg/yurthub/kubernetes/serializer" "github.com/openyurtio/openyurt/pkg/yurthub/proxy/util" hubutil "github.com/openyurtio/openyurt/pkg/yurthub/util" ) +const ( + LabelNodePoolName = "openyurt.io/pool-name" +) + type nopObjectHandler struct { name string } @@ -394,38 +415,56 @@ func newTestRequestInfoResolver() *apirequest.RequestInfoFactory { } func TestResponseFilterForListRequest(t *testing.T) { - discardCloudSvcFilter, _ := discardcloudservice.NewDiscardCloudServiceFilter() - nodePortIsolationFilter, _ := nodeportisolation.NewNodePortIsolationFilter() - if wants, ok := nodePortIsolationFilter.(initializer.WantsNodePoolName); ok { - if err := wants.SetNodePoolName("hangzhou"); err != nil { - t.Errorf("cloudn't set pool name, %v", err) - return - } + currentNodeName := "node1" + nodeName2 := "node2" + nodeName3 := "node3" + poolName := "foo" + masterHost := "169.254.2.1" + masterPort := "10268" + var masterPortInt int32 + masterPortInt = 10268 + scheme := runtime.NewScheme() + apis.AddToScheme(scheme) + nodeBucketGVRToListKind := map[schema.GroupVersionResource]string{ + {Group: "apps.openyurt.io", Version: "v1alpha1", Resource: "nodebuckets"}: "NodeBucketList", + } + gvrToListKind := map[schema.GroupVersionResource]string{ + {Group: "apps.openyurt.io", Version: "v1beta1", Resource: "nodepools"}: "NodePoolList", } + serializerManager := serializer.NewSerializerManager() testcases := map[string]struct { - objectFilters []filter.ObjectFilter - group string - version string - resource string - userAgent string - verb string - path string - accept string - inputObj runtime.Object - names sets.String - expectedObj runtime.Object + poolName string + masterHost string + masterPort string + enableNodePool bool + enablePoolServiceTopology bool + nodeName string + kubeClient *k8sfake.Clientset + yurtClient *fake.FakeDynamicClient + group string + version string + resource string + userAgent string + verb string + path string + accept string + inputObj runtime.Object + expectedObj runtime.Object }{ - "verify discard cloud service filter": { - objectFilters: []filter.ObjectFilter{discardCloudSvcFilter}, - group: "", - version: "v1", - resource: "services", - userAgent: "kube-proxy", - verb: "GET", - path: "/api/v1/services", - accept: "application/json", + "discardcloudservice: discard lb service only": { + masterHost: masterHost, + masterPort: masterPort, + kubeClient: &k8sfake.Clientset{}, + yurtClient: &fake.FakeDynamicClient{}, + group: "", + version: "v1", + resource: "services", + userAgent: "kube-proxy", + verb: "GET", + path: "/api/v1/services", + accept: "application/json", inputObj: &corev1.ServiceList{ Items: []corev1.Service{ { @@ -441,67 +480,1560 @@ func TestResponseFilterForListRequest(t *testing.T) { Type: corev1.ServiceTypeLoadBalancer, }, }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc2", + Namespace: "default", + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.188", + Type: corev1.ServiceTypeClusterIP, + }, + }, }, }, - names: sets.NewString("discardcloudservice"), expectedObj: &corev1.ServiceList{ TypeMeta: metav1.TypeMeta{ Kind: "ServiceList", APIVersion: "v1", }, + Items: []corev1.Service{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc2", + Namespace: "default", + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.188", + Type: corev1.ServiceTypeClusterIP, + }, + }, + }, }, }, - } - - resolver := newTestRequestInfoResolver() - for k, tc := range testcases { - t.Run(k, func(t *testing.T) { - s := serializerManager.CreateSerializer(tc.accept, tc.group, tc.version, tc.resource) - encoder, err := s.Encoder(tc.accept, nil) - if err != nil { - t.Fatalf("could not create encoder, %v", err) - } - - buf := bytes.NewBuffer([]byte{}) - err = encoder.Encode(tc.inputObj, buf) - if err != nil { - t.Fatalf("could not encode input object, %v", err) - } - - req, err := http.NewRequest(tc.verb, tc.path, nil) - if err != nil { - t.Errorf("failed to create request, %v", err) - } - req.RemoteAddr = "127.0.0.1" - - if len(tc.userAgent) != 0 { - req.Header.Set("User-Agent", tc.userAgent) - } - - var newReadCloser io.ReadCloser - var filterErr error - var responseFilter filter.ResponseFilter - var handler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - responseFilter = CreateResponseFilter(tc.objectFilters, serializerManager) - ctx := req.Context() - ctx = hubutil.WithRespContentType(ctx, tc.accept) - req = req.WithContext(ctx) - rc := io.NopCloser(buf) - _, newReadCloser, filterErr = responseFilter.Filter(req, rc, nil) - }) - - handler = util.WithRequestClientComponent(handler) - handler = filters.WithRequestInfo(handler, resolver) - handler.ServeHTTP(httptest.NewRecorder(), req) - - if filterErr != nil { - t.Errorf("get filter err, %v", err) - return - } - - names := strings.Split(responseFilter.Name(), ",") - if !tc.names.Equal(sets.NewString(names...)) { - t.Errorf("expect filter names %v, but got %v", tc.names.List(), names) + "discardcloudservice: discard cloud clusterIP service": { + masterHost: masterHost, + masterPort: masterPort, + kubeClient: &k8sfake.Clientset{}, + yurtClient: &fake.FakeDynamicClient{}, + group: "", + version: "v1", + resource: "services", + userAgent: "kube-proxy", + verb: "GET", + path: "/api/v1/services", + accept: "application/json", + inputObj: &corev1.ServiceList{ + Items: []corev1.Service{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "x-tunnel-server-internal-svc", + Namespace: "kube-system", + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.187", + Type: corev1.ServiceTypeLoadBalancer, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc2", + Namespace: "default", + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.188", + Type: corev1.ServiceTypeClusterIP, + }, + }, + }, + }, + expectedObj: &corev1.ServiceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "ServiceList", + APIVersion: "v1", + }, + Items: []corev1.Service{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc2", + Namespace: "default", + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.188", + Type: corev1.ServiceTypeClusterIP, + }, + }, + }, + }, + }, + "discardcloudservice: discard all service": { + masterHost: masterHost, + masterPort: masterPort, + kubeClient: &k8sfake.Clientset{}, + yurtClient: &fake.FakeDynamicClient{}, + group: "", + version: "v1", + resource: "services", + userAgent: "kube-proxy", + verb: "GET", + path: "/api/v1/services", + accept: "application/json", + inputObj: &corev1.ServiceList{ + Items: []corev1.Service{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + discardcloudservice.DiscardServiceAnnotation: "true", + }, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.187", + Type: corev1.ServiceTypeLoadBalancer, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "x-tunnel-server-internal-svc", + Namespace: "kube-system", + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.188", + Type: corev1.ServiceTypeClusterIP, + }, + }, + }, + }, + expectedObj: &corev1.ServiceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "ServiceList", + APIVersion: "v1", + }, + Items: []corev1.Service{}, + }, + }, + "discardcloudservice: skip all service": { + masterHost: masterHost, + masterPort: masterPort, + kubeClient: &k8sfake.Clientset{}, + yurtClient: &fake.FakeDynamicClient{}, + group: "", + version: "v1", + resource: "services", + userAgent: "kube-proxy", + verb: "GET", + path: "/api/v1/services", + accept: "application/json", + inputObj: &corev1.ServiceList{ + Items: []corev1.Service{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + discardcloudservice.DiscardServiceAnnotation: "false", + }, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.187", + Type: corev1.ServiceTypeLoadBalancer, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc2", + Namespace: "default", + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.188", + Type: corev1.ServiceTypeClusterIP, + }, + }, + }, + }, + expectedObj: &corev1.ServiceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "ServiceList", + APIVersion: "v1", + }, + Items: []corev1.Service{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + discardcloudservice.DiscardServiceAnnotation: "false", + }, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.187", + Type: corev1.ServiceTypeLoadBalancer, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc2", + Namespace: "default", + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.188", + Type: corev1.ServiceTypeClusterIP, + }, + }, + }, + }, + }, + "inclusterconfig: mutate kube-proxy configmap": { + masterHost: masterHost, + masterPort: masterPort, + kubeClient: &k8sfake.Clientset{}, + yurtClient: &fake.FakeDynamicClient{}, + group: "", + version: "v1", + resource: "configmaps", + userAgent: "kubelet", + verb: "GET", + path: "/api/v1/configmaps", + accept: "application/json", + inputObj: &corev1.ConfigMapList{ + Items: []corev1.ConfigMap{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "default", + }, + Data: map[string]string{ + "foo": "bar", + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: inclusterconfig.KubeProxyConfigMapName, + Namespace: inclusterconfig.KubeProxyConfigMapNamespace, + }, + Data: map[string]string{ + "config.conf": " apiVersion: kubeproxy.config.k8s.io/v1alpha1\n bindAddress: 0.0.0.0\n bindAddressHardFail: false\n clientConnection:\n acceptContentTypes: \"\"\n burst: 0\n contentType: \"\"\n kubeconfig: /var/lib/kube-proxy/kubeconfig.conf\n qps: 0\n clusterCIDR: 10.244.0.0/16\n configSyncPeriod: 0s", + }, + }, + }, + }, + expectedObj: &corev1.ConfigMapList{ + TypeMeta: metav1.TypeMeta{ + Kind: "ConfigMapList", + APIVersion: "v1", + }, + Items: []corev1.ConfigMap{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + Namespace: "default", + }, + Data: map[string]string{ + "foo": "bar", + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: inclusterconfig.KubeProxyConfigMapName, + Namespace: inclusterconfig.KubeProxyConfigMapNamespace, + }, + Data: map[string]string{ + "config.conf": " apiVersion: kubeproxy.config.k8s.io/v1alpha1\n bindAddress: 0.0.0.0\n bindAddressHardFail: false\n clientConnection:\n acceptContentTypes: \"\"\n burst: 0\n contentType: \"\"\n #kubeconfig: /var/lib/kube-proxy/kubeconfig.conf\n qps: 0\n clusterCIDR: 10.244.0.0/16\n configSyncPeriod: 0s", + }, + }, + }, + }, + }, + "masterservice: serviceList contains kubernetes service": { + masterHost: masterHost, + masterPort: masterPort, + kubeClient: &k8sfake.Clientset{}, + yurtClient: &fake.FakeDynamicClient{}, + group: "", + version: "v1", + resource: "services", + userAgent: "kubelet", + verb: "GET", + path: "/api/v1/services", + accept: "application/json", + inputObj: &corev1.ServiceList{ + Items: []corev1.Service{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: masterservice.MasterServiceName, + Namespace: masterservice.MasterServiceNamespace, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.0.1", + Ports: []corev1.ServicePort{ + { + Port: 443, + Name: masterservice.MasterServicePortName, + }, + }, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: masterservice.MasterServiceNamespace, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.188", + Ports: []corev1.ServicePort{ + { + Port: 80, + }, + }, + }, + }, + }, + }, + expectedObj: &corev1.ServiceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "ServiceList", + APIVersion: "v1", + }, + Items: []corev1.Service{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: masterservice.MasterServiceName, + Namespace: masterservice.MasterServiceNamespace, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: masterHost, + Ports: []corev1.ServicePort{ + { + Port: masterPortInt, + Name: masterservice.MasterServicePortName, + }, + }, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: masterservice.MasterServiceNamespace, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.188", + Ports: []corev1.ServicePort{ + { + Port: 80, + }, + }, + }, + }, + }, + }, + }, + "masterservice: serviceList doesn't contain kubernetes service": { + masterHost: masterHost, + masterPort: masterPort, + kubeClient: &k8sfake.Clientset{}, + yurtClient: &fake.FakeDynamicClient{}, + group: "", + version: "v1", + resource: "services", + userAgent: "kubelet", + verb: "GET", + path: "/api/v1/services", + accept: "application/json", + inputObj: &corev1.ServiceList{ + Items: []corev1.Service{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: masterservice.MasterServiceNamespace, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.188", + Ports: []corev1.ServicePort{ + { + Port: 80, + }, + }, + }, + }, + }, + }, + expectedObj: &corev1.ServiceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "ServiceList", + APIVersion: "v1", + }, + Items: []corev1.Service{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: masterservice.MasterServiceNamespace, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.188", + Ports: []corev1.ServicePort{ + { + Port: 80, + }, + }, + }, + }, + }, + }, + }, + "masterservice: it is a kubernetes service": { + masterHost: masterHost, + masterPort: masterPort, + kubeClient: &k8sfake.Clientset{}, + yurtClient: &fake.FakeDynamicClient{}, + group: "", + version: "v1", + resource: "services", + userAgent: "kubelet", + verb: "GET", + path: "/api/v1/namespaces/default/services/kubernetes", + accept: "application/json", + inputObj: &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: masterservice.MasterServiceName, + Namespace: masterservice.MasterServiceNamespace, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.188", + Ports: []corev1.ServicePort{ + { + Port: 80, + Name: masterservice.MasterServicePortName, + }, + }, + }, + }, + expectedObj: &corev1.Service{ + TypeMeta: metav1.TypeMeta{ + Kind: "Service", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: masterservice.MasterServiceName, + Namespace: masterservice.MasterServiceNamespace, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: masterHost, + Ports: []corev1.ServicePort{ + { + Port: masterPortInt, + Name: masterservice.MasterServicePortName, + }, + }, + }, + }, + }, + "nodeportisolation: enable NodePort service listening on nodes in foo and bar NodePool": { + masterHost: masterHost, + masterPort: masterPort, + kubeClient: &k8sfake.Clientset{}, + yurtClient: &fake.FakeDynamicClient{}, + poolName: poolName, + group: "", + version: "v1", + resource: "services", + userAgent: "kube-proxy", + verb: "GET", + path: "/api/v1/services", + accept: "application/json", + inputObj: &corev1.ServiceList{ + Items: []corev1.Service{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + nodeportisolation.ServiceAnnotationNodePortListen: "foo, bar", + }, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.187", + Type: corev1.ServiceTypeNodePort, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc2", + Namespace: "default", + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.188", + Type: corev1.ServiceTypeClusterIP, + }, + }, + }, + }, + expectedObj: &corev1.ServiceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "ServiceList", + APIVersion: "v1", + }, + Items: []corev1.Service{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + nodeportisolation.ServiceAnnotationNodePortListen: "foo, bar", + }, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.187", + Type: corev1.ServiceTypeNodePort, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc2", + Namespace: "default", + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.188", + Type: corev1.ServiceTypeClusterIP, + }, + }, + }, + }, + }, + "nodeportisolation: enable NodePort service listening on nodes of all NodePools": { + masterHost: masterHost, + masterPort: masterPort, + kubeClient: &k8sfake.Clientset{}, + yurtClient: &fake.FakeDynamicClient{}, + poolName: poolName, + group: "", + version: "v1", + resource: "services", + userAgent: "kube-proxy", + verb: "GET", + path: "/api/v1/services", + accept: "application/json", + inputObj: &corev1.ServiceList{ + Items: []corev1.Service{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + nodeportisolation.ServiceAnnotationNodePortListen: "foo, *", + }, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.187", + Type: corev1.ServiceTypeNodePort, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc2", + Namespace: "default", + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.188", + Type: corev1.ServiceTypeClusterIP, + }, + }, + }, + }, + expectedObj: &corev1.ServiceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "ServiceList", + APIVersion: "v1", + }, + Items: []corev1.Service{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + nodeportisolation.ServiceAnnotationNodePortListen: "foo, *", + }, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.187", + Type: corev1.ServiceTypeNodePort, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc2", + Namespace: "default", + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.188", + Type: corev1.ServiceTypeClusterIP, + }, + }, + }, + }, + }, + "nodeportisolation: disable NodePort service listening on nodes of all NodePools": { + masterHost: masterHost, + masterPort: masterPort, + kubeClient: &k8sfake.Clientset{}, + yurtClient: &fake.FakeDynamicClient{}, + poolName: poolName, + group: "", + version: "v1", + resource: "services", + userAgent: "kube-proxy", + verb: "GET", + path: "/api/v1/services", + accept: "application/json", + inputObj: &corev1.ServiceList{ + Items: []corev1.Service{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + nodeportisolation.ServiceAnnotationNodePortListen: "-foo,-bar", + }, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.187", + Type: corev1.ServiceTypeNodePort, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc2", + Namespace: "default", + Annotations: map[string]string{ + nodeportisolation.ServiceAnnotationNodePortListen: "-foo", + }, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.188", + Type: corev1.ServiceTypeLoadBalancer, + }, + }, + }, + }, + expectedObj: &corev1.ServiceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "ServiceList", + APIVersion: "v1", + }, + Items: []corev1.Service{}, + }, + }, + "nodeportisolation: NodePort service doesn't listen on nodes in foo NodePool": { + masterHost: masterHost, + masterPort: masterPort, + kubeClient: &k8sfake.Clientset{}, + yurtClient: &fake.FakeDynamicClient{}, + poolName: poolName, + group: "", + version: "v1", + resource: "services", + userAgent: "kube-proxy", + verb: "GET", + path: "/api/v1/services", + accept: "application/json", + inputObj: &corev1.ServiceList{ + Items: []corev1.Service{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + nodeportisolation.ServiceAnnotationNodePortListen: "-foo,*", + }, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.187", + Type: corev1.ServiceTypeNodePort, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc2", + Namespace: "default", + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.188", + Type: corev1.ServiceTypeClusterIP, + }, + }, + }, + }, + expectedObj: &corev1.ServiceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "ServiceList", + APIVersion: "v1", + }, + Items: []corev1.Service{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc2", + Namespace: "default", + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.188", + Type: corev1.ServiceTypeClusterIP, + }, + }, + }, + }, + }, + "nodeportisolation: disable NodePort service listening if no value configured": { + masterHost: masterHost, + masterPort: masterPort, + kubeClient: &k8sfake.Clientset{}, + yurtClient: &fake.FakeDynamicClient{}, + poolName: poolName, + group: "", + version: "v1", + resource: "services", + userAgent: "kube-proxy", + verb: "GET", + path: "/api/v1/services", + accept: "application/json", + inputObj: &corev1.ServiceList{ + Items: []corev1.Service{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + nodeportisolation.ServiceAnnotationNodePortListen: "", + }, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.187", + Type: corev1.ServiceTypeNodePort, + }, + }, + { + ObjectMeta: metav1.ObjectMeta{ + Name: "svc2", + Namespace: "default", + Annotations: map[string]string{ + nodeportisolation.ServiceAnnotationNodePortListen: " ", + }, + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.96.105.188", + Type: corev1.ServiceTypeLoadBalancer, + }, + }, + }, + }, + expectedObj: &corev1.ServiceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "ServiceList", + APIVersion: "v1", + }, + Items: []corev1.Service{}, + }, + }, + "servicetopology: v1beta1.EndpointSliceList: topologyKeys is kubernetes.io/hostname": { + masterHost: masterHost, + masterPort: masterPort, + poolName: "hangzhou", + nodeName: "node1", + enablePoolServiceTopology: true, + kubeClient: k8sfake.NewSimpleClientset( + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node1", + Labels: map[string]string{ + projectinfo.GetNodePoolLabel(): "hangzhou", + }, + }, + }, + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node2", + Labels: map[string]string{ + projectinfo.GetNodePoolLabel(): "shanghai", + }, + }, + }, + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node3", + Labels: map[string]string{ + projectinfo.GetNodePoolLabel(): "hangzhou", + }, + }, + }, + &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + servicetopology.AnnotationServiceTopologyKey: servicetopology.AnnotationServiceTopologyValueNode, + }, + }, + }, + ), + yurtClient: fake.NewSimpleDynamicClientWithCustomListKinds(scheme, nodeBucketGVRToListKind, + &v1alpha1.NodeBucket{ + ObjectMeta: metav1.ObjectMeta{ + Name: "hangzhou", + Labels: map[string]string{ + LabelNodePoolName: "hangzhou", + }, + }, + Nodes: []v1alpha1.Node{ + { + Name: "node1", + }, + { + Name: "node3", + }, + }, + }, + &v1alpha1.NodeBucket{ + ObjectMeta: metav1.ObjectMeta{ + Name: "shanghai", + Labels: map[string]string{ + LabelNodePoolName: "shanghai", + }, + }, + Nodes: []v1alpha1.Node{ + { + Name: "node2", + }, + }, + }, + ), + group: "discovery.k8s.io", + version: "v1beta1", + resource: "endpointslices", + userAgent: "kube-proxy", + verb: "GET", + path: "/apis/discovery.k8s.io/v1beta1/endpointslices", + accept: "application/json", + inputObj: &discoveryV1beta1.EndpointSliceList{ + Items: []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: "node1", + }, + }, + { + 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: "node1", + }, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node3", + }, + }, + }, + }, + }, + }, + expectedObj: &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", + }, + }, + Endpoints: []discoveryV1beta1.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node1", + }, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node1", + }, + }, + }, + }, + }, + }, + }, + "servicetopology: v1beta1.EndpointSliceList: topologyKeys is openyurt.io/nodepool": { + masterHost: masterHost, + masterPort: masterPort, + poolName: "hangzhou", + nodeName: "node1", + enableNodePool: true, + kubeClient: k8sfake.NewSimpleClientset( + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node1", + Labels: map[string]string{ + projectinfo.GetNodePoolLabel(): "hangzhou", + }, + }, + }, + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node2", + Labels: map[string]string{ + projectinfo.GetNodePoolLabel(): "shanghai", + }, + }, + }, + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node3", + Labels: map[string]string{ + projectinfo.GetNodePoolLabel(): "hangzhou", + }, + }, + }, + &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + servicetopology.AnnotationServiceTopologyKey: servicetopology.AnnotationServiceTopologyValueNodePool, + }, + }, + }, + ), + yurtClient: fake.NewSimpleDynamicClientWithCustomListKinds(scheme, gvrToListKind, + &v1beta1.NodePool{ + ObjectMeta: metav1.ObjectMeta{ + Name: "hangzhou", + }, + Spec: v1beta1.NodePoolSpec{ + Type: v1beta1.Edge, + }, + Status: v1beta1.NodePoolStatus{ + Nodes: []string{ + "node1", + "node3", + }, + }, + }, + &v1beta1.NodePool{ + ObjectMeta: metav1.ObjectMeta{ + Name: "shanghai", + }, + Spec: v1beta1.NodePoolSpec{ + Type: v1beta1.Edge, + }, + Status: v1beta1.NodePoolStatus{ + Nodes: []string{ + "node2", + }, + }, + }, + ), + group: "discovery.k8s.io", + version: "v1beta1", + resource: "endpointslices", + userAgent: "kube-proxy", + verb: "GET", + path: "/apis/discovery.k8s.io/v1beta1/endpointslices", + accept: "application/json", + inputObj: &discoveryV1beta1.EndpointSliceList{ + Items: []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: "node1", + }, + }, + { + 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: "node1", + }, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node3", + }, + }, + }, + }, + }, + }, + expectedObj: &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", + }, + }, + Endpoints: []discoveryV1beta1.Endpoint{ + { + Addresses: []string{ + "10.244.1.2", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node1", + }, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node1", + }, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node3", + }, + }, + }, + }, + }, + }, + }, + "servicetopology: v1.EndpointSliceList: topologyKeys is kubernetes.io/hostname": { + masterHost: masterHost, + masterPort: masterPort, + poolName: "hangzhou", + nodeName: "node1", + enablePoolServiceTopology: true, + kubeClient: k8sfake.NewSimpleClientset( + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node1", + Labels: map[string]string{ + projectinfo.GetNodePoolLabel(): "hangzhou", + }, + }, + }, + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node2", + Labels: map[string]string{ + projectinfo.GetNodePoolLabel(): "shanghai", + }, + }, + }, + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node3", + Labels: map[string]string{ + projectinfo.GetNodePoolLabel(): "hangzhou", + }, + }, + }, + &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + servicetopology.AnnotationServiceTopologyKey: servicetopology.AnnotationServiceTopologyValueNode, + }, + }, + }, + ), + yurtClient: fake.NewSimpleDynamicClientWithCustomListKinds(scheme, nodeBucketGVRToListKind, + &v1alpha1.NodeBucket{ + ObjectMeta: metav1.ObjectMeta{ + Name: "hangzhou", + Labels: map[string]string{ + LabelNodePoolName: "hangzhou", + }, + }, + Nodes: []v1alpha1.Node{ + { + Name: "node1", + }, + { + Name: "node3", + }, + }, + }, + &v1alpha1.NodeBucket{ + ObjectMeta: metav1.ObjectMeta{ + Name: "shanghai", + Labels: map[string]string{ + LabelNodePoolName: "shanghai", + }, + }, + Nodes: []v1alpha1.Node{ + { + Name: "node2", + }, + }, + }, + ), + group: "discovery.k8s.io", + version: "v1", + resource: "endpointslices", + userAgent: "kube-proxy", + verb: "GET", + path: "/apis/discovery.k8s.io/v1/endpointslices", + accept: "application/json", + inputObj: &discovery.EndpointSliceList{ + 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.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, + }, + }, + }, + }, + }, + expectedObj: &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.2", + }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + NodeName: ¤tNodeName, + }, + }, + }, + }, + }, + }, + "servicetopology: v1.EndpointSliceList: topologyKeys is openyurt.io/nodepool": { + masterHost: masterHost, + masterPort: masterPort, + poolName: "hangzhou", + nodeName: "node1", + enablePoolServiceTopology: true, + kubeClient: k8sfake.NewSimpleClientset( + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node1", + Labels: map[string]string{ + projectinfo.GetNodePoolLabel(): "hangzhou", + }, + }, + }, + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node2", + Labels: map[string]string{ + projectinfo.GetNodePoolLabel(): "shanghai", + }, + }, + }, + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node3", + Labels: map[string]string{ + projectinfo.GetNodePoolLabel(): "hangzhou", + }, + }, + }, + &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + servicetopology.AnnotationServiceTopologyKey: servicetopology.AnnotationServiceTopologyValueNodePool, + }, + }, + }, + ), + yurtClient: fake.NewSimpleDynamicClientWithCustomListKinds(scheme, nodeBucketGVRToListKind, + &v1alpha1.NodeBucket{ + ObjectMeta: metav1.ObjectMeta{ + Name: "hangzhou", + Labels: map[string]string{ + LabelNodePoolName: "hangzhou", + }, + }, + Nodes: []v1alpha1.Node{ + { + Name: "node1", + }, + { + Name: "node3", + }, + }, + }, + &v1alpha1.NodeBucket{ + ObjectMeta: metav1.ObjectMeta{ + Name: "shanghai", + Labels: map[string]string{ + LabelNodePoolName: "shanghai", + }, + }, + Nodes: []v1alpha1.Node{ + { + Name: "node2", + }, + }, + }, + ), + group: "discovery.k8s.io", + version: "v1", + resource: "endpointslices", + userAgent: "kube-proxy", + verb: "GET", + path: "/apis/discovery.k8s.io/v1/endpointslices", + accept: "application/json", + inputObj: &discovery.EndpointSliceList{ + 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.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, + }, + }, + }, + }, + }, + expectedObj: &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.2", + }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + NodeName: &nodeName3, + }, + }, + }, + }, + }, + }, + "servicetopology: v1.EndpointSliceList: there are no endpoints": { + masterHost: masterHost, + masterPort: masterPort, + poolName: "hangzhou", + nodeName: "node1", + enablePoolServiceTopology: true, + kubeClient: k8sfake.NewSimpleClientset( + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node1", + Labels: map[string]string{ + projectinfo.GetNodePoolLabel(): "hangzhou", + }, + }, + }, + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node2", + Labels: map[string]string{ + projectinfo.GetNodePoolLabel(): "shanghai", + }, + }, + }, + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node3", + Labels: map[string]string{ + projectinfo.GetNodePoolLabel(): "hangzhou", + }, + }, + }, + &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", + Annotations: map[string]string{ + servicetopology.AnnotationServiceTopologyKey: servicetopology.AnnotationServiceTopologyValueNodePool, + }, + }, + }, + ), + yurtClient: fake.NewSimpleDynamicClientWithCustomListKinds(scheme, nodeBucketGVRToListKind, + &v1alpha1.NodeBucket{ + ObjectMeta: metav1.ObjectMeta{ + Name: "hangzhou", + Labels: map[string]string{ + LabelNodePoolName: "hangzhou", + }, + }, + Nodes: []v1alpha1.Node{ + { + Name: "node1", + }, + { + Name: "node3", + }, + }, + }, + &v1alpha1.NodeBucket{ + ObjectMeta: metav1.ObjectMeta{ + Name: "shanghai", + Labels: map[string]string{ + LabelNodePoolName: "shanghai", + }, + }, + Nodes: []v1alpha1.Node{ + { + Name: "node2", + }, + }, + }, + ), + group: "discovery.k8s.io", + version: "v1", + resource: "endpointslices", + userAgent: "kube-proxy", + verb: "GET", + path: "/apis/discovery.k8s.io/v1/endpointslices", + accept: "application/json", + inputObj: &discovery.EndpointSliceList{}, + expectedObj: &discovery.EndpointSliceList{ + TypeMeta: metav1.TypeMeta{ + Kind: "EndpointSliceList", + APIVersion: "discovery.k8s.io/v1", + }, + }, + }, + } + + resolver := newTestRequestInfoResolver() + for k, tc := range testcases { + t.Run(k, func(t *testing.T) { + var factory informers.SharedInformerFactory + var yurtFactory dynamicinformer.DynamicSharedInformerFactory + var nodesInitializer filter.Initializer + var genericInitializer filter.Initializer + + factory = informers.NewSharedInformerFactory(tc.kubeClient, 24*time.Hour) + yurtFactory = dynamicinformer.NewDynamicSharedInformerFactory(tc.yurtClient, 24*time.Hour) + + nodesInitializer = initializer.NewNodesInitializer(tc.enableNodePool, tc.enablePoolServiceTopology, yurtFactory) + genericInitializer = initializer.New(factory, tc.kubeClient, tc.nodeName, tc.poolName, tc.masterHost, tc.masterPort) + initializerChain := base.Initializers{} + initializerChain = append(initializerChain, genericInitializer, nodesInitializer) + + // get all object filters + baseFilters := base.NewFilters([]string{}) + options.RegisterAllFilters(baseFilters) + + objectFilters, err := baseFilters.NewFromFilters(initializerChain) + if err != nil { + t.Errorf("couldn't new object filters, %v", err) + } + + s := serializerManager.CreateSerializer(tc.accept, tc.group, tc.version, tc.resource) + encoder, err := s.Encoder(tc.accept, nil) + if err != nil { + t.Fatalf("could not create encoder, %v", err) + } + + buf := bytes.NewBuffer([]byte{}) + err = encoder.Encode(tc.inputObj, buf) + if err != nil { + t.Fatalf("could not encode input object, %v", err) + } + + req, err := http.NewRequest(tc.verb, tc.path, nil) + if err != nil { + t.Errorf("failed to create request, %v", err) + } + req.RemoteAddr = "127.0.0.1" + + if len(tc.userAgent) != 0 { + req.Header.Set("User-Agent", tc.userAgent) + } + + stopper := make(chan struct{}) + defer close(stopper) + factory.Start(stopper) + factory.WaitForCacheSync(stopper) + + stopper2 := make(chan struct{}) + defer close(stopper2) + yurtFactory.Start(stopper2) + yurtFactory.WaitForCacheSync(stopper2) + + var newReadCloser io.ReadCloser + var filterErr error + var responseFilter filter.ResponseFilter + var handler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + responseFilter = CreateResponseFilter(objectFilters, serializerManager) + ctx := req.Context() + ctx = hubutil.WithRespContentType(ctx, tc.accept) + req = req.WithContext(ctx) + rc := io.NopCloser(buf) + _, newReadCloser, filterErr = responseFilter.Filter(req, rc, nil) + }) + + handler = util.WithRequestClientComponent(handler) + handler = filters.WithRequestInfo(handler, resolver) + handler.ServeHTTP(httptest.NewRecorder(), req) + + if filterErr != nil { + t.Errorf("get filter err, %v", err) + return } newBuf, err := io.ReadAll(newReadCloser) diff --git a/pkg/yurthub/filter/servicetopology/filter.go b/pkg/yurthub/filter/servicetopology/filter.go index fde7b06a71d..2cbd38f969a 100644 --- a/pkg/yurthub/filter/servicetopology/filter.go +++ b/pkg/yurthub/filter/servicetopology/filter.go @@ -130,22 +130,6 @@ func (stf *serviceTopologyFilter) Filter(obj runtime.Object, stopCh <-chan struc } switch v := obj.(type) { - case *discoveryV1beta1.EndpointSliceList: - // filter endpointSlice before k8s 1.21 - for i := range v.Items { - stf.serviceTopologyHandler(&v.Items[i]) - } - return v - case *discovery.EndpointSliceList: - for i := range v.Items { - stf.serviceTopologyHandler(&v.Items[i]) - } - return v - case *v1.EndpointsList: - for i := range v.Items { - stf.serviceTopologyHandler(&v.Items[i]) - } - return v case *v1.Endpoints, *discoveryV1beta1.EndpointSlice, *discovery.EndpointSlice: return stf.serviceTopologyHandler(v) default: diff --git a/pkg/yurthub/filter/servicetopology/filter_test.go b/pkg/yurthub/filter/servicetopology/filter_test.go index f0493623d98..ba945d54db4 100644 --- a/pkg/yurthub/filter/servicetopology/filter_test.go +++ b/pkg/yurthub/filter/servicetopology/filter_test.go @@ -38,6 +38,7 @@ import ( "github.com/openyurtio/openyurt/pkg/apis/apps/v1beta1" "github.com/openyurtio/openyurt/pkg/projectinfo" "github.com/openyurtio/openyurt/pkg/util" + "github.com/openyurtio/openyurt/pkg/yurthub/filter/base" "github.com/openyurtio/openyurt/pkg/yurthub/filter/initializer" ) @@ -45,6 +46,14 @@ const ( LabelNodePoolName = "openyurt.io/pool-name" ) +func TestRegister(t *testing.T) { + filters := base.NewFilters([]string{}) + Register(filters) + if !filters.Enabled(FilterName) { + t.Errorf("couldn't register %s filter", FilterName) + } +} + func TestName(t *testing.T) { stf, _ := NewServiceTopologyFilter() if stf.Name() != FilterName { @@ -93,51 +102,47 @@ func TestFilter(t *testing.T) { yurtClient *fake.FakeDynamicClient expectObject runtime.Object }{ - "v1beta1.EndpointSliceList: topologyKeys is kubernetes.io/hostname": { + "v1beta1.EndpointSlice: topologyKeys is kubernetes.io/hostname": { poolName: "hangzhou", - responseObject: &discoveryV1beta1.EndpointSliceList{ - Items: []discoveryV1beta1.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, + responseObject: &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", }, - 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", - }, - }, + 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", }, }, }, @@ -206,84 +211,76 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &discoveryV1beta1.EndpointSliceList{ - Items: []discoveryV1beta1.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, + expectObject: &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", }, - 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, - }, - }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, }, }, }, }, }, - "v1beta1.EndpointSliceList: topologyKeys is openyurt.io/nodepool": { + "v1beta1.EndpointSlice: topologyKeys is openyurt.io/nodepool": { enableNodePool: true, poolName: "hangzhou", - responseObject: &discoveryV1beta1.EndpointSliceList{ - Items: []discoveryV1beta1.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, + responseObject: &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", }, - 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", - }, - }, + 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", }, }, }, @@ -352,91 +349,85 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &discoveryV1beta1.EndpointSliceList{ - Items: []discoveryV1beta1.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, + expectObject: &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", }, - 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", - }, - }, + 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.EndpointSliceList: topologyKeys is kubernetes.io/zone": { + "v1beta1.EndpointSlice: topologyKeys is kubernetes.io/zone": { enableNodePool: true, - responseObject: &discoveryV1beta1.EndpointSliceList{ - Items: []discoveryV1beta1.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, + responseObject: &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", }, - 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", - }, - }, + 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", }, }, }, @@ -505,90 +496,84 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &discoveryV1beta1.EndpointSliceList{ - Items: []discoveryV1beta1.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, + expectObject: &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", }, - 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", - }, - }, + 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.EndpointSliceList: without openyurt.io/topologyKeys": { - responseObject: &discoveryV1beta1.EndpointSliceList{ - Items: []discoveryV1beta1.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, + "v1beta1.EndpointSlice: without openyurt.io/topologyKeys": { + responseObject: &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", }, - 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", - }, - }, + 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", }, }, }, @@ -655,99 +640,93 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &discoveryV1beta1.EndpointSliceList{ - Items: []discoveryV1beta1.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, + expectObject: &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", }, - 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", - }, - }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, }, }, - }, - }, - }, - "v1beta1.EndpointSliceList: currentNode is not in any nodepool": { - enableNodePool: true, - responseObject: &discoveryV1beta1.EndpointSliceList{ - Items: []discoveryV1beta1.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, + { + 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: "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", - }, - }, + 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": { + enableNodePool: true, + responseObject: &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", + }, + Topology: map[string]string{ + corev1.LabelHostname: "node3", }, }, }, @@ -813,82 +792,76 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &discoveryV1beta1.EndpointSliceList{ - Items: []discoveryV1beta1.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, + expectObject: &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", }, - 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, - }, - }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, + }, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, }, }, }, }, }, - "v1beta1.EndpointSliceList: currentNode has no endpoints on node": { - responseObject: &discoveryV1beta1.EndpointSliceList{ - Items: []discoveryV1beta1.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, + "v1beta1.EndpointSlice: currentNode has no endpoints on node": { + responseObject: &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", }, - 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", - }, - }, + 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", }, }, }, @@ -957,65 +930,58 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &discoveryV1beta1.EndpointSliceList{ - Items: []discoveryV1beta1.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, + expectObject: &discoveryV1beta1.EndpointSlice{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discoveryV1beta1.LabelServiceName: "svc1", }, }, }, }, - "v1beta1.EndpointSliceList: currentNode has no endpoints in nodepool": { + "v1beta1.EndpointSlice: currentNode has no endpoints in nodepool": { enableNodePool: true, - responseObject: &discoveryV1beta1.EndpointSliceList{ - Items: []discoveryV1beta1.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, + responseObject: &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", }, - 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", - }, - }, + 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", }, }, }, @@ -1084,61 +1050,55 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &discoveryV1beta1.EndpointSliceList{ - Items: []discoveryV1beta1.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, - }, + expectObject: &discoveryV1beta1.EndpointSlice{ + + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discoveryV1beta1.LabelServiceName: "svc1", }, }, }, }, - "v1beta1.EndpointSliceList: no service info in endpointslice": { - responseObject: &discoveryV1beta1.EndpointSliceList{ - Items: []discoveryV1beta1.EndpointSlice{ + "v1beta1.EndpointSlice: no service info in endpointslice": { + responseObject: &discoveryV1beta1.EndpointSlice{ + + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + }, + Endpoints: []discoveryV1beta1.Endpoint{ { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", + Addresses: []string{ + "10.244.1.2", }, - 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, - }, - }, + 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, }, }, }, @@ -1207,113 +1167,106 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &discoveryV1beta1.EndpointSliceList{ - Items: []discoveryV1beta1.EndpointSlice{ + expectObject: &discoveryV1beta1.EndpointSlice{ + + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + }, + Endpoints: []discoveryV1beta1.Endpoint{ { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", + Addresses: []string{ + "10.244.1.2", }, - 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, - }, - }, + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, }, }, - }, - }, - }, - "v1.EndpointSliceList: topologyKeys is kubernetes.io/hostname": { - responseObject: &discovery.EndpointSliceList{ - Items: []discovery.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discovery.LabelServiceName: "svc1", - }, + { + Addresses: []string{ + "10.244.1.3", }, - 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, - }, + Topology: map[string]string{ + corev1.LabelHostname: nodeName2, }, }, - }, - }, - kubeClient: k8sfake.NewSimpleClientset( - &corev1.Node{ - ObjectMeta: metav1.ObjectMeta{ - Name: currentNodeName, - Labels: map[string]string{ - projectinfo.GetNodePoolLabel(): "hangzhou", + { + Addresses: []string{ + "10.244.1.4", }, - }, - }, - &corev1.Node{ - ObjectMeta: metav1.ObjectMeta{ - Name: "node2", - Labels: map[string]string{ - projectinfo.GetNodePoolLabel(): "shanghai", + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, }, }, - }, - &corev1.Node{ - ObjectMeta: metav1.ObjectMeta{ - Name: "node3", - Labels: map[string]string{ - projectinfo.GetNodePoolLabel(): "hangzhou", + { + Addresses: []string{ + "10.244.1.5", + }, + Topology: map[string]string{ + corev1.LabelHostname: nodeName3, + }, + }, + }, + }, + }, + "v1.EndpointSlice: topologyKeys is kubernetes.io/hostname": { + responseObject: &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.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, + }, + }, + }, + kubeClient: k8sfake.NewSimpleClientset( + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: currentNodeName, + Labels: map[string]string{ + projectinfo.GetNodePoolLabel(): "hangzhou", + }, + }, + }, + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node2", + Labels: map[string]string{ + projectinfo.GetNodePoolLabel(): "shanghai", + }, + }, + }, + &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "node3", + Labels: map[string]string{ + projectinfo.GetNodePoolLabel(): "hangzhou", }, }, }, @@ -1356,72 +1309,66 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &discovery.EndpointSliceList{ - Items: []discovery.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discovery.LabelServiceName: "svc1", - }, + expectObject: &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.2", }, - Endpoints: []discovery.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", - }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.4", - }, - NodeName: ¤tNodeName, - }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.4", }, + NodeName: ¤tNodeName, }, }, }, }, - "v1.EndpointSliceList: topologyKeys is openyurt.io/nodepool": { + "v1.EndpointSlice: topologyKeys is openyurt.io/nodepool": { enableNodePool: true, - responseObject: &discovery.EndpointSliceList{ - Items: []discovery.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discovery.LabelServiceName: "svc1", - }, + responseObject: &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.2", }, - 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: ¤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, }, }, }, @@ -1489,78 +1436,72 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &discovery.EndpointSliceList{ - Items: []discovery.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discovery.LabelServiceName: "svc1", - }, + expectObject: &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.2", }, - 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: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.4", + }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.5", }, + NodeName: &nodeName3, }, }, }, }, - "v1.EndpointSliceList: topologyKeys is kubernetes.io/zone": { + "v1.EndpointSlice: topologyKeys is kubernetes.io/zone": { enableNodePool: true, - responseObject: &discovery.EndpointSliceList{ - Items: []discovery.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discovery.LabelServiceName: "svc1", - }, + responseObject: &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.2", }, - 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: ¤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, }, }, }, @@ -1628,77 +1569,71 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &discovery.EndpointSliceList{ - Items: []discovery.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discovery.LabelServiceName: "svc1", - }, + expectObject: &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.2", }, - 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: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.4", }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.5", + }, + NodeName: &nodeName3, }, }, }, }, - "v1.EndpointSliceList: without openyurt.io/topologyKeys": { - responseObject: &discovery.EndpointSliceList{ - Items: []discovery.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discovery.LabelServiceName: "svc1", - }, + "v1.EndpointSlice: without openyurt.io/topologyKeys": { + responseObject: &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.2", }, - 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: ¤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, }, }, }, @@ -1764,84 +1699,78 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &discovery.EndpointSliceList{ - Items: []discovery.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discovery.LabelServiceName: "svc1", - }, + expectObject: &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.2", }, - 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: ¤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, }, }, }, }, - "v1.EndpointSliceList: currentNode is not in any nodepool": { + "v1.EndpointSlice: currentNode is not in any nodepool": { enableNodePool: true, - responseObject: &discovery.EndpointSliceList{ - Items: []discovery.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discovery.LabelServiceName: "svc1", - }, + responseObject: &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.2", }, - 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: ¤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, }, }, }, @@ -1906,59 +1835,53 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &discovery.EndpointSliceList{ - Items: []discovery.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discovery.LabelServiceName: "svc1", - }, + expectObject: &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.2", }, - Endpoints: []discovery.Endpoint{ - { - Addresses: []string{ - "10.244.1.2", - }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.4", - }, - NodeName: ¤tNodeName, - }, + NodeName: ¤tNodeName, + }, + { + Addresses: []string{ + "10.244.1.4", }, + NodeName: ¤tNodeName, }, }, }, }, - "v1.EndpointSliceList: currentNode has no endpoints on node": { - responseObject: &discovery.EndpointSliceList{ - Items: []discovery.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discovery.LabelServiceName: "svc1", - }, + "v1.EndpointSlice: currentNode has no endpoints on node": { + responseObject: &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", }, - Endpoints: []discovery.Endpoint{ - { - Addresses: []string{ - "10.244.1.3", - }, - NodeName: &nodeName2, - }, - { - Addresses: []string{ - "10.244.1.5", - }, - NodeName: &nodeName3, - }, + NodeName: &nodeName2, + }, + { + Addresses: []string{ + "10.244.1.5", }, + NodeName: &nodeName3, }, }, }, @@ -2026,46 +1949,39 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &discovery.EndpointSliceList{ - Items: []discovery.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discovery.LabelServiceName: "svc1", - }, - }, + expectObject: &discovery.EndpointSlice{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discovery.LabelServiceName: "svc1", }, }, }, }, - "v1.EndpointSliceList: currentNode has no endpoints in nodePool": { + "v1.EndpointSlice: currentNode has no endpoints in nodePool": { enableNodePool: true, - responseObject: &discovery.EndpointSliceList{ - Items: []discovery.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discovery.LabelServiceName: "svc1", - }, + responseObject: &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", }, - Endpoints: []discovery.Endpoint{ - { - Addresses: []string{ - "10.244.1.3", - }, - NodeName: &nodeName2, - }, - { - Addresses: []string{ - "10.244.1.5", - }, - NodeName: &nodeName3, - }, + NodeName: &nodeName2, + }, + { + Addresses: []string{ + "10.244.1.5", }, + NodeName: &nodeName3, }, }, }, @@ -2133,52 +2049,40 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &discovery.EndpointSliceList{ - Items: []discovery.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discovery.LabelServiceName: "svc1", - }, - }, + expectObject: &discovery.EndpointSlice{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1-np7sf", + Namespace: "default", + Labels: map[string]string{ + discovery.LabelServiceName: "svc1", }, }, }, }, - "v1.EndpointsList: topologyKeys is kubernetes.io/hostname": { - responseObject: &corev1.EndpointsList{ - TypeMeta: metav1.TypeMeta{ - Kind: "EndpointsList", - APIVersion: "v1", + "v1.Endpoints: topologyKeys is kubernetes.io/hostname": { + responseObject: &corev1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", }, - Items: []corev1.Endpoints{ + Subsets: []corev1.EndpointSubset{ { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - }, - Subsets: []corev1.EndpointSubset{ + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.2", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.3", + NodeName: &nodeName2, + }, { - 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, - }, - }, + IP: "10.244.1.4", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.5", + NodeName: &nodeName3, }, }, }, @@ -2248,68 +2152,54 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &corev1.EndpointsList{ - TypeMeta: metav1.TypeMeta{ - Kind: "EndpointsList", - APIVersion: "v1", + expectObject: &corev1.Endpoints{ + + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", }, - Items: []corev1.Endpoints{ + Subsets: []corev1.EndpointSubset{ { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - }, - Subsets: []corev1.EndpointSubset{ + Addresses: []corev1.EndpointAddress{ { - Addresses: []corev1.EndpointAddress{ - { - IP: "10.244.1.2", - NodeName: ¤tNodeName, - }, - { - IP: "10.244.1.4", - NodeName: ¤tNodeName, - }, - }, + IP: "10.244.1.2", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.4", + NodeName: ¤tNodeName, }, }, }, }, }, }, - "v1.EndpointsList: topologyKeys is openyurt.io/nodepool": { + "v1.Endpoints: topologyKeys is openyurt.io/nodepool": { enableNodePool: true, - responseObject: &corev1.EndpointsList{ - TypeMeta: metav1.TypeMeta{ - Kind: "EndpointsList", - APIVersion: "v1", + responseObject: &corev1.Endpoints{ + + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", }, - Items: []corev1.Endpoints{ + Subsets: []corev1.EndpointSubset{ { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - }, - Subsets: []corev1.EndpointSubset{ + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.2", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.3", + NodeName: &nodeName2, + }, + { + IP: "10.244.1.4", + NodeName: ¤tNodeName, + }, { - 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, - }, - }, + IP: "10.244.1.5", + NodeName: &nodeName3, }, }, }, @@ -2379,72 +2269,58 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &corev1.EndpointsList{ - TypeMeta: metav1.TypeMeta{ - Kind: "EndpointsList", - APIVersion: "v1", + expectObject: &corev1.Endpoints{ + + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", }, - Items: []corev1.Endpoints{ + Subsets: []corev1.EndpointSubset{ { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - }, - Subsets: []corev1.EndpointSubset{ + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.2", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.4", + NodeName: ¤tNodeName, + }, { - Addresses: []corev1.EndpointAddress{ - { - IP: "10.244.1.2", - NodeName: ¤tNodeName, - }, - { - IP: "10.244.1.4", - NodeName: ¤tNodeName, - }, - { - IP: "10.244.1.5", - NodeName: &nodeName3, - }, - }, + IP: "10.244.1.5", + NodeName: &nodeName3, }, }, }, }, }, }, - "v1.EndpointsList: topologyKeys is kubernetes.io/zone": { + "v1.Endpoints: topologyKeys is kubernetes.io/zone": { enableNodePool: true, - responseObject: &corev1.EndpointsList{ - TypeMeta: metav1.TypeMeta{ - Kind: "EndpointsList", - APIVersion: "v1", + responseObject: &corev1.Endpoints{ + + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", }, - Items: []corev1.Endpoints{ + Subsets: []corev1.EndpointSubset{ { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - }, - Subsets: []corev1.EndpointSubset{ + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.2", + NodeName: ¤tNodeName, + }, { - 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, - }, - }, + IP: "10.244.1.3", + NodeName: &nodeName2, + }, + { + IP: "10.244.1.4", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.5", + NodeName: &nodeName3, }, }, }, @@ -2514,71 +2390,56 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &corev1.EndpointsList{ - TypeMeta: metav1.TypeMeta{ - Kind: "EndpointsList", - APIVersion: "v1", + expectObject: &corev1.Endpoints{ + + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", }, - Items: []corev1.Endpoints{ + Subsets: []corev1.EndpointSubset{ { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - }, - Subsets: []corev1.EndpointSubset{ + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.2", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.4", + NodeName: ¤tNodeName, + }, { - Addresses: []corev1.EndpointAddress{ - { - IP: "10.244.1.2", - NodeName: ¤tNodeName, - }, - { - IP: "10.244.1.4", - NodeName: ¤tNodeName, - }, - { - IP: "10.244.1.5", - NodeName: &nodeName3, - }, - }, + IP: "10.244.1.5", + NodeName: &nodeName3, }, }, }, }, }, }, - "v1.EndpointsList: without openyurt.io/topologyKeys": { - responseObject: &corev1.EndpointsList{ - TypeMeta: metav1.TypeMeta{ - Kind: "EndpointsList", - APIVersion: "v1", + "v1.Endpoints: without openyurt.io/topologyKeys": { + responseObject: &corev1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", }, - Items: []corev1.Endpoints{ + Subsets: []corev1.EndpointSubset{ { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - }, - Subsets: []corev1.EndpointSubset{ + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.2", + NodeName: ¤tNodeName, + }, { - 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, - }, - }, + IP: "10.244.1.3", + NodeName: &nodeName2, + }, + { + IP: "10.244.1.4", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.5", + NodeName: &nodeName3, }, }, }, @@ -2646,76 +2507,60 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &corev1.EndpointsList{ - TypeMeta: metav1.TypeMeta{ - Kind: "EndpointsList", - APIVersion: "v1", + expectObject: &corev1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", }, - Items: []corev1.Endpoints{ + Subsets: []corev1.EndpointSubset{ { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - }, - Subsets: []corev1.EndpointSubset{ + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.2", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.3", + NodeName: &nodeName2, + }, { - 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, - }, - }, + IP: "10.244.1.4", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.5", + NodeName: &nodeName3, }, }, }, }, }, }, - "v1.EndpointsList: currentNode is not in any nodepool": { + "v1.Endpoints: currentNode is not in any nodepool": { enableNodePool: true, - responseObject: &corev1.EndpointsList{ - TypeMeta: metav1.TypeMeta{ - Kind: "EndpointsList", - APIVersion: "v1", + responseObject: &corev1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", }, - Items: []corev1.Endpoints{ + Subsets: []corev1.EndpointSubset{ { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - }, - Subsets: []corev1.EndpointSubset{ + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.2", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.3", + NodeName: &nodeName2, + }, + { + IP: "10.244.1.4", + NodeName: ¤tNodeName, + }, { - 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, - }, - }, + IP: "10.244.1.5", + NodeName: &nodeName3, }, }, }, @@ -2782,59 +2627,43 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &corev1.EndpointsList{ - TypeMeta: metav1.TypeMeta{ - Kind: "EndpointsList", - APIVersion: "v1", + expectObject: &corev1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", }, - Items: []corev1.Endpoints{ + Subsets: []corev1.EndpointSubset{ { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - }, - Subsets: []corev1.EndpointSubset{ + Addresses: []corev1.EndpointAddress{ { - Addresses: []corev1.EndpointAddress{ - { - IP: "10.244.1.2", - NodeName: ¤tNodeName, - }, - { - IP: "10.244.1.4", - NodeName: ¤tNodeName, - }, - }, + IP: "10.244.1.2", + NodeName: ¤tNodeName, + }, + { + IP: "10.244.1.4", + NodeName: ¤tNodeName, }, }, }, }, }, }, - "v1.EndpointsList: currentNode has no endpoints on node": { - responseObject: &corev1.EndpointsList{ - TypeMeta: metav1.TypeMeta{ - Kind: "EndpointsList", - APIVersion: "v1", + "v1.Endpoints: currentNode has no endpoints on node": { + responseObject: &corev1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", }, - Items: []corev1.Endpoints{ + Subsets: []corev1.EndpointSubset{ { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - }, - Subsets: []corev1.EndpointSubset{ + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.3", + NodeName: &nodeName2, + }, { - Addresses: []corev1.EndpointAddress{ - { - IP: "10.244.1.3", - NodeName: &nodeName2, - }, - { - IP: "10.244.1.5", - NodeName: &nodeName3, - }, - }, + IP: "10.244.1.5", + NodeName: &nodeName3, }, }, }, @@ -2904,46 +2733,30 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &corev1.EndpointsList{ - TypeMeta: metav1.TypeMeta{ - Kind: "EndpointsList", - APIVersion: "v1", - }, - Items: []corev1.Endpoints{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - }, - }, + expectObject: &corev1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", }, }, }, - "v1.EndpointsList: currentNode has no endpoints in nodepool": { + "v1.Endpoints: currentNode has no endpoints in nodepool": { enableNodePool: true, - responseObject: &corev1.EndpointsList{ - TypeMeta: metav1.TypeMeta{ - Kind: "EndpointsList", - APIVersion: "v1", + responseObject: &corev1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", }, - Items: []corev1.Endpoints{ + Subsets: []corev1.EndpointSubset{ { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - }, - Subsets: []corev1.EndpointSubset{ + Addresses: []corev1.EndpointAddress{ { - Addresses: []corev1.EndpointAddress{ - { - IP: "10.244.1.3", - NodeName: &nodeName2, - }, - { - IP: "10.244.1.5", - NodeName: &nodeName3, - }, - }, + IP: "10.244.1.3", + NodeName: &nodeName2, + }, + { + IP: "10.244.1.5", + NodeName: &nodeName3, }, }, }, @@ -3013,45 +2826,29 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &corev1.EndpointsList{ - TypeMeta: metav1.TypeMeta{ - Kind: "EndpointsList", - APIVersion: "v1", - }, - Items: []corev1.Endpoints{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - }, - }, + expectObject: &corev1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", }, }, }, - "v1.EndpointsList: unknown openyurt.io/topologyKeys": { - responseObject: &corev1.EndpointsList{ - TypeMeta: metav1.TypeMeta{ - Kind: "EndpointsList", - APIVersion: "v1", + "v1.Endpoints: unknown openyurt.io/topologyKeys": { + responseObject: &corev1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", }, - Items: []corev1.Endpoints{ + Subsets: []corev1.EndpointSubset{ { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - }, - Subsets: []corev1.EndpointSubset{ + Addresses: []corev1.EndpointAddress{ + { + IP: "10.244.1.3", + NodeName: &nodeName2, + }, { - Addresses: []corev1.EndpointAddress{ - { - IP: "10.244.1.3", - NodeName: &nodeName2, - }, - { - IP: "10.244.1.5", - NodeName: &nodeName3, - }, - }, + IP: "10.244.1.5", + NodeName: &nodeName3, }, }, }, @@ -3121,29 +2918,21 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &corev1.EndpointsList{ - TypeMeta: metav1.TypeMeta{ - Kind: "EndpointsList", - APIVersion: "v1", + expectObject: &corev1.Endpoints{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc1", + Namespace: "default", }, - Items: []corev1.Endpoints{ + Subsets: []corev1.EndpointSubset{ { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - }, - Subsets: []corev1.EndpointSubset{ + Addresses: []corev1.EndpointAddress{ { - Addresses: []corev1.EndpointAddress{ - { - IP: "10.244.1.3", - NodeName: &nodeName2, - }, - { - IP: "10.244.1.5", - NodeName: &nodeName3, - }, - }, + IP: "10.244.1.3", + NodeName: &nodeName2, + }, + { + IP: "10.244.1.5", + NodeName: &nodeName3, }, }, }, @@ -3228,52 +3017,49 @@ func TestFilter(t *testing.T) { }, }, }, - "v1beta1.EndpointSliceList use node bucket: topologyKeys is openyurt.io/nodepool": { + "v1beta1.EndpointSlice use node bucket: topologyKeys is openyurt.io/nodepool": { enablePoolServiceTopology: true, poolName: "hangzhou", - responseObject: &discoveryV1beta1.EndpointSliceList{ - Items: []discoveryV1beta1.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, + responseObject: &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", }, - 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", - }, - }, + 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", }, }, }, @@ -3344,92 +3130,86 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &discoveryV1beta1.EndpointSliceList{ - Items: []discoveryV1beta1.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, + expectObject: &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", }, - 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", - }, - }, + 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.EndpointSliceList use multiple node buckets: topologyKeys is openyurt.io/nodepool": { + "v1beta1.EndpointSlice use multiple node buckets: topologyKeys is openyurt.io/nodepool": { enablePoolServiceTopology: true, poolName: "hangzhou", - responseObject: &discoveryV1beta1.EndpointSliceList{ - Items: []discoveryV1beta1.EndpointSlice{ - { - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1-np7sf", - Namespace: "default", - Labels: map[string]string{ - discoveryV1beta1.LabelServiceName: "svc1", - }, + responseObject: &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", }, - 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", - }, - }, + 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", }, }, }, @@ -3510,166 +3290,39 @@ func TestFilter(t *testing.T) { }, }, ), - expectObject: &discoveryV1beta1.EndpointSliceList{ - Items: []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", - }, - Topology: map[string]string{ - corev1.LabelHostname: "node3", - }, - }, - }, - }, - }, - }, - }, - "v1.EndpointSlice: topologyKeys is kubernetes.io/hostname": { - responseObject: &discovery.EndpointSlice{ + expectObject: &discoveryV1beta1.EndpointSlice{ + ObjectMeta: metav1.ObjectMeta{ Name: "svc1-np7sf", Namespace: "default", Labels: map[string]string{ - discovery.LabelServiceName: "svc1", + discoveryV1beta1.LabelServiceName: "svc1", }, }, - Endpoints: []discovery.Endpoint{ + Endpoints: []discoveryV1beta1.Endpoint{ { Addresses: []string{ "10.244.1.2", }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.3", + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, }, - NodeName: &nodeName2, }, { Addresses: []string{ "10.244.1.4", }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.5", - }, - NodeName: &nodeName3, - }, - }, - }, - kubeClient: k8sfake.NewSimpleClientset( - &corev1.Node{ - ObjectMeta: metav1.ObjectMeta{ - Name: currentNodeName, - Labels: map[string]string{ - projectinfo.GetNodePoolLabel(): "hangzhou", - }, - }, - }, - &corev1.Node{ - ObjectMeta: metav1.ObjectMeta{ - Name: "node2", - Labels: map[string]string{ - projectinfo.GetNodePoolLabel(): "shanghai", - }, - }, - }, - &corev1.Node{ - ObjectMeta: metav1.ObjectMeta{ - Name: "node3", - Labels: map[string]string{ - projectinfo.GetNodePoolLabel(): "hangzhou", - }, - }, - }, - &corev1.Service{ - ObjectMeta: metav1.ObjectMeta{ - Name: "svc1", - Namespace: "default", - Annotations: map[string]string{ - AnnotationServiceTopologyKey: AnnotationServiceTopologyValueNode, - }, - }, - }, - ), - yurtClient: fake.NewSimpleDynamicClientWithCustomListKinds(scheme, gvrToListKind, - &v1beta1.NodePool{ - ObjectMeta: metav1.ObjectMeta{ - Name: "hangzhou", - }, - Spec: v1beta1.NodePoolSpec{ - Type: v1beta1.Edge, - }, - Status: v1beta1.NodePoolStatus{ - Nodes: []string{ - currentNodeName, - "node3", + Topology: map[string]string{ + corev1.LabelHostname: currentNodeName, }, }, - }, - &v1beta1.NodePool{ - ObjectMeta: metav1.ObjectMeta{ - Name: "shanghai", - }, - Spec: v1beta1.NodePoolSpec{ - Type: v1beta1.Edge, - }, - Status: v1beta1.NodePoolStatus{ - Nodes: []string{ - "node2", - }, - }, - }, - ), - expectObject: &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.2", + "10.244.1.5", }, - NodeName: ¤tNodeName, - }, - { - Addresses: []string{ - "10.244.1.4", + Topology: map[string]string{ + corev1.LabelHostname: "node3", }, - NodeName: ¤tNodeName, }, }, },