pkg/proxy: using generic sets
pkg/proxy: using generic sets Signed-off-by: Daman <aroradaman@gmail.com>
This commit is contained in:
@@ -45,11 +45,11 @@ func (proxier *FakeProxier) deleteEndpointSlice(slice *discovery.EndpointSlice)
|
||||
func TestGetLocalEndpointIPs(t *testing.T) {
|
||||
testCases := []struct {
|
||||
endpointsMap EndpointsMap
|
||||
expected map[types.NamespacedName]sets.String
|
||||
expected map[types.NamespacedName]sets.Set[string]
|
||||
}{{
|
||||
// Case[0]: nothing
|
||||
endpointsMap: EndpointsMap{},
|
||||
expected: map[types.NamespacedName]sets.String{},
|
||||
expected: map[types.NamespacedName]sets.Set[string]{},
|
||||
}, {
|
||||
// Case[1]: unnamed port
|
||||
endpointsMap: EndpointsMap{
|
||||
@@ -57,7 +57,7 @@ func TestGetLocalEndpointIPs(t *testing.T) {
|
||||
&BaseEndpointInfo{Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false},
|
||||
},
|
||||
},
|
||||
expected: map[types.NamespacedName]sets.String{},
|
||||
expected: map[types.NamespacedName]sets.Set[string]{},
|
||||
}, {
|
||||
// Case[2]: unnamed port local
|
||||
endpointsMap: EndpointsMap{
|
||||
@@ -65,8 +65,8 @@ func TestGetLocalEndpointIPs(t *testing.T) {
|
||||
&BaseEndpointInfo{Endpoint: "1.1.1.1:11", IsLocal: true, Ready: true, Serving: true, Terminating: false},
|
||||
},
|
||||
},
|
||||
expected: map[types.NamespacedName]sets.String{
|
||||
{Namespace: "ns1", Name: "ep1"}: sets.NewString("1.1.1.1"),
|
||||
expected: map[types.NamespacedName]sets.Set[string]{
|
||||
{Namespace: "ns1", Name: "ep1"}: sets.New[string]("1.1.1.1"),
|
||||
},
|
||||
}, {
|
||||
// Case[3]: named local and non-local ports for the same IP.
|
||||
@@ -80,8 +80,8 @@ func TestGetLocalEndpointIPs(t *testing.T) {
|
||||
&BaseEndpointInfo{Endpoint: "1.1.1.2:12", IsLocal: true, Ready: true, Serving: true, Terminating: false},
|
||||
},
|
||||
},
|
||||
expected: map[types.NamespacedName]sets.String{
|
||||
{Namespace: "ns1", Name: "ep1"}: sets.NewString("1.1.1.2"),
|
||||
expected: map[types.NamespacedName]sets.Set[string]{
|
||||
{Namespace: "ns1", Name: "ep1"}: sets.New[string]("1.1.1.2"),
|
||||
},
|
||||
}, {
|
||||
// Case[4]: named local and non-local ports for different IPs.
|
||||
@@ -104,9 +104,9 @@ func TestGetLocalEndpointIPs(t *testing.T) {
|
||||
&BaseEndpointInfo{Endpoint: "4.4.4.6:45", IsLocal: true, Ready: true, Serving: true, Terminating: false},
|
||||
},
|
||||
},
|
||||
expected: map[types.NamespacedName]sets.String{
|
||||
{Namespace: "ns2", Name: "ep2"}: sets.NewString("2.2.2.2", "2.2.2.22", "2.2.2.3"),
|
||||
{Namespace: "ns4", Name: "ep4"}: sets.NewString("4.4.4.4", "4.4.4.6"),
|
||||
expected: map[types.NamespacedName]sets.Set[string]{
|
||||
{Namespace: "ns2", Name: "ep2"}: sets.New[string]("2.2.2.2", "2.2.2.22", "2.2.2.3"),
|
||||
{Namespace: "ns4", Name: "ep4"}: sets.New[string]("4.4.4.4", "4.4.4.6"),
|
||||
},
|
||||
}, {
|
||||
// Case[5]: named local and non-local ports for different IPs, some not ready.
|
||||
@@ -129,9 +129,9 @@ func TestGetLocalEndpointIPs(t *testing.T) {
|
||||
&BaseEndpointInfo{Endpoint: "4.4.4.6:45", IsLocal: true, Ready: true, Serving: true, Terminating: false},
|
||||
},
|
||||
},
|
||||
expected: map[types.NamespacedName]sets.String{
|
||||
{Namespace: "ns2", Name: "ep2"}: sets.NewString("2.2.2.2", "2.2.2.22"),
|
||||
{Namespace: "ns4", Name: "ep4"}: sets.NewString("4.4.4.4", "4.4.4.6"),
|
||||
expected: map[types.NamespacedName]sets.Set[string]{
|
||||
{Namespace: "ns2", Name: "ep2"}: sets.New[string]("2.2.2.2", "2.2.2.22"),
|
||||
{Namespace: "ns4", Name: "ep4"}: sets.New[string]("4.4.4.4", "4.4.4.6"),
|
||||
},
|
||||
}, {
|
||||
// Case[6]: all endpoints are terminating,, so getLocalReadyEndpointIPs should return 0 ready endpoints
|
||||
@@ -154,7 +154,7 @@ func TestGetLocalEndpointIPs(t *testing.T) {
|
||||
&BaseEndpointInfo{Endpoint: "4.4.4.6:45", IsLocal: true, Ready: false, Serving: true, Terminating: true},
|
||||
},
|
||||
},
|
||||
expected: make(map[types.NamespacedName]sets.String, 0),
|
||||
expected: make(map[types.NamespacedName]sets.Set[string], 0),
|
||||
}}
|
||||
|
||||
for tci, tc := range testCases {
|
||||
@@ -506,7 +506,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
|
||||
expectedDeletedUDPEndpoints []ServiceEndpoint
|
||||
expectedNewlyActiveUDPServices map[ServicePortName]bool
|
||||
expectedLocalEndpoints map[types.NamespacedName]int
|
||||
expectedChangedEndpoints sets.String
|
||||
expectedChangedEndpoints sets.Set[string]
|
||||
}{{
|
||||
name: "empty",
|
||||
oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{},
|
||||
@@ -514,7 +514,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
|
||||
expectedDeletedUDPEndpoints: []ServiceEndpoint{},
|
||||
expectedNewlyActiveUDPServices: map[ServicePortName]bool{},
|
||||
expectedLocalEndpoints: map[types.NamespacedName]int{},
|
||||
expectedChangedEndpoints: sets.NewString(),
|
||||
expectedChangedEndpoints: sets.New[string](),
|
||||
}, {
|
||||
name: "no change, unnamed port",
|
||||
previousEndpoints: []*discovery.EndpointSlice{
|
||||
@@ -536,7 +536,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
|
||||
expectedDeletedUDPEndpoints: []ServiceEndpoint{},
|
||||
expectedNewlyActiveUDPServices: map[ServicePortName]bool{},
|
||||
expectedLocalEndpoints: map[types.NamespacedName]int{},
|
||||
expectedChangedEndpoints: sets.NewString(),
|
||||
expectedChangedEndpoints: sets.New[string](),
|
||||
}, {
|
||||
name: "no change, named port, local",
|
||||
previousEndpoints: []*discovery.EndpointSlice{
|
||||
@@ -560,7 +560,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
|
||||
expectedLocalEndpoints: map[types.NamespacedName]int{
|
||||
makeNSN("ns1", "ep1"): 1,
|
||||
},
|
||||
expectedChangedEndpoints: sets.NewString(),
|
||||
expectedChangedEndpoints: sets.New[string](),
|
||||
}, {
|
||||
name: "no change, multiple slices",
|
||||
previousEndpoints: []*discovery.EndpointSlice{
|
||||
@@ -590,7 +590,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
|
||||
expectedDeletedUDPEndpoints: []ServiceEndpoint{},
|
||||
expectedNewlyActiveUDPServices: map[ServicePortName]bool{},
|
||||
expectedLocalEndpoints: map[types.NamespacedName]int{},
|
||||
expectedChangedEndpoints: sets.NewString(),
|
||||
expectedChangedEndpoints: sets.New[string](),
|
||||
}, {
|
||||
name: "no change, multiple slices, multiple ports, local",
|
||||
previousEndpoints: []*discovery.EndpointSlice{
|
||||
@@ -628,7 +628,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
|
||||
expectedLocalEndpoints: map[types.NamespacedName]int{
|
||||
makeNSN("ns1", "ep1"): 1,
|
||||
},
|
||||
expectedChangedEndpoints: sets.NewString(),
|
||||
expectedChangedEndpoints: sets.New[string](),
|
||||
}, {
|
||||
name: "no change, multiple services, slices, IPs, and ports",
|
||||
previousEndpoints: []*discovery.EndpointSlice{
|
||||
@@ -699,7 +699,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
|
||||
makeNSN("ns1", "ep1"): 2,
|
||||
makeNSN("ns2", "ep2"): 1,
|
||||
},
|
||||
expectedChangedEndpoints: sets.NewString(),
|
||||
expectedChangedEndpoints: sets.New[string](),
|
||||
}, {
|
||||
name: "add an EndpointSlice",
|
||||
previousEndpoints: []*discovery.EndpointSlice{
|
||||
@@ -721,7 +721,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
|
||||
expectedLocalEndpoints: map[types.NamespacedName]int{
|
||||
makeNSN("ns1", "ep1"): 1,
|
||||
},
|
||||
expectedChangedEndpoints: sets.NewString("ns1/ep1"),
|
||||
expectedChangedEndpoints: sets.New[string]("ns1/ep1"),
|
||||
}, {
|
||||
name: "remove an EndpointSlice",
|
||||
previousEndpoints: []*discovery.EndpointSlice{
|
||||
@@ -742,7 +742,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
|
||||
}},
|
||||
expectedNewlyActiveUDPServices: map[ServicePortName]bool{},
|
||||
expectedLocalEndpoints: map[types.NamespacedName]int{},
|
||||
expectedChangedEndpoints: sets.NewString("ns1/ep1"),
|
||||
expectedChangedEndpoints: sets.New[string]("ns1/ep1"),
|
||||
}, {
|
||||
name: "add an IP and port",
|
||||
previousEndpoints: []*discovery.EndpointSlice{
|
||||
@@ -773,7 +773,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
|
||||
expectedLocalEndpoints: map[types.NamespacedName]int{
|
||||
makeNSN("ns1", "ep1"): 1,
|
||||
},
|
||||
expectedChangedEndpoints: sets.NewString("ns1/ep1"),
|
||||
expectedChangedEndpoints: sets.New[string]("ns1/ep1"),
|
||||
}, {
|
||||
name: "remove an IP and port",
|
||||
previousEndpoints: []*discovery.EndpointSlice{
|
||||
@@ -809,7 +809,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
|
||||
}},
|
||||
expectedNewlyActiveUDPServices: map[ServicePortName]bool{},
|
||||
expectedLocalEndpoints: map[types.NamespacedName]int{},
|
||||
expectedChangedEndpoints: sets.NewString("ns1/ep1"),
|
||||
expectedChangedEndpoints: sets.New[string]("ns1/ep1"),
|
||||
}, {
|
||||
name: "add a slice to an endpoint",
|
||||
previousEndpoints: []*discovery.EndpointSlice{
|
||||
@@ -840,7 +840,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
|
||||
expectedLocalEndpoints: map[types.NamespacedName]int{
|
||||
makeNSN("ns1", "ep1"): 1,
|
||||
},
|
||||
expectedChangedEndpoints: sets.NewString("ns1/ep1"),
|
||||
expectedChangedEndpoints: sets.New[string]("ns1/ep1"),
|
||||
}, {
|
||||
name: "remove a slice from an endpoint",
|
||||
previousEndpoints: []*discovery.EndpointSlice{
|
||||
@@ -870,7 +870,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
|
||||
}},
|
||||
expectedNewlyActiveUDPServices: map[ServicePortName]bool{},
|
||||
expectedLocalEndpoints: map[types.NamespacedName]int{},
|
||||
expectedChangedEndpoints: sets.NewString("ns1/ep1"),
|
||||
expectedChangedEndpoints: sets.New[string]("ns1/ep1"),
|
||||
}, {
|
||||
name: "rename a port",
|
||||
previousEndpoints: []*discovery.EndpointSlice{
|
||||
@@ -897,7 +897,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
|
||||
makeServicePortName("ns1", "ep1", "p11-2", v1.ProtocolUDP): true,
|
||||
},
|
||||
expectedLocalEndpoints: map[types.NamespacedName]int{},
|
||||
expectedChangedEndpoints: sets.NewString("ns1/ep1"),
|
||||
expectedChangedEndpoints: sets.New[string]("ns1/ep1"),
|
||||
}, {
|
||||
name: "renumber a port",
|
||||
previousEndpoints: []*discovery.EndpointSlice{
|
||||
@@ -922,7 +922,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
|
||||
}},
|
||||
expectedNewlyActiveUDPServices: map[ServicePortName]bool{},
|
||||
expectedLocalEndpoints: map[types.NamespacedName]int{},
|
||||
expectedChangedEndpoints: sets.NewString("ns1/ep1"),
|
||||
expectedChangedEndpoints: sets.New[string]("ns1/ep1"),
|
||||
}, {
|
||||
name: "complex add and remove",
|
||||
previousEndpoints: []*discovery.EndpointSlice{
|
||||
@@ -1012,7 +1012,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
|
||||
expectedLocalEndpoints: map[types.NamespacedName]int{
|
||||
makeNSN("ns4", "ep4"): 1,
|
||||
},
|
||||
expectedChangedEndpoints: sets.NewString("ns1/ep1", "ns2/ep2", "ns3/ep3", "ns4/ep4"),
|
||||
expectedChangedEndpoints: sets.New[string]("ns1/ep1", "ns2/ep2", "ns3/ep3", "ns4/ep4"),
|
||||
}, {
|
||||
name: "change from 0 endpoint address to 1 unnamed port",
|
||||
previousEndpoints: []*discovery.EndpointSlice{
|
||||
@@ -1032,7 +1032,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
|
||||
makeServicePortName("ns1", "ep1", "", v1.ProtocolUDP): true,
|
||||
},
|
||||
expectedLocalEndpoints: map[types.NamespacedName]int{},
|
||||
expectedChangedEndpoints: sets.NewString("ns1/ep1"),
|
||||
expectedChangedEndpoints: sets.New[string]("ns1/ep1"),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1073,7 +1073,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
|
||||
|
||||
pendingChanges := fp.endpointsChanges.PendingChanges()
|
||||
if !pendingChanges.Equal(tc.expectedChangedEndpoints) {
|
||||
t.Errorf("[%d] expected changed endpoints %q, got %q", tci, tc.expectedChangedEndpoints.List(), pendingChanges.List())
|
||||
t.Errorf("[%d] expected changed endpoints %q, got %q", tci, tc.expectedChangedEndpoints.UnsortedList(), pendingChanges.UnsortedList())
|
||||
}
|
||||
|
||||
result := fp.endpointsMap.Update(fp.endpointsChanges)
|
||||
@@ -1273,7 +1273,7 @@ func TestEndpointSliceUpdate(t *testing.T) {
|
||||
paramRemoveSlice bool
|
||||
expectedReturnVal bool
|
||||
expectedCurrentChange map[ServicePortName][]*BaseEndpointInfo
|
||||
expectedChangedEndpoints sets.String
|
||||
expectedChangedEndpoints sets.Set[string]
|
||||
}{
|
||||
// test starting from an empty state
|
||||
"add a simple slice that doesn't already exist": {
|
||||
@@ -1295,7 +1295,7 @@ func TestEndpointSliceUpdate(t *testing.T) {
|
||||
&BaseEndpointInfo{Endpoint: "10.0.1.3:443", IsLocal: false, Ready: true, Serving: true, Terminating: false},
|
||||
},
|
||||
},
|
||||
expectedChangedEndpoints: sets.NewString("ns1/svc1"),
|
||||
expectedChangedEndpoints: sets.New[string]("ns1/svc1"),
|
||||
},
|
||||
// test no modification to state - current change should be nil as nothing changes
|
||||
"add the same slice that already exists": {
|
||||
@@ -1308,7 +1308,7 @@ func TestEndpointSliceUpdate(t *testing.T) {
|
||||
paramRemoveSlice: false,
|
||||
expectedReturnVal: false,
|
||||
expectedCurrentChange: nil,
|
||||
expectedChangedEndpoints: sets.NewString(),
|
||||
expectedChangedEndpoints: sets.New[string](),
|
||||
},
|
||||
// ensure that only valide address types are processed
|
||||
"add an FQDN slice (invalid address type)": {
|
||||
@@ -1321,7 +1321,7 @@ func TestEndpointSliceUpdate(t *testing.T) {
|
||||
paramRemoveSlice: false,
|
||||
expectedReturnVal: false,
|
||||
expectedCurrentChange: nil,
|
||||
expectedChangedEndpoints: sets.NewString(),
|
||||
expectedChangedEndpoints: sets.New[string](),
|
||||
},
|
||||
// test additions to existing state
|
||||
"add a slice that overlaps with existing state": {
|
||||
@@ -1354,7 +1354,7 @@ func TestEndpointSliceUpdate(t *testing.T) {
|
||||
&BaseEndpointInfo{Endpoint: "10.0.2.2:443", IsLocal: true, Ready: true, Serving: true, Terminating: false},
|
||||
},
|
||||
},
|
||||
expectedChangedEndpoints: sets.NewString("ns1/svc1"),
|
||||
expectedChangedEndpoints: sets.New[string]("ns1/svc1"),
|
||||
},
|
||||
// test additions to existing state with partially overlapping slices and ports
|
||||
"add a slice that overlaps with existing state and partial ports": {
|
||||
@@ -1385,7 +1385,7 @@ func TestEndpointSliceUpdate(t *testing.T) {
|
||||
&BaseEndpointInfo{Endpoint: "10.0.2.2:443", IsLocal: true, Ready: true, Serving: true, Terminating: false},
|
||||
},
|
||||
},
|
||||
expectedChangedEndpoints: sets.NewString("ns1/svc1"),
|
||||
expectedChangedEndpoints: sets.New[string]("ns1/svc1"),
|
||||
},
|
||||
// test deletions from existing state with partially overlapping slices and ports
|
||||
"remove a slice that overlaps with existing state": {
|
||||
@@ -1408,7 +1408,7 @@ func TestEndpointSliceUpdate(t *testing.T) {
|
||||
&BaseEndpointInfo{Endpoint: "10.0.2.2:443", IsLocal: true, Ready: true, Serving: true, Terminating: false},
|
||||
},
|
||||
},
|
||||
expectedChangedEndpoints: sets.NewString("ns1/svc1"),
|
||||
expectedChangedEndpoints: sets.New[string]("ns1/svc1"),
|
||||
},
|
||||
// ensure a removal that has no effect turns into a no-op
|
||||
"remove a slice that doesn't even exist in current state": {
|
||||
@@ -1422,7 +1422,7 @@ func TestEndpointSliceUpdate(t *testing.T) {
|
||||
paramRemoveSlice: true,
|
||||
expectedReturnVal: false,
|
||||
expectedCurrentChange: nil,
|
||||
expectedChangedEndpoints: sets.NewString(),
|
||||
expectedChangedEndpoints: sets.New[string](),
|
||||
},
|
||||
// start with all endpoints ready, transition to no endpoints ready
|
||||
"transition all endpoints to unready state": {
|
||||
@@ -1446,7 +1446,7 @@ func TestEndpointSliceUpdate(t *testing.T) {
|
||||
&BaseEndpointInfo{Endpoint: "10.0.1.3:443", IsLocal: true, Ready: false, Serving: false, Terminating: false},
|
||||
},
|
||||
},
|
||||
expectedChangedEndpoints: sets.NewString("ns1/svc1"),
|
||||
expectedChangedEndpoints: sets.New[string]("ns1/svc1"),
|
||||
},
|
||||
// start with no endpoints ready, transition to all endpoints ready
|
||||
"transition all endpoints to ready state": {
|
||||
@@ -1468,7 +1468,7 @@ func TestEndpointSliceUpdate(t *testing.T) {
|
||||
&BaseEndpointInfo{Endpoint: "10.0.1.2:443", IsLocal: true, Ready: true, Serving: true, Terminating: false},
|
||||
},
|
||||
},
|
||||
expectedChangedEndpoints: sets.NewString("ns1/svc1"),
|
||||
expectedChangedEndpoints: sets.New[string]("ns1/svc1"),
|
||||
},
|
||||
// start with some endpoints ready, transition to more endpoints ready
|
||||
"transition some endpoints to ready state": {
|
||||
@@ -1497,7 +1497,7 @@ func TestEndpointSliceUpdate(t *testing.T) {
|
||||
&BaseEndpointInfo{Endpoint: "10.0.2.2:443", IsLocal: true, Ready: false, Serving: false, Terminating: false},
|
||||
},
|
||||
},
|
||||
expectedChangedEndpoints: sets.NewString("ns1/svc1"),
|
||||
expectedChangedEndpoints: sets.New[string]("ns1/svc1"),
|
||||
},
|
||||
// start with some endpoints ready, transition to some terminating
|
||||
"transition some endpoints to terminating state": {
|
||||
@@ -1526,7 +1526,7 @@ func TestEndpointSliceUpdate(t *testing.T) {
|
||||
&BaseEndpointInfo{Endpoint: "10.0.2.2:443", IsLocal: true, Ready: false, Serving: false, Terminating: true},
|
||||
},
|
||||
},
|
||||
expectedChangedEndpoints: sets.NewString("ns1/svc1"),
|
||||
expectedChangedEndpoints: sets.New[string]("ns1/svc1"),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1541,7 +1541,7 @@ func TestEndpointSliceUpdate(t *testing.T) {
|
||||
|
||||
pendingChanges := tc.endpointChangeTracker.PendingChanges()
|
||||
if !pendingChanges.Equal(tc.expectedChangedEndpoints) {
|
||||
t.Errorf("expected changed endpoints %q, got %q", tc.expectedChangedEndpoints.List(), pendingChanges.List())
|
||||
t.Errorf("expected changed endpoints %q, got %q", tc.expectedChangedEndpoints.UnsortedList(), pendingChanges.UnsortedList())
|
||||
}
|
||||
|
||||
changes := tc.endpointChangeTracker.checkoutChanges()
|
||||
|
Reference in New Issue
Block a user