@@ -34,6 +34,8 @@ import (
34
34
35
35
compute "google.golang.org/api/compute/v1"
36
36
37
+ "k8s.io/client-go/tools/cache"
38
+
37
39
appsv1 "k8s.io/api/apps/v1"
38
40
v1 "k8s.io/api/core/v1"
39
41
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -42,7 +44,9 @@ import (
42
44
"k8s.io/apimachinery/pkg/util/intstr"
43
45
"k8s.io/apimachinery/pkg/util/sets"
44
46
"k8s.io/apimachinery/pkg/util/wait"
47
+ watch "k8s.io/apimachinery/pkg/watch"
45
48
clientset "k8s.io/client-go/kubernetes"
49
+ watchtools "k8s.io/client-go/tools/watch"
46
50
cloudprovider "k8s.io/cloud-provider"
47
51
"k8s.io/kubernetes/test/e2e/framework"
48
52
e2edeployment "k8s.io/kubernetes/test/e2e/framework/deployment"
@@ -2767,16 +2771,13 @@ var _ = SIGDescribe("Services", func() {
2767
2771
})
2768
2772
2769
2773
ginkgo .It ("should test the lifecycle of an Endpoint" , func () {
2770
- ns := f .Namespace .Name
2774
+ testNamespaceName := f .Namespace .Name
2771
2775
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 {
2775
2777
ObjectMeta : metav1.ObjectMeta {
2776
- Name : testEndpointName ,
2777
- Namespace : ns ,
2778
+ Name : testEndpointName ,
2778
2779
Labels : map [string ]string {
2779
- "testendpoint -static" : "true" ,
2780
+ "test-endpoint -static" : "true" ,
2780
2781
},
2781
2782
},
2782
2783
Subsets : []v1.EndpointSubset {{
@@ -2789,50 +2790,82 @@ var _ = SIGDescribe("Services", func() {
2789
2790
Protocol : v1 .ProtocolTCP ,
2790
2791
}},
2791
2792
}},
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" )
2794
2802
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" )
2801
2806
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 )
2805
2819
}
2806
- }
2820
+ return false , nil
2821
+ })
2822
+ framework .ExpectNoError (err , "failed to see %v event" , watch .Added )
2807
2823
2808
2824
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" })
2810
2826
framework .ExpectNoError (err , "failed to list Endpoints" )
2811
- foundEndpointService := false
2827
+ eventFound := false
2812
2828
var foundEndpoint v1.Endpoints
2813
2829
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
2816
2832
foundEndpoint = endpoint
2817
2833
break
2818
2834
}
2819
2835
}
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" )
2821
2837
2822
2838
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 {})
2825
2841
framework .ExpectNoError (err , "failed to update Endpoint with new label" )
2826
2842
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
+
2827
2860
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 {})
2829
2862
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 )
2831
2864
2832
2865
endpointPatch , err := json .Marshal (map [string ]interface {}{
2833
2866
"metadata" : map [string ]interface {}{
2834
2867
"labels" : map [string ]string {
2835
- "testservice " : "second-modification " ,
2868
+ "test-service " : "patched " ,
2836
2869
},
2837
2870
},
2838
2871
"subsets" : []map [string ]interface {}{
@@ -2853,30 +2886,61 @@ var _ = SIGDescribe("Services", func() {
2853
2886
})
2854
2887
framework .ExpectNoError (err , "failed to marshal JSON for WatchEvent patch" )
2855
2888
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 {})
2857
2890
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 )
2858
2907
2859
2908
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 {})
2861
2910
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 ]
2864
2913
endpointSubsetOneAddresses := endpointSubsetOne .Addresses [0 ]
2865
2914
endpointSubsetOnePorts := endpointSubsetOne .Ports [0 ]
2866
2915
framework .ExpectEqual (endpointSubsetOneAddresses .IP , "10.0.0.25" , "failed to patch Endpoint" )
2867
2916
framework .ExpectEqual (endpointSubsetOnePorts .Name , "http-test" , "failed to patch Endpoint" )
2868
2917
framework .ExpectEqual (endpointSubsetOnePorts .Port , int32 (8080 ), "failed to patch Endpoint" )
2869
2918
2870
2919
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" })
2872
2921
framework .ExpectNoError (err , "failed to delete Endpoint by Collection" )
2873
2922
2874
2923
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 )
2878
2936
}
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" )
2880
2944
})
2881
2945
})
2882
2946
0 commit comments