Skip to content

Commit 7478a30

Browse files
authored
Merge pull request kubernetes#127260 from carlory/fix-124136
Fix TestPersistentVolumeProvisionMultiPVCs
2 parents 65bc7c0 + 1eee8ff commit 7478a30

File tree

2 files changed

+39
-105
lines changed

2 files changed

+39
-105
lines changed

test/integration/volume/persistent_volumes_test.go

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ import (
2929
storage "k8s.io/api/storage/v1"
3030
"k8s.io/apimachinery/pkg/api/resource"
3131
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32+
"k8s.io/apimachinery/pkg/util/wait"
3233
"k8s.io/apimachinery/pkg/watch"
3334
utilfeature "k8s.io/apiserver/pkg/util/feature"
3435
"k8s.io/client-go/informers"
3536
clientset "k8s.io/client-go/kubernetes"
3637
restclient "k8s.io/client-go/rest"
3738
ref "k8s.io/client-go/tools/reference"
39+
"k8s.io/client-go/util/retry"
3840
featuregatetesting "k8s.io/component-base/featuregate/testing"
3941
kubeapiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
4042
"k8s.io/kubernetes/pkg/api/legacyscheme"
@@ -284,7 +286,9 @@ func TestPersistentVolumeBindRace(t *testing.T) {
284286

285287
waitForPersistentVolumePhase(testClient, pv.Name, watchPV, v1.VolumeBound)
286288
klog.V(2).Infof("TestPersistentVolumeBindRace pv bound")
287-
waitForAnyPersistentVolumeClaimPhase(watchPVC, v1.ClaimBound)
289+
if err := waitForAnyPersistentVolumeClaimPhase(watchPVC, v1.ClaimBound); err != nil {
290+
t.Fatalf("Unexpected error waiting for any pvc to be bound: %v", err)
291+
}
288292
klog.V(2).Infof("TestPersistentVolumeBindRace pvc bound")
289293

290294
pv, err = testClient.CoreV1().PersistentVolumes().Get(context.TODO(), pv.Name, metav1.GetOptions{})
@@ -871,10 +875,11 @@ func TestPersistentVolumeMultiPVsPVCs(t *testing.T) {
871875
}()
872876

873877
// wait until the binder pairs all claims
874-
for i := 0; i < objCount; i++ {
875-
waitForAnyPersistentVolumeClaimPhase(watchPVC, v1.ClaimBound)
876-
klog.V(1).Infof("%d claims bound", i+1)
878+
err := waitForSomePersistentVolumeClaimPhase(tCtx, testClient, namespaceName, objCount, watchPVC, v1.ClaimBound)
879+
if err != nil {
880+
t.Fatalf("Failed to wait for all claims to be bound: %v", err)
877881
}
882+
878883
// wait until the binder pairs all volumes
879884
for i := 0; i < objCount; i++ {
880885
waitForPersistentVolumePhase(testClient, pvs[i].Name, watchPV, v1.VolumeBound)
@@ -952,7 +957,9 @@ func TestPersistentVolumeControllerStartup(t *testing.T) {
952957
// Drain watchPVC with all events generated by the PVC until it's bound
953958
// We don't want to catch "PVC created with Status.Phase == Pending"
954959
// later in this test.
955-
waitForAnyPersistentVolumeClaimPhase(watchPVC, v1.ClaimBound)
960+
if err := waitForAnyPersistentVolumeClaimPhase(watchPVC, v1.ClaimBound); err != nil {
961+
t.Fatalf("Unexpected error waiting for any pvc to be bound: %v", err)
962+
}
956963

957964
pv := createPV(pvName, "/tmp/foo"+strconv.Itoa(i), "1G",
958965
[]v1.PersistentVolumeAccessMode{v1.ReadWriteOnce}, v1.PersistentVolumeReclaimRetain)
@@ -1058,34 +1065,6 @@ func TestPersistentVolumeProvisionMultiPVCs(t *testing.T) {
10581065
defer testClient.CoreV1().PersistentVolumes().DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{})
10591066
defer testClient.StorageV1().StorageClasses().DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{})
10601067

1061-
// Watch all events in the namespace, and save them to artifacts for debugging.
1062-
// TODO: This is a temporary solution to debug flaky tests `panic: test timed out after 10m0s`.
1063-
// We should remove this once https://github.com/kubernetes/kubernetes/issues/124136 is fixed.
1064-
go func() {
1065-
w, err := testClient.EventsV1().Events(ns.Name).Watch(tCtx, metav1.ListOptions{})
1066-
if err != nil {
1067-
return
1068-
}
1069-
for {
1070-
select {
1071-
case event, ok := <-w.ResultChan():
1072-
if !ok {
1073-
klog.Info("Event watch channel closed")
1074-
w, err = testClient.EventsV1().Events(ns.Name).Watch(tCtx, metav1.ListOptions{})
1075-
if err != nil {
1076-
klog.ErrorS(err, "Failed to restart event watch")
1077-
return
1078-
}
1079-
continue
1080-
}
1081-
reportToArtifacts(t.Name()+"-events.text", event.Object)
1082-
case <-tCtx.Done():
1083-
w.Stop()
1084-
return
1085-
}
1086-
}
1087-
}()
1088-
10891068
storageClass := storage.StorageClass{
10901069
TypeMeta: metav1.TypeMeta{
10911070
Kind: "StorageClass",
@@ -1117,9 +1096,9 @@ func TestPersistentVolumeProvisionMultiPVCs(t *testing.T) {
11171096
}()
11181097

11191098
// Wait until the controller provisions and binds all of them
1120-
for i := 0; i < objCount; i++ {
1121-
waitForAnyPersistentVolumeClaimPhaseAndReportIt(t, watchPVC, v1.ClaimBound)
1122-
klog.V(1).Infof("%d claims bound", i+1)
1099+
err := waitForSomePersistentVolumeClaimPhase(tCtx, testClient, namespaceName, objCount, watchPVC, v1.ClaimBound)
1100+
if err != nil {
1101+
t.Fatalf("Failed to wait for all claims to be bound: %v", err)
11231102
}
11241103
klog.V(2).Infof("TestPersistentVolumeProvisionMultiPVCs: claims are bound")
11251104

@@ -1488,31 +1467,40 @@ func waitForAnyPersistentVolumePhase(w watch.Interface, phase v1.PersistentVolum
14881467
}
14891468
}
14901469

1491-
func waitForAnyPersistentVolumeClaimPhase(w watch.Interface, phase v1.PersistentVolumeClaimPhase) {
1492-
for {
1493-
event := <-w.ResultChan()
1494-
claim, ok := event.Object.(*v1.PersistentVolumeClaim)
1495-
if !ok {
1496-
continue
1497-
}
1498-
if claim.Status.Phase == phase {
1499-
klog.V(2).Infof("claim %q is %s", claim.Name, phase)
1500-
break
1470+
func waitForSomePersistentVolumeClaimPhase(ctx context.Context, testClient clientset.Interface, namespace string, objCount int, watchPVC watch.Interface, phase v1.PersistentVolumeClaimPhase) error {
1471+
return wait.ExponentialBackoffWithContext(ctx, retry.DefaultBackoff, func(ctx context.Context) (bool, error) {
1472+
for i := 0; i < objCount; i++ {
1473+
waitErr := waitForAnyPersistentVolumeClaimPhase(watchPVC, v1.ClaimBound)
1474+
if waitErr != nil {
1475+
klog.Errorf("Failed to wait for a claim (%d/%d) to be bound: %v", i+1, objCount, waitErr)
1476+
klog.Info("Recreating a watch for claims and re-checking the count of bound claims")
1477+
newWatchPVC, err := testClient.CoreV1().PersistentVolumeClaims(namespace).Watch(ctx, metav1.ListOptions{})
1478+
if err != nil {
1479+
return false, err
1480+
}
1481+
watchPVC.Stop()
1482+
watchPVC = newWatchPVC
1483+
return false, waitErr
1484+
}
1485+
klog.V(1).Infof("%d claims bound", i+1)
15011486
}
1502-
}
1487+
return true, nil
1488+
})
15031489
}
15041490

1505-
func waitForAnyPersistentVolumeClaimPhaseAndReportIt(t *testing.T, w watch.Interface, phase v1.PersistentVolumeClaimPhase) {
1491+
func waitForAnyPersistentVolumeClaimPhase(w watch.Interface, phase v1.PersistentVolumeClaimPhase) error {
15061492
for {
1507-
event := <-w.ResultChan()
1508-
reportToArtifacts(t.Name()+"-watched-pvcs.text", event)
1493+
event, ok := <-w.ResultChan()
1494+
if !ok {
1495+
return fmt.Errorf("watch closed")
1496+
}
15091497
claim, ok := event.Object.(*v1.PersistentVolumeClaim)
15101498
if !ok {
15111499
continue
15121500
}
15131501
if claim.Status.Phase == phase {
15141502
klog.V(2).Infof("claim %q is %s", claim.Name, phase)
1515-
break
1503+
return nil
15161504
}
15171505
}
15181506
}

test/integration/volume/util_test.go

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)