From a0f07f2b4d99759ff63c7921486c46e188866761 Mon Sep 17 00:00:00 2001 From: Antonio Ojea Date: Sun, 13 Aug 2023 17:42:07 +0000 Subject: [PATCH] e2e WaitForServiceEndpointsNum take into account the address family Since EndpointSlices can carry dual stack families, but Endpoints can only have one single family, the function must take this into account and only compare the addresses of the same family, otherwise it will always fail for Services with dual stack endoints, because the endpoint slices will have always twice addresses than the Endpoints. Change-Id: Id08cb22f8a2adc103a4f5a4fe3eec25f448cd21b --- test/e2e/framework/util.go | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/test/e2e/framework/util.go b/test/e2e/framework/util.go index 8182bc11d65..407c9e60d79 100644 --- a/test/e2e/framework/util.go +++ b/test/e2e/framework/util.go @@ -436,6 +436,13 @@ func WaitForServiceEndpointsNum(ctx context.Context, c clientset.Interface, name return false, nil } + // Endpoints are single family but EndpointSlices can have dual stack addresses, + // so we verify the number of addresses that matches the same family on both. + addressType := discoveryv1.AddressTypeIPv4 + if isIPv6Endpoint(endpoint) { + addressType = discoveryv1.AddressTypeIPv6 + } + esList, err := c.DiscoveryV1().EndpointSlices(namespace).List(ctx, metav1.ListOptions{LabelSelector: fmt.Sprintf("%s=%s", discoveryv1.LabelServiceName, serviceName)}) if err != nil { Logf("Unexpected error trying to get EndpointSlices for %s : %v", serviceName, err) @@ -447,8 +454,8 @@ func WaitForServiceEndpointsNum(ctx context.Context, c clientset.Interface, name return false, nil } - if countEndpointsSlicesNum(esList) != expectNum { - Logf("Unexpected number of Endpoints on Slices, got %d, expected %d", countEndpointsSlicesNum(esList), expectNum) + if countEndpointsSlicesNum(esList, addressType) != expectNum { + Logf("Unexpected number of Endpoints on Slices, got %d, expected %d", countEndpointsSlicesNum(esList, addressType), expectNum) return false, nil } return true, nil @@ -463,10 +470,28 @@ func countEndpointsNum(e *v1.Endpoints) int { return num } -func countEndpointsSlicesNum(epList *discoveryv1.EndpointSliceList) int { +// isIPv6Endpoint returns true if the Endpoint uses IPv6 addresses +func isIPv6Endpoint(e *v1.Endpoints) bool { + for _, sub := range e.Subsets { + for _, addr := range sub.Addresses { + if len(addr.IP) == 0 { + continue + } + // Endpoints are single family, so it is enough to check only one address + return netutils.IsIPv6String(sub.Addresses[0].IP) + } + } + // default to IPv4 an Endpoint without IP addresses + return false +} + +func countEndpointsSlicesNum(epList *discoveryv1.EndpointSliceList, addressType discoveryv1.AddressType) int { // EndpointSlices can contain the same address on multiple Slices addresses := sets.Set[string]{} for _, epSlice := range epList.Items { + if epSlice.AddressType != addressType { + continue + } for _, ep := range epSlice.Endpoints { if len(ep.Addresses) > 0 { addresses.Insert(ep.Addresses[0])