From 1197b941499b15bc3a3b659c821b7825c48ff7a2 Mon Sep 17 00:00:00 2001 From: Madhu Rajanna Date: Fri, 6 May 2022 12:34:08 +0530 Subject: [PATCH] e2e: add getPersistentVolume helper function added getPersistentVolume helper function to get the PV and also try if there is any API error to improve the CI. Signed-off-by: Madhu Rajanna --- e2e/pvc.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/e2e/pvc.go b/e2e/pvc.go index 2b62466af..9f1854474 100644 --- a/e2e/pvc.go +++ b/e2e/pvc.go @@ -214,6 +214,29 @@ func getPersistentVolumeClaim(c kubernetes.Interface, namespace, name string) (* return pvc, err } +// getPersistentVolume returns the PersistentVolume with the given +// name and retries if there is any API error. +func getPersistentVolume(c kubernetes.Interface, name string) (*v1.PersistentVolume, error) { + var pv *v1.PersistentVolume + var err error + timeout := time.Duration(deployTimeout) * time.Minute + err = wait.PollImmediate(1*time.Second, timeout, func() (bool, error) { + pv, err = c.CoreV1().PersistentVolumes().Get(context.TODO(), name, metav1.GetOptions{}) + if err != nil { + e2elog.Logf("Error getting pv %q: %v", name, err) + if isRetryableAPIError(err) { + return false, nil + } + + return false, fmt.Errorf("failed to get pv: %w", err) + } + + return true, err + }) + + return pv, err +} + func getPVCAndPV( c kubernetes.Interface, pvcName, pvcNamespace string) (*v1.PersistentVolumeClaim, *v1.PersistentVolume, error) { @@ -221,7 +244,7 @@ func getPVCAndPV( if err != nil { return nil, nil, fmt.Errorf("failed to get PVC: %w", err) } - pv, err := c.CoreV1().PersistentVolumes().Get(context.TODO(), pvc.Spec.VolumeName, metav1.GetOptions{}) + pv, err := getPersistentVolume(c, pvc.Spec.VolumeName) if err != nil { return pvc, nil, fmt.Errorf("failed to get PV: %w", err) } @@ -240,7 +263,7 @@ func deletePVCAndValidatePV(c kubernetes.Interface, pvc *v1.PersistentVolumeClai if err != nil { return fmt.Errorf("failed to get pvc: %w", err) } - pv, err := c.CoreV1().PersistentVolumes().Get(context.TODO(), pvc.Spec.VolumeName, metav1.GetOptions{}) + pv, err := getPersistentVolume(c, pvc.Spec.VolumeName) if err != nil { return fmt.Errorf("failed to get pv: %w", err) }