Skip to content

Commit 68d79b0

Browse files
authored
Merge pull request kubernetes#119798 from aojea/endpoints_and_slices
Fix flaky test depending on EndpointSlices to be ready
2 parents 815b18a + 2ceca1c commit 68d79b0

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

test/e2e/framework/util.go

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/onsi/gomega"
3737

3838
v1 "k8s.io/api/core/v1"
39+
discoveryv1 "k8s.io/api/discovery/v1"
3940
apierrors "k8s.io/apimachinery/pkg/api/errors"
4041
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4142
"k8s.io/apimachinery/pkg/fields"
@@ -420,20 +421,37 @@ func CheckTestingNSDeletedExcept(ctx context.Context, c clientset.Interface, ski
420421
}
421422

422423
// WaitForServiceEndpointsNum waits until the amount of endpoints that implement service to expectNum.
424+
// Some components use EndpointSlices other Endpoints, we must verify that both objects meet the requirements.
423425
func WaitForServiceEndpointsNum(ctx context.Context, c clientset.Interface, namespace, serviceName string, expectNum int, interval, timeout time.Duration) error {
424426
return wait.PollWithContext(ctx, interval, timeout, func(ctx context.Context) (bool, error) {
425427
Logf("Waiting for amount of service:%s endpoints to be %d", serviceName, expectNum)
426-
list, err := c.CoreV1().Endpoints(namespace).List(ctx, metav1.ListOptions{})
428+
endpoint, err := c.CoreV1().Endpoints(namespace).Get(ctx, serviceName, metav1.GetOptions{})
427429
if err != nil {
428-
return false, err
430+
Logf("Unexpected error trying to get Endpoints for %s : %v", serviceName, err)
431+
return false, nil
429432
}
430433

431-
for _, e := range list.Items {
432-
if e.Name == serviceName && countEndpointsNum(&e) == expectNum {
433-
return true, nil
434-
}
434+
if countEndpointsNum(endpoint) != expectNum {
435+
Logf("Unexpected number of Endpoints, got %d, expected %d", countEndpointsNum(endpoint), expectNum)
436+
return false, nil
435437
}
436-
return false, nil
438+
439+
esList, err := c.DiscoveryV1().EndpointSlices(namespace).List(ctx, metav1.ListOptions{LabelSelector: fmt.Sprintf("%s=%s", discoveryv1.LabelServiceName, serviceName)})
440+
if err != nil {
441+
Logf("Unexpected error trying to get EndpointSlices for %s : %v", serviceName, err)
442+
return false, nil
443+
}
444+
445+
if len(esList.Items) == 0 {
446+
Logf("Waiting for at least 1 EndpointSlice to exist")
447+
return false, nil
448+
}
449+
450+
if countEndpointsSlicesNum(esList) != expectNum {
451+
Logf("Unexpected number of Endpoints on Slices, got %d, expected %d", countEndpointsSlicesNum(esList), expectNum)
452+
return false, nil
453+
}
454+
return true, nil
437455
})
438456
}
439457

@@ -445,6 +463,19 @@ func countEndpointsNum(e *v1.Endpoints) int {
445463
return num
446464
}
447465

466+
func countEndpointsSlicesNum(epList *discoveryv1.EndpointSliceList) int {
467+
// EndpointSlices can contain the same address on multiple Slices
468+
addresses := sets.Set[string]{}
469+
for _, epSlice := range epList.Items {
470+
for _, ep := range epSlice.Endpoints {
471+
if len(ep.Addresses) > 0 {
472+
addresses.Insert(ep.Addresses[0])
473+
}
474+
}
475+
}
476+
return addresses.Len()
477+
}
478+
448479
// restclientConfig returns a config holds the information needed to build connection to kubernetes clusters.
449480
func restclientConfig(kubeContext string) (*clientcmdapi.Config, error) {
450481
Logf(">>> kubeConfig: %s", TestContext.KubeConfig)

0 commit comments

Comments
 (0)