Skip to content

Commit b6c44e4

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 e525a0a commit b6c44e4

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
@@ -13,6 +13,7 @@ import (
1313
"github.com/stretchr/testify/require"
1414
appsv1 "k8s.io/api/apps/v1"
1515
corev1 "k8s.io/api/core/v1"
16+
discoveryv1 "k8s.io/api/discovery/v1"
1617
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1718
"k8s.io/apimachinery/pkg/api/errors"
1819
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -313,34 +314,75 @@ func logPodDetails(t *testing.T, testenv *TestEnvironment, namespace string) {
313314
func logServiceEndpoints(t *testing.T, testenv *TestEnvironment, namespace, serviceName string) {
314315
t.Helper()
315316

316-
endpoints := &corev1.Endpoints{}
317-
err := testenv.Client.Get(testenv.Ctx, types.NamespacedName{
318-
Name: serviceName,
319-
Namespace: namespace,
320-
}, endpoints)
317+
// List all EndpointSlices for the service
318+
endpointSliceList := &discoveryv1.EndpointSliceList{}
319+
err := testenv.Client.List(testenv.Ctx, endpointSliceList,
320+
client.InNamespace(namespace),
321+
client.MatchingLabels{"kubernetes.io/service-name": serviceName})
321322

322323
if err != nil {
323-
t.Logf("Failed to get endpoints for service %s: %v", serviceName, err)
324+
t.Logf("Failed to get endpoint slices for service %s: %v", serviceName, err)
325+
return
326+
}
327+
328+
if len(endpointSliceList.Items) == 0 {
329+
t.Logf("🔗 Service %s has no endpoint slices", serviceName)
324330
return
325331
}
326332

327333
t.Logf("🔗 Service %s endpoints:", serviceName)
328-
for i, subset := range endpoints.Subsets {
329-
t.Logf(" Subset %d:", i)
330-
// Ready addresses indicate pods that passed health checks and can receive traffic
331-
t.Logf(" Ready addresses: %d", len(subset.Addresses))
332-
for _, addr := range subset.Addresses {
333-
t.Logf(" - %s", addr.IP)
334+
for i, slice := range endpointSliceList.Items {
335+
t.Logf(" EndpointSlice %d (%s):", i, slice.Name)
336+
logEndpointSliceDetails(t, &slice)
337+
}
338+
}
339+
340+
// logEndpointSliceDetails logs the details of a single endpoint slice.
341+
func logEndpointSliceDetails(t *testing.T, slice *discoveryv1.EndpointSlice) {
342+
t.Helper()
343+
344+
readyCount, notReadyCount := logEndpointAddresses(t, slice.Endpoints)
345+
t.Logf(" Summary: %d ready, %d not ready", readyCount, notReadyCount)
346+
347+
t.Logf(" Ports:")
348+
logPortDetails(t, slice.Ports)
349+
}
350+
351+
// logEndpointAddresses logs endpoint addresses and returns counts of ready and not-ready endpoints.
352+
func logEndpointAddresses(t *testing.T, endpoints []discoveryv1.Endpoint) (readyCount, notReadyCount int) {
353+
t.Helper()
354+
355+
for _, endpoint := range endpoints {
356+
isReady := endpoint.Conditions.Ready != nil && *endpoint.Conditions.Ready
357+
if isReady {
358+
readyCount++
359+
for _, addr := range endpoint.Addresses {
360+
t.Logf(" Ready: %s", addr)
361+
}
362+
} else {
363+
notReadyCount++
364+
for _, addr := range endpoint.Addresses {
365+
t.Logf(" Not ready: %s", addr)
366+
}
334367
}
335-
// Not ready addresses show pods that exist but failed health checks
336-
t.Logf(" Not ready addresses: %d", len(subset.NotReadyAddresses))
337-
for _, addr := range subset.NotReadyAddresses {
338-
t.Logf(" - %s", addr.IP)
368+
}
369+
return readyCount, notReadyCount
370+
}
371+
372+
// logPortDetails logs port information from an endpoint slice.
373+
func logPortDetails(t *testing.T, ports []discoveryv1.EndpointPort) {
374+
t.Helper()
375+
376+
for _, port := range ports {
377+
portNum := int32(0)
378+
if port.Port != nil {
379+
portNum = *port.Port
339380
}
340-
t.Logf(" Ports:")
341-
for _, port := range subset.Ports {
342-
t.Logf(" - %s: %d", port.Name, port.Port)
381+
portName := ""
382+
if port.Name != nil {
383+
portName = *port.Name
343384
}
385+
t.Logf(" - %s: %d", portName, portNum)
344386
}
345387
}
346388

0 commit comments

Comments
 (0)