Port TestUpdateEndpointsMap from Endpoints tracker to EndpointSlice

This is an ugly-but-simple rewrite (particularly involving having to
rewrite "single Endpoints with multiple Subsets" as "multiple
EndpointSlices"). Can be cleaned up more later...

The slice code sorts the results slightly differently from the old
code in two cases, and it was simpler to just reorder the expectations
rather than fixing the comparison code. But other than that, the
expected results are exactly the same as before.
This commit is contained in:
Dan Winship
2022-12-31 12:00:42 -05:00
parent cc1847e6ee
commit c78b057d85
2 changed files with 390 additions and 397 deletions

View File

@@ -17,6 +17,7 @@ limitations under the License.
package proxy package proxy
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
"time" "time"
@@ -29,18 +30,6 @@ import (
"k8s.io/utils/pointer" "k8s.io/utils/pointer"
) )
func (proxier *FakeProxier) addEndpoints(endpoints *v1.Endpoints) {
proxier.endpointsChanges.Update(nil, endpoints)
}
func (proxier *FakeProxier) updateEndpoints(oldEndpoints, endpoints *v1.Endpoints) {
proxier.endpointsChanges.Update(oldEndpoints, endpoints)
}
func (proxier *FakeProxier) deleteEndpoints(endpoints *v1.Endpoints) {
proxier.endpointsChanges.Update(endpoints, nil)
}
func (proxier *FakeProxier) addEndpointSlice(slice *discovery.EndpointSlice) { func (proxier *FakeProxier) addEndpointSlice(slice *discovery.EndpointSlice) {
proxier.endpointsChanges.EndpointSliceUpdate(slice, false) proxier.endpointsChanges.EndpointSliceUpdate(slice, false)
} }
@@ -178,362 +167,340 @@ func TestGetLocalEndpointIPs(t *testing.T) {
} }
} }
func makeTestEndpoints(namespace, name string, eptFunc func(*v1.Endpoints)) *v1.Endpoints { func makeTestEndpointSlice(namespace, name string, slice int, epsFunc func(*discovery.EndpointSlice)) *discovery.EndpointSlice {
ept := &v1.Endpoints{ eps := &discovery.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: name, Name: fmt.Sprintf("%s-%d", name, slice),
Namespace: namespace, Namespace: namespace,
Annotations: make(map[string]string), Annotations: map[string]string{},
Labels: map[string]string{
discovery.LabelServiceName: name,
},
}, },
AddressType: discovery.AddressTypeIPv4,
} }
eptFunc(ept) epsFunc(eps)
return ept return eps
} }
func TestUpdateEndpointsMap(t *testing.T) { func TestUpdateEndpointsMap(t *testing.T) {
var nodeName = testHostname var nodeName = testHostname
udp := v1.ProtocolUDP
emptyEndpoint := func(ept *v1.Endpoints) { emptyEndpoint := func(eps *discovery.EndpointSlice) {
ept.Subsets = []v1.EndpointSubset{} eps.Endpoints = []discovery.Endpoint{}
} }
unnamedPort := func(ept *v1.Endpoints) { unnamedPort := func(eps *discovery.EndpointSlice) {
ept.Subsets = []v1.EndpointSubset{{ eps.Endpoints = []discovery.Endpoint{{
Addresses: []v1.EndpointAddress{{ Addresses: []string{"1.1.1.1"},
IP: "1.1.1.1", }}
}}, eps.Ports = []discovery.EndpointPort{{
Ports: []v1.EndpointPort{{ Name: pointer.String(""),
Port: 11, Port: pointer.Int32(11),
Protocol: v1.ProtocolUDP, Protocol: &udp,
}},
}} }}
} }
unnamedPortLocal := func(ept *v1.Endpoints) { unnamedPortLocal := func(eps *discovery.EndpointSlice) {
ept.Subsets = []v1.EndpointSubset{{ eps.Endpoints = []discovery.Endpoint{{
Addresses: []v1.EndpointAddress{{ Addresses: []string{"1.1.1.1"},
IP: "1.1.1.1", NodeName: &nodeName,
NodeName: &nodeName, }}
}}, eps.Ports = []discovery.EndpointPort{{
Ports: []v1.EndpointPort{{ Name: pointer.String(""),
Port: 11, Port: pointer.Int32(11),
Protocol: v1.ProtocolUDP, Protocol: &udp,
}},
}} }}
} }
namedPortLocal := func(ept *v1.Endpoints) { namedPortLocal := func(eps *discovery.EndpointSlice) {
ept.Subsets = []v1.EndpointSubset{{ eps.Endpoints = []discovery.Endpoint{{
Addresses: []v1.EndpointAddress{{ Addresses: []string{"1.1.1.1"},
IP: "1.1.1.1", NodeName: &nodeName,
NodeName: &nodeName, }}
}}, eps.Ports = []discovery.EndpointPort{{
Ports: []v1.EndpointPort{{ Name: pointer.String("p11"),
Name: "p11", Port: pointer.Int32(11),
Port: 11, Protocol: &udp,
Protocol: v1.ProtocolUDP,
}},
}} }}
} }
namedPort := func(ept *v1.Endpoints) { namedPort := func(eps *discovery.EndpointSlice) {
ept.Subsets = []v1.EndpointSubset{{ eps.Endpoints = []discovery.Endpoint{{
Addresses: []v1.EndpointAddress{{ Addresses: []string{"1.1.1.1"},
IP: "1.1.1.1", }}
}}, eps.Ports = []discovery.EndpointPort{{
Ports: []v1.EndpointPort{{ Name: pointer.String("p11"),
Name: "p11", Port: pointer.Int32(11),
Port: 11, Protocol: &udp,
Protocol: v1.ProtocolUDP,
}},
}} }}
} }
namedPortRenamed := func(ept *v1.Endpoints) { namedPortRenamed := func(eps *discovery.EndpointSlice) {
ept.Subsets = []v1.EndpointSubset{{ eps.Endpoints = []discovery.Endpoint{{
Addresses: []v1.EndpointAddress{{ Addresses: []string{"1.1.1.1"},
IP: "1.1.1.1", }}
}}, eps.Ports = []discovery.EndpointPort{{
Ports: []v1.EndpointPort{{ Name: pointer.String("p11-2"),
Name: "p11-2", Port: pointer.Int32(11),
Port: 11, Protocol: &udp,
Protocol: v1.ProtocolUDP,
}},
}} }}
} }
namedPortRenumbered := func(ept *v1.Endpoints) { namedPortRenumbered := func(eps *discovery.EndpointSlice) {
ept.Subsets = []v1.EndpointSubset{{ eps.Endpoints = []discovery.Endpoint{{
Addresses: []v1.EndpointAddress{{ Addresses: []string{"1.1.1.1"},
IP: "1.1.1.1", }}
}}, eps.Ports = []discovery.EndpointPort{{
Ports: []v1.EndpointPort{{ Name: pointer.String("p11"),
Name: "p11", Port: pointer.Int32(22),
Port: 22, Protocol: &udp,
Protocol: v1.ProtocolUDP,
}},
}} }}
} }
namedPortsLocalNoLocal := func(ept *v1.Endpoints) { namedPortsLocalNoLocal := func(eps *discovery.EndpointSlice) {
ept.Subsets = []v1.EndpointSubset{{ eps.Endpoints = []discovery.Endpoint{{
Addresses: []v1.EndpointAddress{{ Addresses: []string{"1.1.1.1"},
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,
}},
}, { }, {
Addresses: []v1.EndpointAddress{{ Addresses: []string{"1.1.1.2"},
IP: "1.1.1.2", NodeName: &nodeName,
}},
Ports: []v1.EndpointPort{{
Name: "p12",
Port: 12,
Protocol: v1.ProtocolUDP,
}},
}} }}
} eps.Ports = []discovery.EndpointPort{{
multipleSubsetsWithLocal := func(ept *v1.Endpoints) { Name: pointer.String("p11"),
ept.Subsets = []v1.EndpointSubset{{ Port: pointer.Int32(11),
Addresses: []v1.EndpointAddress{{ Protocol: &udp,
IP: "1.1.1.1",
}},
Ports: []v1.EndpointPort{{
Name: "p11",
Port: 11,
Protocol: v1.ProtocolUDP,
}},
}, { }, {
Addresses: []v1.EndpointAddress{{ Name: pointer.String("p12"),
IP: "1.1.1.2", Port: pointer.Int32(12),
NodeName: &nodeName, Protocol: &udp,
}},
Ports: []v1.EndpointPort{{
Name: "p12",
Port: 12,
Protocol: v1.ProtocolUDP,
}},
}} }}
} }
multipleSubsetsMultiplePortsLocal := func(ept *v1.Endpoints) { multipleSubsets_s1 := func(eps *discovery.EndpointSlice) {
ept.Subsets = []v1.EndpointSubset{{ eps.Endpoints = []discovery.Endpoint{{
Addresses: []v1.EndpointAddress{{ Addresses: []string{"1.1.1.1"},
IP: "1.1.1.1", }}
NodeName: &nodeName, eps.Ports = []discovery.EndpointPort{{
}}, Name: pointer.String("p11"),
Ports: []v1.EndpointPort{{ Port: pointer.Int32(11),
Name: "p11", Protocol: &udp,
Port: 11, }}
Protocol: v1.ProtocolUDP, }
}, { multipleSubsets_s2 := func(eps *discovery.EndpointSlice) {
Name: "p12", eps.Endpoints = []discovery.Endpoint{{
Port: 12, Addresses: []string{"1.1.1.2"},
Protocol: v1.ProtocolUDP, }}
}}, eps.Ports = []discovery.EndpointPort{{
Name: pointer.String("p12"),
Port: pointer.Int32(12),
Protocol: &udp,
}}
}
multipleSubsetsWithLocal_s1 := func(eps *discovery.EndpointSlice) {
eps.Endpoints = []discovery.Endpoint{{
Addresses: []string{"1.1.1.1"},
}}
eps.Ports = []discovery.EndpointPort{{
Name: pointer.String("p11"),
Port: pointer.Int32(11),
Protocol: &udp,
}}
}
multipleSubsetsWithLocal_s2 := func(eps *discovery.EndpointSlice) {
eps.Endpoints = []discovery.Endpoint{{
Addresses: []string{"1.1.1.2"},
NodeName: &nodeName,
}}
eps.Ports = []discovery.EndpointPort{{
Name: pointer.String("p12"),
Port: pointer.Int32(12),
Protocol: &udp,
}}
}
multipleSubsetsMultiplePortsLocal_s1 := func(eps *discovery.EndpointSlice) {
eps.Endpoints = []discovery.Endpoint{{
Addresses: []string{"1.1.1.1"},
NodeName: &nodeName,
}}
eps.Ports = []discovery.EndpointPort{{
Name: pointer.String("p11"),
Port: pointer.Int32(11),
Protocol: &udp,
}, { }, {
Addresses: []v1.EndpointAddress{{ Name: pointer.String("p12"),
IP: "1.1.1.3", Port: pointer.Int32(12),
}}, Protocol: &udp,
Ports: []v1.EndpointPort{{
Name: "p13",
Port: 13,
Protocol: v1.ProtocolUDP,
}},
}} }}
} }
multipleSubsetsIPsPorts1 := func(ept *v1.Endpoints) { multipleSubsetsMultiplePortsLocal_s2 := func(eps *discovery.EndpointSlice) {
ept.Subsets = []v1.EndpointSubset{{ eps.Endpoints = []discovery.Endpoint{{
Addresses: []v1.EndpointAddress{{ Addresses: []string{"1.1.1.3"},
IP: "1.1.1.1", }}
}, { eps.Ports = []discovery.EndpointPort{{
IP: "1.1.1.2", Name: pointer.String("p13"),
NodeName: &nodeName, Port: pointer.Int32(13),
}}, Protocol: &udp,
Ports: []v1.EndpointPort{{ }}
Name: "p11", }
Port: 11, multipleSubsetsIPsPorts1_s1 := func(eps *discovery.EndpointSlice) {
Protocol: v1.ProtocolUDP, eps.Endpoints = []discovery.Endpoint{{
}, { Addresses: []string{"1.1.1.1"},
Name: "p12",
Port: 12,
Protocol: v1.ProtocolUDP,
}},
}, { }, {
Addresses: []v1.EndpointAddress{{ Addresses: []string{"1.1.1.2"},
IP: "1.1.1.3", NodeName: &nodeName,
}, {
IP: "1.1.1.4",
NodeName: &nodeName,
}},
Ports: []v1.EndpointPort{{
Name: "p13",
Port: 13,
Protocol: v1.ProtocolUDP,
}, {
Name: "p14",
Port: 14,
Protocol: v1.ProtocolUDP,
}},
}} }}
} eps.Ports = []discovery.EndpointPort{{
multipleSubsetsIPsPorts2 := func(ept *v1.Endpoints) { Name: pointer.String("p11"),
ept.Subsets = []v1.EndpointSubset{{ Port: pointer.Int32(11),
Addresses: []v1.EndpointAddress{{ Protocol: &udp,
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,
}},
}, { }, {
Addresses: []v1.EndpointAddress{{ Name: pointer.String("p12"),
IP: "2.2.2.3", Port: pointer.Int32(12),
NodeName: &nodeName, Protocol: &udp,
}},
Ports: []v1.EndpointPort{{
Name: "p23",
Port: 23,
Protocol: v1.ProtocolUDP,
}},
}} }}
} }
complexBefore4 := func(ept *v1.Endpoints) { multipleSubsetsIPsPorts1_s2 := func(eps *discovery.EndpointSlice) {
ept.Subsets = []v1.EndpointSubset{{ eps.Endpoints = []discovery.Endpoint{{
Addresses: []v1.EndpointAddress{{ Addresses: []string{"1.1.1.3"},
IP: "4.4.4.4",
NodeName: &nodeName,
}, {
IP: "4.4.4.5",
NodeName: &nodeName,
}},
Ports: []v1.EndpointPort{{
Name: "p44",
Port: 44,
Protocol: v1.ProtocolUDP,
}},
}, { }, {
Addresses: []v1.EndpointAddress{{ Addresses: []string{"1.1.1.4"},
IP: "4.4.4.6", NodeName: &nodeName,
NodeName: &nodeName,
}},
Ports: []v1.EndpointPort{{
Name: "p45",
Port: 45,
Protocol: v1.ProtocolUDP,
}},
}} }}
} eps.Ports = []discovery.EndpointPort{{
complexAfter1 := func(ept *v1.Endpoints) { Name: pointer.String("p13"),
ept.Subsets = []v1.EndpointSubset{{ Port: pointer.Int32(13),
Addresses: []v1.EndpointAddress{{ Protocol: &udp,
IP: "1.1.1.1",
}, {
IP: "1.1.1.11",
}},
Ports: []v1.EndpointPort{{
Name: "p11",
Port: 11,
Protocol: v1.ProtocolUDP,
}},
}, { }, {
Addresses: []v1.EndpointAddress{{ Name: pointer.String("p14"),
IP: "1.1.1.2", Port: pointer.Int32(14),
}}, Protocol: &udp,
Ports: []v1.EndpointPort{{
Name: "p12",
Port: 12,
Protocol: v1.ProtocolUDP,
}, {
Name: "p122",
Port: 122,
Protocol: v1.ProtocolUDP,
}},
}} }}
} }
complexAfter3 := func(ept *v1.Endpoints) { multipleSubsetsIPsPorts2 := func(eps *discovery.EndpointSlice) {
ept.Subsets = []v1.EndpointSubset{{ eps.Endpoints = []discovery.Endpoint{{
Addresses: []v1.EndpointAddress{{ Addresses: []string{"2.2.2.1"},
IP: "3.3.3.3", }, {
}}, Addresses: []string{"2.2.2.2"},
Ports: []v1.EndpointPort{{ NodeName: &nodeName,
Name: "p33", }}
Port: 33, eps.Ports = []discovery.EndpointPort{{
Protocol: v1.ProtocolUDP, Name: pointer.String("p21"),
}}, Port: pointer.Int32(21),
Protocol: &udp,
}, {
Name: pointer.String("p22"),
Port: pointer.Int32(22),
Protocol: &udp,
}} }}
} }
complexAfter4 := func(ept *v1.Endpoints) { complexBefore1 := func(eps *discovery.EndpointSlice) {
ept.Subsets = []v1.EndpointSubset{{ eps.Endpoints = []discovery.Endpoint{{
Addresses: []v1.EndpointAddress{{ Addresses: []string{"1.1.1.1"},
IP: "4.4.4.4", }}
NodeName: &nodeName, eps.Ports = []discovery.EndpointPort{{
}}, Name: pointer.String("p11"),
Ports: []v1.EndpointPort{{ Port: pointer.Int32(11),
Name: "p44", Protocol: &udp,
Port: 44, }}
Protocol: v1.ProtocolUDP, }
}}, complexBefore2_s1 := func(eps *discovery.EndpointSlice) {
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: pointer.String("p22"),
Port: pointer.Int32(22),
Protocol: &udp,
}}
}
complexBefore2_s2 := func(eps *discovery.EndpointSlice) {
eps.Endpoints = []discovery.Endpoint{{
Addresses: []string{"2.2.2.3"},
NodeName: &nodeName,
}}
eps.Ports = []discovery.EndpointPort{{
Name: pointer.String("p23"),
Port: pointer.Int32(23),
Protocol: &udp,
}}
}
complexBefore4_s1 := func(eps *discovery.EndpointSlice) {
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: pointer.String("p44"),
Port: pointer.Int32(44),
Protocol: &udp,
}}
}
complexBefore4_s2 := func(eps *discovery.EndpointSlice) {
eps.Endpoints = []discovery.Endpoint{{
Addresses: []string{"4.4.4.6"},
NodeName: &nodeName,
}}
eps.Ports = []discovery.EndpointPort{{
Name: pointer.String("p45"),
Port: pointer.Int32(45),
Protocol: &udp,
}}
}
complexAfter1_s1 := func(eps *discovery.EndpointSlice) {
eps.Endpoints = []discovery.Endpoint{{
Addresses: []string{"1.1.1.1"},
}, {
Addresses: []string{"1.1.1.11"},
}}
eps.Ports = []discovery.EndpointPort{{
Name: pointer.String("p11"),
Port: pointer.Int32(11),
Protocol: &udp,
}}
}
complexAfter1_s2 := func(eps *discovery.EndpointSlice) {
eps.Endpoints = []discovery.Endpoint{{
Addresses: []string{"1.1.1.2"},
}}
eps.Ports = []discovery.EndpointPort{{
Name: pointer.String("p12"),
Port: pointer.Int32(12),
Protocol: &udp,
}, {
Name: pointer.String("p122"),
Port: pointer.Int32(122),
Protocol: &udp,
}}
}
complexAfter3 := func(eps *discovery.EndpointSlice) {
eps.Endpoints = []discovery.Endpoint{{
Addresses: []string{"3.3.3.3"},
}}
eps.Ports = []discovery.EndpointPort{{
Name: pointer.String("p33"),
Port: pointer.Int32(33),
Protocol: &udp,
}}
}
complexAfter4 := func(eps *discovery.EndpointSlice) {
eps.Endpoints = []discovery.Endpoint{{
Addresses: []string{"4.4.4.4"},
NodeName: &nodeName,
}}
eps.Ports = []discovery.EndpointPort{{
Name: pointer.String("p44"),
Port: pointer.Int32(44),
Protocol: &udp,
}} }}
} }
testCases := []struct { testCases := []struct {
// previousEndpoints and currentEndpoints are used to call appropriate // previousEndpoints and currentEndpoints are used to call appropriate
// handlers OnEndpoints* (based on whether corresponding values are nil // handlers OnEndpointSlice* (based on whether corresponding values are nil
// or non-nil) and must be of equal length. // or non-nil) and must be of equal length.
name string name string
previousEndpoints []*v1.Endpoints previousEndpoints []*discovery.EndpointSlice
currentEndpoints []*v1.Endpoints currentEndpoints []*discovery.EndpointSlice
oldEndpoints map[ServicePortName][]*BaseEndpointInfo oldEndpoints map[ServicePortName][]*BaseEndpointInfo
expectedResult map[ServicePortName][]*BaseEndpointInfo expectedResult map[ServicePortName][]*BaseEndpointInfo
expectedStaleEndpoints []ServiceEndpoint expectedStaleEndpoints []ServiceEndpoint
@@ -550,11 +517,11 @@ func TestUpdateEndpointsMap(t *testing.T) {
expectedChangedEndpoints: sets.NewString(), expectedChangedEndpoints: sets.NewString(),
}, { }, {
name: "no change, unnamed port", name: "no change, unnamed port",
previousEndpoints: []*v1.Endpoints{ previousEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", unnamedPort), makeTestEndpointSlice("ns1", "ep1", 1, unnamedPort),
}, },
currentEndpoints: []*v1.Endpoints{ currentEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", unnamedPort), makeTestEndpointSlice("ns1", "ep1", 1, unnamedPort),
}, },
oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{ oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{
makeServicePortName("ns1", "ep1", "", v1.ProtocolUDP): { makeServicePortName("ns1", "ep1", "", v1.ProtocolUDP): {
@@ -572,11 +539,11 @@ func TestUpdateEndpointsMap(t *testing.T) {
expectedChangedEndpoints: sets.NewString(), expectedChangedEndpoints: sets.NewString(),
}, { }, {
name: "no change, named port, local", name: "no change, named port, local",
previousEndpoints: []*v1.Endpoints{ previousEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", namedPortLocal), makeTestEndpointSlice("ns1", "ep1", 1, namedPortLocal),
}, },
currentEndpoints: []*v1.Endpoints{ currentEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", namedPortLocal), makeTestEndpointSlice("ns1", "ep1", 1, namedPortLocal),
}, },
oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{ oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{
makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): {
@@ -595,12 +562,14 @@ func TestUpdateEndpointsMap(t *testing.T) {
}, },
expectedChangedEndpoints: sets.NewString(), expectedChangedEndpoints: sets.NewString(),
}, { }, {
name: "no change, multiple subsets", name: "no change, multiple slices",
previousEndpoints: []*v1.Endpoints{ previousEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", multipleSubsets), makeTestEndpointSlice("ns1", "ep1", 1, multipleSubsets_s1),
makeTestEndpointSlice("ns1", "ep1", 2, multipleSubsets_s2),
}, },
currentEndpoints: []*v1.Endpoints{ currentEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", multipleSubsets), makeTestEndpointSlice("ns1", "ep1", 1, multipleSubsets_s1),
makeTestEndpointSlice("ns1", "ep1", 2, multipleSubsets_s2),
}, },
oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{ oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{
makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): {
@@ -623,12 +592,14 @@ func TestUpdateEndpointsMap(t *testing.T) {
expectedHealthchecks: map[types.NamespacedName]int{}, expectedHealthchecks: map[types.NamespacedName]int{},
expectedChangedEndpoints: sets.NewString(), expectedChangedEndpoints: sets.NewString(),
}, { }, {
name: "no change, multiple subsets, multiple ports, local", name: "no change, multiple slices, multiple ports, local",
previousEndpoints: []*v1.Endpoints{ previousEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", multipleSubsetsMultiplePortsLocal), makeTestEndpointSlice("ns1", "ep1", 1, multipleSubsetsMultiplePortsLocal_s1),
makeTestEndpointSlice("ns1", "ep1", 2, multipleSubsetsMultiplePortsLocal_s2),
}, },
currentEndpoints: []*v1.Endpoints{ currentEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", multipleSubsetsMultiplePortsLocal), makeTestEndpointSlice("ns1", "ep1", 1, multipleSubsetsMultiplePortsLocal_s1),
makeTestEndpointSlice("ns1", "ep1", 2, multipleSubsetsMultiplePortsLocal_s2),
}, },
oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{ oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{
makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): {
@@ -659,14 +630,16 @@ func TestUpdateEndpointsMap(t *testing.T) {
}, },
expectedChangedEndpoints: sets.NewString(), expectedChangedEndpoints: sets.NewString(),
}, { }, {
name: "no change, multiple endpoints, subsets, IPs, and ports", name: "no change, multiple services, slices, IPs, and ports",
previousEndpoints: []*v1.Endpoints{ previousEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", multipleSubsetsIPsPorts1), makeTestEndpointSlice("ns1", "ep1", 1, multipleSubsetsIPsPorts1_s1),
makeTestEndpoints("ns2", "ep2", multipleSubsetsIPsPorts2), makeTestEndpointSlice("ns1", "ep1", 2, multipleSubsetsIPsPorts1_s2),
makeTestEndpointSlice("ns2", "ep2", 1, multipleSubsetsIPsPorts2),
}, },
currentEndpoints: []*v1.Endpoints{ currentEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", multipleSubsetsIPsPorts1), makeTestEndpointSlice("ns1", "ep1", 1, multipleSubsetsIPsPorts1_s1),
makeTestEndpoints("ns2", "ep2", multipleSubsetsIPsPorts2), makeTestEndpointSlice("ns1", "ep1", 2, multipleSubsetsIPsPorts1_s2),
makeTestEndpointSlice("ns2", "ep2", 1, multipleSubsetsIPsPorts2),
}, },
oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{ oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{
makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): {
@@ -728,12 +701,12 @@ func TestUpdateEndpointsMap(t *testing.T) {
}, },
expectedChangedEndpoints: sets.NewString(), expectedChangedEndpoints: sets.NewString(),
}, { }, {
name: "add an Endpoints", name: "add an EndpointSlice",
previousEndpoints: []*v1.Endpoints{ previousEndpoints: []*discovery.EndpointSlice{
nil, nil,
}, },
currentEndpoints: []*v1.Endpoints{ currentEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", unnamedPortLocal), makeTestEndpointSlice("ns1", "ep1", 1, unnamedPortLocal),
}, },
oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{}, oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{},
expectedResult: map[ServicePortName][]*BaseEndpointInfo{ expectedResult: map[ServicePortName][]*BaseEndpointInfo{
@@ -750,11 +723,11 @@ func TestUpdateEndpointsMap(t *testing.T) {
}, },
expectedChangedEndpoints: sets.NewString("ns1/ep1"), expectedChangedEndpoints: sets.NewString("ns1/ep1"),
}, { }, {
name: "remove an Endpoints", name: "remove an EndpointSlice",
previousEndpoints: []*v1.Endpoints{ previousEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", unnamedPortLocal), makeTestEndpointSlice("ns1", "ep1", 1, unnamedPortLocal),
}, },
currentEndpoints: []*v1.Endpoints{ currentEndpoints: []*discovery.EndpointSlice{
nil, nil,
}, },
oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{ oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{
@@ -772,11 +745,11 @@ func TestUpdateEndpointsMap(t *testing.T) {
expectedChangedEndpoints: sets.NewString("ns1/ep1"), expectedChangedEndpoints: sets.NewString("ns1/ep1"),
}, { }, {
name: "add an IP and port", name: "add an IP and port",
previousEndpoints: []*v1.Endpoints{ previousEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", namedPort), makeTestEndpointSlice("ns1", "ep1", 1, namedPort),
}, },
currentEndpoints: []*v1.Endpoints{ currentEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", namedPortsLocalNoLocal), makeTestEndpointSlice("ns1", "ep1", 1, namedPortsLocalNoLocal),
}, },
oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{ oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{
makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): {
@@ -803,11 +776,11 @@ func TestUpdateEndpointsMap(t *testing.T) {
expectedChangedEndpoints: sets.NewString("ns1/ep1"), expectedChangedEndpoints: sets.NewString("ns1/ep1"),
}, { }, {
name: "remove an IP and port", name: "remove an IP and port",
previousEndpoints: []*v1.Endpoints{ previousEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", namedPortsLocalNoLocal), makeTestEndpointSlice("ns1", "ep1", 1, namedPortsLocalNoLocal),
}, },
currentEndpoints: []*v1.Endpoints{ currentEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", namedPort), makeTestEndpointSlice("ns1", "ep1", 1, namedPort),
}, },
oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{ oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{
makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): {
@@ -838,12 +811,14 @@ func TestUpdateEndpointsMap(t *testing.T) {
expectedHealthchecks: map[types.NamespacedName]int{}, expectedHealthchecks: map[types.NamespacedName]int{},
expectedChangedEndpoints: sets.NewString("ns1/ep1"), expectedChangedEndpoints: sets.NewString("ns1/ep1"),
}, { }, {
name: "add a subset", name: "add a slice to an endpoint",
previousEndpoints: []*v1.Endpoints{ previousEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", namedPort), makeTestEndpointSlice("ns1", "ep1", 1, namedPort),
nil,
}, },
currentEndpoints: []*v1.Endpoints{ currentEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", multipleSubsetsWithLocal), makeTestEndpointSlice("ns1", "ep1", 1, multipleSubsetsWithLocal_s1),
makeTestEndpointSlice("ns1", "ep1", 2, multipleSubsetsWithLocal_s2),
}, },
oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{ oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{
makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): {
@@ -867,12 +842,14 @@ func TestUpdateEndpointsMap(t *testing.T) {
}, },
expectedChangedEndpoints: sets.NewString("ns1/ep1"), expectedChangedEndpoints: sets.NewString("ns1/ep1"),
}, { }, {
name: "remove a subset", name: "remove a slice from an endpoint",
previousEndpoints: []*v1.Endpoints{ previousEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", multipleSubsets), makeTestEndpointSlice("ns1", "ep1", 1, multipleSubsets_s1),
makeTestEndpointSlice("ns1", "ep1", 2, multipleSubsets_s2),
}, },
currentEndpoints: []*v1.Endpoints{ currentEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", namedPort), makeTestEndpointSlice("ns1", "ep1", 1, namedPort),
nil,
}, },
oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{ oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{
makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): {
@@ -896,11 +873,11 @@ func TestUpdateEndpointsMap(t *testing.T) {
expectedChangedEndpoints: sets.NewString("ns1/ep1"), expectedChangedEndpoints: sets.NewString("ns1/ep1"),
}, { }, {
name: "rename a port", name: "rename a port",
previousEndpoints: []*v1.Endpoints{ previousEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", namedPort), makeTestEndpointSlice("ns1", "ep1", 1, namedPort),
}, },
currentEndpoints: []*v1.Endpoints{ currentEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", namedPortRenamed), makeTestEndpointSlice("ns1", "ep1", 1, namedPortRenamed),
}, },
oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{ oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{
makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): {
@@ -923,11 +900,11 @@ func TestUpdateEndpointsMap(t *testing.T) {
expectedChangedEndpoints: sets.NewString("ns1/ep1"), expectedChangedEndpoints: sets.NewString("ns1/ep1"),
}, { }, {
name: "renumber a port", name: "renumber a port",
previousEndpoints: []*v1.Endpoints{ previousEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", namedPort), makeTestEndpointSlice("ns1", "ep1", 1, namedPort),
}, },
currentEndpoints: []*v1.Endpoints{ currentEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", namedPortRenumbered), makeTestEndpointSlice("ns1", "ep1", 1, namedPortRenumbered),
}, },
oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{ oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{
makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): {
@@ -948,25 +925,39 @@ func TestUpdateEndpointsMap(t *testing.T) {
expectedChangedEndpoints: sets.NewString("ns1/ep1"), expectedChangedEndpoints: sets.NewString("ns1/ep1"),
}, { }, {
name: "complex add and remove", name: "complex add and remove",
previousEndpoints: []*v1.Endpoints{ previousEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", complexBefore1), makeTestEndpointSlice("ns1", "ep1", 1, complexBefore1),
makeTestEndpoints("ns2", "ep2", complexBefore2),
nil, nil,
makeTestEndpoints("ns4", "ep4", complexBefore4),
makeTestEndpointSlice("ns2", "ep2", 1, complexBefore2_s1),
makeTestEndpointSlice("ns2", "ep2", 2, complexBefore2_s2),
nil,
nil,
makeTestEndpointSlice("ns4", "ep4", 1, complexBefore4_s1),
makeTestEndpointSlice("ns4", "ep4", 2, complexBefore4_s2),
}, },
currentEndpoints: []*v1.Endpoints{ currentEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", complexAfter1), makeTestEndpointSlice("ns1", "ep1", 1, complexAfter1_s1),
makeTestEndpointSlice("ns1", "ep1", 2, complexAfter1_s2),
nil,
nil,
makeTestEndpointSlice("ns3", "ep3", 1, complexAfter3),
nil,
makeTestEndpointSlice("ns4", "ep4", 1, complexAfter4),
nil, nil,
makeTestEndpoints("ns3", "ep3", complexAfter3),
makeTestEndpoints("ns4", "ep4", complexAfter4),
}, },
oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{ oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{
makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): {
{Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}, {Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false},
}, },
makeServicePortName("ns2", "ep2", "p22", v1.ProtocolUDP): { makeServicePortName("ns2", "ep2", "p22", v1.ProtocolUDP): {
{Endpoint: "2.2.2.2:22", IsLocal: true, Ready: true, Serving: true, Terminating: false},
{Endpoint: "2.2.2.22:22", IsLocal: true, Ready: true, Serving: true, Terminating: false}, {Endpoint: "2.2.2.22:22", IsLocal: true, Ready: true, Serving: true, Terminating: false},
{Endpoint: "2.2.2.2:22", IsLocal: true, Ready: true, Serving: true, Terminating: false},
}, },
makeServicePortName("ns2", "ep2", "p23", v1.ProtocolUDP): { makeServicePortName("ns2", "ep2", "p23", v1.ProtocolUDP): {
{Endpoint: "2.2.2.3:23", IsLocal: true, Ready: true, Serving: true, Terminating: false}, {Endpoint: "2.2.2.3:23", IsLocal: true, Ready: true, Serving: true, Terminating: false},
@@ -981,8 +972,8 @@ func TestUpdateEndpointsMap(t *testing.T) {
}, },
expectedResult: map[ServicePortName][]*BaseEndpointInfo{ expectedResult: map[ServicePortName][]*BaseEndpointInfo{
makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): { makeServicePortName("ns1", "ep1", "p11", v1.ProtocolUDP): {
{Endpoint: "1.1.1.1:11", IsLocal: false, Ready: true, Serving: true, Terminating: false},
{Endpoint: "1.1.1.11:11", IsLocal: false, Ready: true, Serving: true, Terminating: false}, {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): { makeServicePortName("ns1", "ep1", "p12", v1.ProtocolUDP): {
{Endpoint: "1.1.1.2:12", IsLocal: false, Ready: true, Serving: true, Terminating: false}, {Endpoint: "1.1.1.2:12", IsLocal: false, Ready: true, Serving: true, Terminating: false},
@@ -1024,11 +1015,11 @@ func TestUpdateEndpointsMap(t *testing.T) {
expectedChangedEndpoints: sets.NewString("ns1/ep1", "ns2/ep2", "ns3/ep3", "ns4/ep4"), expectedChangedEndpoints: sets.NewString("ns1/ep1", "ns2/ep2", "ns3/ep3", "ns4/ep4"),
}, { }, {
name: "change from 0 endpoint address to 1 unnamed port", name: "change from 0 endpoint address to 1 unnamed port",
previousEndpoints: []*v1.Endpoints{ previousEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", emptyEndpoint), makeTestEndpointSlice("ns1", "ep1", 1, emptyEndpoint),
}, },
currentEndpoints: []*v1.Endpoints{ currentEndpoints: []*discovery.EndpointSlice{
makeTestEndpoints("ns1", "ep1", unnamedPort), makeTestEndpointSlice("ns1", "ep1", 1, unnamedPort),
}, },
oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{}, oldEndpoints: map[ServicePortName][]*BaseEndpointInfo{},
expectedResult: map[ServicePortName][]*BaseEndpointInfo{ expectedResult: map[ServicePortName][]*BaseEndpointInfo{
@@ -1054,7 +1045,7 @@ func TestUpdateEndpointsMap(t *testing.T) {
// the fp.oldEndpoints is as we expect. // the fp.oldEndpoints is as we expect.
for i := range tc.previousEndpoints { for i := range tc.previousEndpoints {
if tc.previousEndpoints[i] != nil { if tc.previousEndpoints[i] != nil {
fp.addEndpoints(tc.previousEndpoints[i]) fp.addEndpointSlice(tc.previousEndpoints[i])
} }
} }
fp.endpointsMap.Update(fp.endpointsChanges) fp.endpointsMap.Update(fp.endpointsChanges)
@@ -1069,12 +1060,14 @@ func TestUpdateEndpointsMap(t *testing.T) {
for i := range tc.previousEndpoints { for i := range tc.previousEndpoints {
prev, curr := tc.previousEndpoints[i], tc.currentEndpoints[i] prev, curr := tc.previousEndpoints[i], tc.currentEndpoints[i]
switch { switch {
case prev == nil && curr == nil:
continue
case prev == nil: case prev == nil:
fp.addEndpoints(curr) fp.addEndpointSlice(curr)
case curr == nil: case curr == nil:
fp.deleteEndpoints(prev) fp.deleteEndpointSlice(prev)
default: default:
fp.updateEndpoints(prev, curr) fp.updateEndpointSlice(prev, curr)
} }
} }
@@ -1253,7 +1246,6 @@ func TestLastChangeTriggerTime(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
fp := newFakeProxier(v1.IPv4Protocol, startTime) fp := newFakeProxier(v1.IPv4Protocol, startTime)
fp.endpointsChanges.endpointSliceCache = NewEndpointSliceCache(testHostname, v1.IPv4Protocol, nil, nil)
tc.scenario(fp) tc.scenario(fp)

View File

@@ -526,6 +526,7 @@ func newFakeProxier(ipFamily v1.IPFamily, t time.Time) *FakeProxier {
lastChangeTriggerTimes: make(map[types.NamespacedName][]time.Time), lastChangeTriggerTimes: make(map[types.NamespacedName][]time.Time),
trackerStartTime: t, trackerStartTime: t,
processEndpointsMapChange: nil, processEndpointsMapChange: nil,
endpointSliceCache: NewEndpointSliceCache(testHostname, ipFamily, nil, nil),
}, },
} }
} }