From a7333e1a3e9cf817e10789d4565636746c513420 Mon Sep 17 00:00:00 2001 From: Andrew Sy Kim Date: Mon, 9 Nov 2020 10:24:43 -0500 Subject: [PATCH] kube-proxy: add endpointslice cache unit tests for terminating endpoints Signed-off-by: Andrew Sy Kim --- pkg/proxy/endpoints_test.go | 32 ++++++++++++++++++++++++++-- pkg/proxy/endpointslicecache_test.go | 22 +++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/pkg/proxy/endpoints_test.go b/pkg/proxy/endpoints_test.go index b6cd025399d..469521e8514 100644 --- a/pkg/proxy/endpoints_test.go +++ b/pkg/proxy/endpoints_test.go @@ -1710,6 +1710,34 @@ func TestEndpointSliceUpdate(t *testing.T) { }, }, }, + // start with some endpoints ready, transition to some terminating + "transition some endpoints to terminating state": { + startingSlices: []*discovery.EndpointSlice{ + 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), + 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, + expectedReturnVal: true, + expectedCurrentChange: map[ServicePortName][]*BaseEndpointInfo{ + makeServicePortName("ns1", "svc1", "port-0", v1.ProtocolTCP): { + &BaseEndpointInfo{Endpoint: "10.0.1.1:80", IsLocal: true, Ready: true, Serving: true, Terminating: false}, + &BaseEndpointInfo{Endpoint: "10.0.1.2:80", IsLocal: true, Ready: false, Serving: true, Terminating: true}, + &BaseEndpointInfo{Endpoint: "10.0.1.3:80", IsLocal: true, Ready: false, Serving: false, Terminating: false}, + &BaseEndpointInfo{Endpoint: "10.0.2.1:80", IsLocal: true, Ready: true, Serving: true, Terminating: false}, + &BaseEndpointInfo{Endpoint: "10.0.2.2:80", IsLocal: true, Ready: false, Serving: false, Terminating: true}, + }, + makeServicePortName("ns1", "svc1", "port-1", v1.ProtocolTCP): { + &BaseEndpointInfo{Endpoint: "10.0.1.1:443", IsLocal: true, Ready: true, Serving: true, Terminating: false}, + &BaseEndpointInfo{Endpoint: "10.0.1.2:443", IsLocal: true, Ready: false, Serving: true, Terminating: true}, + &BaseEndpointInfo{Endpoint: "10.0.1.3:443", IsLocal: true, Ready: false, Serving: false, Terminating: false}, + &BaseEndpointInfo{Endpoint: "10.0.2.1:443", IsLocal: true, Ready: true, Serving: true, Terminating: false}, + &BaseEndpointInfo{Endpoint: "10.0.2.2:443", IsLocal: true, Ready: false, Serving: false, Terminating: true}, + }, + }, + }, } for name, tc := range testCases { @@ -1792,13 +1820,13 @@ func TestCheckoutChanges(t *testing.T) { 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", true, true, false), newTestEp("10.0.1.3:80", "host1", false, false, false)}, + 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{}, pendingSlices: []*discovery.EndpointSlice{ - generateEndpointSlice("svc1", "ns1", 1, 3, 3, 999, []string{"host1"}, []*int32{utilpointer.Int32Ptr(80)}), + generateEndpointSlice("svc1", "ns1", 1, 3, 3, 2, []string{"host1"}, []*int32{utilpointer.Int32Ptr(80)}), }, }, "removing port in update": { diff --git a/pkg/proxy/endpointslicecache_test.go b/pkg/proxy/endpointslicecache_test.go index fbe246bcfcf..256c9878e5a 100644 --- a/pkg/proxy/endpointslicecache_test.go +++ b/pkg/proxy/endpointslicecache_test.go @@ -114,6 +114,28 @@ func TestEndpointsMapFromESC(t *testing.T) { }, }, }, + // 2 slices with all endpoints overlapping, some unready and terminating. + "2 slices, overlapping endpoints, some endpoints unready and some endpoints terminating": { + namespacedName: types.NamespacedName{Name: "svc1", Namespace: "ns1"}, + endpointSlices: []*discovery.EndpointSlice{ + generateEndpointSlice("svc1", "ns1", 1, 10, 3, 5, []string{}, []*int32{utilpointer.Int32Ptr(80)}), + generateEndpointSlice("svc1", "ns1", 1, 10, 6, 5, []string{}, []*int32{utilpointer.Int32Ptr(80)}), + }, + expectedMap: map[ServicePortName][]*BaseEndpointInfo{ + makeServicePortName("ns1", "svc1", "port-0", v1.ProtocolTCP): { + &BaseEndpointInfo{Endpoint: "10.0.1.10:80", IsLocal: false, Ready: false, Serving: true, Terminating: true}, + &BaseEndpointInfo{Endpoint: "10.0.1.1:80", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + &BaseEndpointInfo{Endpoint: "10.0.1.2:80", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + &BaseEndpointInfo{Endpoint: "10.0.1.3:80", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + &BaseEndpointInfo{Endpoint: "10.0.1.4:80", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + &BaseEndpointInfo{Endpoint: "10.0.1.5:80", IsLocal: false, Ready: false, Serving: true, Terminating: true}, + &BaseEndpointInfo{Endpoint: "10.0.1.6:80", IsLocal: false, Ready: false, Serving: false, Terminating: false}, + &BaseEndpointInfo{Endpoint: "10.0.1.7:80", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + &BaseEndpointInfo{Endpoint: "10.0.1.8:80", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + &BaseEndpointInfo{Endpoint: "10.0.1.9:80", IsLocal: false, Ready: true, Serving: true, Terminating: false}, + }, + }, + }, "2 slices, overlapping endpoints, all unready": { namespacedName: types.NamespacedName{Name: "svc1", Namespace: "ns1"}, endpointSlices: []*discovery.EndpointSlice{