Skip to content

Commit c9acca7

Browse files
committed
Update and improve Endpoints resource lifecycle test
1 parent 7de3f93 commit c9acca7

File tree

2 files changed

+103
-38
lines changed

2 files changed

+103
-38
lines changed

test/e2e/network/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ go_library(
6262
"//staging/src/k8s.io/apiserver/pkg/authentication/serviceaccount:go_default_library",
6363
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
6464
"//staging/src/k8s.io/client-go/tools/cache:go_default_library",
65+
"//staging/src/k8s.io/client-go/tools/watch:go_default_library",
6566
"//staging/src/k8s.io/client-go/util/flowcontrol:go_default_library",
6667
"//staging/src/k8s.io/client-go/util/retry:go_default_library",
6768
"//staging/src/k8s.io/client-go/util/workqueue:go_default_library",

test/e2e/network/service.go

Lines changed: 102 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import (
3434

3535
compute "google.golang.org/api/compute/v1"
3636

37+
"k8s.io/client-go/tools/cache"
38+
3739
appsv1 "k8s.io/api/apps/v1"
3840
v1 "k8s.io/api/core/v1"
3941
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -42,7 +44,9 @@ import (
4244
"k8s.io/apimachinery/pkg/util/intstr"
4345
"k8s.io/apimachinery/pkg/util/sets"
4446
"k8s.io/apimachinery/pkg/util/wait"
47+
watch "k8s.io/apimachinery/pkg/watch"
4548
clientset "k8s.io/client-go/kubernetes"
49+
watchtools "k8s.io/client-go/tools/watch"
4650
cloudprovider "k8s.io/cloud-provider"
4751
"k8s.io/kubernetes/test/e2e/framework"
4852
e2edeployment "k8s.io/kubernetes/test/e2e/framework/deployment"
@@ -2767,16 +2771,13 @@ var _ = SIGDescribe("Services", func() {
27672771
})
27682772

27692773
ginkgo.It("should test the lifecycle of an Endpoint", func() {
2770-
ns := f.Namespace.Name
2774+
testNamespaceName := f.Namespace.Name
27712775
testEndpointName := "testservice"
2772-
2773-
ginkgo.By("creating an Endpoint")
2774-
_, err := f.ClientSet.CoreV1().Endpoints(ns).Create(context.TODO(), &v1.Endpoints{
2776+
testEndpoints := v1.Endpoints{
27752777
ObjectMeta: metav1.ObjectMeta{
2776-
Name: testEndpointName,
2777-
Namespace: ns,
2778+
Name: testEndpointName,
27782779
Labels: map[string]string{
2779-
"testendpoint-static": "true",
2780+
"test-endpoint-static": "true",
27802781
},
27812782
},
27822783
Subsets: []v1.EndpointSubset{{
@@ -2789,50 +2790,82 @@ var _ = SIGDescribe("Services", func() {
27892790
Protocol: v1.ProtocolTCP,
27902791
}},
27912792
}},
2792-
}, metav1.CreateOptions{})
2793-
framework.ExpectNoError(err, "failed to create Endpoint")
2793+
}
2794+
w := &cache.ListWatch{
2795+
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
2796+
options.LabelSelector = "test-endpoint-static=true"
2797+
return f.ClientSet.CoreV1().Endpoints(testNamespaceName).Watch(context.TODO(), options)
2798+
},
2799+
}
2800+
endpointsList, err := f.ClientSet.CoreV1().Endpoints("").List(context.TODO(), metav1.ListOptions{LabelSelector: "test-endpoint-static=true"})
2801+
framework.ExpectNoError(err, "failed to list Endpoints")
27942802

2795-
// set up a watch for the Endpoint
2796-
// this timeout was chosen as there was timeout failure from the CI
2797-
endpointWatchTimeoutSeconds := int64(180)
2798-
endpointWatch, err := f.ClientSet.CoreV1().Endpoints(ns).Watch(context.TODO(), metav1.ListOptions{LabelSelector: "testendpoint-static=true", TimeoutSeconds: &endpointWatchTimeoutSeconds})
2799-
framework.ExpectNoError(err, "failed to setup watch on newly created Endpoint")
2800-
endpointWatchChan := endpointWatch.ResultChan()
2803+
ginkgo.By("creating an Endpoint")
2804+
_, err = f.ClientSet.CoreV1().Endpoints(testNamespaceName).Create(context.TODO(), &testEndpoints, metav1.CreateOptions{})
2805+
framework.ExpectNoError(err, "failed to create Endpoint")
28012806
ginkgo.By("waiting for available Endpoint")
2802-
for watchEvent := range endpointWatchChan {
2803-
if watchEvent.Type == "ADDED" {
2804-
break
2807+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
2808+
defer cancel()
2809+
_, err = watchtools.Until(ctx, endpointsList.ResourceVersion, w, func(event watch.Event) (bool, error) {
2810+
switch event.Type {
2811+
case watch.Added:
2812+
if endpoints, ok := event.Object.(*v1.Endpoints); ok {
2813+
found := endpoints.ObjectMeta.Name == endpoints.Name &&
2814+
endpoints.Labels["test-endpoint-static"] == "true"
2815+
return found, nil
2816+
}
2817+
default:
2818+
framework.Logf("observed event type %v", event.Type)
28052819
}
2806-
}
2820+
return false, nil
2821+
})
2822+
framework.ExpectNoError(err, "failed to see %v event", watch.Added)
28072823

28082824
ginkgo.By("listing all Endpoints")
2809-
endpointsList, err := f.ClientSet.CoreV1().Endpoints("").List(context.TODO(), metav1.ListOptions{LabelSelector: "testendpoint-static=true"})
2825+
endpointsList, err = f.ClientSet.CoreV1().Endpoints("").List(context.TODO(), metav1.ListOptions{LabelSelector: "test-endpoint-static=true"})
28102826
framework.ExpectNoError(err, "failed to list Endpoints")
2811-
foundEndpointService := false
2827+
eventFound := false
28122828
var foundEndpoint v1.Endpoints
28132829
for _, endpoint := range endpointsList.Items {
2814-
if endpoint.ObjectMeta.Name == testEndpointName && endpoint.ObjectMeta.Namespace == ns {
2815-
foundEndpointService = true
2830+
if endpoint.ObjectMeta.Name == testEndpointName && endpoint.ObjectMeta.Namespace == testNamespaceName {
2831+
eventFound = true
28162832
foundEndpoint = endpoint
28172833
break
28182834
}
28192835
}
2820-
framework.ExpectEqual(foundEndpointService, true, "unable to find Endpoint Service in list of Endpoints")
2836+
framework.ExpectEqual(eventFound, true, "unable to find Endpoint Service in list of Endpoints")
28212837

28222838
ginkgo.By("updating the Endpoint")
2823-
foundEndpoint.ObjectMeta.Labels["testservice"] = "first-modification"
2824-
_, err = f.ClientSet.CoreV1().Endpoints(ns).Update(context.TODO(), &foundEndpoint, metav1.UpdateOptions{})
2839+
foundEndpoint.ObjectMeta.Labels["test-service"] = "updated"
2840+
_, err = f.ClientSet.CoreV1().Endpoints(testNamespaceName).Update(context.TODO(), &foundEndpoint, metav1.UpdateOptions{})
28252841
framework.ExpectNoError(err, "failed to update Endpoint with new label")
28262842

2843+
ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
2844+
defer cancel()
2845+
_, err = watchtools.Until(ctx, endpointsList.ResourceVersion, w, func(event watch.Event) (bool, error) {
2846+
switch event.Type {
2847+
case watch.Modified:
2848+
if endpoints, ok := event.Object.(*v1.Endpoints); ok {
2849+
found := endpoints.ObjectMeta.Name == endpoints.Name &&
2850+
endpoints.Labels["test-endpoint-static"] == "true"
2851+
return found, nil
2852+
}
2853+
default:
2854+
framework.Logf("observed event type %v", event.Type)
2855+
}
2856+
return false, nil
2857+
})
2858+
framework.ExpectNoError(err, "failed to see %v event", watch.Modified)
2859+
28272860
ginkgo.By("fetching the Endpoint")
2828-
_, err = f.ClientSet.CoreV1().Endpoints(ns).Get(context.TODO(), testEndpointName, metav1.GetOptions{})
2861+
endpoints, err := f.ClientSet.CoreV1().Endpoints(testNamespaceName).Get(context.TODO(), testEndpointName, metav1.GetOptions{})
28292862
framework.ExpectNoError(err, "failed to fetch Endpoint")
2830-
framework.ExpectEqual(foundEndpoint.ObjectMeta.Labels["testservice"], "first-modification", "label not patched")
2863+
framework.ExpectEqual(foundEndpoint.ObjectMeta.Labels["test-service"], "updated", "failed to update Endpoint %v in namespace %v label not updated", testEndpointName, testNamespaceName)
28312864

28322865
endpointPatch, err := json.Marshal(map[string]interface{}{
28332866
"metadata": map[string]interface{}{
28342867
"labels": map[string]string{
2835-
"testservice": "second-modification",
2868+
"test-service": "patched",
28362869
},
28372870
},
28382871
"subsets": []map[string]interface{}{
@@ -2853,30 +2886,61 @@ var _ = SIGDescribe("Services", func() {
28532886
})
28542887
framework.ExpectNoError(err, "failed to marshal JSON for WatchEvent patch")
28552888
ginkgo.By("patching the Endpoint")
2856-
_, err = f.ClientSet.CoreV1().Endpoints(ns).Patch(context.TODO(), testEndpointName, types.StrategicMergePatchType, []byte(endpointPatch), metav1.PatchOptions{})
2889+
_, err = f.ClientSet.CoreV1().Endpoints(testNamespaceName).Patch(context.TODO(), testEndpointName, types.StrategicMergePatchType, []byte(endpointPatch), metav1.PatchOptions{})
28572890
framework.ExpectNoError(err, "failed to patch Endpoint")
2891+
ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
2892+
defer cancel()
2893+
_, err = watchtools.Until(ctx, endpoints.ResourceVersion, w, func(event watch.Event) (bool, error) {
2894+
switch event.Type {
2895+
case watch.Modified:
2896+
if endpoints, ok := event.Object.(*v1.Endpoints); ok {
2897+
found := endpoints.ObjectMeta.Name == endpoints.Name &&
2898+
endpoints.Labels["test-endpoint-static"] == "true"
2899+
return found, nil
2900+
}
2901+
default:
2902+
framework.Logf("observed event type %v", event.Type)
2903+
}
2904+
return false, nil
2905+
})
2906+
framework.ExpectNoError(err, "failed to see %v event", watch.Modified)
28582907

28592908
ginkgo.By("fetching the Endpoint")
2860-
endpoint, err := f.ClientSet.CoreV1().Endpoints(ns).Get(context.TODO(), testEndpointName, metav1.GetOptions{})
2909+
endpoints, err = f.ClientSet.CoreV1().Endpoints(testNamespaceName).Get(context.TODO(), testEndpointName, metav1.GetOptions{})
28612910
framework.ExpectNoError(err, "failed to fetch Endpoint")
2862-
framework.ExpectEqual(endpoint.ObjectMeta.Labels["testservice"], "second-modification", "failed to patch Endpoint with Label")
2863-
endpointSubsetOne := endpoint.Subsets[0]
2911+
framework.ExpectEqual(endpoints.ObjectMeta.Labels["test-service"], "patched", "failed to patch Endpoint with Label")
2912+
endpointSubsetOne := endpoints.Subsets[0]
28642913
endpointSubsetOneAddresses := endpointSubsetOne.Addresses[0]
28652914
endpointSubsetOnePorts := endpointSubsetOne.Ports[0]
28662915
framework.ExpectEqual(endpointSubsetOneAddresses.IP, "10.0.0.25", "failed to patch Endpoint")
28672916
framework.ExpectEqual(endpointSubsetOnePorts.Name, "http-test", "failed to patch Endpoint")
28682917
framework.ExpectEqual(endpointSubsetOnePorts.Port, int32(8080), "failed to patch Endpoint")
28692918

28702919
ginkgo.By("deleting the Endpoint by Collection")
2871-
err = f.ClientSet.CoreV1().Endpoints(ns).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{LabelSelector: "testendpoint-static=true"})
2920+
err = f.ClientSet.CoreV1().Endpoints(testNamespaceName).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{LabelSelector: "test-endpoint-static=true"})
28722921
framework.ExpectNoError(err, "failed to delete Endpoint by Collection")
28732922

28742923
ginkgo.By("waiting for Endpoint deletion")
2875-
for watchEvent := range endpointWatchChan {
2876-
if watchEvent.Type == "DELETED" {
2877-
break
2924+
ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
2925+
defer cancel()
2926+
_, err = watchtools.Until(ctx, endpoints.ResourceVersion, w, func(event watch.Event) (bool, error) {
2927+
switch event.Type {
2928+
case watch.Deleted:
2929+
if endpoints, ok := event.Object.(*v1.Endpoints); ok {
2930+
found := endpoints.ObjectMeta.Name == endpoints.Name &&
2931+
endpoints.Labels["test-endpoint-static"] == "true"
2932+
return found, nil
2933+
}
2934+
default:
2935+
framework.Logf("observed event type %v", event.Type)
28782936
}
2879-
}
2937+
return false, nil
2938+
})
2939+
framework.ExpectNoError(err, "failed to see %v event", watch.Deleted)
2940+
2941+
ginkgo.By("fetching the Endpoint")
2942+
_, err = f.ClientSet.CoreV1().Endpoints(testNamespaceName).Get(context.TODO(), testEndpointName, metav1.GetOptions{})
2943+
framework.ExpectError(err, "should not be able to fetch Endpoint")
28802944
})
28812945
})
28822946

0 commit comments

Comments
 (0)