From d293ae1298beac72fa3b670770d5391dea4bf041 Mon Sep 17 00:00:00 2001 From: Dave Chen Date: Wed, 18 Nov 2020 14:26:11 +0800 Subject: [PATCH] Create PVC first if it's referenced by PV Signed-off-by: Dave Chen --- test/utils/runners.go | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/test/utils/runners.go b/test/utils/runners.go index 89a3102f6e6..f81c56132c1 100644 --- a/test/utils/runners.go +++ b/test/utils/runners.go @@ -1369,11 +1369,27 @@ func CreatePodWithPersistentVolume(client clientset.Interface, namespace string, pv.Status.Phase = v1.VolumeBound // bind pvc to "pv-$i" - // pvc.Spec.VolumeName = pv.Name pvc.Status.Phase = v1.ClaimBound } else { pv.Status.Phase = v1.VolumeAvailable } + + // Create PVC first as it's referenced by the PV when the `bindVolume` is true. + if err := CreatePersistentVolumeClaimWithRetries(client, namespace, pvc); err != nil { + lock.Lock() + defer lock.Unlock() + createError = fmt.Errorf("error creating PVC: %s", err) + return + } + + // We need to update statuses separately, as creating pv/pvc resets status to the default one. + if _, err := client.CoreV1().PersistentVolumeClaims(namespace).UpdateStatus(context.TODO(), pvc, metav1.UpdateOptions{}); err != nil { + lock.Lock() + defer lock.Unlock() + createError = fmt.Errorf("error updating PVC status: %s", err) + return + } + if err := CreatePersistentVolumeWithRetries(client, pv); err != nil { lock.Lock() defer lock.Unlock() @@ -1388,19 +1404,6 @@ func CreatePodWithPersistentVolume(client clientset.Interface, namespace string, return } - if err := CreatePersistentVolumeClaimWithRetries(client, namespace, pvc); err != nil { - lock.Lock() - defer lock.Unlock() - createError = fmt.Errorf("error creating PVC: %s", err) - return - } - if _, err := client.CoreV1().PersistentVolumeClaims(namespace).UpdateStatus(context.TODO(), pvc, metav1.UpdateOptions{}); err != nil { - lock.Lock() - defer lock.Unlock() - createError = fmt.Errorf("error updating PVC status: %s", err) - return - } - // pod pod := podTemplate.DeepCopy() pod.Spec.Volumes = []v1.Volume{