@@ -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"
@@ -826,7 +828,20 @@ var _ = utils.SIGDescribe("CSI mock volume", func() {
826
828
827
829
ctx , cancel := context .WithTimeout (context .Background (), csiPodRunningTimeout )
828
830
defer cancel ()
829
- pvcWatch , err := f .ClientSet .CoreV1 ().PersistentVolumeClaims (f .Namespace .Name ).Watch (ctx , metav1.ListOptions {})
831
+
832
+ // In contrast to the raw watch, RetryWatcher is expected to deliver all events even
833
+ // when the underlying raw watch gets closed prematurely
834
+ // (https://github.com/kubernetes/kubernetes/pull/93777#discussion_r467932080).
835
+ // This is important because below the test is going to make assertions about the
836
+ // PVC state changes.
837
+ initResource , err := f .ClientSet .CoreV1 ().PersistentVolumeClaims (f .Namespace .Name ).List (ctx , metav1.ListOptions {})
838
+ framework .ExpectNoError (err , "Failed to fetch initial PVC resource" )
839
+ listWatcher := & cachetools.ListWatch {
840
+ WatchFunc : func (listOptions metav1.ListOptions ) (watch.Interface , error ) {
841
+ return f .ClientSet .CoreV1 ().PersistentVolumeClaims (f .Namespace .Name ).Watch (ctx , listOptions )
842
+ },
843
+ }
844
+ pvcWatch , err := watchtools .NewRetryWatcher (initResource .GetResourceVersion (), listWatcher )
830
845
framework .ExpectNoError (err , "create PVC watch" )
831
846
defer pvcWatch .Stop ()
832
847
@@ -894,12 +909,14 @@ var _ = utils.SIGDescribe("CSI mock volume", func() {
894
909
ginkgo .By ("Checking PVC events" )
895
910
nodeAnnotationSet := false
896
911
nodeAnnotationReset := false
912
+ watchFailed := false
897
913
loop:
898
914
for {
899
915
select {
900
916
case event , ok := <- pvcWatch .ResultChan ():
901
917
if ! ok {
902
- framework .Failf ("PVC watch ended prematurely" )
918
+ watchFailed = true
919
+ break loop
903
920
}
904
921
905
922
framework .Logf ("PVC event %s: %#v" , event .Type , event .Object )
@@ -918,10 +935,8 @@ var _ = utils.SIGDescribe("CSI mock volume", func() {
918
935
case watch .Deleted :
919
936
break loop
920
937
case watch .Error :
921
- // Can this occur when the apiserver is under heavy load?
922
- // If yes, then we should bail out of the test here early and
923
- // skip further checks instead of treating it as a test failure.
924
- framework .Failf ("PVC watch failed prematurely: %v" , event .Object )
938
+ watchFailed = true
939
+ break
925
940
}
926
941
case <- ctx .Done ():
927
942
framework .Failf ("Timeout while waiting to observe PVC list" )
@@ -937,7 +952,13 @@ var _ = utils.SIGDescribe("CSI mock volume", func() {
937
952
}
938
953
}
939
954
940
- if test .lateBinding {
955
+ switch {
956
+ case watchFailed :
957
+ // If the watch failed or stopped prematurely (which can happen at any time), then we cannot
958
+ // verify whether the annotation was set as expected. This is still considered a successful
959
+ // test.
960
+ framework .Logf ("PVC watch delivered incomplete data, cannot check annotation" )
961
+ case test .lateBinding :
941
962
gomega .Expect (nodeAnnotationSet ).To (gomega .BeTrue (), "selected-node should have been set" )
942
963
// Whether it gets reset depends on whether we have topology enabled. Without
943
964
// it, rescheduling is unnecessary.
@@ -946,7 +967,7 @@ var _ = utils.SIGDescribe("CSI mock volume", func() {
946
967
} else {
947
968
gomega .Expect (nodeAnnotationReset ).To (gomega .BeFalse (), "selected-node should not have been reset" )
948
969
}
949
- } else {
970
+ default :
950
971
gomega .Expect (nodeAnnotationSet ).To (gomega .BeFalse (), "selected-node should not have been set" )
951
972
gomega .Expect (nodeAnnotationReset ).To (gomega .BeFalse (), "selected-node should not have been reset" )
952
973
}
0 commit comments