Skip to content

Commit 6b6c579

Browse files
committed
Switch deprecated corev1.Endpoints with discoveryv1.EndpointSlices
Had to rewrite logServiceEndpoints function: - Changed from using corev1.Endpoints to discoveryv1.EndpointSliceList - Now lists all EndpointSlices for a service using the kubernetes.io/service-name label - Updated logic to work with the new EndpointSlice structure where: - Each endpoint has a Conditions.Ready field instead of separate ready/not-ready address lists - Ports are at the slice level with optional pointer fields Signed-off-by: Anik Bhattacharjee <anbhatta@redhat.com>
1 parent c1d99d0 commit 6b6c579

File tree

1 file changed

+61
-19
lines changed

1 file changed

+61
-19
lines changed

tests/e2e/test_utils.go

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/stretchr/testify/require"
1515
appsv1 "k8s.io/api/apps/v1"
1616
corev1 "k8s.io/api/core/v1"
17+
discoveryv1 "k8s.io/api/discovery/v1"
1718
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1819
"k8s.io/apimachinery/pkg/api/errors"
1920
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -423,34 +424,75 @@ func logPodDetails(t *testing.T, testenv *TestEnvironment, namespace string) {
423424
func logServiceEndpoints(t *testing.T, testenv *TestEnvironment, namespace, serviceName string) {
424425
t.Helper()
425426

426-
endpoints := &corev1.Endpoints{}
427-
err := testenv.Client.Get(testenv.Ctx, types.NamespacedName{
428-
Name: serviceName,
429-
Namespace: namespace,
430-
}, endpoints)
427+
// List all EndpointSlices for the service
428+
endpointSliceList := &discoveryv1.EndpointSliceList{}
429+
err := testenv.Client.List(testenv.Ctx, endpointSliceList,
430+
client.InNamespace(namespace),
431+
client.MatchingLabels{"kubernetes.io/service-name": serviceName})
431432

432433
if err != nil {
433-
t.Logf("Failed to get endpoints for service %s: %v", serviceName, err)
434+
t.Logf("Failed to get endpoint slices for service %s: %v", serviceName, err)
435+
return
436+
}
437+
438+
if len(endpointSliceList.Items) == 0 {
439+
t.Logf("🔗 Service %s has no endpoint slices", serviceName)
434440
return
435441
}
436442

437443
t.Logf("🔗 Service %s endpoints:", serviceName)
438-
for i, subset := range endpoints.Subsets {
439-
t.Logf(" Subset %d:", i)
440-
// Ready addresses indicate pods that passed health checks and can receive traffic
441-
t.Logf(" Ready addresses: %d", len(subset.Addresses))
442-
for _, addr := range subset.Addresses {
443-
t.Logf(" - %s", addr.IP)
444+
for i, slice := range endpointSliceList.Items {
445+
t.Logf(" EndpointSlice %d (%s):", i, slice.Name)
446+
logEndpointSliceDetails(t, &slice)
447+
}
448+
}
449+
450+
// logEndpointSliceDetails logs the details of a single endpoint slice.
451+
func logEndpointSliceDetails(t *testing.T, slice *discoveryv1.EndpointSlice) {
452+
t.Helper()
453+
454+
readyCount, notReadyCount := logEndpointAddresses(t, slice.Endpoints)
455+
t.Logf(" Summary: %d ready, %d not ready", readyCount, notReadyCount)
456+
457+
t.Logf(" Ports:")
458+
logPortDetails(t, slice.Ports)
459+
}
460+
461+
// logEndpointAddresses logs endpoint addresses and returns counts of ready and not-ready endpoints.
462+
func logEndpointAddresses(t *testing.T, endpoints []discoveryv1.Endpoint) (readyCount, notReadyCount int) {
463+
t.Helper()
464+
465+
for _, endpoint := range endpoints {
466+
isReady := endpoint.Conditions.Ready != nil && *endpoint.Conditions.Ready
467+
if isReady {
468+
readyCount++
469+
for _, addr := range endpoint.Addresses {
470+
t.Logf(" Ready: %s", addr)
471+
}
472+
} else {
473+
notReadyCount++
474+
for _, addr := range endpoint.Addresses {
475+
t.Logf(" Not ready: %s", addr)
476+
}
444477
}
445-
// Not ready addresses show pods that exist but failed health checks
446-
t.Logf(" Not ready addresses: %d", len(subset.NotReadyAddresses))
447-
for _, addr := range subset.NotReadyAddresses {
448-
t.Logf(" - %s", addr.IP)
478+
}
479+
return readyCount, notReadyCount
480+
}
481+
482+
// logPortDetails logs port information from an endpoint slice.
483+
func logPortDetails(t *testing.T, ports []discoveryv1.EndpointPort) {
484+
t.Helper()
485+
486+
for _, port := range ports {
487+
portNum := int32(0)
488+
if port.Port != nil {
489+
portNum = *port.Port
449490
}
450-
t.Logf(" Ports:")
451-
for _, port := range subset.Ports {
452-
t.Logf(" - %s: %d", port.Name, port.Port)
491+
portName := ""
492+
if port.Name != nil {
493+
portName = *port.Name
453494
}
495+
t.Logf(" - %s: %d", portName, portNum)
454496
}
455497
}
456498

0 commit comments

Comments
 (0)