From 0a42f7b9890b9ffc68e88d9eede5207e7f3ea869 Mon Sep 17 00:00:00 2001 From: Swetha Repakula Date: Fri, 2 Jul 2021 15:49:19 -0700 Subject: [PATCH] Graduate EndpointSliceProxying and WindowsEndpointSliceProxying Gates --- cmd/kube-proxy/app/server_others.go | 2 +- cmd/kube-proxy/app/server_windows.go | 2 +- pkg/features/kube_features.go | 6 +- pkg/proxy/endpoints.go | 9 +- pkg/proxy/endpoints_test.go | 83 +- pkg/proxy/iptables/proxier.go | 40 +- pkg/proxy/iptables/proxier_test.go | 1227 ++++++++++----------- pkg/proxy/ipvs/proxier.go | 40 +- pkg/proxy/ipvs/proxier_test.go | 1521 ++++++++++++-------------- pkg/proxy/topology.go | 2 +- pkg/proxy/topology_test.go | 77 +- pkg/proxy/winkernel/proxier.go | 41 +- pkg/proxy/winkernel/proxier_test.go | 298 +++-- 13 files changed, 1498 insertions(+), 1850 deletions(-) diff --git a/cmd/kube-proxy/app/server_others.go b/cmd/kube-proxy/app/server_others.go index e2c4efc8b47..77efde0280c 100644 --- a/cmd/kube-proxy/app/server_others.go +++ b/cmd/kube-proxy/app/server_others.go @@ -364,7 +364,7 @@ func newProxyServer( } } - useEndpointSlices := utilfeature.DefaultFeatureGate.Enabled(features.EndpointSliceProxying) + useEndpointSlices := true if proxyMode == proxyModeUserspace { // userspace mode doesn't support endpointslice. useEndpointSlices = false diff --git a/cmd/kube-proxy/app/server_windows.go b/cmd/kube-proxy/app/server_windows.go index b8084db6647..fd0c2f25bf1 100644 --- a/cmd/kube-proxy/app/server_windows.go +++ b/cmd/kube-proxy/app/server_windows.go @@ -160,7 +160,7 @@ func newProxyServer(config *proxyconfigapi.KubeProxyConfiguration, cleanupAndExi return nil, fmt.Errorf("unable to create proxier: %v", err) } } - useEndpointSlices := utilfeature.DefaultFeatureGate.Enabled(features.WindowsEndpointSliceProxying) + useEndpointSlices := true if proxyMode == proxyModeUserspace { // userspace mode doesn't support endpointslice. useEndpointSlices = false diff --git a/pkg/features/kube_features.go b/pkg/features/kube_features.go index 7f32ad1db5a..5da24135298 100644 --- a/pkg/features/kube_features.go +++ b/pkg/features/kube_features.go @@ -425,6 +425,7 @@ const ( // kep: http://kep.k8s.io/752 // alpha: v1.18 // beta: v1.19 + // ga: v1.22 // // Enable Endpoint Slice consumption by kube-proxy for improved scalability. EndpointSliceProxying featuregate.Feature = "EndpointSliceProxying" @@ -433,6 +434,7 @@ const ( // kep: http://kep.k8s.io/752 // alpha: v1.19 // beta: v1.21 + // ga: v1.22 // // Enable Endpoint Slice consumption by kube-proxy in Windows for improved scalability. WindowsEndpointSliceProxying featuregate.Feature = "WindowsEndpointSliceProxying" @@ -824,11 +826,11 @@ var defaultKubernetesFeatureGates = map[featuregate.Feature]featuregate.FeatureS PodOverhead: {Default: true, PreRelease: featuregate.Beta}, IPv6DualStack: {Default: true, PreRelease: featuregate.Beta}, EndpointSlice: {Default: true, PreRelease: featuregate.GA, LockToDefault: true}, // remove in 1.25 - EndpointSliceProxying: {Default: true, PreRelease: featuregate.Beta}, + EndpointSliceProxying: {Default: true, PreRelease: featuregate.GA, LockToDefault: true}, // remove in 1.25 EndpointSliceTerminatingCondition: {Default: false, PreRelease: featuregate.Alpha}, ProxyTerminatingEndpoints: {Default: false, PreRelease: featuregate.Alpha}, EndpointSliceNodeName: {Default: true, PreRelease: featuregate.GA, LockToDefault: true}, //remove in 1.25 - WindowsEndpointSliceProxying: {Default: true, PreRelease: featuregate.Beta}, + WindowsEndpointSliceProxying: {Default: true, PreRelease: featuregate.GA, LockToDefault: true}, // remove in 1.25 StartupProbe: {Default: true, PreRelease: featuregate.GA, LockToDefault: true}, // remove in 1.23 AllowInsecureBackendProxy: {Default: true, PreRelease: featuregate.GA, LockToDefault: true}, // remove in 1.23 PodDisruptionBudget: {Default: true, PreRelease: featuregate.GA, LockToDefault: true}, // remove in 1.25 diff --git a/pkg/proxy/endpoints.go b/pkg/proxy/endpoints.go index 0022798e8bb..c42f532f983 100644 --- a/pkg/proxy/endpoints.go +++ b/pkg/proxy/endpoints.go @@ -182,8 +182,8 @@ type EndpointChangeTracker struct { } // NewEndpointChangeTracker initializes an EndpointsChangeMap -func NewEndpointChangeTracker(hostname string, makeEndpointInfo makeEndpointFunc, ipFamily v1.IPFamily, recorder events.EventRecorder, endpointSlicesEnabled bool, processEndpointsMapChange processEndpointsMapChangeFunc) *EndpointChangeTracker { - ect := &EndpointChangeTracker{ +func NewEndpointChangeTracker(hostname string, makeEndpointInfo makeEndpointFunc, ipFamily v1.IPFamily, recorder events.EventRecorder, processEndpointsMapChange processEndpointsMapChangeFunc) *EndpointChangeTracker { + return &EndpointChangeTracker{ hostname: hostname, items: make(map[types.NamespacedName]*endpointsChange), makeEndpointInfo: makeEndpointInfo, @@ -192,11 +192,8 @@ func NewEndpointChangeTracker(hostname string, makeEndpointInfo makeEndpointFunc lastChangeTriggerTimes: make(map[types.NamespacedName][]time.Time), trackerStartTime: time.Now(), processEndpointsMapChange: processEndpointsMapChange, + endpointSliceCache: NewEndpointSliceCache(hostname, ipFamily, recorder, makeEndpointInfo), } - if endpointSlicesEnabled { - ect.endpointSliceCache = NewEndpointSliceCache(hostname, ipFamily, recorder, makeEndpointInfo) - } - return ect } // Update updates given service's endpoints change map based on the endpoints pair. It returns true diff --git a/pkg/proxy/endpoints_test.go b/pkg/proxy/endpoints_test.go index 8d560ea1469..31f1370bb6e 100644 --- a/pkg/proxy/endpoints_test.go +++ b/pkg/proxy/endpoints_test.go @@ -452,7 +452,7 @@ func TestEndpointsToEndpointsMap(t *testing.T) { for _, tc := range testCases { t.Run(tc.desc, func(t *testing.T) { - epTracker := NewEndpointChangeTracker("test-hostname", nil, tc.ipFamily, nil, false, nil) + epTracker := NewEndpointChangeTracker("test-hostname", nil, tc.ipFamily, nil, nil) // outputs newEndpoints := epTracker.endpointsToEndpointsMap(tc.newEndpoints) @@ -1531,7 +1531,7 @@ func TestEndpointSliceUpdate(t *testing.T) { // test starting from an empty state "add a simple slice that doesn't already exist": { startingSlices: []*discovery.EndpointSlice{}, - endpointChangeTracker: NewEndpointChangeTracker("host1", nil, v1.IPv4Protocol, nil, true, nil), + endpointChangeTracker: NewEndpointChangeTracker("host1", nil, v1.IPv4Protocol, nil, nil), namespacedName: types.NamespacedName{Name: "svc1", Namespace: "ns1"}, paramEndpointSlice: generateEndpointSlice("svc1", "ns1", 1, 3, 999, 999, []string{"host1", "host2"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), paramRemoveSlice: false, @@ -1554,7 +1554,7 @@ func TestEndpointSliceUpdate(t *testing.T) { startingSlices: []*discovery.EndpointSlice{ generateEndpointSlice("svc1", "ns1", 1, 3, 999, 999, []string{"host1", "host2"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), }, - endpointChangeTracker: NewEndpointChangeTracker("host1", nil, v1.IPv4Protocol, nil, true, nil), + endpointChangeTracker: NewEndpointChangeTracker("host1", nil, v1.IPv4Protocol, nil, nil), namespacedName: types.NamespacedName{Name: "svc1", Namespace: "ns1"}, paramEndpointSlice: generateEndpointSlice("svc1", "ns1", 1, 3, 999, 999, []string{"host1", "host2"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), paramRemoveSlice: false, @@ -1566,7 +1566,7 @@ func TestEndpointSliceUpdate(t *testing.T) { startingSlices: []*discovery.EndpointSlice{ generateEndpointSlice("svc1", "ns1", 1, 3, 999, 999, []string{"host1", "host2"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), }, - endpointChangeTracker: NewEndpointChangeTracker("host1", nil, v1.IPv4Protocol, nil, true, nil), + endpointChangeTracker: NewEndpointChangeTracker("host1", nil, v1.IPv4Protocol, nil, nil), namespacedName: types.NamespacedName{Name: "svc1", Namespace: "ns1"}, paramEndpointSlice: fqdnSlice, paramRemoveSlice: false, @@ -1579,7 +1579,7 @@ func TestEndpointSliceUpdate(t *testing.T) { generateEndpointSlice("svc1", "ns1", 1, 3, 999, 999, []string{"host1", "host2"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), generateEndpointSlice("svc1", "ns1", 2, 2, 999, 999, []string{"host1", "host2"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), }, - endpointChangeTracker: NewEndpointChangeTracker("host1", nil, v1.IPv4Protocol, nil, true, nil), + endpointChangeTracker: NewEndpointChangeTracker("host1", nil, v1.IPv4Protocol, nil, nil), namespacedName: types.NamespacedName{Name: "svc1", Namespace: "ns1"}, paramEndpointSlice: generateEndpointSlice("svc1", "ns1", 1, 5, 999, 999, []string{"host1"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), paramRemoveSlice: false, @@ -1611,7 +1611,7 @@ func TestEndpointSliceUpdate(t *testing.T) { generateEndpointSlice("svc1", "ns1", 1, 3, 999, 999, []string{"host1", "host2"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), generateEndpointSlice("svc1", "ns1", 2, 2, 999, 999, []string{"host1", "host2"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), }, - endpointChangeTracker: NewEndpointChangeTracker("host1", nil, v1.IPv4Protocol, nil, true, nil), + endpointChangeTracker: NewEndpointChangeTracker("host1", nil, v1.IPv4Protocol, nil, nil), namespacedName: types.NamespacedName{Name: "svc1", Namespace: "ns1"}, paramEndpointSlice: generateEndpointSliceWithOffset("svc1", "ns1", 3, 1, 5, 999, 999, []string{"host1"}, []*int32{utilpointer.Int32Ptr(80)}), paramRemoveSlice: false, @@ -1641,7 +1641,7 @@ func TestEndpointSliceUpdate(t *testing.T) { generateEndpointSlice("svc1", "ns1", 1, 3, 999, 999, []string{"host1", "host2"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), generateEndpointSlice("svc1", "ns1", 2, 2, 999, 999, []string{"host1", "host2"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), }, - endpointChangeTracker: NewEndpointChangeTracker("host1", nil, v1.IPv4Protocol, nil, true, nil), + endpointChangeTracker: NewEndpointChangeTracker("host1", nil, v1.IPv4Protocol, nil, nil), namespacedName: types.NamespacedName{Name: "svc1", Namespace: "ns1"}, paramEndpointSlice: generateEndpointSlice("svc1", "ns1", 1, 5, 999, 999, []string{"host1"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), paramRemoveSlice: true, @@ -1663,7 +1663,7 @@ func TestEndpointSliceUpdate(t *testing.T) { generateEndpointSlice("svc1", "ns1", 1, 5, 999, 999, []string{"host1"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), generateEndpointSlice("svc1", "ns1", 2, 2, 999, 999, []string{"host1"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), }, - endpointChangeTracker: NewEndpointChangeTracker("host1", nil, v1.IPv4Protocol, nil, true, nil), + endpointChangeTracker: NewEndpointChangeTracker("host1", nil, v1.IPv4Protocol, nil, nil), namespacedName: types.NamespacedName{Name: "svc1", Namespace: "ns1"}, paramEndpointSlice: generateEndpointSlice("svc1", "ns1", 3, 5, 999, 999, []string{"host1"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), paramRemoveSlice: true, @@ -1675,7 +1675,7 @@ func TestEndpointSliceUpdate(t *testing.T) { startingSlices: []*discovery.EndpointSlice{ generateEndpointSlice("svc1", "ns1", 1, 3, 999, 999, []string{"host1", "host2"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), }, - endpointChangeTracker: NewEndpointChangeTracker("host1", nil, v1.IPv4Protocol, nil, true, nil), + endpointChangeTracker: NewEndpointChangeTracker("host1", nil, v1.IPv4Protocol, nil, nil), namespacedName: types.NamespacedName{Name: "svc1", Namespace: "ns1"}, paramEndpointSlice: generateEndpointSlice("svc1", "ns1", 1, 3, 1, 999, []string{"host1"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), paramRemoveSlice: false, @@ -1698,7 +1698,7 @@ func TestEndpointSliceUpdate(t *testing.T) { startingSlices: []*discovery.EndpointSlice{ generateEndpointSlice("svc1", "ns1", 1, 2, 1, 999, []string{"host1", "host2"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), }, - endpointChangeTracker: NewEndpointChangeTracker("host1", nil, v1.IPv4Protocol, nil, true, nil), + endpointChangeTracker: NewEndpointChangeTracker("host1", nil, v1.IPv4Protocol, nil, nil), namespacedName: types.NamespacedName{Name: "svc1", Namespace: "ns1"}, paramEndpointSlice: generateEndpointSlice("svc1", "ns1", 1, 2, 999, 999, []string{"host1"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), paramRemoveSlice: false, @@ -1720,7 +1720,7 @@ func TestEndpointSliceUpdate(t *testing.T) { generateEndpointSlice("svc1", "ns1", 1, 3, 2, 999, []string{"host1"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), generateEndpointSlice("svc1", "ns1", 2, 2, 2, 999, []string{"host1"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), }, - endpointChangeTracker: NewEndpointChangeTracker("host1", nil, v1.IPv4Protocol, nil, true, nil), + endpointChangeTracker: NewEndpointChangeTracker("host1", nil, v1.IPv4Protocol, nil, nil), namespacedName: types.NamespacedName{Name: "svc1", Namespace: "ns1"}, paramEndpointSlice: generateEndpointSlice("svc1", "ns1", 1, 3, 3, 999, []string{"host1"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), paramRemoveSlice: false, @@ -1748,7 +1748,7 @@ func TestEndpointSliceUpdate(t *testing.T) { generateEndpointSlice("svc1", "ns1", 1, 3, 2, 2, []string{"host1"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), generateEndpointSlice("svc1", "ns1", 2, 2, 2, 2, []string{"host1"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), }, - endpointChangeTracker: NewEndpointChangeTracker("host1", nil, v1.IPv4Protocol, nil, true, nil), + endpointChangeTracker: NewEndpointChangeTracker("host1", nil, v1.IPv4Protocol, nil, nil), namespacedName: types.NamespacedName{Name: "svc1", Namespace: "ns1"}, paramEndpointSlice: generateEndpointSlice("svc1", "ns1", 1, 3, 3, 2, []string{"host1"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), paramRemoveSlice: false, @@ -1805,64 +1805,31 @@ func TestCheckoutChanges(t *testing.T) { testCases := map[string]struct { endpointChangeTracker *EndpointChangeTracker expectedChanges []*endpointsChange - useEndpointSlices bool items map[types.NamespacedName]*endpointsChange appliedSlices []*discovery.EndpointSlice pendingSlices []*discovery.EndpointSlice }{ "empty slices": { - endpointChangeTracker: NewEndpointChangeTracker("", nil, v1.IPv4Protocol, nil, true, nil), + endpointChangeTracker: NewEndpointChangeTracker("", nil, v1.IPv4Protocol, nil, nil), expectedChanges: []*endpointsChange{}, - useEndpointSlices: true, appliedSlices: []*discovery.EndpointSlice{}, pendingSlices: []*discovery.EndpointSlice{}, }, - "without slices, empty items": { - endpointChangeTracker: NewEndpointChangeTracker("", nil, v1.IPv4Protocol, nil, false, nil), - expectedChanges: []*endpointsChange{}, - items: map[types.NamespacedName]*endpointsChange{}, - useEndpointSlices: false, - }, - "without slices, simple items": { - endpointChangeTracker: NewEndpointChangeTracker("", nil, v1.IPv4Protocol, nil, false, nil), - expectedChanges: []*endpointsChange{{ - previous: EndpointsMap{ - svcPortName0: []Endpoint{newTestEp("10.0.1.1:80", "", true, true, false), newTestEp("10.0.1.2:80", "", true, true, false)}, - svcPortName1: []Endpoint{newTestEp("10.0.1.1:443", "", true, true, false), newTestEp("10.0.1.2:443", "", true, true, false)}, - }, - current: EndpointsMap{ - svcPortName0: []Endpoint{newTestEp("10.0.1.1:80", "", true, true, false), newTestEp("10.0.1.2:80", "", true, true, false)}, - }, - }}, - items: map[types.NamespacedName]*endpointsChange{ - {Namespace: "ns1", Name: "svc1"}: { - previous: EndpointsMap{ - svcPortName0: []Endpoint{newTestEp("10.0.1.1:80", "", true, true, false), newTestEp("10.0.1.2:80", "", true, true, false)}, - svcPortName1: []Endpoint{newTestEp("10.0.1.1:443", "", true, true, false), newTestEp("10.0.1.2:443", "", true, true, false)}, - }, - current: EndpointsMap{ - svcPortName0: []Endpoint{newTestEp("10.0.1.1:80", "", true, true, false), newTestEp("10.0.1.2:80", "", true, true, false)}, - }, - }, - }, - useEndpointSlices: false, - }, "adding initial slice": { - endpointChangeTracker: NewEndpointChangeTracker("", nil, v1.IPv4Protocol, nil, true, nil), + endpointChangeTracker: NewEndpointChangeTracker("", nil, v1.IPv4Protocol, nil, nil), expectedChanges: []*endpointsChange{{ previous: EndpointsMap{}, current: EndpointsMap{ svcPortName0: []Endpoint{newTestEp("10.0.1.1:80", "host1", true, true, false), newTestEp("10.0.1.2:80", "host1", false, true, true), newTestEp("10.0.1.3:80", "host1", false, false, false)}, }, }}, - useEndpointSlices: true, - appliedSlices: []*discovery.EndpointSlice{}, + appliedSlices: []*discovery.EndpointSlice{}, pendingSlices: []*discovery.EndpointSlice{ generateEndpointSlice("svc1", "ns1", 1, 3, 3, 2, []string{"host1"}, []*int32{utilpointer.Int32Ptr(80)}), }, }, "removing port in update": { - endpointChangeTracker: NewEndpointChangeTracker("", nil, v1.IPv4Protocol, nil, true, nil), + endpointChangeTracker: NewEndpointChangeTracker("", nil, v1.IPv4Protocol, nil, nil), expectedChanges: []*endpointsChange{{ previous: EndpointsMap{ svcPortName0: []Endpoint{newTestEp("10.0.1.1:80", "host1", true, true, false), newTestEp("10.0.1.2:80", "host1", true, true, false), newTestEp("10.0.1.3:80", "host1", false, false, false)}, @@ -1872,7 +1839,6 @@ func TestCheckoutChanges(t *testing.T) { svcPortName0: []Endpoint{newTestEp("10.0.1.1:80", "host1", true, true, false), newTestEp("10.0.1.2:80", "host1", true, true, false), newTestEp("10.0.1.3:80", "host1", false, false, false)}, }, }}, - useEndpointSlices: true, appliedSlices: []*discovery.EndpointSlice{ generateEndpointSlice("svc1", "ns1", 1, 3, 3, 999, []string{"host1"}, []*int32{utilpointer.Int32Ptr(80), utilpointer.Int32Ptr(443)}), }, @@ -1884,18 +1850,13 @@ func TestCheckoutChanges(t *testing.T) { for name, tc := range testCases { t.Run(name, func(t *testing.T) { - if tc.useEndpointSlices { - for _, slice := range tc.appliedSlices { - tc.endpointChangeTracker.EndpointSliceUpdate(slice, false) - } - tc.endpointChangeTracker.checkoutChanges() - for _, slice := range tc.pendingSlices { - tc.endpointChangeTracker.EndpointSliceUpdate(slice, false) - } - } else { - tc.endpointChangeTracker.items = tc.items + for _, slice := range tc.appliedSlices { + tc.endpointChangeTracker.EndpointSliceUpdate(slice, false) + } + tc.endpointChangeTracker.checkoutChanges() + for _, slice := range tc.pendingSlices { + tc.endpointChangeTracker.EndpointSliceUpdate(slice, false) } - changes := tc.endpointChangeTracker.checkoutChanges() if len(tc.expectedChanges) != len(changes) { diff --git a/pkg/proxy/iptables/proxier.go b/pkg/proxy/iptables/proxier.go index 25092c0f7aa..7f015b4be37 100644 --- a/pkg/proxy/iptables/proxier.go +++ b/pkg/proxy/iptables/proxier.go @@ -191,10 +191,9 @@ type Proxier struct { endpointsMap proxy.EndpointsMap portsMap map[utilnet.LocalPort]utilnet.Closeable nodeLabels map[string]string - // endpointsSynced, endpointSlicesSynced, and servicesSynced are set to true + // endpointSlicesSynced, and servicesSynced are set to true // when corresponding objects are synced after startup. This is used to avoid // updating iptables with some partial data after kube-proxy restart. - endpointsSynced bool endpointSlicesSynced bool servicesSynced bool initialized int32 @@ -281,8 +280,6 @@ func NewProxier(ipt utiliptables.Interface, masqueradeMark := fmt.Sprintf("%#08x", masqueradeValue) klog.V(2).InfoS("Using iptables mark for masquerade", "ipFamily", ipt.Protocol(), "mark", masqueradeMark) - endpointSlicesEnabled := utilfeature.DefaultFeatureGate.Enabled(features.EndpointSliceProxying) - serviceHealthServer := healthcheck.NewServiceHealthServer(hostname, recorder) ipFamily := v1.IPv4Protocol @@ -302,7 +299,7 @@ func NewProxier(ipt utiliptables.Interface, serviceMap: make(proxy.ServiceMap), serviceChanges: proxy.NewServiceChangeTracker(newServiceInfo, ipFamily, recorder, nil), endpointsMap: make(proxy.EndpointsMap), - endpointsChanges: proxy.NewEndpointChangeTracker(hostname, newEndpointInfo, ipFamily, recorder, endpointSlicesEnabled, nil), + endpointsChanges: proxy.NewEndpointChangeTracker(hostname, newEndpointInfo, ipFamily, recorder, nil), syncPeriod: syncPeriod, iptables: ipt, masqueradeAll: masqueradeAll, @@ -575,48 +572,31 @@ func (proxier *Proxier) OnServiceDelete(service *v1.Service) { func (proxier *Proxier) OnServiceSynced() { proxier.mu.Lock() proxier.servicesSynced = true - if utilfeature.DefaultFeatureGate.Enabled(features.EndpointSliceProxying) { - proxier.setInitialized(proxier.endpointSlicesSynced) - } else { - proxier.setInitialized(proxier.endpointsSynced) - } + proxier.setInitialized(proxier.endpointSlicesSynced) proxier.mu.Unlock() // Sync unconditionally - this is called once per lifetime. proxier.syncProxyRules() } +// iptables proxier only uses EndpointSlice, the following methods +// exist to implement the Proxier interface but are noops + // OnEndpointsAdd is called whenever creation of new endpoints object // is observed. -func (proxier *Proxier) OnEndpointsAdd(endpoints *v1.Endpoints) { - proxier.OnEndpointsUpdate(nil, endpoints) -} +func (proxier *Proxier) OnEndpointsAdd(endpoints *v1.Endpoints) {} // OnEndpointsUpdate is called whenever modification of an existing // endpoints object is observed. -func (proxier *Proxier) OnEndpointsUpdate(oldEndpoints, endpoints *v1.Endpoints) { - if proxier.endpointsChanges.Update(oldEndpoints, endpoints) && proxier.isInitialized() { - proxier.Sync() - } -} +func (proxier *Proxier) OnEndpointsUpdate(oldEndpoints, endpoints *v1.Endpoints) {} // OnEndpointsDelete is called whenever deletion of an existing endpoints // object is observed. -func (proxier *Proxier) OnEndpointsDelete(endpoints *v1.Endpoints) { - proxier.OnEndpointsUpdate(endpoints, nil) -} +func (proxier *Proxier) OnEndpointsDelete(endpoints *v1.Endpoints) {} // OnEndpointsSynced is called once all the initial event handlers were // called and the state is fully propagated to local cache. -func (proxier *Proxier) OnEndpointsSynced() { - proxier.mu.Lock() - proxier.endpointsSynced = true - proxier.setInitialized(proxier.servicesSynced) - proxier.mu.Unlock() - - // Sync unconditionally - this is called once per lifetime. - proxier.syncProxyRules() -} +func (proxier *Proxier) OnEndpointsSynced() {} // OnEndpointSliceAdd is called whenever creation of a new endpoint slice object // is observed. diff --git a/pkg/proxy/iptables/proxier_test.go b/pkg/proxy/iptables/proxier_test.go index 81d272d6175..61a53dc51e5 100644 --- a/pkg/proxy/iptables/proxier_test.go +++ b/pkg/proxy/iptables/proxier_test.go @@ -243,7 +243,7 @@ func TestDeleteEndpointConnectionsIPv4(t *testing.T) { } ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) fp.exec = &fexec for _, tc := range testCases { @@ -386,7 +386,7 @@ func TestDeleteEndpointConnectionsIPv6(t *testing.T) { } ipt := iptablestest.NewIPv6Fake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) fp.exec = &fexec for _, tc := range testCases { @@ -483,7 +483,7 @@ func (f *fakePortOpener) OpenLocalPort(lp *utilnet.LocalPort) (utilnet.Closeable const testHostname = "test-hostname" -func NewFakeProxier(ipt utiliptables.Interface, endpointSlicesEnabled bool) *Proxier { +func NewFakeProxier(ipt utiliptables.Interface) *Proxier { // TODO: Call NewProxier after refactoring out the goroutine // invocation into a Run() method. ipfamily := v1.IPv4Protocol @@ -496,7 +496,7 @@ func NewFakeProxier(ipt utiliptables.Interface, endpointSlicesEnabled bool) *Pro serviceMap: make(proxy.ServiceMap), serviceChanges: proxy.NewServiceChangeTracker(newServiceInfo, ipfamily, nil, nil), endpointsMap: make(proxy.EndpointsMap), - endpointsChanges: proxy.NewEndpointChangeTracker(testHostname, newEndpointInfo, ipfamily, nil, endpointSlicesEnabled, nil), + endpointsChanges: proxy.NewEndpointChangeTracker(testHostname, newEndpointInfo, ipfamily, nil, nil), iptables: ipt, masqueradeMark: "0x4000", localDetector: detectLocal, @@ -715,12 +715,13 @@ func errorf(msg string, rules []iptablestest.Rule, t *testing.T) { // the NAT table rules look like with the different jumps. func TestOverallIPTablesRulesWithMultipleServices(t *testing.T) { ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) metrics.RegisterMetrics() + tcpProtocol := v1.ProtocolTCP makeServiceMap(fp, // create ClusterIP service - makeTestService(makeNSN("ns1", "svc1").Namespace, "svc1", func(svc *v1.Service) { + makeTestService("ns1", "svc1", func(svc *v1.Service) { svc.Spec.ClusterIP = "10.20.30.41" svc.Spec.Ports = []v1.ServicePort{{ Name: "p80", @@ -729,7 +730,7 @@ func TestOverallIPTablesRulesWithMultipleServices(t *testing.T) { }} }), // create LoadBalancer service - makeTestService(makeNSN("ns2", "svc2").Namespace, "svc2", func(svc *v1.Service) { + makeTestService("ns2", "svc2", func(svc *v1.Service) { svc.Spec.Type = "LoadBalancer" svc.Spec.ExternalTrafficPolicy = v1.ServiceExternalTrafficPolicyTypeLocal svc.Spec.ClusterIP = "10.20.30.42" @@ -749,7 +750,7 @@ func TestOverallIPTablesRulesWithMultipleServices(t *testing.T) { svc.Spec.HealthCheckNodePort = 30000 }), // create NodePort service - makeTestService(makeNSN("ns3", "svc3").Namespace, "svc3", func(svc *v1.Service) { + makeTestService("ns3", "svc3", func(svc *v1.Service) { svc.Spec.Type = "NodePort" svc.Spec.ClusterIP = "10.20.30.43" svc.Spec.Ports = []v1.ServicePort{{ @@ -760,7 +761,7 @@ func TestOverallIPTablesRulesWithMultipleServices(t *testing.T) { }} }), // create ExternalIP service - makeTestService(makeNSN("ns4", "svc4").Namespace, "svc4", func(svc *v1.Service) { + makeTestService("ns4", "svc4", func(svc *v1.Service) { svc.Spec.Type = "NodePort" svc.Spec.ClusterIP = "10.20.30.44" svc.Spec.ExternalIPs = []string{"50.60.70.81"} @@ -772,61 +773,56 @@ func TestOverallIPTablesRulesWithMultipleServices(t *testing.T) { }} }), ) - makeEndpointsMap(fp, + populateEndpointSlices(fp, // create ClusterIP service endpoints - makeTestEndpoints(makeNSN("ns1", "svc1").Namespace, "svc1", func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "10.180.0.1", - }}, - Ports: []v1.EndpointPort{{ - Name: "p80", - Port: 80, - Protocol: v1.ProtocolTCP, - }}, + makeTestEndpointSlice("ns1", "svc1", 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"10.180.0.1"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr("p80"), + Port: utilpointer.Int32(80), + Protocol: &tcpProtocol, }} }), // create LoadBalancer endpoints - makeTestEndpoints(makeNSN("ns2", "svc2").Namespace, "svc2", func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "10.180.0.2", - }}, - Ports: []v1.EndpointPort{{ - Name: "p80", - Port: 80, - Protocol: v1.ProtocolTCP, - }}, + makeTestEndpointSlice("ns2", "svc2", 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"10.180.0.2"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr("p80"), + Port: utilpointer.Int32(80), + Protocol: &tcpProtocol, }} }), // create NodePort service endpoints - makeTestEndpoints(makeNSN("ns3", "svc3").Namespace, "svc3", func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "10.180.0.3", - }}, - Ports: []v1.EndpointPort{{ - Name: "p80", - Port: 80, - Protocol: v1.ProtocolTCP, - }}, + makeTestEndpointSlice("ns3", "svc3", 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"10.180.0.3"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr("p80"), + Port: utilpointer.Int32(80), + Protocol: &tcpProtocol, }} }), // create ExternalIP service endpoints - makeTestEndpoints(makeNSN("ns4", "svc4").Namespace, "svc4", func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "10.180.0.4", - NodeName: nil, - }, { - IP: "10.180.0.5", - NodeName: utilpointer.StringPtr(testHostname), - }}, - Ports: []v1.EndpointPort{{ - Name: "p80", - Port: 80, - Protocol: v1.ProtocolTCP, - }}, + makeTestEndpointSlice("ns4", "svc4", 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"10.180.0.4"}, + }, { + Addresses: []string{"10.180.0.5"}, + NodeName: utilpointer.StringPtr(testHostname), + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr("p80"), + Port: utilpointer.Int32(80), + Protocol: &tcpProtocol, }} }), ) @@ -925,7 +921,7 @@ COMMIT func TestClusterIPReject(t *testing.T) { ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) svcIP := "10.20.30.41" svcPort := 80 svcPortName := proxy.ServicePortName{ @@ -943,7 +939,6 @@ func TestClusterIPReject(t *testing.T) { }} }), ) - makeEndpointsMap(fp) fp.syncProxyRules() svcChain := string(servicePortChainName(svcPortName.String(), strings.ToLower(string(v1.ProtocolTCP)))) @@ -959,7 +954,7 @@ func TestClusterIPReject(t *testing.T) { func TestClusterIPEndpointsJump(t *testing.T) { ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) svcIP := "10.20.30.41" svcPort := 80 svcPortName := proxy.ServicePortName{ @@ -980,17 +975,17 @@ func TestClusterIPEndpointsJump(t *testing.T) { ) epIP := "10.180.0.1" - makeEndpointsMap(fp, - makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIP, - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName.Port, - Port: int32(svcPort), - Protocol: v1.ProtocolTCP, - }}, + tcpProtocol := v1.ProtocolTCP + populateEndpointSlices(fp, + makeTestEndpointSlice(svcPortName.Namespace, svcPortName.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIP}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName.Port), + Port: utilpointer.Int32(int32(svcPort)), + Protocol: &tcpProtocol, }} }), ) @@ -1022,7 +1017,7 @@ func TestClusterIPEndpointsJump(t *testing.T) { func TestLoadBalancer(t *testing.T) { ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) svcIP := "10.20.30.41" svcPort := 80 svcNodePort := 3001 @@ -1054,17 +1049,17 @@ func TestLoadBalancer(t *testing.T) { ) epIP := "10.180.0.1" - makeEndpointsMap(fp, - makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIP, - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName.Port, - Port: int32(svcPort), - Protocol: v1.ProtocolTCP, - }}, + tcpProtocol := v1.ProtocolTCP + populateEndpointSlices(fp, + makeTestEndpointSlice(svcPortName.Namespace, svcPortName.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIP}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName.Port), + Port: utilpointer.Int32(int32(svcPort)), + Protocol: &tcpProtocol, }} }), ) @@ -1088,7 +1083,7 @@ func TestLoadBalancer(t *testing.T) { func TestNodePort(t *testing.T) { ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) svcIP := "10.20.30.41" svcPort := 80 svcNodePort := 3001 @@ -1112,17 +1107,17 @@ func TestNodePort(t *testing.T) { ) epIP := "10.180.0.1" - makeEndpointsMap(fp, - makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIP, - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName.Port, - Port: int32(svcPort), - Protocol: v1.ProtocolTCP, - }}, + tcpProtocol := v1.ProtocolTCP + populateEndpointSlices(fp, + makeTestEndpointSlice(svcPortName.Namespace, svcPortName.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIP}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName.Port), + Port: utilpointer.Int32(int32(svcPort)), + Protocol: &tcpProtocol, }} }), ) @@ -1153,7 +1148,7 @@ func TestNodePort(t *testing.T) { func TestHealthCheckNodePort(t *testing.T) { ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) svcIP := "10.20.30.42" svcPort := 80 svcNodePort := 3001 @@ -1198,9 +1193,7 @@ func TestHealthCheckNodePort(t *testing.T) { func TestMasqueradeRule(t *testing.T) { for _, testcase := range []bool{false, true} { ipt := iptablestest.NewFake().SetHasRandomFully(testcase) - fp := NewFakeProxier(ipt, false) - makeServiceMap(fp) - makeEndpointsMap(fp) + fp := NewFakeProxier(ipt) fp.syncProxyRules() postRoutingRules := ipt.GetRules(string(kubePostroutingChain)) @@ -1216,7 +1209,7 @@ func TestMasqueradeRule(t *testing.T) { func TestExternalIPsReject(t *testing.T) { ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) svcIP := "10.20.30.41" svcPort := 80 svcExternalIPs := "50.60.70.81" @@ -1238,7 +1231,6 @@ func TestExternalIPsReject(t *testing.T) { }} }), ) - makeEndpointsMap(fp) fp.syncProxyRules() @@ -1250,7 +1242,7 @@ func TestExternalIPsReject(t *testing.T) { func TestOnlyLocalExternalIPs(t *testing.T) { ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) svcIP := "10.20.30.41" svcPort := 80 svcExternalIPs := "50.60.70.81" @@ -1273,26 +1265,24 @@ func TestOnlyLocalExternalIPs(t *testing.T) { }} }), ) - makeEndpointsMap(fp) epIP1 := "10.180.0.1" epIP2 := "10.180.2.1" epStrLocal := fmt.Sprintf("%s:%d", epIP1, svcPort) epStrNonLocal := fmt.Sprintf("%s:%d", epIP2, svcPort) - makeEndpointsMap(fp, - makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIP1, - NodeName: nil, - }, { - IP: epIP2, - NodeName: utilpointer.StringPtr(testHostname), - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName.Port, - Port: int32(svcPort), - Protocol: v1.ProtocolTCP, - }}, + tcpProtocol := v1.ProtocolTCP + populateEndpointSlices(fp, + makeTestEndpointSlice(svcPortName.Namespace, svcPortName.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIP1}, + }, { + Addresses: []string{epIP2}, + NodeName: utilpointer.StringPtr(testHostname), + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName.Port), + Port: utilpointer.Int32(int32(svcPort)), + Protocol: &tcpProtocol, }} }), ) @@ -1323,7 +1313,7 @@ func TestOnlyLocalExternalIPs(t *testing.T) { // SNAT packets to external IPs if externalTrafficPolicy is cluster and the traffic is NOT Local. func TestNonLocalExternalIPs(t *testing.T) { ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) svcIP := "10.20.30.41" svcPort := 80 svcExternalIPs := "50.60.70.81" @@ -1344,24 +1334,23 @@ func TestNonLocalExternalIPs(t *testing.T) { }} }), ) - makeEndpointsMap(fp) epIP1 := "10.180.0.1" epIP2 := "10.180.2.1" - makeEndpointsMap(fp, - makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIP1, - NodeName: nil, - }, { - IP: epIP2, - NodeName: utilpointer.StringPtr(testHostname), - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName.Port, - Port: int32(svcPort), - Protocol: v1.ProtocolTCP, - }}, + tcpProtocol := v1.ProtocolTCP + populateEndpointSlices(fp, + makeTestEndpointSlice(svcPortName.Namespace, svcPortName.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIP1}, + NodeName: nil, + }, { + Addresses: []string{epIP2}, + NodeName: utilpointer.StringPtr(testHostname), + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName.Port), + Port: utilpointer.Int32(int32(svcPort)), + Protocol: &tcpProtocol, }} }), ) @@ -1397,7 +1386,7 @@ func TestNonLocalExternalIPs(t *testing.T) { func TestNodePortReject(t *testing.T) { ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) svcIP := "10.20.30.41" svcPort := 80 svcNodePort := 3001 @@ -1418,7 +1407,6 @@ func TestNodePortReject(t *testing.T) { }} }), ) - makeEndpointsMap(fp) fp.syncProxyRules() @@ -1430,7 +1418,7 @@ func TestNodePortReject(t *testing.T) { func TestLoadBalancerReject(t *testing.T) { ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) svcIP := "10.20.30.41" svcPort := 80 svcNodePort := 3001 @@ -1461,7 +1449,6 @@ func TestLoadBalancerReject(t *testing.T) { } }), ) - makeEndpointsMap(fp) fp.syncProxyRules() @@ -1479,7 +1466,7 @@ func TestLoadBalancerReject(t *testing.T) { func TestOnlyLocalLoadBalancing(t *testing.T) { ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) svcIP := "10.20.30.41" svcPort := 80 svcNodePort := 3001 @@ -1516,21 +1503,20 @@ func TestOnlyLocalLoadBalancing(t *testing.T) { epIP2 := "10.180.2.1" epStrLocal := fmt.Sprintf("%s:%d", epIP1, svcPort) epStrNonLocal := fmt.Sprintf("%s:%d", epIP2, svcPort) - makeEndpointsMap(fp, - makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIP1, - NodeName: nil, - }, { - IP: epIP2, - NodeName: utilpointer.StringPtr(testHostname), - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName.Port, - Port: int32(svcPort), - Protocol: v1.ProtocolTCP, - }}, + tcpProtocol := v1.ProtocolTCP + populateEndpointSlices(fp, + makeTestEndpointSlice(svcPortName.Namespace, svcPortName.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIP1}, + }, { + Addresses: []string{epIP2}, + NodeName: utilpointer.StringPtr(testHostname), + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName.Port), + Port: utilpointer.Int32(int32(svcPort)), + Protocol: &tcpProtocol, }} }), ) @@ -1571,13 +1557,13 @@ func TestOnlyLocalLoadBalancing(t *testing.T) { func TestOnlyLocalNodePortsNoClusterCIDR(t *testing.T) { ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) onlyLocalNodePorts(t, fp, ipt) } func TestOnlyLocalNodePorts(t *testing.T) { ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) onlyLocalNodePorts(t, fp, ipt) } @@ -1609,21 +1595,21 @@ func onlyLocalNodePorts(t *testing.T, fp *Proxier, ipt *iptablestest.FakeIPTable epIP2 := "10.180.2.1" epStrLocal := fmt.Sprintf("%s:%d", epIP1, svcPort) epStrNonLocal := fmt.Sprintf("%s:%d", epIP2, svcPort) - makeEndpointsMap(fp, - makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIP1, - NodeName: nil, - }, { - IP: epIP2, - NodeName: utilpointer.StringPtr(testHostname), - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName.Port, - Port: int32(svcPort), - Protocol: v1.ProtocolTCP, - }}, + tcpProtocol := v1.ProtocolTCP + populateEndpointSlices(fp, + makeTestEndpointSlice(svcPortName.Namespace, svcPortName.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIP1}, + NodeName: nil, + }, { + Addresses: []string{epIP2}, + NodeName: utilpointer.StringPtr(testHostname), + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName.Port), + Port: utilpointer.Int32(int32(svcPort)), + Protocol: &tcpProtocol, }} }), ) @@ -1726,7 +1712,7 @@ func addTestPort(array []v1.ServicePort, name string, protocol v1.Protocol, port func TestBuildServiceMapAddRemove(t *testing.T) { ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) services := []*v1.Service{ makeTestService("somewhere-else", "cluster-ip", func(svc *v1.Service) { @@ -1832,7 +1818,7 @@ func TestBuildServiceMapAddRemove(t *testing.T) { func TestBuildServiceMapServiceHeadless(t *testing.T) { ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) makeServiceMap(fp, makeTestService("somewhere-else", "headless", func(svc *v1.Service) { @@ -1864,7 +1850,7 @@ func TestBuildServiceMapServiceHeadless(t *testing.T) { func TestBuildServiceMapServiceTypeExternalName(t *testing.T) { ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) makeServiceMap(fp, makeTestService("somewhere-else", "external-name", func(svc *v1.Service) { @@ -1890,7 +1876,7 @@ func TestBuildServiceMapServiceTypeExternalName(t *testing.T) { func TestBuildServiceMapServiceUpdate(t *testing.T) { ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) servicev1 := makeTestService("somewhere", "some-service", func(svc *v1.Service) { svc.Spec.Type = v1.ServiceTypeClusterIP @@ -1969,25 +1955,22 @@ func TestBuildServiceMapServiceUpdate(t *testing.T) { } } -func makeTestEndpoints(namespace, name string, eptFunc func(*v1.Endpoints)) *v1.Endpoints { - ept := &v1.Endpoints{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: namespace, - }, +func populateEndpointSlices(proxier *Proxier, allEndpointSlices ...*discovery.EndpointSlice) { + for i := range allEndpointSlices { + proxier.OnEndpointSliceAdd(allEndpointSlices[i]) } - eptFunc(ept) - return ept } -func makeEndpointsMap(proxier *Proxier, allEndpoints ...*v1.Endpoints) { - for i := range allEndpoints { - proxier.OnEndpointsAdd(allEndpoints[i]) +func makeTestEndpointSlice(namespace, name string, sliceNum int, epsFunc func(*discovery.EndpointSlice)) *discovery.EndpointSlice { + eps := &discovery.EndpointSlice{ + ObjectMeta: metav1.ObjectMeta{ + Name: fmt.Sprintf("%s-%d", name, sliceNum), + Namespace: namespace, + Labels: map[string]string{discovery.LabelServiceName: name}, + }, } - - proxier.mu.Lock() - defer proxier.mu.Unlock() - proxier.endpointsSynced = true + epsFunc(eps) + return eps } func makeNSN(namespace, name string) types.NamespacedName { @@ -2039,347 +2022,340 @@ func compareEndpointsMaps(t *testing.T, tci int, newMap proxy.EndpointsMap, expe func Test_updateEndpointsMap(t *testing.T) { var nodeName = testHostname + udpProtocol := v1.ProtocolUDP - emptyEndpoint := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{} + emptyEndpointSlices := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, func(*discovery.EndpointSlice) {}), } - unnamedPort := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - }}, - Ports: []v1.EndpointPort{{ - Port: 11, - Protocol: v1.ProtocolUDP, - }}, + subset1 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.1"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p11"), + Port: utilpointer.Int32(11), + Protocol: &udpProtocol, }} } - unnamedPortLocal := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Port: 11, - Protocol: v1.ProtocolUDP, - }}, + subset2 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.2"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p12"), + Port: utilpointer.Int32(12), + Protocol: &udpProtocol, }} } - namedPortLocal := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p11", - Port: 11, - Protocol: v1.ProtocolUDP, - }}, + namedPortLocal := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, + func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.1"}, + NodeName: &nodeName, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p11"), + Port: utilpointer.Int32(11), + Protocol: &udpProtocol, + }} + }), + } + namedPort := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, subset1), + } + namedPortRenamed := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, + func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.1"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p11-2"), + Port: utilpointer.Int32(11), + Protocol: &udpProtocol, + }} + }), + } + namedPortRenumbered := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, + func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.1"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p11"), + Port: utilpointer.Int32(22), + Protocol: &udpProtocol, + }} + }), + } + namedPortsLocalNoLocal := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, + func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.1"}, + }, { + Addresses: []string{"1.1.1.2"}, + NodeName: &nodeName, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p11"), + Port: utilpointer.Int32(11), + Protocol: &udpProtocol, + }, { + Name: utilpointer.String("p12"), + Port: utilpointer.Int32(12), + Protocol: &udpProtocol, + }} + }), + } + multipleSubsets := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, subset1), + makeTestEndpointSlice("ns1", "ep1", 2, subset2), + } + subsetLocal := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.2"}, + NodeName: &nodeName, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p12"), + Port: utilpointer.Int32(12), + Protocol: &udpProtocol, }} } - namedPort := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - }}, - Ports: []v1.EndpointPort{{ - Name: "p11", - Port: 11, - Protocol: v1.ProtocolUDP, - }}, - }} + multipleSubsetsWithLocal := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, subset1), + makeTestEndpointSlice("ns1", "ep1", 2, subsetLocal), } - namedPortRenamed := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - }}, - Ports: []v1.EndpointPort{{ - Name: "p11-2", - Port: 11, - Protocol: v1.ProtocolUDP, - }}, + subsetMultiplePortsLocal := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.1"}, + NodeName: &nodeName, }} - } - namedPortRenumbered := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - }}, - Ports: []v1.EndpointPort{{ - Name: "p11", - Port: 22, - Protocol: v1.ProtocolUDP, - }}, - }} - } - namedPortsLocalNoLocal := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - }, { - IP: "1.1.1.2", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p11", - Port: 11, - Protocol: v1.ProtocolUDP, - }, { - Name: "p12", - Port: 12, - Protocol: v1.ProtocolUDP, - }}, - }} - } - multipleSubsets := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - }}, - Ports: []v1.EndpointPort{{ - Name: "p11", - Port: 11, - Protocol: v1.ProtocolUDP, - }}, + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p11"), + Port: utilpointer.Int32(11), + Protocol: &udpProtocol, }, { - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.2", - }}, - Ports: []v1.EndpointPort{{ - Name: "p12", - Port: 12, - Protocol: v1.ProtocolUDP, - }}, + Name: utilpointer.String("p12"), + Port: utilpointer.Int32(12), + Protocol: &udpProtocol, }} } - multipleSubsetsWithLocal := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - }}, - Ports: []v1.EndpointPort{{ - Name: "p11", - Port: 11, - Protocol: v1.ProtocolUDP, - }}, + subset3 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.3"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p13"), + Port: utilpointer.Int32(13), + Protocol: &udpProtocol, + }} + } + multipleSubsetsMultiplePortsLocal := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, subsetMultiplePortsLocal), + makeTestEndpointSlice("ns1", "ep1", 2, subset3), + } + subsetMultipleIPsPorts1 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.1"}, }, { - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.2", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p12", - Port: 12, - Protocol: v1.ProtocolUDP, - }}, + Addresses: []string{"1.1.1.2"}, + NodeName: &nodeName, }} - } - multipleSubsetsMultiplePortsLocal := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p11", - Port: 11, - Protocol: v1.ProtocolUDP, - }, { - Name: "p12", - Port: 12, - Protocol: v1.ProtocolUDP, - }}, + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p11"), + Port: utilpointer.Int32(11), + Protocol: &udpProtocol, }, { - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.3", - }}, - Ports: []v1.EndpointPort{{ - Name: "p13", - Port: 13, - Protocol: v1.ProtocolUDP, - }}, + Name: utilpointer.String("p12"), + Port: utilpointer.Int32(12), + Protocol: &udpProtocol, }} } - multipleSubsetsIPsPorts1 := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - }, { - IP: "1.1.1.2", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p11", - Port: 11, - Protocol: v1.ProtocolUDP, - }, { - Name: "p12", - Port: 12, - Protocol: v1.ProtocolUDP, - }}, + subsetMultipleIPsPorts2 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.3"}, }, { - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.3", - }, { - IP: "1.1.1.4", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p13", - Port: 13, - Protocol: v1.ProtocolUDP, - }, { - Name: "p14", - Port: 14, - Protocol: v1.ProtocolUDP, - }}, + Addresses: []string{"1.1.1.4"}, + NodeName: &nodeName, }} - } - multipleSubsetsIPsPorts2 := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "2.2.2.1", - }, { - IP: "2.2.2.2", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p21", - Port: 21, - Protocol: v1.ProtocolUDP, - }, { - Name: "p22", - Port: 22, - Protocol: v1.ProtocolUDP, - }}, - }} - } - complexBefore1 := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - }}, - Ports: []v1.EndpointPort{{ - Name: "p11", - Port: 11, - Protocol: v1.ProtocolUDP, - }}, - }} - } - complexBefore2 := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "2.2.2.2", - NodeName: &nodeName, - }, { - IP: "2.2.2.22", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p22", - Port: 22, - Protocol: v1.ProtocolUDP, - }}, + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p13"), + Port: utilpointer.Int32(13), + Protocol: &udpProtocol, }, { - Addresses: []v1.EndpointAddress{{ - IP: "2.2.2.3", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p23", - Port: 23, - Protocol: v1.ProtocolUDP, - }}, + Name: utilpointer.String("p14"), + Port: utilpointer.Int32(14), + Protocol: &udpProtocol, }} } - complexBefore4 := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "4.4.4.4", - NodeName: &nodeName, - }, { - IP: "4.4.4.5", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p44", - Port: 44, - Protocol: v1.ProtocolUDP, - }}, + subsetMultipleIPsPorts3 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"2.2.2.1"}, }, { - Addresses: []v1.EndpointAddress{{ - IP: "4.4.4.6", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p45", - Port: 45, - Protocol: v1.ProtocolUDP, - }}, + Addresses: []string{"2.2.2.2"}, + NodeName: &nodeName, }} - } - complexAfter1 := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - }, { - IP: "1.1.1.11", - }}, - Ports: []v1.EndpointPort{{ - Name: "p11", - Port: 11, - Protocol: v1.ProtocolUDP, - }}, + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p21"), + Port: utilpointer.Int32(21), + Protocol: &udpProtocol, }, { - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.2", - }}, - Ports: []v1.EndpointPort{{ - Name: "p12", - Port: 12, - Protocol: v1.ProtocolUDP, - }, { - Name: "p122", - Port: 122, - Protocol: v1.ProtocolUDP, - }}, + Name: utilpointer.String("p22"), + Port: utilpointer.Int32(22), + Protocol: &udpProtocol, }} } - complexAfter3 := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "3.3.3.3", - }}, - Ports: []v1.EndpointPort{{ - Name: "p33", - Port: 33, - Protocol: v1.ProtocolUDP, - }}, + multipleSubsetsIPsPorts := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, subsetMultipleIPsPorts1), + makeTestEndpointSlice("ns1", "ep1", 2, subsetMultipleIPsPorts2), + makeTestEndpointSlice("ns2", "ep2", 1, subsetMultipleIPsPorts3), + } + complexSubset1 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"2.2.2.2"}, + NodeName: &nodeName, + }, { + Addresses: []string{"2.2.2.22"}, + NodeName: &nodeName, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p22"), + Port: utilpointer.Int32(22), + Protocol: &udpProtocol, }} } - complexAfter4 := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "4.4.4.4", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p44", - Port: 44, - Protocol: v1.ProtocolUDP, - }}, + complexSubset2 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"2.2.2.3"}, + NodeName: &nodeName, }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p23"), + Port: utilpointer.Int32(23), + Protocol: &udpProtocol, + }} + } + complexSubset3 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"4.4.4.4"}, + NodeName: &nodeName, + }, { + Addresses: []string{"4.4.4.5"}, + NodeName: &nodeName, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p44"), + Port: utilpointer.Int32(44), + Protocol: &udpProtocol, + }} + } + complexSubset4 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"4.4.4.6"}, + NodeName: &nodeName, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p45"), + Port: utilpointer.Int32(45), + Protocol: &udpProtocol, + }} + } + complexSubset5 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.1"}, + }, { + Addresses: []string{"1.1.1.11"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p11"), + Port: utilpointer.Int32(11), + Protocol: &udpProtocol, + }} + } + complexSubset6 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.2"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p12"), + Port: utilpointer.Int32(12), + Protocol: &udpProtocol, + }, { + Name: utilpointer.String("p122"), + Port: utilpointer.Int32(122), + Protocol: &udpProtocol, + }} + } + complexSubset7 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"3.3.3.3"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p33"), + Port: utilpointer.Int32(33), + Protocol: &udpProtocol, + }} + } + complexSubset8 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"4.4.4.4"}, + NodeName: &nodeName, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p44"), + Port: utilpointer.Int32(44), + Protocol: &udpProtocol, + }} + } + complexBefore := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, subset1), + nil, + makeTestEndpointSlice("ns2", "ep2", 1, complexSubset1), + makeTestEndpointSlice("ns2", "ep2", 2, complexSubset2), + nil, + makeTestEndpointSlice("ns4", "ep4", 1, complexSubset3), + makeTestEndpointSlice("ns4", "ep4", 2, complexSubset4), + } + complexAfter := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, complexSubset5), + makeTestEndpointSlice("ns1", "ep1", 2, complexSubset6), + nil, + nil, + makeTestEndpointSlice("ns3", "ep3", 1, complexSubset7), + makeTestEndpointSlice("ns4", "ep4", 1, complexSubset8), + nil, } testCases := []struct { // previousEndpoints and currentEndpoints are used to call appropriate // handlers OnEndpoints* (based on whether corresponding values are nil // or non-nil) and must be of equal length. - previousEndpoints []*v1.Endpoints - currentEndpoints []*v1.Endpoints + previousEndpoints []*discovery.EndpointSlice + currentEndpoints []*discovery.EndpointSlice oldEndpoints map[proxy.ServicePortName][]*endpointsInfo expectedResult map[proxy.ServicePortName][]*endpointsInfo expectedStaleEndpoints []proxy.ServiceEndpoint @@ -2393,34 +2369,9 @@ func Test_updateEndpointsMap(t *testing.T) { expectedStaleServiceNames: map[proxy.ServicePortName]bool{}, expectedHealthchecks: map[types.NamespacedName]int{}, }, { - // Case[1]: no change, unnamed port - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", unnamedPort), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", unnamedPort), - }, - oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ - makeServicePortName("ns1", "ep1", "", v1.ProtocolUDP): { - {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}}, - }, - }, - expectedResult: map[proxy.ServicePortName][]*endpointsInfo{ - makeServicePortName("ns1", "ep1", "", v1.ProtocolUDP): { - {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}}, - }, - }, - expectedStaleEndpoints: []proxy.ServiceEndpoint{}, - expectedStaleServiceNames: map[proxy.ServicePortName]bool{}, - expectedHealthchecks: map[types.NamespacedName]int{}, - }, { - // Case[2]: no change, named port, local - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPortLocal), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPortLocal), - }, + // Case[1]: no change, named port, local + previousEndpoints: namedPortLocal, + currentEndpoints: namedPortLocal, oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "1.1.1.1:11", IsLocal: true, Ready: true, Serving: true, Terminating: false}}, @@ -2437,13 +2388,9 @@ func Test_updateEndpointsMap(t *testing.T) { makeNSN("ns1", "ep1"): 1, }, }, { - // Case[3]: no change, multiple subsets - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", multipleSubsets), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", multipleSubsets), - }, + // Case[2]: no change, multiple subsets + previousEndpoints: multipleSubsets, + currentEndpoints: multipleSubsets, oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}}, @@ -2464,13 +2411,9 @@ func Test_updateEndpointsMap(t *testing.T) { expectedStaleServiceNames: map[proxy.ServicePortName]bool{}, expectedHealthchecks: map[types.NamespacedName]int{}, }, { - // Case[4]: no change, multiple subsets, multiple ports, local - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", multipleSubsetsMultiplePortsLocal), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", multipleSubsetsMultiplePortsLocal), - }, + // Case[3]: no change, multiple subsets, multiple ports, local + previousEndpoints: multipleSubsetsMultiplePortsLocal, + currentEndpoints: multipleSubsetsMultiplePortsLocal, oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "1.1.1.1:11", IsLocal: true, Ready: true, Serving: true, Terminating: false}}, @@ -2499,15 +2442,9 @@ func Test_updateEndpointsMap(t *testing.T) { makeNSN("ns1", "ep1"): 1, }, }, { - // Case[5]: no change, multiple endpoints, subsets, IPs, and ports - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", multipleSubsetsIPsPorts1), - makeTestEndpoints("ns2", "ep2", multipleSubsetsIPsPorts2), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", multipleSubsetsIPsPorts1), - makeTestEndpoints("ns2", "ep2", multipleSubsetsIPsPorts2), - }, + // Case[4]: no change, multiple endpoints, subsets, IPs, and ports + previousEndpoints: multipleSubsetsIPsPorts, + currentEndpoints: multipleSubsetsIPsPorts, oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}}, @@ -2567,54 +2504,42 @@ func Test_updateEndpointsMap(t *testing.T) { makeNSN("ns2", "ep2"): 1, }, }, { - // Case[6]: add an Endpoints - previousEndpoints: []*v1.Endpoints{ - nil, - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", unnamedPortLocal), - }, - oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{}, + // Case[5]: add an Endpoints + previousEndpoints: []*discovery.EndpointSlice{nil}, + currentEndpoints: namedPortLocal, + oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{}, expectedResult: map[proxy.ServicePortName][]*endpointsInfo{ - makeServicePortName("ns1", "ep1", "", v1.ProtocolUDP): { + makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "1.1.1.1:11", IsLocal: true, Ready: true, Serving: true, Terminating: false}}, }, }, expectedStaleEndpoints: []proxy.ServiceEndpoint{}, expectedStaleServiceNames: map[proxy.ServicePortName]bool{ - makeServicePortName("ns1", "ep1", "", v1.ProtocolUDP): true, + makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): true, }, expectedHealthchecks: map[types.NamespacedName]int{ makeNSN("ns1", "ep1"): 1, }, }, { - // Case[7]: remove an Endpoints - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", unnamedPortLocal), - }, - currentEndpoints: []*v1.Endpoints{ - nil, - }, + // Case[6]: remove an Endpoints + previousEndpoints: namedPortLocal, + currentEndpoints: []*discovery.EndpointSlice{nil}, oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ - makeServicePortName("ns1", "ep1", "", v1.ProtocolUDP): { + makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "1.1.1.1:11", IsLocal: true, Ready: true, Serving: true, Terminating: false}}, }, }, expectedResult: map[proxy.ServicePortName][]*endpointsInfo{}, expectedStaleEndpoints: []proxy.ServiceEndpoint{{ Endpoint: "1.1.1.1:11", - ServicePortName: makeServicePortName("ns1", "ep1", "", v1.ProtocolUDP), + ServicePortName: makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP), }}, expectedStaleServiceNames: map[proxy.ServicePortName]bool{}, expectedHealthchecks: map[types.NamespacedName]int{}, }, { - // Case[8]: add an IP and port - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPort), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPortsLocalNoLocal), - }, + // Case[7]: add an IP and port + previousEndpoints: namedPort, + currentEndpoints: namedPortsLocalNoLocal, oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}}, @@ -2638,13 +2563,9 @@ func Test_updateEndpointsMap(t *testing.T) { makeNSN("ns1", "ep1"): 1, }, }, { - // Case[9]: remove an IP and port - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPortsLocalNoLocal), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPort), - }, + // Case[8]: remove an IP and port + previousEndpoints: namedPortsLocalNoLocal, + currentEndpoints: namedPort, oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}}, @@ -2673,13 +2594,9 @@ func Test_updateEndpointsMap(t *testing.T) { expectedStaleServiceNames: map[proxy.ServicePortName]bool{}, expectedHealthchecks: map[types.NamespacedName]int{}, }, { - // Case[10]: add a subset - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPort), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", multipleSubsetsWithLocal), - }, + // Case[9]: add a subset + previousEndpoints: []*discovery.EndpointSlice{namedPort[0], nil}, + currentEndpoints: multipleSubsetsWithLocal, oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}}, @@ -2701,13 +2618,9 @@ func Test_updateEndpointsMap(t *testing.T) { makeNSN("ns1", "ep1"): 1, }, }, { - // Case[11]: remove a subset - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", multipleSubsets), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPort), - }, + // Case[10]: remove a subset + previousEndpoints: multipleSubsets, + currentEndpoints: []*discovery.EndpointSlice{namedPort[0], nil}, oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}}, @@ -2728,13 +2641,9 @@ func Test_updateEndpointsMap(t *testing.T) { expectedStaleServiceNames: map[proxy.ServicePortName]bool{}, expectedHealthchecks: map[types.NamespacedName]int{}, }, { - // Case[12]: rename a port - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPort), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPortRenamed), - }, + // Case[11]: rename a port + previousEndpoints: namedPort, + currentEndpoints: namedPortRenamed, oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}}, @@ -2754,13 +2663,9 @@ func Test_updateEndpointsMap(t *testing.T) { }, expectedHealthchecks: map[types.NamespacedName]int{}, }, { - // Case[13]: renumber a port - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPort), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPortRenumbered), - }, + // Case[12]: renumber a port + previousEndpoints: namedPort, + currentEndpoints: namedPortRenumbered, oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}}, @@ -2778,26 +2683,16 @@ func Test_updateEndpointsMap(t *testing.T) { expectedStaleServiceNames: map[proxy.ServicePortName]bool{}, expectedHealthchecks: map[types.NamespacedName]int{}, }, { - // Case[14]: complex add and remove - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", complexBefore1), - makeTestEndpoints("ns2", "ep2", complexBefore2), - nil, - makeTestEndpoints("ns4", "ep4", complexBefore4), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", complexAfter1), - nil, - makeTestEndpoints("ns3", "ep3", complexAfter3), - makeTestEndpoints("ns4", "ep4", complexAfter4), - }, + // Case[13]: complex add and remove + previousEndpoints: complexBefore, + currentEndpoints: complexAfter, oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}}, }, makeServicePortName("ns2", "ep2", "p22", v1.ProtocolUDP): { - {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "2.2.2.2:22", IsLocal: true, Ready: true, Serving: true, Terminating: false}}, {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "2.2.2.22:22", IsLocal: true, Ready: true, Serving: true, Terminating: false}}, + {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "2.2.2.2:22", IsLocal: true, Ready: true, Serving: true, Terminating: false}}, }, makeServicePortName("ns2", "ep2", "p23", v1.ProtocolUDP): { {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "2.2.2.3:23", IsLocal: true, Ready: true, Serving: true, Terminating: false}}, @@ -2812,8 +2707,8 @@ func Test_updateEndpointsMap(t *testing.T) { }, expectedResult: map[proxy.ServicePortName][]*endpointsInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { - {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}}, {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "1.1.1.11:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}}, + {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}}, }, makeServicePortName("ns1", "ep1", "p12", v1.ProtocolUDP): { {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "1.1.1.2:12", IsLocal: false, Ready: true, Serving: true, Terminating: false}}, @@ -2853,22 +2748,18 @@ func Test_updateEndpointsMap(t *testing.T) { makeNSN("ns4", "ep4"): 1, }, }, { - // Case[15]: change from 0 endpoint address to 1 unnamed port - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", emptyEndpoint), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", unnamedPort), - }, - oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{}, + // Case[14]: change from 0 endpoint address to 1 unnamed port + previousEndpoints: emptyEndpointSlices, + currentEndpoints: namedPort, + oldEndpoints: map[proxy.ServicePortName][]*endpointsInfo{}, expectedResult: map[proxy.ServicePortName][]*endpointsInfo{ - makeServicePortName("ns1", "ep1", "", v1.ProtocolUDP): { + makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { {BaseEndpointInfo: &proxy.BaseEndpointInfo{Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}}, }, }, expectedStaleEndpoints: []proxy.ServiceEndpoint{}, expectedStaleServiceNames: map[proxy.ServicePortName]bool{ - makeServicePortName("ns1", "ep1", "", v1.ProtocolUDP): true, + makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): true, }, expectedHealthchecks: map[types.NamespacedName]int{}, }, @@ -2876,14 +2767,14 @@ func Test_updateEndpointsMap(t *testing.T) { for tci, tc := range testCases { ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) fp.hostname = nodeName // First check that after adding all previous versions of endpoints, // the fp.oldEndpoints is as we expect. for i := range tc.previousEndpoints { if tc.previousEndpoints[i] != nil { - fp.OnEndpointsAdd(tc.previousEndpoints[i]) + fp.OnEndpointSliceAdd(tc.previousEndpoints[i]) } } fp.endpointsMap.Update(fp.endpointsChanges) @@ -2899,11 +2790,11 @@ func Test_updateEndpointsMap(t *testing.T) { prev, curr := tc.previousEndpoints[i], tc.currentEndpoints[i] switch { case prev == nil: - fp.OnEndpointsAdd(curr) + fp.OnEndpointSliceAdd(curr) case curr == nil: - fp.OnEndpointsDelete(prev) + fp.OnEndpointSliceDelete(prev) default: - fp.OnEndpointsUpdate(prev, curr) + fp.OnEndpointSliceUpdate(prev, curr) } } result := fp.endpointsMap.Update(fp.endpointsChanges) @@ -2990,7 +2881,7 @@ COMMIT ` ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, true) + fp := NewFakeProxier(ipt) fp.OnServiceSynced() fp.OnEndpointsSynced() fp.OnEndpointSlicesSynced() @@ -3099,7 +2990,7 @@ COMMIT ` ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, true) + fp := NewFakeProxier(ipt) fp.OnServiceSynced() fp.OnEndpointsSynced() fp.OnEndpointSlicesSynced() @@ -3163,7 +3054,7 @@ COMMIT // Test_HealthCheckNodePortWhenTerminating tests that health check node ports are not enabled when all local endpoints are terminating func Test_HealthCheckNodePortWhenTerminating(t *testing.T) { ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, true) + fp := NewFakeProxier(ipt) fp.OnServiceSynced() fp.OnEndpointsSynced() fp.OnEndpointSlicesSynced() @@ -3292,7 +3183,7 @@ func TestProxierDeleteNodePortStaleUDP(t *testing.T) { fexec.CommandScript = append(fexec.CommandScript, execFunc) ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) fp.exec = &fexec svcIP := "10.20.30.41" @@ -3315,7 +3206,6 @@ func TestProxierDeleteNodePortStaleUDP(t *testing.T) { }} }), ) - makeEndpointsMap(fp) fp.syncProxyRules() if fexec.CommandCalls != 0 { @@ -3323,17 +3213,17 @@ func TestProxierDeleteNodePortStaleUDP(t *testing.T) { } epIP := "10.180.0.1" - makeEndpointsMap(fp, - makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIP, - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName.Port, - Port: int32(svcPort), - Protocol: v1.ProtocolUDP, - }}, + udpProtocol := v1.ProtocolUDP + populateEndpointSlices(fp, + makeTestEndpointSlice(svcPortName.Namespace, svcPortName.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIP}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName.Port), + Port: utilpointer.Int32(int32(svcPort)), + Protocol: &udpProtocol, }} }), ) @@ -3359,7 +3249,8 @@ func TestProxierDeleteNodePortStaleUDP(t *testing.T) { func TestProxierMetricsIptablesTotalRules(t *testing.T) { ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, false) + fp := NewFakeProxier(ipt) + tcpProtocol := v1.ProtocolTCP metrics.RegisterMetrics() @@ -3383,8 +3274,6 @@ func TestProxierMetricsIptablesTotalRules(t *testing.T) { }} }), ) - makeEndpointsMap(fp) - fp.syncProxyRules() nFilterRules, err := testutil.GetGaugeMetricValue(metrics.IptablesRulesTotal.WithLabelValues(string(utiliptables.TableFilter))) @@ -3418,22 +3307,18 @@ func TestProxierMetricsIptablesTotalRules(t *testing.T) { t.Fatalf("Wrong number of nat rules: expected 6 received %f", nNatRules) } - makeEndpointsMap(fp, - makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{ - { - IP: "10.0.0.2", - }, - { - IP: "10.0.0.5", - }, - }, - Ports: []v1.EndpointPort{{ - Name: svcPortName.Port, - Port: int32(svcPort), - Protocol: v1.ProtocolTCP, - }}, + populateEndpointSlices(fp, + makeTestEndpointSlice(svcPortName.Namespace, svcPortName.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"10.0.0.2"}, + }, { + Addresses: []string{"10.0.0.5"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName.Port), + Port: utilpointer.Int32(int32(svcPort)), + Protocol: &tcpProtocol, }} }), ) @@ -3639,7 +3524,7 @@ COMMIT for _, tc := range testCases { defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ServiceInternalTrafficPolicy, tc.featureGateOn)() ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, true) + fp := NewFakeProxier(ipt) fp.OnServiceSynced() fp.OnEndpointsSynced() fp.OnEndpointSlicesSynced() @@ -4227,7 +4112,7 @@ COMMIT defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ProxyTerminatingEndpoints, testcase.terminatingFeatureGate)() ipt := iptablestest.NewFake() - fp := NewFakeProxier(ipt, true) + fp := NewFakeProxier(ipt) fp.OnServiceSynced() fp.OnEndpointsSynced() fp.OnEndpointSlicesSynced() diff --git a/pkg/proxy/ipvs/proxier.go b/pkg/proxy/ipvs/proxier.go index f135d187d1d..315f0060f1f 100644 --- a/pkg/proxy/ipvs/proxier.go +++ b/pkg/proxy/ipvs/proxier.go @@ -221,10 +221,9 @@ type Proxier struct { endpointsMap proxy.EndpointsMap portsMap map[utilnet.LocalPort]utilnet.Closeable nodeLabels map[string]string - // endpointsSynced, endpointSlicesSynced, and servicesSynced are set to true when + // endpointSlicesSynced, and servicesSynced are set to true when // corresponding objects are synced after startup. This is used to avoid updating // ipvs rules with some partial data after kube-proxy restart. - endpointsSynced bool endpointSlicesSynced bool servicesSynced bool initialized int32 @@ -438,8 +437,6 @@ func NewProxier(ipt utiliptables.Interface, serviceHealthServer := healthcheck.NewServiceHealthServer(hostname, recorder) - endpointSlicesEnabled := utilfeature.DefaultFeatureGate.Enabled(features.EndpointSliceProxying) - ipFamilyMap := utilproxy.MapCIDRsByIPFamily(nodePortAddresses) nodePortAddresses = ipFamilyMap[ipFamily] // Log the IPs not matching the ipFamily @@ -456,7 +453,7 @@ func NewProxier(ipt utiliptables.Interface, serviceMap: make(proxy.ServiceMap), serviceChanges: proxy.NewServiceChangeTracker(newServiceInfo, ipFamily, recorder, nil), endpointsMap: make(proxy.EndpointsMap), - endpointsChanges: proxy.NewEndpointChangeTracker(hostname, nil, ipFamily, recorder, endpointSlicesEnabled, nil), + endpointsChanges: proxy.NewEndpointChangeTracker(hostname, nil, ipFamily, recorder, nil), syncPeriod: syncPeriod, minSyncPeriod: minSyncPeriod, excludeCIDRs: parsedExcludeCIDRs, @@ -881,44 +878,27 @@ func (proxier *Proxier) OnServiceDelete(service *v1.Service) { func (proxier *Proxier) OnServiceSynced() { proxier.mu.Lock() proxier.servicesSynced = true - if utilfeature.DefaultFeatureGate.Enabled(features.EndpointSliceProxying) { - proxier.setInitialized(proxier.endpointSlicesSynced) - } else { - proxier.setInitialized(proxier.endpointsSynced) - } + proxier.setInitialized(proxier.endpointSlicesSynced) proxier.mu.Unlock() // Sync unconditionally - this is called once per lifetime. proxier.syncProxyRules() } +// The following methods exist to implement the Proxier interface however +// ipvs proxier only uses EndpointSlices so the following are noops + // OnEndpointsAdd is called whenever creation of new endpoints object is observed. -func (proxier *Proxier) OnEndpointsAdd(endpoints *v1.Endpoints) { - proxier.OnEndpointsUpdate(nil, endpoints) -} +func (proxier *Proxier) OnEndpointsAdd(endpoints *v1.Endpoints) {} // OnEndpointsUpdate is called whenever modification of an existing endpoints object is observed. -func (proxier *Proxier) OnEndpointsUpdate(oldEndpoints, endpoints *v1.Endpoints) { - if proxier.endpointsChanges.Update(oldEndpoints, endpoints) && proxier.isInitialized() { - proxier.Sync() - } -} +func (proxier *Proxier) OnEndpointsUpdate(oldEndpoints, endpoints *v1.Endpoints) {} // OnEndpointsDelete is called whenever deletion of an existing endpoints object is observed. -func (proxier *Proxier) OnEndpointsDelete(endpoints *v1.Endpoints) { - proxier.OnEndpointsUpdate(endpoints, nil) -} +func (proxier *Proxier) OnEndpointsDelete(endpoints *v1.Endpoints) {} // OnEndpointsSynced is called once all the initial event handlers were called and the state is fully propagated to local cache. -func (proxier *Proxier) OnEndpointsSynced() { - proxier.mu.Lock() - proxier.endpointsSynced = true - proxier.setInitialized(proxier.servicesSynced) - proxier.mu.Unlock() - - // Sync unconditionally - this is called once per lifetime. - proxier.syncProxyRules() -} +func (proxier *Proxier) OnEndpointsSynced() {} // OnEndpointSliceAdd is called whenever creation of a new endpoint slice object // is observed. diff --git a/pkg/proxy/ipvs/proxier_test.go b/pkg/proxy/ipvs/proxier_test.go index 36b80b6d7ab..e8796654054 100644 --- a/pkg/proxy/ipvs/proxier_test.go +++ b/pkg/proxy/ipvs/proxier_test.go @@ -106,7 +106,7 @@ func (fake *fakeIPSetVersioner) GetVersion() (string, error) { return fake.version, fake.err } -func NewFakeProxier(ipt utiliptables.Interface, ipvs utilipvs.Interface, ipset utilipset.Interface, nodeIPs []net.IP, excludeCIDRs []*net.IPNet, endpointSlicesEnabled bool, ipFamily v1.IPFamily) *Proxier { +func NewFakeProxier(ipt utiliptables.Interface, ipvs utilipvs.Interface, ipset utilipset.Interface, nodeIPs []net.IP, excludeCIDRs []*net.IPNet, ipFamily v1.IPFamily) *Proxier { // unlike actual proxier, this fake proxier does not filter node IPs per family requested // which can lead to false postives. @@ -145,7 +145,7 @@ func NewFakeProxier(ipt utiliptables.Interface, ipvs utilipvs.Interface, ipset u serviceMap: make(proxy.ServiceMap), serviceChanges: proxy.NewServiceChangeTracker(newServiceInfo, ipFamily, nil, nil), endpointsMap: make(proxy.EndpointsMap), - endpointsChanges: proxy.NewEndpointChangeTracker(testHostname, nil, ipFamily, nil, endpointSlicesEnabled, nil), + endpointsChanges: proxy.NewEndpointChangeTracker(testHostname, nil, ipFamily, nil, nil), excludeCIDRs: excludeCIDRs, iptables: ipt, ipvs: ipvs, @@ -204,32 +204,29 @@ func makeTestService(namespace, name string, svcFunc func(*v1.Service)) *v1.Serv return svc } -func makeEndpointsMap(proxier *Proxier, allEndpoints ...*v1.Endpoints) { - for i := range allEndpoints { - proxier.OnEndpointsAdd(allEndpoints[i]) +func populateEndpointSlices(proxier *Proxier, allEndpointSlices ...*discovery.EndpointSlice) { + for i := range allEndpointSlices { + proxier.OnEndpointSliceAdd(allEndpointSlices[i]) } - - proxier.mu.Lock() - defer proxier.mu.Unlock() - proxier.endpointsSynced = true } -func makeTestEndpoints(namespace, name string, eptFunc func(*v1.Endpoints)) *v1.Endpoints { - ept := &v1.Endpoints{ +func makeTestEndpointSlice(namespace, name string, sliceNum int, epsFunc func(*discovery.EndpointSlice)) *discovery.EndpointSlice { + eps := &discovery.EndpointSlice{ ObjectMeta: metav1.ObjectMeta{ - Name: name, + Name: fmt.Sprintf("%s-%d", name, sliceNum), Namespace: namespace, + Labels: map[string]string{discovery.LabelServiceName: name}, }, } - eptFunc(ept) - return ept + epsFunc(eps) + return eps } func TestCleanupLeftovers(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, false, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) svcIP := "10.20.30.41" svcPort := 80 svcNodePort := 3001 @@ -251,16 +248,17 @@ func TestCleanupLeftovers(t *testing.T) { }), ) epIP := "10.180.0.1" - makeEndpointsMap(fp, - makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIP, - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName.Port, - Port: int32(svcPort), - }}, + tcpProtocol := v1.ProtocolTCP + populateEndpointSlices(fp, + makeTestEndpointSlice(svcPortName.Namespace, svcPortName.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIP}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName.Port), + Port: utilpointer.Int32(int32(svcPort)), + Protocol: &tcpProtocol, }} }), ) @@ -463,10 +461,13 @@ func TestGetNodeIPs(t *testing.T) { } func TestNodePortIPv4(t *testing.T) { + tcpProtocol := v1.ProtocolTCP + udpProtocol := v1.ProtocolUDP + sctpProtocol := v1.ProtocolSCTP tests := []struct { name string services []*v1.Service - endpoints []*v1.Endpoints + endpoints []*discovery.EndpointSlice nodeIPs []net.IP nodePortAddresses []string expectedIPVS *ipvstest.FakeIPVS @@ -487,19 +488,27 @@ func TestNodePortIPv4(t *testing.T) { }} }), }, - endpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "svc1", func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "10.180.0.1", - }, { - IP: "1002:ab8::2:10", - }}, - Ports: []v1.EndpointPort{{ - Name: "p80", - Port: int32(80), - Protocol: v1.ProtocolTCP, - }}, + endpoints: []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "svc1", 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"10.180.0.1"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p80"), + Port: utilpointer.Int32(80), + Protocol: &tcpProtocol, + }} + }), + makeTestEndpointSlice("ns1", "svc1", 2, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv6 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1002:ab8::2:10"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p80"), + Port: utilpointer.Int32(80), + Protocol: &tcpProtocol, }} }), }, @@ -571,17 +580,16 @@ func TestNodePortIPv4(t *testing.T) { }} }), }, - endpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "svc1", func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "10.180.0.1", - }}, - Ports: []v1.EndpointPort{{ - Name: "p80", - Port: int32(80), - Protocol: v1.ProtocolUDP, - }}, + endpoints: []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "svc1", 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"10.180.0.1"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p80"), + Port: utilpointer.Int32(80), + Protocol: &udpProtocol, }} }), }, @@ -667,7 +675,7 @@ func TestNodePortIPv4(t *testing.T) { }} }), }, - endpoints: []*v1.Endpoints{}, + endpoints: []*discovery.EndpointSlice{}, nodeIPs: []net.IP{ net.ParseIP("100.101.102.103"), }, @@ -723,17 +731,16 @@ func TestNodePortIPv4(t *testing.T) { }} }), }, - endpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "svc1", func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "10.180.0.1", - }}, - Ports: []v1.EndpointPort{{ - Name: "p80", - Port: int32(80), - Protocol: v1.ProtocolSCTP, - }}, + endpoints: []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "svc1", 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"10.180.0.1"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p80"), + Port: utilpointer.Int32(80), + Protocol: &sctpProtocol, }} }), }, @@ -866,11 +873,11 @@ func TestNodePortIPv4(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, test.nodeIPs, nil, false, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, test.nodeIPs, nil, v1.IPv4Protocol) fp.nodePortAddresses = test.nodePortAddresses makeServiceMap(fp, test.services...) - makeEndpointsMap(fp, test.endpoints...) + populateEndpointSlices(fp, test.endpoints...) fp.syncProxyRules() @@ -892,10 +899,13 @@ func TestNodePortIPv4(t *testing.T) { } func TestNodePortIPv6(t *testing.T) { + tcpProtocol := v1.ProtocolTCP + udpProtocol := v1.ProtocolUDP + sctpProtocol := v1.ProtocolSCTP tests := []struct { name string services []*v1.Service - endpoints []*v1.Endpoints + endpoints []*discovery.EndpointSlice nodeIPs []net.IP nodePortAddresses []string expectedIPVS *ipvstest.FakeIPVS @@ -916,19 +926,27 @@ func TestNodePortIPv6(t *testing.T) { }} }), }, - endpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "svc1", func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "10.180.0.1", - }, { - IP: "1002:ab8::2:10", - }}, - Ports: []v1.EndpointPort{{ - Name: "p80", - Port: int32(80), - Protocol: v1.ProtocolTCP, - }}, + endpoints: []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "svc1", 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"10.180.0.1"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr("p80"), + Port: utilpointer.Int32(80), + Protocol: &tcpProtocol, + }} + }), + makeTestEndpointSlice("ns1", "svc1", 2, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv6 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1002:ab8::2:10"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr("p80"), + Port: utilpointer.Int32(80), + Protocol: &tcpProtocol, }} }), }, @@ -1002,17 +1020,16 @@ func TestNodePortIPv6(t *testing.T) { }} }), }, - endpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "svc1", func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "10.180.0.1", - }}, - Ports: []v1.EndpointPort{{ - Name: "p80", - Port: int32(80), - Protocol: v1.ProtocolUDP, - }}, + endpoints: []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "svc1", 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv6 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"10.180.0.1"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr("p80"), + Port: utilpointer.Int32(80), + Protocol: &udpProtocol, }} }), }, @@ -1043,7 +1060,7 @@ func TestNodePortIPv6(t *testing.T) { }} }), }, - endpoints: []*v1.Endpoints{}, + endpoints: []*discovery.EndpointSlice{}, nodeIPs: []net.IP{ net.ParseIP("100.101.102.103"), net.ParseIP("2001:db8::1:1"), @@ -1101,17 +1118,16 @@ func TestNodePortIPv6(t *testing.T) { }} }), }, - endpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "svc1", func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "2001::1", - }}, - Ports: []v1.EndpointPort{{ - Name: "p80", - Port: int32(80), - Protocol: v1.ProtocolSCTP, - }}, + endpoints: []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "svc1", 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv6 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"2001::1"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p80"), + Port: utilpointer.Int32(80), + Protocol: &sctpProtocol, }} }), }, @@ -1213,11 +1229,11 @@ func TestNodePortIPv6(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, test.nodeIPs, nil, false, v1.IPv6Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, test.nodeIPs, nil, v1.IPv6Protocol) fp.nodePortAddresses = test.nodePortAddresses makeServiceMap(fp, test.services...) - makeEndpointsMap(fp, test.endpoints...) + populateEndpointSlices(fp, test.endpoints...) fp.syncProxyRules() @@ -1239,10 +1255,11 @@ func TestNodePortIPv6(t *testing.T) { } func TestIPv4Proxier(t *testing.T) { + tcpProtocol := v1.ProtocolTCP tests := []struct { name string services []*v1.Service - endpoints []*v1.Endpoints + endpoints []*discovery.EndpointSlice expectedIPVS *ipvstest.FakeIPVS }{ { @@ -1265,29 +1282,27 @@ func TestIPv4Proxier(t *testing.T) { }} }), }, - endpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "svc1", func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "10.180.0.1", - }}, - Ports: []v1.EndpointPort{{ - Name: "p80", - Port: int32(80), - Protocol: v1.ProtocolTCP, - }}, + endpoints: []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "svc1", 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"10.180.0.1"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr("p80"), + Port: utilpointer.Int32(80), + Protocol: &tcpProtocol, }} }), - makeTestEndpoints("ns2", "svc2", func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1009:ab8::5:6", - }}, - Ports: []v1.EndpointPort{{ - Name: "p8080", - Port: int32(8080), - Protocol: v1.ProtocolTCP, - }}, + makeTestEndpointSlice("ns2", "svc2", 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv6 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1009:ab8::5:6"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr("p8080"), + Port: utilpointer.Int32(8080), + Protocol: &tcpProtocol, }} }), }, @@ -1331,7 +1346,7 @@ func TestIPv4Proxier(t *testing.T) { }} }), }, - endpoints: []*v1.Endpoints{}, + endpoints: []*discovery.EndpointSlice{}, expectedIPVS: &ipvstest.FakeIPVS{ Services: map[ipvstest.ServiceKey]*utilipvs.VirtualServer{ { @@ -1361,10 +1376,10 @@ func TestIPv4Proxier(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, false, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) makeServiceMap(fp, test.services...) - makeEndpointsMap(fp, test.endpoints...) + populateEndpointSlices(fp, test.endpoints...) fp.syncProxyRules() @@ -1378,10 +1393,11 @@ func TestIPv4Proxier(t *testing.T) { } func TestIPv6Proxier(t *testing.T) { + tcpProtocol := v1.ProtocolTCP tests := []struct { name string services []*v1.Service - endpoints []*v1.Endpoints + endpoints []*discovery.EndpointSlice expectedIPVS *ipvstest.FakeIPVS }{ { @@ -1404,29 +1420,27 @@ func TestIPv6Proxier(t *testing.T) { }} }), }, - endpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "svc1", func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "10.180.0.1", - }}, - Ports: []v1.EndpointPort{{ - Name: "p80", - Port: int32(80), - Protocol: v1.ProtocolTCP, - }}, + endpoints: []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "svc1", 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"10.180.0.1"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr("p80"), + Port: utilpointer.Int32(80), + Protocol: &tcpProtocol, }} }), - makeTestEndpoints("ns2", "svc2", func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1009:ab8::5:6", - }}, - Ports: []v1.EndpointPort{{ - Name: "p8080", - Port: int32(8080), - Protocol: v1.ProtocolTCP, - }}, + makeTestEndpointSlice("ns2", "svc2", 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv6 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1009:ab8::5:6"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr("p8080"), + Port: utilpointer.Int32(8080), + Protocol: &tcpProtocol, }} }), }, @@ -1470,7 +1484,7 @@ func TestIPv6Proxier(t *testing.T) { }} }), }, - endpoints: []*v1.Endpoints{}, + endpoints: []*discovery.EndpointSlice{}, expectedIPVS: &ipvstest.FakeIPVS{ Services: map[ipvstest.ServiceKey]*utilipvs.VirtualServer{ { @@ -1500,10 +1514,10 @@ func TestIPv6Proxier(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, false, v1.IPv6Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv6Protocol) makeServiceMap(fp, test.services...) - makeEndpointsMap(fp, test.endpoints...) + populateEndpointSlices(fp, test.endpoints...) fp.syncProxyRules() @@ -1521,9 +1535,8 @@ func TestMasqueradeRule(t *testing.T) { ipt := iptablestest.NewFake().SetHasRandomFully(testcase) ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, false, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) makeServiceMap(fp) - makeEndpointsMap(fp) fp.syncProxyRules() postRoutingRules := ipt.GetRules(string(kubePostroutingChain)) @@ -1541,7 +1554,7 @@ func TestExternalIPsNoEndpoint(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, false, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) svcIP := "10.20.30.41" svcPort := 80 svcExternalIPs := "50.60.70.81" @@ -1563,9 +1576,6 @@ func TestExternalIPsNoEndpoint(t *testing.T) { }} }), ) - - makeEndpointsMap(fp) - fp.syncProxyRules() // check ipvs service and destinations @@ -1596,7 +1606,7 @@ func TestExternalIPs(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, false, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) svcIP := "10.20.30.41" svcPort := 80 svcExternalIPs := sets.NewString("50.60.70.81", "2012::51", "127.0.0.1") @@ -1620,16 +1630,17 @@ func TestExternalIPs(t *testing.T) { ) epIP := "10.180.0.1" - makeEndpointsMap(fp, - makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIP, - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName.Port, - Port: int32(svcPort), - }}, + udpProtocol := v1.ProtocolUDP + populateEndpointSlices(fp, + makeTestEndpointSlice(svcPortName.Namespace, svcPortName.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIP}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName.Port), + Port: utilpointer.Int32(int32(svcPort)), + Protocol: &udpProtocol, }} }), ) @@ -1666,7 +1677,7 @@ func TestOnlyLocalExternalIPs(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, false, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) svcIP := "10.20.30.41" svcPort := 80 svcExternalIPs := sets.NewString("50.60.70.81", "2012::51", "127.0.0.1") @@ -1693,23 +1704,22 @@ func TestOnlyLocalExternalIPs(t *testing.T) { epIP1 := "10.180.1.1" thisHostname := testHostname otherHostname := "other-hostname" - makeEndpointsMap(fp, - makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIP, - NodeName: utilpointer.StringPtr(thisHostname), - }, - { - IP: epIP1, - NodeName: utilpointer.StringPtr(otherHostname), - }, - }, - Ports: []v1.EndpointPort{{ - Name: svcPortName.Port, - Port: int32(svcPort), - Protocol: v1.ProtocolTCP, - }}, + tcpProtocol := v1.ProtocolTCP + populateEndpointSlices(fp, + makeTestEndpointSlice(svcPortName.Namespace, svcPortName.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIP}, + NodeName: utilpointer.StringPtr(thisHostname), + }, + { + Addresses: []string{epIP1}, + NodeName: utilpointer.StringPtr(otherHostname), + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName.Port), + Port: utilpointer.Int32(int32(svcPort)), + Protocol: &tcpProtocol, }} }), ) @@ -1773,16 +1783,17 @@ func TestLoadBalancer(t *testing.T) { ) epIP := "10.180.0.1" - makeEndpointsMap(fp, - makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIP, - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName.Port, - Port: int32(svcPort), - }}, + udpProtocol := v1.ProtocolUDP + populateEndpointSlices(fp, + makeTestEndpointSlice(svcPortName.Namespace, svcPortName.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIP}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName.Port), + Port: utilpointer.Int32(int32(svcPort)), + Protocol: &udpProtocol, }} }), ) @@ -1850,31 +1861,23 @@ func TestOnlyLocalNodePorts(t *testing.T) { epIP1 := "10.180.1.1" thisHostname := testHostname otherHostname := "other-hostname" + tcpProtocol := v1.ProtocolTCP - makeEndpointsMap(fp, - makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{ - { // **local** endpoint address, should be added as RS - Addresses: []v1.EndpointAddress{{ - IP: epIP, - NodeName: &thisHostname, - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName.Port, - Port: int32(svcPort), - Protocol: v1.ProtocolTCP, - }}}, - { // **remote** endpoint address, should not be added as RS - Addresses: []v1.EndpointAddress{{ - IP: epIP1, - NodeName: &otherHostname, - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName.Port, - Port: int32(svcPort), - Protocol: v1.ProtocolTCP, - }}, - }} + populateEndpointSlices(fp, + makeTestEndpointSlice(svcPortName.Namespace, svcPortName.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIP}, + NodeName: &thisHostname, + }, { + Addresses: []string{epIP1}, + NodeName: &otherHostname, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName.Port), + Port: utilpointer.Int32(int32(svcPort)), + Protocol: &tcpProtocol, + }} }), ) @@ -2003,6 +2006,7 @@ func TestLoadBalanceSourceRanges(t *testing.T) { Port: "p80", } epIP := "10.180.0.1" + tcpProtocol := v1.ProtocolTCP makeServiceMap(fp, makeTestService(svcPortName.Namespace, svcPortName.Name, func(svc *v1.Service) { @@ -2021,18 +2025,16 @@ func TestLoadBalanceSourceRanges(t *testing.T) { } }), ) - makeEndpointsMap(fp, - makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIP, - NodeName: nil, - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName.Port, - Port: int32(svcPort), - Protocol: v1.ProtocolTCP, - }}, + populateEndpointSlices(fp, + makeTestEndpointSlice(svcPortName.Namespace, svcPortName.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIP}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName.Port), + Port: utilpointer.Int32(int32(svcPort)), + Protocol: &tcpProtocol, }} }), ) @@ -2128,16 +2130,16 @@ func TestAcceptIPVSTraffic(t *testing.T) { }), ) - makeEndpointsMap(fp, - makeTestEndpoints("ns1", "p80", func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: svcInfo.epIP, - }}, - Ports: []v1.EndpointPort{{ - Name: "p80", - Port: 80, - }}, + udpProtocol := v1.ProtocolUDP + populateEndpointSlices(fp, + makeTestEndpointSlice("ns1", "p80", 1, func(eps *discovery.EndpointSlice) { + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{svcInfo.epIP}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr("p80"), + Port: utilpointer.Int32(80), + Protocol: &udpProtocol, }} }), ) @@ -2188,31 +2190,25 @@ func TestOnlyLocalLoadBalancing(t *testing.T) { epIP1 := "10.180.1.1" thisHostname := testHostname otherHostname := "other-hostname" + tcpProtocol := v1.ProtocolTCP - makeEndpointsMap(fp, - makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{ + populateEndpointSlices(fp, + makeTestEndpointSlice(svcPortName.Namespace, svcPortName.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{ { // **local** endpoint address, should be added as RS - Addresses: []v1.EndpointAddress{{ - IP: epIP, - NodeName: &thisHostname, - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName.Port, - Port: int32(svcPort), - Protocol: v1.ProtocolTCP, - }}}, + Addresses: []string{epIP}, + NodeName: &thisHostname, + }, { // **remote** endpoint address, should not be added as RS - Addresses: []v1.EndpointAddress{{ - IP: epIP1, - NodeName: &otherHostname, - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName.Port, - Port: int32(svcPort), - Protocol: v1.ProtocolTCP, - }}, + Addresses: []string{epIP1}, + NodeName: &otherHostname, }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName.Port), + Port: utilpointer.Int32(int32(svcPort)), + Protocol: &tcpProtocol, + }} }), ) @@ -2272,7 +2268,7 @@ func TestBuildServiceMapAddRemove(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, false, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) services := []*v1.Service{ makeTestService("somewhere-else", "cluster-ip", func(svc *v1.Service) { @@ -2382,7 +2378,7 @@ func TestBuildServiceMapServiceHeadless(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, false, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) makeServiceMap(fp, makeTestService("somewhere-else", "headless", func(svc *v1.Service) { @@ -2421,7 +2417,7 @@ func TestBuildServiceMapServiceTypeExternalName(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, false, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) makeServiceMap(fp, makeTestService("somewhere-else", "external-name", func(svc *v1.Service) { @@ -2449,7 +2445,7 @@ func TestBuildServiceMapServiceUpdate(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, false, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) servicev1 := makeTestService("somewhere", "some-service", func(svc *v1.Service) { svc.Spec.Type = v1.ServiceTypeClusterIP @@ -2533,7 +2529,7 @@ func TestSessionAffinity(t *testing.T) { ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) nodeIP := net.ParseIP("100.101.102.103") - fp := NewFakeProxier(ipt, ipvs, ipset, []net.IP{nodeIP}, nil, false, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, []net.IP{nodeIP}, nil, v1.IPv4Protocol) svcIP := "10.20.30.41" svcPort := 80 svcNodePort := 3001 @@ -2563,8 +2559,6 @@ func TestSessionAffinity(t *testing.T) { }} }), ) - makeEndpointsMap(fp) - fp.syncProxyRules() // check ipvs service and destinations @@ -2589,347 +2583,340 @@ func makeServicePortName(ns, name, port string, protocol v1.Protocol) proxy.Serv func Test_updateEndpointsMap(t *testing.T) { var nodeName = testHostname + udpProtocol := v1.ProtocolUDP - emptyEndpoint := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{} + emptyEndpointSlices := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, func(*discovery.EndpointSlice) {}), } - unnamedPort := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - }}, - Ports: []v1.EndpointPort{{ - Port: 11, - Protocol: v1.ProtocolUDP, - }}, + subset1 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.1"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p11"), + Port: utilpointer.Int32(11), + Protocol: &udpProtocol, }} } - unnamedPortLocal := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Port: 11, - Protocol: v1.ProtocolUDP, - }}, + subset2 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.2"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p12"), + Port: utilpointer.Int32(12), + Protocol: &udpProtocol, }} } - namedPortLocal := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p11", - Port: 11, - Protocol: v1.ProtocolUDP, - }}, + namedPortLocal := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, + func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.1"}, + NodeName: &nodeName, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p11"), + Port: utilpointer.Int32(11), + Protocol: &udpProtocol, + }} + }), + } + namedPort := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, subset1), + } + namedPortRenamed := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, + func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.1"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p11-2"), + Port: utilpointer.Int32(11), + Protocol: &udpProtocol, + }} + }), + } + namedPortRenumbered := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, + func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.1"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p11"), + Port: utilpointer.Int32(22), + Protocol: &udpProtocol, + }} + }), + } + namedPortsLocalNoLocal := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, + func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.1"}, + }, { + Addresses: []string{"1.1.1.2"}, + NodeName: &nodeName, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p11"), + Port: utilpointer.Int32(11), + Protocol: &udpProtocol, + }, { + Name: utilpointer.String("p12"), + Port: utilpointer.Int32(12), + Protocol: &udpProtocol, + }} + }), + } + multipleSubsets := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, subset1), + makeTestEndpointSlice("ns1", "ep1", 2, subset2), + } + subsetLocal := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.2"}, + NodeName: &nodeName, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p12"), + Port: utilpointer.Int32(12), + Protocol: &udpProtocol, }} } - namedPort := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - }}, - Ports: []v1.EndpointPort{{ - Name: "p11", - Port: 11, - Protocol: v1.ProtocolUDP, - }}, - }} + multipleSubsetsWithLocal := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, subset1), + makeTestEndpointSlice("ns1", "ep1", 2, subsetLocal), } - namedPortRenamed := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - }}, - Ports: []v1.EndpointPort{{ - Name: "p11-2", - Port: 11, - Protocol: v1.ProtocolUDP, - }}, + subsetMultiplePortsLocal := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.1"}, + NodeName: &nodeName, }} - } - namedPortRenumbered := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - }}, - Ports: []v1.EndpointPort{{ - Name: "p11", - Port: 22, - Protocol: v1.ProtocolUDP, - }}, - }} - } - namedPortsLocalNoLocal := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - }, { - IP: "1.1.1.2", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p11", - Port: 11, - Protocol: v1.ProtocolUDP, - }, { - Name: "p12", - Port: 12, - Protocol: v1.ProtocolUDP, - }}, - }} - } - multipleSubsets := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - }}, - Ports: []v1.EndpointPort{{ - Name: "p11", - Port: 11, - Protocol: v1.ProtocolUDP, - }}, + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p11"), + Port: utilpointer.Int32(11), + Protocol: &udpProtocol, }, { - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.2", - }}, - Ports: []v1.EndpointPort{{ - Name: "p12", - Port: 12, - Protocol: v1.ProtocolUDP, - }}, + Name: utilpointer.String("p12"), + Port: utilpointer.Int32(12), + Protocol: &udpProtocol, }} } - multipleSubsetsWithLocal := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - }}, - Ports: []v1.EndpointPort{{ - Name: "p11", - Port: 11, - Protocol: v1.ProtocolUDP, - }}, + subset3 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.3"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p13"), + Port: utilpointer.Int32(13), + Protocol: &udpProtocol, + }} + } + multipleSubsetsMultiplePortsLocal := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, subsetMultiplePortsLocal), + makeTestEndpointSlice("ns1", "ep1", 2, subset3), + } + subsetMultipleIPsPorts1 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.1"}, }, { - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.2", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p12", - Port: 12, - Protocol: v1.ProtocolUDP, - }}, + Addresses: []string{"1.1.1.2"}, + NodeName: &nodeName, }} - } - multipleSubsetsMultiplePortsLocal := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p11", - Port: 11, - Protocol: v1.ProtocolUDP, - }, { - Name: "p12", - Port: 12, - Protocol: v1.ProtocolUDP, - }}, + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p11"), + Port: utilpointer.Int32(11), + Protocol: &udpProtocol, }, { - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.3", - }}, - Ports: []v1.EndpointPort{{ - Name: "p13", - Port: 13, - Protocol: v1.ProtocolUDP, - }}, + Name: utilpointer.String("p12"), + Port: utilpointer.Int32(12), + Protocol: &udpProtocol, }} } - multipleSubsetsIPsPorts1 := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - }, { - IP: "1.1.1.2", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p11", - Port: 11, - Protocol: v1.ProtocolUDP, - }, { - Name: "p12", - Port: 12, - Protocol: v1.ProtocolUDP, - }}, + subsetMultipleIPsPorts2 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.3"}, }, { - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.3", - }, { - IP: "1.1.1.4", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p13", - Port: 13, - Protocol: v1.ProtocolUDP, - }, { - Name: "p14", - Port: 14, - Protocol: v1.ProtocolUDP, - }}, + Addresses: []string{"1.1.1.4"}, + NodeName: &nodeName, }} - } - multipleSubsetsIPsPorts2 := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "2.2.2.1", - }, { - IP: "2.2.2.2", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p21", - Port: 21, - Protocol: v1.ProtocolUDP, - }, { - Name: "p22", - Port: 22, - Protocol: v1.ProtocolUDP, - }}, - }} - } - complexBefore1 := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - }}, - Ports: []v1.EndpointPort{{ - Name: "p11", - Port: 11, - Protocol: v1.ProtocolUDP, - }}, - }} - } - complexBefore2 := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "2.2.2.2", - NodeName: &nodeName, - }, { - IP: "2.2.2.22", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p22", - Port: 22, - Protocol: v1.ProtocolUDP, - }}, + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p13"), + Port: utilpointer.Int32(13), + Protocol: &udpProtocol, }, { - Addresses: []v1.EndpointAddress{{ - IP: "2.2.2.3", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p23", - Port: 23, - Protocol: v1.ProtocolUDP, - }}, + Name: utilpointer.String("p14"), + Port: utilpointer.Int32(14), + Protocol: &udpProtocol, }} } - complexBefore4 := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "4.4.4.4", - NodeName: &nodeName, - }, { - IP: "4.4.4.5", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p44", - Port: 44, - Protocol: v1.ProtocolUDP, - }}, + subsetMultipleIPsPorts3 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"2.2.2.1"}, }, { - Addresses: []v1.EndpointAddress{{ - IP: "4.4.4.6", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p45", - Port: 45, - Protocol: v1.ProtocolUDP, - }}, + Addresses: []string{"2.2.2.2"}, + NodeName: &nodeName, }} - } - complexAfter1 := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.1", - }, { - IP: "1.1.1.11", - }}, - Ports: []v1.EndpointPort{{ - Name: "p11", - Port: 11, - Protocol: v1.ProtocolUDP, - }}, + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p21"), + Port: utilpointer.Int32(21), + Protocol: &udpProtocol, }, { - Addresses: []v1.EndpointAddress{{ - IP: "1.1.1.2", - }}, - Ports: []v1.EndpointPort{{ - Name: "p12", - Port: 12, - Protocol: v1.ProtocolUDP, - }, { - Name: "p122", - Port: 122, - Protocol: v1.ProtocolUDP, - }}, + Name: utilpointer.String("p22"), + Port: utilpointer.Int32(22), + Protocol: &udpProtocol, }} } - complexAfter3 := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "3.3.3.3", - }}, - Ports: []v1.EndpointPort{{ - Name: "p33", - Port: 33, - Protocol: v1.ProtocolUDP, - }}, + multipleSubsetsIPsPorts := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, subsetMultipleIPsPorts1), + makeTestEndpointSlice("ns1", "ep1", 2, subsetMultipleIPsPorts2), + makeTestEndpointSlice("ns2", "ep2", 1, subsetMultipleIPsPorts3), + } + complexSubset1 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"2.2.2.2"}, + NodeName: &nodeName, + }, { + Addresses: []string{"2.2.2.22"}, + NodeName: &nodeName, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p22"), + Port: utilpointer.Int32(22), + Protocol: &udpProtocol, }} } - complexAfter4 := func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: "4.4.4.4", - NodeName: &nodeName, - }}, - Ports: []v1.EndpointPort{{ - Name: "p44", - Port: 44, - Protocol: v1.ProtocolUDP, - }}, + complexSubset2 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"2.2.2.3"}, + NodeName: &nodeName, }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p23"), + Port: utilpointer.Int32(23), + Protocol: &udpProtocol, + }} + } + complexSubset3 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"4.4.4.4"}, + NodeName: &nodeName, + }, { + Addresses: []string{"4.4.4.5"}, + NodeName: &nodeName, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p44"), + Port: utilpointer.Int32(44), + Protocol: &udpProtocol, + }} + } + complexSubset4 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"4.4.4.6"}, + NodeName: &nodeName, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p45"), + Port: utilpointer.Int32(45), + Protocol: &udpProtocol, + }} + } + complexSubset5 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.1"}, + }, { + Addresses: []string{"1.1.1.11"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p11"), + Port: utilpointer.Int32(11), + Protocol: &udpProtocol, + }} + } + complexSubset6 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"1.1.1.2"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p12"), + Port: utilpointer.Int32(12), + Protocol: &udpProtocol, + }, { + Name: utilpointer.String("p122"), + Port: utilpointer.Int32(122), + Protocol: &udpProtocol, + }} + } + complexSubset7 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"3.3.3.3"}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p33"), + Port: utilpointer.Int32(33), + Protocol: &udpProtocol, + }} + } + complexSubset8 := func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{"4.4.4.4"}, + NodeName: &nodeName, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String("p44"), + Port: utilpointer.Int32(44), + Protocol: &udpProtocol, + }} + } + complexBefore := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, subset1), + nil, + makeTestEndpointSlice("ns2", "ep2", 1, complexSubset1), + makeTestEndpointSlice("ns2", "ep2", 2, complexSubset2), + nil, + makeTestEndpointSlice("ns4", "ep4", 1, complexSubset3), + makeTestEndpointSlice("ns4", "ep4", 2, complexSubset4), + } + complexAfter := []*discovery.EndpointSlice{ + makeTestEndpointSlice("ns1", "ep1", 1, complexSubset5), + makeTestEndpointSlice("ns1", "ep1", 2, complexSubset6), + nil, + nil, + makeTestEndpointSlice("ns3", "ep3", 1, complexSubset7), + makeTestEndpointSlice("ns4", "ep4", 1, complexSubset8), + nil, } testCases := []struct { // previousEndpoints and currentEndpoints are used to call appropriate // handlers OnEndpoints* (based on whether corresponding values are nil // or non-nil) and must be of equal length. - previousEndpoints []*v1.Endpoints - currentEndpoints []*v1.Endpoints + previousEndpoints []*discovery.EndpointSlice + currentEndpoints []*discovery.EndpointSlice oldEndpoints map[proxy.ServicePortName][]*proxy.BaseEndpointInfo expectedResult map[proxy.ServicePortName][]*proxy.BaseEndpointInfo expectedStaleEndpoints []proxy.ServiceEndpoint @@ -2943,42 +2930,17 @@ func Test_updateEndpointsMap(t *testing.T) { expectedStaleServiceNames: map[proxy.ServicePortName]bool{}, expectedHealthchecks: map[types.NamespacedName]int{}, }, { - // Case[1]: no change, unnamed port - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", unnamedPort), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", unnamedPort), - }, - oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ - makeServicePortName("ns1", "ep1", "", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, - }, - }, - expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ - makeServicePortName("ns1", "ep1", "", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, - }, - }, - expectedStaleEndpoints: []proxy.ServiceEndpoint{}, - expectedStaleServiceNames: map[proxy.ServicePortName]bool{}, - expectedHealthchecks: map[types.NamespacedName]int{}, - }, { - // Case[2]: no change, named port, local - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPortLocal), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPortLocal), - }, + // Case[1]: no change, named port, local + previousEndpoints: namedPortLocal, + currentEndpoints: namedPortLocal, oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:11", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, }, expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:11", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, }, expectedStaleEndpoints: []proxy.ServiceEndpoint{}, @@ -2987,60 +2949,52 @@ func Test_updateEndpointsMap(t *testing.T) { makeNSN("ns1", "ep1"): 1, }, }, { - // Case[3]: no change, multiple subsets - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", multipleSubsets), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", multipleSubsets), - }, + // Case[2]: no change, multiple subsets + previousEndpoints: multipleSubsets, + currentEndpoints: multipleSubsets, oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns1", "ep1", "p12", v1.ProtocolUDP): { - {Endpoint: "1.1.1.2:12", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.2:12", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, }, expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns1", "ep1", "p12", v1.ProtocolUDP): { - {Endpoint: "1.1.1.2:12", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.2:12", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, }, expectedStaleEndpoints: []proxy.ServiceEndpoint{}, expectedStaleServiceNames: map[proxy.ServicePortName]bool{}, expectedHealthchecks: map[types.NamespacedName]int{}, }, { - // Case[4]: no change, multiple subsets, multiple ports, local - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", multipleSubsetsMultiplePortsLocal), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", multipleSubsetsMultiplePortsLocal), - }, + // Case[3]: no change, multiple subsets, multiple ports, local + previousEndpoints: multipleSubsetsMultiplePortsLocal, + currentEndpoints: multipleSubsetsMultiplePortsLocal, oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:11", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns1", "ep1", "p12", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:12", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:12", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns1", "ep1", "p13", v1.ProtocolUDP): { - {Endpoint: "1.1.1.3:13", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.3:13", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, }, expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:11", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns1", "ep1", "p12", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:12", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:12", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns1", "ep1", "p13", v1.ProtocolUDP): { - {Endpoint: "1.1.1.3:13", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.3:13", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, }, expectedStaleEndpoints: []proxy.ServiceEndpoint{}, @@ -3049,65 +3003,59 @@ func Test_updateEndpointsMap(t *testing.T) { makeNSN("ns1", "ep1"): 1, }, }, { - // Case[5]: no change, multiple endpoints, subsets, IPs, and ports - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", multipleSubsetsIPsPorts1), - makeTestEndpoints("ns2", "ep2", multipleSubsetsIPsPorts2), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", multipleSubsetsIPsPorts1), - makeTestEndpoints("ns2", "ep2", multipleSubsetsIPsPorts2), - }, + // Case[4]: no change, multiple endpoints, subsets, IPs, and ports + previousEndpoints: multipleSubsetsIPsPorts, + currentEndpoints: multipleSubsetsIPsPorts, oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, - {Endpoint: "1.1.1.2:11", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + {Endpoint: "1.1.1.2:11", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns1", "ep1", "p12", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:12", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, - {Endpoint: "1.1.1.2:12", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:12", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + {Endpoint: "1.1.1.2:12", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns1", "ep1", "p13", v1.ProtocolUDP): { - {Endpoint: "1.1.1.3:13", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, - {Endpoint: "1.1.1.4:13", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.3:13", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + {Endpoint: "1.1.1.4:13", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns1", "ep1", "p14", v1.ProtocolUDP): { - {Endpoint: "1.1.1.3:14", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, - {Endpoint: "1.1.1.4:14", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.3:14", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + {Endpoint: "1.1.1.4:14", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns2", "ep2", "p21", v1.ProtocolUDP): { - {Endpoint: "2.2.2.1:21", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, - {Endpoint: "2.2.2.2:21", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "2.2.2.1:21", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + {Endpoint: "2.2.2.2:21", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns2", "ep2", "p22", v1.ProtocolUDP): { - {Endpoint: "2.2.2.1:22", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, - {Endpoint: "2.2.2.2:22", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "2.2.2.1:22", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + {Endpoint: "2.2.2.2:22", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, }, expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, - {Endpoint: "1.1.1.2:11", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + {Endpoint: "1.1.1.2:11", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns1", "ep1", "p12", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:12", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, - {Endpoint: "1.1.1.2:12", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:12", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + {Endpoint: "1.1.1.2:12", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns1", "ep1", "p13", v1.ProtocolUDP): { - {Endpoint: "1.1.1.3:13", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, - {Endpoint: "1.1.1.4:13", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.3:13", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + {Endpoint: "1.1.1.4:13", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns1", "ep1", "p14", v1.ProtocolUDP): { - {Endpoint: "1.1.1.3:14", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, - {Endpoint: "1.1.1.4:14", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.3:14", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + {Endpoint: "1.1.1.4:14", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns2", "ep2", "p21", v1.ProtocolUDP): { - {Endpoint: "2.2.2.1:21", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, - {Endpoint: "2.2.2.2:21", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "2.2.2.1:21", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + {Endpoint: "2.2.2.2:21", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns2", "ep2", "p22", v1.ProtocolUDP): { - {Endpoint: "2.2.2.1:22", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, - {Endpoint: "2.2.2.2:22", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "2.2.2.1:22", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + {Endpoint: "2.2.2.2:22", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, }, expectedStaleEndpoints: []proxy.ServiceEndpoint{}, @@ -3117,67 +3065,55 @@ func Test_updateEndpointsMap(t *testing.T) { makeNSN("ns2", "ep2"): 1, }, }, { - // Case[6]: add an Endpoints - previousEndpoints: []*v1.Endpoints{ - nil, - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", unnamedPortLocal), - }, - oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{}, + // Case[5]: add an Endpoints + previousEndpoints: []*discovery.EndpointSlice{nil}, + currentEndpoints: namedPortLocal, + oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{}, expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ - makeServicePortName("ns1", "ep1", "", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { + {Endpoint: "1.1.1.1:11", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, }, expectedStaleEndpoints: []proxy.ServiceEndpoint{}, expectedStaleServiceNames: map[proxy.ServicePortName]bool{ - makeServicePortName("ns1", "ep1", "", v1.ProtocolUDP): true, + makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): true, }, expectedHealthchecks: map[types.NamespacedName]int{ makeNSN("ns1", "ep1"): 1, }, }, { - // Case[7]: remove an Endpoints - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", unnamedPortLocal), - }, - currentEndpoints: []*v1.Endpoints{ - nil, - }, + // Case[6]: remove an Endpoints + previousEndpoints: namedPortLocal, + currentEndpoints: []*discovery.EndpointSlice{nil}, oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ - makeServicePortName("ns1", "ep1", "", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { + {Endpoint: "1.1.1.1:11", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, }, expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{}, expectedStaleEndpoints: []proxy.ServiceEndpoint{{ Endpoint: "1.1.1.1:11", - ServicePortName: makeServicePortName("ns1", "ep1", "", v1.ProtocolUDP), + ServicePortName: makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP), }}, expectedStaleServiceNames: map[proxy.ServicePortName]bool{}, expectedHealthchecks: map[types.NamespacedName]int{}, }, { - // Case[8]: add an IP and port - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPort), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPortsLocalNoLocal), - }, + // Case[7]: add an IP and port + previousEndpoints: namedPort, + currentEndpoints: namedPortsLocalNoLocal, oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, }, expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, - {Endpoint: "1.1.1.2:11", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + {Endpoint: "1.1.1.2:11", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns1", "ep1", "p12", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:12", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, - {Endpoint: "1.1.1.2:12", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:12", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + {Endpoint: "1.1.1.2:12", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, }, expectedStaleEndpoints: []proxy.ServiceEndpoint{}, @@ -3188,26 +3124,22 @@ func Test_updateEndpointsMap(t *testing.T) { makeNSN("ns1", "ep1"): 1, }, }, { - // Case[9]: remove an IP and port - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPortsLocalNoLocal), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPort), - }, + // Case[8]: remove an IP and port + previousEndpoints: namedPortsLocalNoLocal, + currentEndpoints: namedPort, oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, - {Endpoint: "1.1.1.2:11", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + {Endpoint: "1.1.1.2:11", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns1", "ep1", "p12", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:12", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, - {Endpoint: "1.1.1.2:12", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:12", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + {Endpoint: "1.1.1.2:12", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, }, expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, }, expectedStaleEndpoints: []proxy.ServiceEndpoint{{ @@ -3223,24 +3155,20 @@ func Test_updateEndpointsMap(t *testing.T) { expectedStaleServiceNames: map[proxy.ServicePortName]bool{}, expectedHealthchecks: map[types.NamespacedName]int{}, }, { - // Case[10]: add a subset - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPort), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", multipleSubsetsWithLocal), - }, + // Case[9]: add a subset + previousEndpoints: []*discovery.EndpointSlice{namedPort[0], nil}, + currentEndpoints: multipleSubsetsWithLocal, oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, }, expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns1", "ep1", "p12", v1.ProtocolUDP): { - {Endpoint: "1.1.1.2:12", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.2:12", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, }, expectedStaleEndpoints: []proxy.ServiceEndpoint{}, @@ -3251,24 +3179,20 @@ func Test_updateEndpointsMap(t *testing.T) { makeNSN("ns1", "ep1"): 1, }, }, { - // Case[11]: remove a subset - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", multipleSubsets), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPort), - }, + // Case[10]: remove a subset + previousEndpoints: multipleSubsets, + currentEndpoints: []*discovery.EndpointSlice{namedPort[0], nil}, oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns1", "ep1", "p12", v1.ProtocolUDP): { - {Endpoint: "1.1.1.2:12", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.2:12", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, }, expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, }, expectedStaleEndpoints: []proxy.ServiceEndpoint{{ @@ -3278,21 +3202,17 @@ func Test_updateEndpointsMap(t *testing.T) { expectedStaleServiceNames: map[proxy.ServicePortName]bool{}, expectedHealthchecks: map[types.NamespacedName]int{}, }, { - // Case[12]: rename a port - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPort), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPortRenamed), - }, + // Case[11]: rename a port + previousEndpoints: namedPort, + currentEndpoints: namedPortRenamed, oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, }, expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ makeServicePortName("ns1", "ep1", "p11-2", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, }, expectedStaleEndpoints: []proxy.ServiceEndpoint{{ @@ -3304,21 +3224,17 @@ func Test_updateEndpointsMap(t *testing.T) { }, expectedHealthchecks: map[types.NamespacedName]int{}, }, { - // Case[13]: renumber a port - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPort), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", namedPortRenumbered), - }, + // Case[12]: renumber a port + previousEndpoints: namedPort, + currentEndpoints: namedPortRenumbered, oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, }, expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:22", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:22", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, }, expectedStaleEndpoints: []proxy.ServiceEndpoint{{ @@ -3328,54 +3244,44 @@ func Test_updateEndpointsMap(t *testing.T) { expectedStaleServiceNames: map[proxy.ServicePortName]bool{}, expectedHealthchecks: map[types.NamespacedName]int{}, }, { - // Case[14]: complex add and remove - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", complexBefore1), - makeTestEndpoints("ns2", "ep2", complexBefore2), - nil, - makeTestEndpoints("ns4", "ep4", complexBefore4), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", complexAfter1), - nil, - makeTestEndpoints("ns3", "ep3", complexAfter3), - makeTestEndpoints("ns4", "ep4", complexAfter4), - }, + // Case[13]: complex add and remove + previousEndpoints: complexBefore, + currentEndpoints: complexAfter, oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns2", "ep2", "p22", v1.ProtocolUDP): { - {Endpoint: "2.2.2.2:22", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, - {Endpoint: "2.2.2.22:22", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "2.2.2.22:22", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, + {Endpoint: "2.2.2.2:22", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns2", "ep2", "p23", v1.ProtocolUDP): { - {Endpoint: "2.2.2.3:23", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "2.2.2.3:23", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns4", "ep4", "p44", v1.ProtocolUDP): { - {Endpoint: "4.4.4.4:44", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, - {Endpoint: "4.4.4.5:44", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "4.4.4.4:44", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, + {Endpoint: "4.4.4.5:44", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns4", "ep4", "p45", v1.ProtocolUDP): { - {Endpoint: "4.4.4.6:45", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "4.4.4.6:45", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, }, expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, - {Endpoint: "1.1.1.11:11", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.11:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns1", "ep1", "p12", v1.ProtocolUDP): { - {Endpoint: "1.1.1.2:12", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.2:12", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns1", "ep1", "p122", v1.ProtocolUDP): { - {Endpoint: "1.1.1.2:122", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "1.1.1.2:122", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns3", "ep3", "p33", v1.ProtocolUDP): { - {Endpoint: "3.3.3.3:33", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "3.3.3.3:33", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, makeServicePortName("ns4", "ep4", "p44", v1.ProtocolUDP): { - {Endpoint: "4.4.4.4:44", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + {Endpoint: "4.4.4.4:44", NodeName: nodeName, IsLocal: true, Ready: true, Serving: true, Terminating: false}, }, }, expectedStaleEndpoints: []proxy.ServiceEndpoint{{ @@ -3403,22 +3309,18 @@ func Test_updateEndpointsMap(t *testing.T) { makeNSN("ns4", "ep4"): 1, }, }, { - // Case[15]: change from 0 endpoint address to 1 unnamed port - previousEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", emptyEndpoint), - }, - currentEndpoints: []*v1.Endpoints{ - makeTestEndpoints("ns1", "ep1", unnamedPort), - }, - oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{}, + // Case[15]: change from 0 endpoint address to 1 named port + previousEndpoints: emptyEndpointSlices, + currentEndpoints: namedPort, + oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{}, expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{ - makeServicePortName("ns1", "ep1", "", v1.ProtocolUDP): { - {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false, ZoneHints: sets.String{}}, + makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { + {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}, }, }, expectedStaleEndpoints: []proxy.ServiceEndpoint{}, expectedStaleServiceNames: map[proxy.ServicePortName]bool{ - makeServicePortName("ns1", "ep1", "", v1.ProtocolUDP): true, + makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): true, }, expectedHealthchecks: map[types.NamespacedName]int{}, }, @@ -3428,14 +3330,14 @@ func Test_updateEndpointsMap(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, false, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) fp.hostname = nodeName // First check that after adding all previous versions of endpoints, // the fp.oldEndpoints is as we expect. for i := range tc.previousEndpoints { if tc.previousEndpoints[i] != nil { - fp.OnEndpointsAdd(tc.previousEndpoints[i]) + fp.OnEndpointSliceAdd(tc.previousEndpoints[i]) } } fp.endpointsMap.Update(fp.endpointsChanges) @@ -3451,11 +3353,11 @@ func Test_updateEndpointsMap(t *testing.T) { prev, curr := tc.previousEndpoints[i], tc.currentEndpoints[i] switch { case prev == nil: - fp.OnEndpointsAdd(curr) + fp.OnEndpointSliceAdd(curr) case curr == nil: - fp.OnEndpointsDelete(prev) + fp.OnEndpointSliceDelete(prev) default: - fp.OnEndpointsUpdate(prev, curr) + fp.OnEndpointSliceUpdate(prev, curr) } } result := fp.endpointsMap.Update(fp.endpointsChanges) @@ -3701,7 +3603,7 @@ func Test_syncService(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - proxier := NewFakeProxier(ipt, ipvs, ipset, nil, nil, false, v1.IPv4Protocol) + proxier := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) proxier.netlinkHandle.EnsureDummyDevice(DefaultDummyDevice) if testCases[i].oldVirtualServer != nil { @@ -3731,7 +3633,7 @@ func buildFakeProxier() (*iptablestest.FakeIPTables, *Proxier) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - return ipt, NewFakeProxier(ipt, ipvs, ipset, nil, nil, false, v1.IPv4Protocol) + return ipt, NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) } func hasJump(rules []iptablestest.Rule, destChain, ipSet string) bool { @@ -3819,7 +3721,7 @@ func TestCleanLegacyService(t *testing.T) { ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) excludeCIDRs, _ := utilnet.ParseCIDRs([]string{"3.3.3.0/24", "4.4.4.0/24"}) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, excludeCIDRs, false, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, excludeCIDRs, v1.IPv4Protocol) // All ipvs services that were processed in the latest sync loop. activeServices := map[string]bool{"ipvs0": true, "ipvs1": true} @@ -3925,7 +3827,7 @@ func TestCleanLegacyServiceWithRealServers(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, false, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) // all deleted expect ipvs2 activeServices := map[string]bool{"ipvs2": true} @@ -4020,7 +3922,7 @@ func TestCleanLegacyRealServersExcludeCIDRs(t *testing.T) { ipset := ipsettest.NewFake(testIPSetVersion) gtm := NewGracefulTerminationManager(ipvs) excludeCIDRs, _ := utilnet.ParseCIDRs([]string{"4.4.4.4/32"}) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, excludeCIDRs, false, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, excludeCIDRs, v1.IPv4Protocol) fp.gracefuldeleteManager = gtm vs := &utilipvs.VirtualServer{ @@ -4075,7 +3977,7 @@ func TestCleanLegacyService6(t *testing.T) { ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) excludeCIDRs, _ := utilnet.ParseCIDRs([]string{"3000::/64", "4000::/64"}) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, excludeCIDRs, false, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, excludeCIDRs, v1.IPv4Protocol) fp.nodeIP = net.ParseIP("::1") // All ipvs services that were processed in the latest sync loop. @@ -4182,7 +4084,7 @@ func TestMultiPortServiceBindAddr(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, false, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) service1 := makeTestService("ns1", "svc1", func(svc *v1.Service) { svc.Spec.Type = v1.ServiceTypeClusterIP @@ -4204,7 +4106,6 @@ func TestMultiPortServiceBindAddr(t *testing.T) { }) fp.servicesSynced = true - fp.endpointsSynced = true // first, add multi-port service1 fp.OnServiceAdd(service1) @@ -4287,9 +4188,8 @@ func TestEndpointSliceE2E(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, true, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) fp.servicesSynced = true - fp.endpointsSynced = true fp.endpointSlicesSynced = true // Add initial service @@ -4374,9 +4274,8 @@ func TestHealthCheckNodePortE2E(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, true, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) fp.servicesSynced = true - fp.endpointsSynced = true fp.endpointSlicesSynced = true // Add initial service @@ -4429,9 +4328,9 @@ func Test_HealthCheckNodePortWhenTerminating(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, true, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) fp.servicesSynced = true - fp.endpointsSynced = true + // fp.endpointsSynced = true fp.endpointSlicesSynced = true serviceName := "svc1" @@ -4572,7 +4471,7 @@ func TestCreateAndLinkKubeChain(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, true, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) fp.createAndLinkKubeChain() expectedNATChains := `:KUBE-SERVICES - [0:0] :KUBE-POSTROUTING - [0:0] @@ -4677,9 +4576,9 @@ func TestTestInternalTrafficPolicyE2E(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, true, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) fp.servicesSynced = true - fp.endpointsSynced = true + // fp.endpointsSynced = true fp.endpointSlicesSynced = true // Add initial service @@ -4776,9 +4675,9 @@ func Test_EndpointSliceReadyAndTerminatingLocal(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, true, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) fp.servicesSynced = true - fp.endpointsSynced = true + // fp.endpointsSynced = true fp.endpointSlicesSynced = true clusterInternalTrafficPolicy := v1.ServiceInternalTrafficPolicyCluster @@ -4952,9 +4851,9 @@ func Test_EndpointSliceOnlyReadyAndTerminatingLocal(t *testing.T) { ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, true, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) fp.servicesSynced = true - fp.endpointsSynced = true + // fp.endpointsSynced = true fp.endpointSlicesSynced = true clusterInternalTrafficPolicy := v1.ServiceInternalTrafficPolicyCluster @@ -5125,9 +5024,9 @@ func Test_EndpointSliceOnlyReadyAndTerminatingLocalWithFeatureGateDisabled(t *te ipt := iptablestest.NewFake() ipvs := ipvstest.NewFake() ipset := ipsettest.NewFake(testIPSetVersion) - fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, true, v1.IPv4Protocol) + fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil, v1.IPv4Protocol) fp.servicesSynced = true - fp.endpointsSynced = true + // fp.endpointsSynced = true fp.endpointSlicesSynced = true clusterInternalTrafficPolicy := v1.ServiceInternalTrafficPolicyCluster diff --git a/pkg/proxy/topology.go b/pkg/proxy/topology.go index c66c721b72a..9b36e0b7df2 100644 --- a/pkg/proxy/topology.go +++ b/pkg/proxy/topology.go @@ -27,7 +27,7 @@ import ( // labels, and enabled feature gates. This is primarily used to enable topology // aware routing. func FilterEndpoints(endpoints []Endpoint, svcInfo ServicePort, nodeLabels map[string]string) []Endpoint { - if svcInfo.NodeLocalExternal() || !utilfeature.DefaultFeatureGate.Enabled(features.EndpointSliceProxying) { + if svcInfo.NodeLocalExternal() { return endpoints } diff --git a/pkg/proxy/topology_test.go b/pkg/proxy/topology_test.go index 7d686cf2a47..9587d70933a 100644 --- a/pkg/proxy/topology_test.go +++ b/pkg/proxy/topology_test.go @@ -33,19 +33,17 @@ func TestFilterEndpoints(t *testing.T) { zoneHints sets.String } testCases := []struct { - name string - epsProxyingEnabled bool - hintsEnabled bool - nodeLabels map[string]string - serviceInfo ServicePort - endpoints []endpoint - expectedEndpoints []endpoint + name string + hintsEnabled bool + nodeLabels map[string]string + serviceInfo ServicePort + endpoints []endpoint + expectedEndpoints []endpoint }{{ - name: "hints + eps proxying enabled, hints annotation == auto", - hintsEnabled: true, - epsProxyingEnabled: true, - nodeLabels: map[string]string{v1.LabelTopologyZone: "zone-a"}, - serviceInfo: &BaseServiceInfo{nodeLocalExternal: false, hintsAnnotation: "auto"}, + name: "hints enabled, hints annotation == auto", + hintsEnabled: true, + nodeLabels: map[string]string{v1.LabelTopologyZone: "zone-a"}, + serviceInfo: &BaseServiceInfo{nodeLocalExternal: false, hintsAnnotation: "auto"}, endpoints: []endpoint{ {ip: "10.1.2.3", zoneHints: sets.NewString("zone-a")}, {ip: "10.1.2.4", zoneHints: sets.NewString("zone-b")}, @@ -57,11 +55,10 @@ func TestFilterEndpoints(t *testing.T) { {ip: "10.1.2.6", zoneHints: sets.NewString("zone-a")}, }, }, { - name: "hints + eps proxying enabled, hints annotation == disabled, hints ignored", - hintsEnabled: true, - epsProxyingEnabled: true, - nodeLabels: map[string]string{v1.LabelTopologyZone: "zone-a"}, - serviceInfo: &BaseServiceInfo{nodeLocalExternal: false, hintsAnnotation: "disabled"}, + name: "hints, hints annotation == disabled, hints ignored", + hintsEnabled: true, + nodeLabels: map[string]string{v1.LabelTopologyZone: "zone-a"}, + serviceInfo: &BaseServiceInfo{nodeLocalExternal: false, hintsAnnotation: "disabled"}, endpoints: []endpoint{ {ip: "10.1.2.3", zoneHints: sets.NewString("zone-a")}, {ip: "10.1.2.4", zoneHints: sets.NewString("zone-b")}, @@ -75,11 +72,10 @@ func TestFilterEndpoints(t *testing.T) { {ip: "10.1.2.6", zoneHints: sets.NewString("zone-a")}, }, }, { - name: "hints + eps proxying enabled, hints annotation == aUto (wrong capitalization), hints ignored", - hintsEnabled: true, - epsProxyingEnabled: true, - nodeLabels: map[string]string{v1.LabelTopologyZone: "zone-a"}, - serviceInfo: &BaseServiceInfo{nodeLocalExternal: false, hintsAnnotation: "aUto"}, + name: "hints, hints annotation == aUto (wrong capitalization), hints ignored", + hintsEnabled: true, + nodeLabels: map[string]string{v1.LabelTopologyZone: "zone-a"}, + serviceInfo: &BaseServiceInfo{nodeLocalExternal: false, hintsAnnotation: "aUto"}, endpoints: []endpoint{ {ip: "10.1.2.3", zoneHints: sets.NewString("zone-a")}, {ip: "10.1.2.4", zoneHints: sets.NewString("zone-b")}, @@ -93,11 +89,10 @@ func TestFilterEndpoints(t *testing.T) { {ip: "10.1.2.6", zoneHints: sets.NewString("zone-a")}, }, }, { - name: "hints + eps proxying enabled, hints annotation empty, hints ignored", - hintsEnabled: true, - epsProxyingEnabled: true, - nodeLabels: map[string]string{v1.LabelTopologyZone: "zone-a"}, - serviceInfo: &BaseServiceInfo{nodeLocalExternal: false}, + name: "hints, hints annotation empty, hints ignored", + hintsEnabled: true, + nodeLabels: map[string]string{v1.LabelTopologyZone: "zone-a"}, + serviceInfo: &BaseServiceInfo{nodeLocalExternal: false}, endpoints: []endpoint{ {ip: "10.1.2.3", zoneHints: sets.NewString("zone-a")}, {ip: "10.1.2.4", zoneHints: sets.NewString("zone-b")}, @@ -111,29 +106,10 @@ func TestFilterEndpoints(t *testing.T) { {ip: "10.1.2.6", zoneHints: sets.NewString("zone-a")}, }, }, { - name: "hints enabled, eps proxying not, hints are ignored", - hintsEnabled: true, - epsProxyingEnabled: false, - nodeLabels: map[string]string{v1.LabelTopologyZone: "zone-a"}, - serviceInfo: &BaseServiceInfo{nodeLocalExternal: false}, - endpoints: []endpoint{ - {ip: "10.1.2.3", zoneHints: sets.NewString("zone-a")}, - {ip: "10.1.2.4", zoneHints: sets.NewString("zone-b")}, - {ip: "10.1.2.5", zoneHints: sets.NewString("zone-c")}, - {ip: "10.1.2.6", zoneHints: sets.NewString("zone-a")}, - }, - expectedEndpoints: []endpoint{ - {ip: "10.1.2.3", zoneHints: sets.NewString("zone-a")}, - {ip: "10.1.2.4", zoneHints: sets.NewString("zone-b")}, - {ip: "10.1.2.5", zoneHints: sets.NewString("zone-c")}, - {ip: "10.1.2.6", zoneHints: sets.NewString("zone-a")}, - }, - }, { - name: "node local endpoints, hints are ignored", - hintsEnabled: true, - epsProxyingEnabled: true, - nodeLabels: map[string]string{v1.LabelTopologyZone: "zone-a"}, - serviceInfo: &BaseServiceInfo{nodeLocalExternal: true}, + name: "node local endpoints, hints are ignored", + hintsEnabled: true, + nodeLabels: map[string]string{v1.LabelTopologyZone: "zone-a"}, + serviceInfo: &BaseServiceInfo{nodeLocalExternal: true}, endpoints: []endpoint{ {ip: "10.1.2.3", zoneHints: sets.NewString("zone-a")}, {ip: "10.1.2.4", zoneHints: sets.NewString("zone-b")}, @@ -158,7 +134,6 @@ func TestFilterEndpoints(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.EndpointSliceProxying, tc.epsProxyingEnabled)() defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.TopologyAwareHints, tc.hintsEnabled)() endpoints := []Endpoint{} diff --git a/pkg/proxy/winkernel/proxier.go b/pkg/proxy/winkernel/proxier.go index d2a9abe1223..f212f7a6d45 100644 --- a/pkg/proxy/winkernel/proxier.go +++ b/pkg/proxy/winkernel/proxier.go @@ -43,7 +43,6 @@ import ( "k8s.io/client-go/tools/events" "k8s.io/klog/v2" "k8s.io/kubernetes/pkg/apis/core/v1/helper" - "k8s.io/kubernetes/pkg/features" kubefeatures "k8s.io/kubernetes/pkg/features" "k8s.io/kubernetes/pkg/proxy" "k8s.io/kubernetes/pkg/proxy/apis/config" @@ -458,10 +457,9 @@ type Proxier struct { mu sync.Mutex // protects the following fields serviceMap proxy.ServiceMap endpointsMap proxy.EndpointsMap - // endpointsSynced and servicesSynced are set to true when corresponding + // endpointSlicesSynced and servicesSynced are set to true when corresponding // objects are synced after startup. This is used to avoid updating hns policies // with some partial data after kube-proxy restart. - endpointsSynced bool endpointSlicesSynced bool servicesSynced bool isIPv6Mode bool @@ -627,7 +625,6 @@ func NewProxier( } isIPv6 := utilnet.IsIPv6(nodeIP) - endpointSlicesEnabled := utilfeature.DefaultFeatureGate.Enabled(features.WindowsEndpointSliceProxying) proxier := &Proxier{ endPointsRefCount: make(endPointsReferenceCountMap), serviceMap: make(proxy.ServiceMap), @@ -654,7 +651,7 @@ func NewProxier( ipFamily = v1.IPv6Protocol } serviceChanges := proxy.NewServiceChangeTracker(proxier.newServiceInfo, ipFamily, recorder, proxier.serviceMapChange) - endPointChangeTracker := proxy.NewEndpointChangeTracker(hostname, proxier.newEndpointInfo, ipFamily, recorder, endpointSlicesEnabled, proxier.endpointsMapChange) + endPointChangeTracker := proxy.NewEndpointChangeTracker(hostname, proxier.newEndpointInfo, ipFamily, recorder, proxier.endpointsMapChange) proxier.endpointsChanges = endPointChangeTracker proxier.serviceChanges = serviceChanges @@ -830,11 +827,7 @@ func (proxier *Proxier) OnServiceDelete(service *v1.Service) { func (proxier *Proxier) OnServiceSynced() { proxier.mu.Lock() proxier.servicesSynced = true - if utilfeature.DefaultFeatureGate.Enabled(features.WindowsEndpointSliceProxying) { - proxier.setInitialized(proxier.endpointSlicesSynced) - } else { - proxier.setInitialized(proxier.endpointsSynced) - } + proxier.setInitialized(proxier.endpointSlicesSynced) proxier.mu.Unlock() // Sync unconditionally - this is called once per lifetime. @@ -855,38 +848,24 @@ func shouldSkipService(svcName types.NamespacedName, service *v1.Service) bool { return false } +// The following methods exist to implement the proxier interface, however +// winkernel proxier only uses EndpointSlice, so the following are noops. + // OnEndpointsAdd is called whenever creation of new endpoints object // is observed. -func (proxier *Proxier) OnEndpointsAdd(endpoints *v1.Endpoints) { - proxier.OnEndpointsUpdate(nil, endpoints) -} +func (proxier *Proxier) OnEndpointsAdd(endpoints *v1.Endpoints) {} // OnEndpointsUpdate is called whenever modification of an existing // endpoints object is observed. -func (proxier *Proxier) OnEndpointsUpdate(oldEndpoints, endpoints *v1.Endpoints) { - - if proxier.endpointsChanges.Update(oldEndpoints, endpoints) && proxier.isInitialized() { - proxier.Sync() - } -} +func (proxier *Proxier) OnEndpointsUpdate(oldEndpoints, endpoints *v1.Endpoints) {} // OnEndpointsDelete is called whenever deletion of an existing endpoints // object is observed. -func (proxier *Proxier) OnEndpointsDelete(endpoints *v1.Endpoints) { - proxier.OnEndpointsUpdate(endpoints, nil) -} +func (proxier *Proxier) OnEndpointsDelete(endpoints *v1.Endpoints) {} // OnEndpointsSynced is called once all the initial event handlers were // called and the state is fully propagated to local cache. -func (proxier *Proxier) OnEndpointsSynced() { - proxier.mu.Lock() - proxier.endpointsSynced = true - proxier.setInitialized(proxier.servicesSynced && proxier.endpointsSynced) - proxier.mu.Unlock() - - // Sync unconditionally - this is called once per lifetime. - proxier.syncProxyRules() -} +func (proxier *Proxier) OnEndpointsSynced() {} // OnEndpointSliceAdd is called whenever creation of a new endpoint slice object // is observed. diff --git a/pkg/proxy/winkernel/proxier_test.go b/pkg/proxy/winkernel/proxier_test.go index c132e6d36cb..177711076ec 100644 --- a/pkg/proxy/winkernel/proxier_test.go +++ b/pkg/proxy/winkernel/proxier_test.go @@ -111,7 +111,7 @@ func (hns fakeHNS) deleteLoadBalancer(hnsID string) error { return nil } -func NewFakeProxier(syncPeriod time.Duration, minSyncPeriod time.Duration, clusterCIDR string, hostname string, nodeIP net.IP, networkType string, endpointSliceEnabled bool) *Proxier { +func NewFakeProxier(syncPeriod time.Duration, minSyncPeriod time.Duration, clusterCIDR string, hostname string, nodeIP net.IP, networkType string) *Proxier { sourceVip := "192.168.1.2" hnsNetworkInfo := &hnsNetworkInfo{ id: strings.ToUpper(guid), @@ -134,7 +134,7 @@ func NewFakeProxier(syncPeriod time.Duration, minSyncPeriod time.Duration, clust } serviceChanges := proxy.NewServiceChangeTracker(proxier.newServiceInfo, v1.IPv4Protocol, nil, proxier.serviceMapChange) - endpointChangeTracker := proxy.NewEndpointChangeTracker(hostname, proxier.newEndpointInfo, v1.IPv4Protocol, nil, endpointSliceEnabled, proxier.endpointsMapChange) + endpointChangeTracker := proxy.NewEndpointChangeTracker(hostname, proxier.newEndpointInfo, v1.IPv4Protocol, nil, proxier.endpointsMapChange) proxier.endpointsChanges = endpointChangeTracker proxier.serviceChanges = serviceChanges @@ -143,7 +143,7 @@ func NewFakeProxier(syncPeriod time.Duration, minSyncPeriod time.Duration, clust func TestCreateServiceVip(t *testing.T) { syncPeriod := 30 * time.Second - proxier := NewFakeProxier(syncPeriod, syncPeriod, clusterCIDR, "testhost", net.ParseIP("10.0.0.1"), NETWORK_TYPE_OVERLAY, false) + proxier := NewFakeProxier(syncPeriod, syncPeriod, clusterCIDR, "testhost", net.ParseIP("10.0.0.1"), NETWORK_TYPE_OVERLAY) if proxier == nil { t.Error() } @@ -178,7 +178,6 @@ func TestCreateServiceVip(t *testing.T) { }} }), ) - makeEndpointsMap(proxier) proxier.setInitialized(true) proxier.syncProxyRules() @@ -199,7 +198,7 @@ func TestCreateServiceVip(t *testing.T) { func TestCreateRemoteEndpointOverlay(t *testing.T) { syncPeriod := 30 * time.Second - proxier := NewFakeProxier(syncPeriod, syncPeriod, clusterCIDR, "testhost", net.ParseIP("10.0.0.1"), NETWORK_TYPE_OVERLAY, false) + proxier := NewFakeProxier(syncPeriod, syncPeriod, clusterCIDR, "testhost", net.ParseIP("10.0.0.1"), NETWORK_TYPE_OVERLAY) if proxier == nil { t.Error() } @@ -212,6 +211,7 @@ func TestCreateRemoteEndpointOverlay(t *testing.T) { Port: "p80", Protocol: v1.ProtocolTCP, } + tcpProtocol := v1.ProtocolTCP makeServiceMap(proxier, makeTestService(svcPortName.Namespace, svcPortName.Name, func(svc *v1.Service) { @@ -225,17 +225,16 @@ func TestCreateRemoteEndpointOverlay(t *testing.T) { }} }), ) - makeEndpointsMap(proxier, - makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIpAddressRemote, - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName.Port, - Port: int32(svcPort), - Protocol: v1.ProtocolTCP, - }}, + populateEndpointSlices(proxier, + makeTestEndpointSlice(svcPortName.Namespace, svcPortName.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIpAddressRemote}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName.Port), + Port: utilpointer.Int32(int32(svcPort)), + Protocol: &tcpProtocol, }} }), ) @@ -264,18 +263,19 @@ func TestCreateRemoteEndpointOverlay(t *testing.T) { func TestCreateRemoteEndpointL2Bridge(t *testing.T) { syncPeriod := 30 * time.Second - proxier := NewFakeProxier(syncPeriod, syncPeriod, clusterCIDR, "testhost", net.ParseIP("10.0.0.1"), "L2Bridge", false) + proxier := NewFakeProxier(syncPeriod, syncPeriod, clusterCIDR, "testhost", net.ParseIP("10.0.0.1"), "L2Bridge") if proxier == nil { t.Error() } + tcpProtocol := v1.ProtocolTCP svcIP := "10.20.30.41" svcPort := 80 svcNodePort := 3001 svcPortName := proxy.ServicePortName{ NamespacedName: makeNSN("ns1", "svc1"), Port: "p80", - Protocol: v1.ProtocolTCP, + Protocol: tcpProtocol, } makeServiceMap(proxier, @@ -285,22 +285,21 @@ func TestCreateRemoteEndpointL2Bridge(t *testing.T) { svc.Spec.Ports = []v1.ServicePort{{ Name: svcPortName.Port, Port: int32(svcPort), - Protocol: v1.ProtocolTCP, + Protocol: tcpProtocol, NodePort: int32(svcNodePort), }} }), ) - makeEndpointsMap(proxier, - makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIpAddressRemote, - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName.Port, - Port: int32(svcPort), - Protocol: v1.ProtocolTCP, - }}, + populateEndpointSlices(proxier, + makeTestEndpointSlice(svcPortName.Namespace, svcPortName.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIpAddressRemote}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.String(svcPortName.Port), + Port: utilpointer.Int32(int32(svcPort)), + Protocol: &tcpProtocol, }} }), ) @@ -327,7 +326,8 @@ func TestCreateRemoteEndpointL2Bridge(t *testing.T) { } func TestSharedRemoteEndpointDelete(t *testing.T) { syncPeriod := 30 * time.Second - proxier := NewFakeProxier(syncPeriod, syncPeriod, clusterCIDR, "testhost", net.ParseIP("10.0.0.1"), "L2Bridge", false) + tcpProtocol := v1.ProtocolTCP + proxier := NewFakeProxier(syncPeriod, syncPeriod, clusterCIDR, "testhost", net.ParseIP("10.0.0.1"), "L2Bridge") if proxier == nil { t.Error() } @@ -372,29 +372,27 @@ func TestSharedRemoteEndpointDelete(t *testing.T) { }} }), ) - makeEndpointsMap(proxier, - makeTestEndpoints(svcPortName1.Namespace, svcPortName1.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIpAddressRemote, - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName1.Port, - Port: int32(svcPort1), - Protocol: v1.ProtocolTCP, - }}, + populateEndpointSlices(proxier, + makeTestEndpointSlice(svcPortName1.Namespace, svcPortName1.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIpAddressRemote}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName1.Port), + Port: utilpointer.Int32(int32(svcPort1)), + Protocol: &tcpProtocol, }} }), - makeTestEndpoints(svcPortName2.Namespace, svcPortName2.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIpAddressRemote, - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName2.Port, - Port: int32(svcPort2), - Protocol: v1.ProtocolTCP, - }}, + makeTestEndpointSlice(svcPortName2.Namespace, svcPortName2.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIpAddressRemote}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName2.Port), + Port: utilpointer.Int32(int32(svcPort2)), + Protocol: &tcpProtocol, }} }), ) @@ -433,17 +431,16 @@ func TestSharedRemoteEndpointDelete(t *testing.T) { }), ) - deleteEndpoints(proxier, - makeTestEndpoints(svcPortName2.Namespace, svcPortName2.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIpAddressRemote, - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName2.Port, - Port: int32(svcPort2), - Protocol: v1.ProtocolTCP, - }}, + deleteEndpointSlices(proxier, + makeTestEndpointSlice(svcPortName2.Namespace, svcPortName2.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIpAddressRemote}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName2.Port), + Port: utilpointer.Int32(int32(svcPort2)), + Protocol: &tcpProtocol, }} }), ) @@ -472,7 +469,7 @@ func TestSharedRemoteEndpointDelete(t *testing.T) { } func TestSharedRemoteEndpointUpdate(t *testing.T) { syncPeriod := 30 * time.Second - proxier := NewFakeProxier(syncPeriod, syncPeriod, clusterCIDR, "testhost", net.ParseIP("10.0.0.1"), "L2Bridge", false) + proxier := NewFakeProxier(syncPeriod, syncPeriod, clusterCIDR, "testhost", net.ParseIP("10.0.0.1"), "L2Bridge") if proxier == nil { t.Error() } @@ -518,29 +515,28 @@ func TestSharedRemoteEndpointUpdate(t *testing.T) { }), ) - makeEndpointsMap(proxier, - makeTestEndpoints(svcPortName1.Namespace, svcPortName1.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIpAddressRemote, - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName1.Port, - Port: int32(svcPort1), - Protocol: v1.ProtocolTCP, - }}, + tcpProtocol := v1.ProtocolTCP + populateEndpointSlices(proxier, + makeTestEndpointSlice(svcPortName1.Namespace, svcPortName1.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIpAddressRemote}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName1.Port), + Port: utilpointer.Int32(int32(svcPort1)), + Protocol: &tcpProtocol, }} }), - makeTestEndpoints(svcPortName2.Namespace, svcPortName2.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIpAddressRemote, - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName2.Port, - Port: int32(svcPort2), - Protocol: v1.ProtocolTCP, - }}, + makeTestEndpointSlice(svcPortName2.Namespace, svcPortName2.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIpAddressRemote}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName2.Port), + Port: utilpointer.Int32(int32(svcPort2)), + Protocol: &tcpProtocol, }} }), ) @@ -589,40 +585,37 @@ func TestSharedRemoteEndpointUpdate(t *testing.T) { }} })) - proxier.OnEndpointsUpdate( - makeTestEndpoints(svcPortName1.Namespace, svcPortName1.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIpAddressRemote, - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName1.Port, - Port: int32(svcPort1), - Protocol: v1.ProtocolTCP, - }}, + proxier.OnEndpointSliceUpdate( + makeTestEndpointSlice(svcPortName1.Namespace, svcPortName1.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIpAddressRemote}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName1.Port), + Port: utilpointer.Int32(int32(svcPort1)), + Protocol: &tcpProtocol, }} }), - makeTestEndpoints(svcPortName1.Namespace, svcPortName1.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIpAddressRemote, - }}, - Ports: []v1.EndpointPort{ - { - Name: svcPortName1.Port, - Port: int32(svcPort1), - Protocol: v1.ProtocolTCP, - }, - { - Name: "p443", - Port: int32(443), - Protocol: v1.ProtocolTCP, - }}, + makeTestEndpointSlice(svcPortName1.Namespace, svcPortName1.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIpAddressRemote}, }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName1.Port), + Port: utilpointer.Int32(int32(svcPort1)), + Protocol: &tcpProtocol, + }, + { + Name: utilpointer.StringPtr("p443"), + Port: utilpointer.Int32(int32(443)), + Protocol: &tcpProtocol, + }} })) proxier.mu.Lock() - proxier.endpointsSynced = true + proxier.endpointSlicesSynced = true proxier.mu.Unlock() proxier.setInitialized(true) @@ -650,7 +643,8 @@ func TestSharedRemoteEndpointUpdate(t *testing.T) { } func TestCreateLoadBalancer(t *testing.T) { syncPeriod := 30 * time.Second - proxier := NewFakeProxier(syncPeriod, syncPeriod, clusterCIDR, "testhost", net.ParseIP("10.0.0.1"), NETWORK_TYPE_OVERLAY, false) + tcpProtocol := v1.ProtocolTCP + proxier := NewFakeProxier(syncPeriod, syncPeriod, clusterCIDR, "testhost", net.ParseIP("10.0.0.1"), NETWORK_TYPE_OVERLAY) if proxier == nil { t.Error() } @@ -676,17 +670,16 @@ func TestCreateLoadBalancer(t *testing.T) { }} }), ) - makeEndpointsMap(proxier, - makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIpAddressRemote, - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName.Port, - Port: int32(svcPort), - Protocol: v1.ProtocolTCP, - }}, + populateEndpointSlices(proxier, + makeTestEndpointSlice(svcPortName.Namespace, svcPortName.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIpAddressRemote}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName.Port), + Port: utilpointer.Int32(int32(svcPort)), + Protocol: &tcpProtocol, }} }), ) @@ -709,7 +702,7 @@ func TestCreateLoadBalancer(t *testing.T) { func TestCreateDsrLoadBalancer(t *testing.T) { syncPeriod := 30 * time.Second - proxier := NewFakeProxier(syncPeriod, syncPeriod, clusterCIDR, "testhost", net.ParseIP("10.0.0.1"), NETWORK_TYPE_OVERLAY, false) + proxier := NewFakeProxier(syncPeriod, syncPeriod, clusterCIDR, "testhost", net.ParseIP("10.0.0.1"), NETWORK_TYPE_OVERLAY) if proxier == nil { t.Error() } @@ -736,17 +729,17 @@ func TestCreateDsrLoadBalancer(t *testing.T) { }} }), ) - makeEndpointsMap(proxier, - makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) { - ept.Subsets = []v1.EndpointSubset{{ - Addresses: []v1.EndpointAddress{{ - IP: epIpAddressRemote, - }}, - Ports: []v1.EndpointPort{{ - Name: svcPortName.Port, - Port: int32(svcPort), - Protocol: v1.ProtocolTCP, - }}, + tcpProtocol := v1.ProtocolTCP + populateEndpointSlices(proxier, + makeTestEndpointSlice(svcPortName.Namespace, svcPortName.Name, 1, func(eps *discovery.EndpointSlice) { + eps.AddressType = discovery.AddressTypeIPv4 + eps.Endpoints = []discovery.Endpoint{{ + Addresses: []string{epIpAddressRemote}, + }} + eps.Ports = []discovery.EndpointPort{{ + Name: utilpointer.StringPtr(svcPortName.Port), + Port: utilpointer.Int32(int32(svcPort)), + Protocol: &tcpProtocol, }} }), ) @@ -771,7 +764,7 @@ func TestCreateDsrLoadBalancer(t *testing.T) { func TestEndpointSlice(t *testing.T) { syncPeriod := 30 * time.Second - proxier := NewFakeProxier(syncPeriod, syncPeriod, clusterCIDR, "testhost", net.ParseIP("10.0.0.1"), NETWORK_TYPE_OVERLAY, true) + proxier := NewFakeProxier(syncPeriod, syncPeriod, clusterCIDR, "testhost", net.ParseIP("10.0.0.1"), NETWORK_TYPE_OVERLAY) if proxier == nil { t.Error() } @@ -908,33 +901,30 @@ func makeTestService(namespace, name string, svcFunc func(*v1.Service)) *v1.Serv return svc } -func makeEndpointsMap(proxier *Proxier, allEndpoints ...*v1.Endpoints) { - for i := range allEndpoints { - proxier.OnEndpointsAdd(allEndpoints[i]) +func deleteEndpointSlices(proxier *Proxier, allEndpointSlices ...*discovery.EndpointSlice) { + for i := range allEndpointSlices { + proxier.OnEndpointSliceDelete(allEndpointSlices[i]) } proxier.mu.Lock() defer proxier.mu.Unlock() - proxier.endpointsSynced = true + proxier.endpointSlicesSynced = true } -func deleteEndpoints(proxier *Proxier, allEndpoints ...*v1.Endpoints) { - for i := range allEndpoints { - proxier.OnEndpointsDelete(allEndpoints[i]) +func populateEndpointSlices(proxier *Proxier, allEndpointSlices ...*discovery.EndpointSlice) { + for i := range allEndpointSlices { + proxier.OnEndpointSliceAdd(allEndpointSlices[i]) } - - proxier.mu.Lock() - defer proxier.mu.Unlock() - proxier.endpointsSynced = true } -func makeTestEndpoints(namespace, name string, eptFunc func(*v1.Endpoints)) *v1.Endpoints { - ept := &v1.Endpoints{ +func makeTestEndpointSlice(namespace, name string, sliceNum int, epsFunc func(*discovery.EndpointSlice)) *discovery.EndpointSlice { + eps := &discovery.EndpointSlice{ ObjectMeta: metav1.ObjectMeta{ - Name: name, + Name: fmt.Sprintf("%s-%d", name, sliceNum), Namespace: namespace, + Labels: map[string]string{discovery.LabelServiceName: name}, }, } - eptFunc(ept) - return ept + epsFunc(eps) + return eps }