@@ -39,6 +39,8 @@ import (
39
39
"k8s.io/apimachinery/pkg/util/wait"
40
40
"k8s.io/apimachinery/pkg/watch"
41
41
clientset "k8s.io/client-go/kubernetes"
42
+ cachetools "k8s.io/client-go/tools/cache"
43
+ watchtools "k8s.io/client-go/tools/watch"
42
44
"k8s.io/kubernetes/pkg/controller/volume/scheduling"
43
45
"k8s.io/kubernetes/test/e2e/framework"
44
46
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
@@ -821,7 +823,20 @@ var _ = utils.SIGDescribe("CSI mock volume", func() {
821
823
822
824
ctx , cancel := context .WithTimeout (context .Background (), csiPodRunningTimeout )
823
825
defer cancel ()
824
- pvcWatch , err := f .ClientSet .CoreV1 ().PersistentVolumeClaims (f .Namespace .Name ).Watch (ctx , metav1.ListOptions {})
826
+
827
+ // In contrast to the raw watch, RetryWatcher is expected to deliver all events even
828
+ // when the underlying raw watch gets closed prematurely
829
+ // (https://github.com/kubernetes/kubernetes/pull/93777#discussion_r467932080).
830
+ // This is important because below the test is going to make assertions about the
831
+ // PVC state changes.
832
+ initResource , err := f .ClientSet .CoreV1 ().PersistentVolumeClaims (f .Namespace .Name ).List (ctx , metav1.ListOptions {})
833
+ framework .ExpectNoError (err , "Failed to fetch initial PVC resource" )
834
+ listWatcher := & cachetools.ListWatch {
835
+ WatchFunc : func (listOptions metav1.ListOptions ) (watch.Interface , error ) {
836
+ return f .ClientSet .CoreV1 ().PersistentVolumeClaims (f .Namespace .Name ).Watch (ctx , listOptions )
837
+ },
838
+ }
839
+ pvcWatch , err := watchtools .NewRetryWatcher (initResource .GetResourceVersion (), listWatcher )
825
840
framework .ExpectNoError (err , "create PVC watch" )
826
841
defer pvcWatch .Stop ()
827
842
@@ -889,12 +904,14 @@ var _ = utils.SIGDescribe("CSI mock volume", func() {
889
904
ginkgo .By ("Checking PVC events" )
890
905
nodeAnnotationSet := false
891
906
nodeAnnotationReset := false
907
+ watchFailed := false
892
908
loop:
893
909
for {
894
910
select {
895
911
case event , ok := <- pvcWatch .ResultChan ():
896
912
if ! ok {
897
- framework .Failf ("PVC watch ended prematurely" )
913
+ watchFailed = true
914
+ break loop
898
915
}
899
916
900
917
framework .Logf ("PVC event %s: %#v" , event .Type , event .Object )
@@ -913,10 +930,8 @@ var _ = utils.SIGDescribe("CSI mock volume", func() {
913
930
case watch .Deleted :
914
931
break loop
915
932
case watch .Error :
916
- // Can this occur when the apiserver is under heavy load?
917
- // If yes, then we should bail out of the test here early and
918
- // skip further checks instead of treating it as a test failure.
919
- framework .Failf ("PVC watch failed prematurely: %v" , event .Object )
933
+ watchFailed = true
934
+ break
920
935
}
921
936
case <- ctx .Done ():
922
937
framework .Failf ("Timeout while waiting to observe PVC list" )
@@ -932,7 +947,13 @@ var _ = utils.SIGDescribe("CSI mock volume", func() {
932
947
}
933
948
}
934
949
935
- if test .lateBinding {
950
+ switch {
951
+ case watchFailed :
952
+ // If the watch failed or stopped prematurely (which can happen at any time), then we cannot
953
+ // verify whether the annotation was set as expected. This is still considered a successful
954
+ // test.
955
+ framework .Logf ("PVC watch delivered incomplete data, cannot check annotation" )
956
+ case test .lateBinding :
936
957
gomega .Expect (nodeAnnotationSet ).To (gomega .BeTrue (), "selected-node should have been set" )
937
958
// Whether it gets reset depends on whether we have topology enabled. Without
938
959
// it, rescheduling is unnecessary.
@@ -941,7 +962,7 @@ var _ = utils.SIGDescribe("CSI mock volume", func() {
941
962
} else {
942
963
gomega .Expect (nodeAnnotationReset ).To (gomega .BeFalse (), "selected-node should not have been reset" )
943
964
}
944
- } else {
965
+ default :
945
966
gomega .Expect (nodeAnnotationSet ).To (gomega .BeFalse (), "selected-node should not have been set" )
946
967
gomega .Expect (nodeAnnotationReset ).To (gomega .BeFalse (), "selected-node should not have been reset" )
947
968
}
0 commit comments