Skip to content

Commit abf87e5

Browse files
committed
fix: Replace deprecated corev1.Endpoints with discoveryv1.EndpointSlice
Replace the deprecated corev1.Endpoints API with discoveryv1.EndpointSlice in test utility functions to fix linting warnings. Changes: - Add discoveryv1 import to tests/e2e/test_utils.go - Replace corev1.Endpoints with discoveryv1.EndpointSlice in logServiceEndpoints() - Refactor logServiceEndpoints() into smaller helper functions to reduce complexity: - getEndpointSlices() - retrieves EndpointSlices for a service - logEndpointSliceDetails() - logs details of a single EndpointSlice - logEndpoints() - logs endpoint addresses and returns ready/not-ready counts - logPorts() - logs ports from an EndpointSlice This resolves the staticcheck warning: SA1019: corev1.Endpoints is deprecated: This API is deprecated in v1.33+ Signed-off-by: Derek Higgins <derekh@redhat.com>
1 parent 5680bca commit abf87e5

File tree

1 file changed

+65
-21
lines changed

1 file changed

+65
-21
lines changed

tests/e2e/test_utils.go

Lines changed: 65 additions & 21 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,77 @@ 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)
321-
317+
endpointSliceList, err := getEndpointSlices(testenv, namespace, serviceName)
322318
if err != nil {
323-
t.Logf("Failed to get endpoints for service %s: %v", serviceName, err)
319+
t.Logf("Failed to get endpoint slices for service %s: %v", serviceName, err)
320+
return
321+
}
322+
323+
if len(endpointSliceList.Items) == 0 {
324+
t.Logf("🔗 Service %s has no endpoint slices", serviceName)
324325
return
325326
}
326327

327-
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)
328+
t.Logf("🔗 Service %s endpoint slices:", serviceName)
329+
for i, endpointSlice := range endpointSliceList.Items {
330+
logEndpointSliceDetails(t, i, &endpointSlice)
331+
}
332+
}
333+
334+
// getEndpointSlices retrieves the EndpointSlices for a service.
335+
func getEndpointSlices(testenv *TestEnvironment, namespace, serviceName string) (*discoveryv1.EndpointSliceList, error) {
336+
endpointSliceList := &discoveryv1.EndpointSliceList{}
337+
err := testenv.Client.List(testenv.Ctx, endpointSliceList,
338+
client.InNamespace(namespace),
339+
client.MatchingLabels{"kubernetes.io/service-name": serviceName})
340+
return endpointSliceList, err
341+
}
342+
343+
// logEndpointSliceDetails logs the details of a single EndpointSlice.
344+
func logEndpointSliceDetails(t *testing.T, index int, endpointSlice *discoveryv1.EndpointSlice) {
345+
t.Helper()
346+
t.Logf(" EndpointSlice %d (%s):", index, endpointSlice.Name)
347+
348+
readyCount, notReadyCount := logEndpoints(t, endpointSlice.Endpoints)
349+
t.Logf(" Ready endpoints: %d, Not ready endpoints: %d", readyCount, notReadyCount)
350+
351+
logPorts(t, endpointSlice.Ports)
352+
}
353+
354+
// logEndpoints logs endpoint addresses and returns counts of ready and not ready endpoints.
355+
func logEndpoints(t *testing.T, endpoints []discoveryv1.Endpoint) (readyCount, notReadyCount int) {
356+
t.Helper()
357+
for _, endpoint := range endpoints {
358+
isReady := endpoint.Conditions.Ready != nil && *endpoint.Conditions.Ready
359+
if isReady {
360+
readyCount++
361+
for _, addr := range endpoint.Addresses {
362+
t.Logf(" Ready address: %s", addr)
363+
}
364+
} else {
365+
notReadyCount++
366+
for _, addr := range endpoint.Addresses {
367+
t.Logf(" Not ready address: %s", addr)
368+
}
334369
}
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)
370+
}
371+
return readyCount, notReadyCount
372+
}
373+
374+
// logPorts logs the ports from an EndpointSlice.
375+
func logPorts(t *testing.T, ports []discoveryv1.EndpointPort) {
376+
t.Helper()
377+
t.Logf(" Ports:")
378+
for _, port := range ports {
379+
portName := ""
380+
if port.Name != nil {
381+
portName = *port.Name
339382
}
340-
t.Logf(" Ports:")
341-
for _, port := range subset.Ports {
342-
t.Logf(" - %s: %d", port.Name, port.Port)
383+
portNum := int32(0)
384+
if port.Port != nil {
385+
portNum = *port.Port
343386
}
387+
t.Logf(" - %s: %d", portName, portNum)
344388
}
345389
}
346390

0 commit comments

Comments
 (0)