From 439918c10d1920ebc6038db28b651f952739261c Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Mon, 5 Jun 2023 16:41:04 +0200 Subject: [PATCH] e2e: re-use `context.Context` for `PollUntilContextTimeout()` calls There are many locations where a new context is created. These can be reduced when subsequent calls re-use a previously created context object. Signed-off-by: Niels de Vos --- e2e/cephfs_helper.go | 4 ++-- e2e/deployment.go | 32 +++++++++++++++++--------------- e2e/namespace.go | 14 ++++++++------ e2e/nfs.go | 4 ++-- e2e/pod.go | 21 +++++++++++---------- e2e/pvc.go | 41 ++++++++++++++++++++++------------------- e2e/rbd_helper.go | 4 ++-- e2e/resize.go | 7 ++++--- e2e/snapshot.go | 27 +++++++++++++++------------ e2e/utils.go | 8 ++++---- 10 files changed, 87 insertions(+), 75 deletions(-) diff --git a/e2e/cephfs_helper.go b/e2e/cephfs_helper.go index cf0d756ac..b2b2e5596 100644 --- a/e2e/cephfs_helper.go +++ b/e2e/cephfs_helper.go @@ -102,8 +102,8 @@ func createCephfsStorageClass( timeout := time.Duration(deployTimeout) * time.Minute - return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) { - _, err = c.StorageV1().StorageClasses().Create(context.TODO(), &sc, metav1.CreateOptions{}) + return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(ctx context.Context) (bool, error) { + _, err = c.StorageV1().StorageClasses().Create(ctx, &sc, metav1.CreateOptions{}) if err != nil { framework.Logf("error creating StorageClass %q: %v", sc.Name, err) if isRetryableAPIError(err) { diff --git a/e2e/deployment.go b/e2e/deployment.go index 9ecb0e85b..98ee7ae1e 100644 --- a/e2e/deployment.go +++ b/e2e/deployment.go @@ -86,15 +86,16 @@ func createDeploymentApp(clientSet kubernetes.Interface, app *appsv1.Deployment, // deleteDeploymentApp deletes the deployment object. func deleteDeploymentApp(clientSet kubernetes.Interface, name, ns string, deployTimeout int) error { timeout := time.Duration(deployTimeout) * time.Minute - err := clientSet.AppsV1().Deployments(ns).Delete(context.TODO(), name, metav1.DeleteOptions{}) + ctx := context.TODO() + err := clientSet.AppsV1().Deployments(ns).Delete(ctx, name, metav1.DeleteOptions{}) if err != nil { return fmt.Errorf("failed to delete deployment: %w", err) } start := time.Now() framework.Logf("Waiting for deployment %q to be deleted", name) - return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) { - _, err := clientSet.AppsV1().Deployments(ns).Get(context.TODO(), name, metav1.GetOptions{}) + return wait.PollUntilContextTimeout(ctx, poll, timeout, true, func(ctx context.Context) (bool, error) { + _, err := clientSet.AppsV1().Deployments(ns).Get(ctx, name, metav1.GetOptions{}) if err != nil { if isRetryableAPIError(err) { return false, nil @@ -117,8 +118,8 @@ func waitForDeploymentInAvailableState(clientSet kubernetes.Interface, name, ns start := time.Now() framework.Logf("Waiting up to %q to be in Available state", name) - return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) { - d, err := clientSet.AppsV1().Deployments(ns).Get(context.TODO(), name, metav1.GetOptions{}) + return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(ctx context.Context) (bool, error) { + d, err := clientSet.AppsV1().Deployments(ns).Get(ctx, name, metav1.GetOptions{}) if err != nil { if isRetryableAPIError(err) { return false, nil @@ -144,8 +145,8 @@ func waitForDeploymentComplete(clientSet kubernetes.Interface, name, ns string, err error ) timeout := time.Duration(deployTimeout) * time.Minute - err = wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) { - deployment, err = clientSet.AppsV1().Deployments(ns).Get(context.TODO(), name, metav1.GetOptions{}) + err = wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(ctx context.Context) (bool, error) { + deployment, err = clientSet.AppsV1().Deployments(ns).Get(ctx, name, metav1.GetOptions{}) if err != nil { if isRetryableAPIError(err) { return false, nil @@ -310,8 +311,8 @@ func waitForDeploymentUpdateScale( ) error { t := time.Duration(timeout) * time.Minute start := time.Now() - err := wait.PollUntilContextTimeout(context.TODO(), poll, t, true, func(_ context.Context) (bool, error) { - scaleResult, upsErr := c.AppsV1().Deployments(ns).UpdateScale(context.TODO(), + err := wait.PollUntilContextTimeout(context.TODO(), poll, t, true, func(ctx context.Context) (bool, error) { + scaleResult, upsErr := c.AppsV1().Deployments(ns).UpdateScale(ctx, deploymentName, scale, metav1.UpdateOptions{}) if upsErr != nil { if isRetryableAPIError(upsErr) { @@ -346,9 +347,9 @@ func waitForDeploymentUpdate( ) error { t := time.Duration(timeout) * time.Minute start := time.Now() - err := wait.PollUntilContextTimeout(context.TODO(), poll, t, true, func(_ context.Context) (bool, error) { + err := wait.PollUntilContextTimeout(context.TODO(), poll, t, true, func(ctx context.Context) (bool, error) { _, upErr := c.AppsV1().Deployments(deployment.Namespace).Update( - context.TODO(), deployment, metav1.UpdateOptions{}) + ctx, deployment, metav1.UpdateOptions{}) if upErr != nil { if isRetryableAPIError(upErr) { return false, nil @@ -391,6 +392,7 @@ func waitForContainersArgsUpdate( timeout int, ) error { framework.Logf("waiting for deployment updates %s/%s", ns, deploymentName) + ctx := context.TODO() // wait for the deployment to be available err := waitForDeploymentInAvailableState(c, deploymentName, ns, deployTimeout) @@ -399,7 +401,7 @@ func waitForContainersArgsUpdate( } // Scale down to 0. - scale, err := c.AppsV1().Deployments(ns).GetScale(context.TODO(), deploymentName, metav1.GetOptions{}) + scale, err := c.AppsV1().Deployments(ns).GetScale(ctx, deploymentName, metav1.GetOptions{}) if err != nil { return fmt.Errorf("error get scale deployment %s/%s: %w", ns, deploymentName, err) } @@ -412,7 +414,7 @@ func waitForContainersArgsUpdate( } // Update deployment. - deployment, err := c.AppsV1().Deployments(ns).Get(context.TODO(), deploymentName, metav1.GetOptions{}) + deployment, err := c.AppsV1().Deployments(ns).Get(ctx, deploymentName, metav1.GetOptions{}) if err != nil { return fmt.Errorf("error get deployment %s/%s: %w", ns, deploymentName, err) } @@ -456,8 +458,8 @@ func waitForContainersArgsUpdate( // wait for scale to become count t := time.Duration(timeout) * time.Minute start := time.Now() - err = wait.PollUntilContextTimeout(context.TODO(), poll, t, true, func(_ context.Context) (bool, error) { - deploy, getErr := c.AppsV1().Deployments(ns).Get(context.TODO(), deploymentName, metav1.GetOptions{}) + err = wait.PollUntilContextTimeout(ctx, poll, t, true, func(ctx context.Context) (bool, error) { + deploy, getErr := c.AppsV1().Deployments(ns).Get(ctx, deploymentName, metav1.GetOptions{}) if getErr != nil { if isRetryableAPIError(getErr) { return false, nil diff --git a/e2e/namespace.go b/e2e/namespace.go index 964418342..21eafd15e 100644 --- a/e2e/namespace.go +++ b/e2e/namespace.go @@ -38,13 +38,14 @@ func createNamespace(c kubernetes.Interface, name string) error { Name: name, }, } - _, err := c.CoreV1().Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{}) + ctx := context.TODO() + _, err := c.CoreV1().Namespaces().Create(ctx, ns, metav1.CreateOptions{}) if err != nil && !apierrs.IsAlreadyExists(err) { return fmt.Errorf("failed to create namespace: %w", err) } - return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) { - _, err := c.CoreV1().Namespaces().Get(context.TODO(), name, metav1.GetOptions{}) + return wait.PollUntilContextTimeout(ctx, poll, timeout, true, func(ctx context.Context) (bool, error) { + _, err := c.CoreV1().Namespaces().Get(ctx, name, metav1.GetOptions{}) if err != nil { framework.Logf("Error getting namespace: '%s': %v", name, err) if apierrs.IsNotFound(err) { @@ -63,13 +64,14 @@ func createNamespace(c kubernetes.Interface, name string) error { func deleteNamespace(c kubernetes.Interface, name string) error { timeout := time.Duration(deployTimeout) * time.Minute - err := c.CoreV1().Namespaces().Delete(context.TODO(), name, metav1.DeleteOptions{}) + ctx := context.TODO() + err := c.CoreV1().Namespaces().Delete(ctx, name, metav1.DeleteOptions{}) if err != nil && !apierrs.IsNotFound(err) { return fmt.Errorf("failed to delete namespace: %w", err) } - return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) { - _, err = c.CoreV1().Namespaces().Get(context.TODO(), name, metav1.GetOptions{}) + return wait.PollUntilContextTimeout(ctx, poll, timeout, true, func(ctx context.Context) (bool, error) { + _, err = c.CoreV1().Namespaces().Get(ctx, name, metav1.GetOptions{}) if err != nil { if apierrs.IsNotFound(err) { return true, nil diff --git a/e2e/nfs.go b/e2e/nfs.go index 044a4cf5f..60d594e00 100644 --- a/e2e/nfs.go +++ b/e2e/nfs.go @@ -183,8 +183,8 @@ func createNFSStorageClass( timeout := time.Duration(deployTimeout) * time.Minute - return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) { - _, err = c.StorageV1().StorageClasses().Create(context.TODO(), &sc, metav1.CreateOptions{}) + return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(ctx context.Context) (bool, error) { + _, err = c.StorageV1().StorageClasses().Create(ctx, &sc, metav1.CreateOptions{}) if err != nil { framework.Logf("error creating StorageClass %q: %v", sc.Name, err) if apierrs.IsAlreadyExists(err) { diff --git a/e2e/pod.go b/e2e/pod.go index 5a8d379ca..e25430268 100644 --- a/e2e/pod.go +++ b/e2e/pod.go @@ -60,8 +60,8 @@ func waitForDaemonSets(name, ns string, c kubernetes.Interface, t int) error { start := time.Now() framework.Logf("Waiting up to %v for all daemonsets in namespace '%s' to start", timeout, ns) - return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) { - ds, err := c.AppsV1().DaemonSets(ns).Get(context.TODO(), name, metav1.GetOptions{}) + return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(ctx context.Context) (bool, error) { + ds, err := c.AppsV1().DaemonSets(ns).Get(ctx, name, metav1.GetOptions{}) if err != nil { framework.Logf("Error getting daemonsets in namespace: '%s': %v", ns, err) if strings.Contains(err.Error(), "not found") { @@ -97,8 +97,8 @@ func findPodAndContainerName(f *framework.Framework, ns, cn string, opt *metav1. podList *v1.PodList listErr error ) - err := wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) { - podList, listErr = e2epod.PodClientNS(f, ns).List(context.TODO(), *opt) + err := wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(ctx context.Context) (bool, error) { + podList, listErr = e2epod.PodClientNS(f, ns).List(ctx, *opt) if listErr != nil { if isRetryableAPIError(listErr) { return false, nil @@ -353,8 +353,8 @@ func waitForPodInRunningState(name, ns string, c kubernetes.Interface, t int, ex start := time.Now() framework.Logf("Waiting up to %v to be in Running state", name) - return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) { - pod, err := c.CoreV1().Pods(ns).Get(context.TODO(), name, metav1.GetOptions{}) + return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(ctx context.Context) (bool, error) { + pod, err := c.CoreV1().Pods(ns).Get(ctx, name, metav1.GetOptions{}) if err != nil { if isRetryableAPIError(err) { return false, nil @@ -369,7 +369,7 @@ func waitForPodInRunningState(name, ns string, c kubernetes.Interface, t int, ex return false, conditions.ErrPodCompleted case v1.PodPending: if expectedError != "" { - events, err := c.CoreV1().Events(ns).List(context.TODO(), metav1.ListOptions{ + events, err := c.CoreV1().Events(ns).List(ctx, metav1.ListOptions{ FieldSelector: fmt.Sprintf("involvedObject.name=%s", name), }) if err != nil { @@ -395,15 +395,16 @@ func waitForPodInRunningState(name, ns string, c kubernetes.Interface, t int, ex func deletePod(name, ns string, c kubernetes.Interface, t int) error { timeout := time.Duration(t) * time.Minute - err := c.CoreV1().Pods(ns).Delete(context.TODO(), name, metav1.DeleteOptions{}) + ctx := context.TODO() + err := c.CoreV1().Pods(ns).Delete(ctx, name, metav1.DeleteOptions{}) if err != nil { return fmt.Errorf("failed to delete app: %w", err) } start := time.Now() framework.Logf("Waiting for pod %v to be deleted", name) - return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) { - _, err := c.CoreV1().Pods(ns).Get(context.TODO(), name, metav1.GetOptions{}) + return wait.PollUntilContextTimeout(ctx, poll, timeout, true, func(ctx context.Context) (bool, error) { + _, err := c.CoreV1().Pods(ns).Get(ctx, name, metav1.GetOptions{}) if err != nil { if isRetryableAPIError(err) { return false, nil diff --git a/e2e/pvc.go b/e2e/pvc.go index 318fa8b5e..fe4fb9cda 100644 --- a/e2e/pvc.go +++ b/e2e/pvc.go @@ -44,9 +44,10 @@ func loadPVC(path string) (*v1.PersistentVolumeClaim, error) { func createPVCAndvalidatePV(c kubernetes.Interface, pvc *v1.PersistentVolumeClaim, t int) error { timeout := time.Duration(t) * time.Minute + ctx := context.TODO() pv := &v1.PersistentVolume{} var err error - _, err = c.CoreV1().PersistentVolumeClaims(pvc.Namespace).Create(context.TODO(), pvc, metav1.CreateOptions{}) + _, err = c.CoreV1().PersistentVolumeClaims(pvc.Namespace).Create(ctx, pvc, metav1.CreateOptions{}) if err != nil { return fmt.Errorf("failed to create pvc: %w", err) } @@ -58,9 +59,9 @@ func createPVCAndvalidatePV(c kubernetes.Interface, pvc *v1.PersistentVolumeClai start := time.Now() framework.Logf("Waiting up to %v to be in Bound state", pvc) - return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) { + return wait.PollUntilContextTimeout(ctx, poll, timeout, true, func(ctx context.Context) (bool, error) { framework.Logf("waiting for PVC %s (%d seconds elapsed)", name, int(time.Since(start).Seconds())) - pvc, err = c.CoreV1().PersistentVolumeClaims(namespace).Get(context.TODO(), name, metav1.GetOptions{}) + pvc, err = c.CoreV1().PersistentVolumeClaims(namespace).Get(ctx, name, metav1.GetOptions{}) if err != nil { framework.Logf("Error getting pvc %q in namespace %q: %v", name, namespace, err) if isRetryableAPIError(err) { @@ -77,7 +78,7 @@ func createPVCAndvalidatePV(c kubernetes.Interface, pvc *v1.PersistentVolumeClai return false, nil } - pv, err = c.CoreV1().PersistentVolumes().Get(context.TODO(), pvc.Spec.VolumeName, metav1.GetOptions{}) + pv, err = c.CoreV1().PersistentVolumes().Get(ctx, pvc.Spec.VolumeName, metav1.GetOptions{}) if err != nil { if isRetryableAPIError(err) { return false, nil @@ -89,7 +90,7 @@ func createPVCAndvalidatePV(c kubernetes.Interface, pvc *v1.PersistentVolumeClai return false, fmt.Errorf("failed to get pv: %w", err) } err = e2epv.WaitOnPVandPVC( - context.TODO(), + ctx, c, &framework.TimeoutContext{ClaimBound: timeout, PVBound: timeout}, namespace, @@ -117,11 +118,12 @@ func createPVCAndPV(c kubernetes.Interface, pvc *v1.PersistentVolumeClaim, pv *v } func deletePVCAndPV(c kubernetes.Interface, pvc *v1.PersistentVolumeClaim, pv *v1.PersistentVolume, t int) error { - err := c.CoreV1().PersistentVolumeClaims(pvc.Namespace).Delete(context.TODO(), pvc.Name, metav1.DeleteOptions{}) + ctx := context.TODO() + err := c.CoreV1().PersistentVolumeClaims(pvc.Namespace).Delete(ctx, pvc.Name, metav1.DeleteOptions{}) if err != nil { return fmt.Errorf("failed to delete pvc: %w", err) } - err = c.CoreV1().PersistentVolumes().Delete(context.TODO(), pv.Name, metav1.DeleteOptions{}) + err = c.CoreV1().PersistentVolumes().Delete(ctx, pv.Name, metav1.DeleteOptions{}) if err != nil { return fmt.Errorf("failed to delete pv: %w", err) } @@ -130,7 +132,7 @@ func deletePVCAndPV(c kubernetes.Interface, pvc *v1.PersistentVolumeClaim, pv *v start := time.Now() pvcToDelete := pvc - err = wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) { + err = wait.PollUntilContextTimeout(ctx, poll, timeout, true, func(ctx context.Context) (bool, error) { // Check that the PVC is deleted. framework.Logf( "waiting for PVC %s in state %s to be deleted (%d seconds elapsed)", @@ -139,7 +141,7 @@ func deletePVCAndPV(c kubernetes.Interface, pvc *v1.PersistentVolumeClaim, pv *v int(time.Since(start).Seconds())) pvcToDelete, err = c.CoreV1(). PersistentVolumeClaims(pvc.Namespace). - Get(context.TODO(), pvc.Name, metav1.GetOptions{}) + Get(ctx, pvc.Name, metav1.GetOptions{}) if err == nil { if pvcToDelete.Status.Phase == "" { // this is unexpected, an empty Phase is not defined @@ -168,7 +170,7 @@ func deletePVCAndPV(c kubernetes.Interface, pvc *v1.PersistentVolumeClaim, pv *v start = time.Now() pvToDelete := pv - return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) { + return wait.PollUntilContextTimeout(ctx, poll, timeout, true, func(ctx context.Context) (bool, error) { // Check that the PV is deleted. framework.Logf( "waiting for PV %s in state %s to be deleted (%d seconds elapsed)", @@ -176,7 +178,7 @@ func deletePVCAndPV(c kubernetes.Interface, pvc *v1.PersistentVolumeClaim, pv *v pvToDelete.Status.String(), int(time.Since(start).Seconds())) - pvToDelete, err = c.CoreV1().PersistentVolumes().Get(context.TODO(), pv.Name, metav1.GetOptions{}) + pvToDelete, err = c.CoreV1().PersistentVolumes().Get(ctx, pv.Name, metav1.GetOptions{}) if err == nil { return false, nil } @@ -202,8 +204,8 @@ func getPersistentVolumeClaim(c kubernetes.Interface, namespace, name string) (* 1*time.Second, timeout, true, - func(_ context.Context) (bool, error) { - pvc, err = c.CoreV1().PersistentVolumeClaims(namespace).Get(context.TODO(), name, metav1.GetOptions{}) + func(ctx context.Context) (bool, error) { + pvc, err = c.CoreV1().PersistentVolumeClaims(namespace).Get(ctx, name, metav1.GetOptions{}) if err != nil { framework.Logf("Error getting pvc %q in namespace %q: %v", name, namespace, err) if isRetryableAPIError(err) { @@ -230,8 +232,8 @@ func getPersistentVolume(c kubernetes.Interface, name string) (*v1.PersistentVol 1*time.Second, timeout, true, - func(_ context.Context) (bool, error) { - pv, err = c.CoreV1().PersistentVolumes().Get(context.TODO(), name, metav1.GetOptions{}) + func(ctx context.Context) (bool, error) { + pv, err = c.CoreV1().PersistentVolumes().Get(ctx, name, metav1.GetOptions{}) if err != nil { framework.Logf("Error getting pv %q: %v", name, err) if isRetryableAPIError(err) { @@ -267,6 +269,7 @@ func deletePVCAndValidatePV(c kubernetes.Interface, pvc *v1.PersistentVolumeClai timeout := time.Duration(t) * time.Minute nameSpace := pvc.Namespace name := pvc.Name + ctx := context.TODO() var err error framework.Logf("Deleting PersistentVolumeClaim %v on namespace %v", name, nameSpace) @@ -279,20 +282,20 @@ func deletePVCAndValidatePV(c kubernetes.Interface, pvc *v1.PersistentVolumeClai return fmt.Errorf("failed to get pv: %w", err) } - err = c.CoreV1().PersistentVolumeClaims(nameSpace).Delete(context.TODO(), name, metav1.DeleteOptions{}) + err = c.CoreV1().PersistentVolumeClaims(nameSpace).Delete(ctx, name, metav1.DeleteOptions{}) if err != nil { return fmt.Errorf("delete of PVC %v failed: %w", name, err) } start := time.Now() - return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) { + return wait.PollUntilContextTimeout(ctx, poll, timeout, true, func(ctx context.Context) (bool, error) { // Check that the PVC is really deleted. framework.Logf( "waiting for PVC %s in state %s to be deleted (%d seconds elapsed)", name, pvc.Status.String(), int(time.Since(start).Seconds())) - pvc, err = c.CoreV1().PersistentVolumeClaims(nameSpace).Get(context.TODO(), name, metav1.GetOptions{}) + pvc, err = c.CoreV1().PersistentVolumeClaims(nameSpace).Get(ctx, name, metav1.GetOptions{}) if err == nil { framework.Logf("PVC %s (status: %s) has not been deleted yet, rechecking...", name, pvc.Status) @@ -308,7 +311,7 @@ func deletePVCAndValidatePV(c kubernetes.Interface, pvc *v1.PersistentVolumeClai } // Examine the pv.ClaimRef and UID. Expect nil values. - oldPV, err := c.CoreV1().PersistentVolumes().Get(context.TODO(), pv.Name, metav1.GetOptions{}) + oldPV, err := c.CoreV1().PersistentVolumes().Get(ctx, pv.Name, metav1.GetOptions{}) if err == nil { framework.Logf("PV %s (status: %s) has not been deleted yet, rechecking...", pv.Name, oldPV.Status) diff --git a/e2e/rbd_helper.go b/e2e/rbd_helper.go index 3c58068d1..e72e45483 100644 --- a/e2e/rbd_helper.go +++ b/e2e/rbd_helper.go @@ -164,8 +164,8 @@ func createRBDStorageClass( timeout := time.Duration(deployTimeout) * time.Minute - return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) { - _, err = c.StorageV1().StorageClasses().Create(context.TODO(), &sc, metav1.CreateOptions{}) + return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(ctx context.Context) (bool, error) { + _, err = c.StorageV1().StorageClasses().Create(ctx, &sc, metav1.CreateOptions{}) if err != nil { framework.Logf("error creating StorageClass %q: %v", sc.Name, err) if isRetryableAPIError(err) { diff --git a/e2e/resize.go b/e2e/resize.go index e03148497..013bfb113 100644 --- a/e2e/resize.go +++ b/e2e/resize.go @@ -33,6 +33,7 @@ import ( ) func expandPVCSize(c kubernetes.Interface, pvc *v1.PersistentVolumeClaim, size string, t int) error { + ctx := context.TODO() pvcName := pvc.Name pvcNamespace := pvc.Namespace updatedPVC, err := getPersistentVolumeClaim(c, pvcNamespace, pvcName) @@ -44,17 +45,17 @@ func expandPVCSize(c kubernetes.Interface, pvc *v1.PersistentVolumeClaim, size s updatedPVC.Spec.Resources.Requests[v1.ResourceStorage] = resource.MustParse(size) _, err = c.CoreV1(). PersistentVolumeClaims(updatedPVC.Namespace). - Update(context.TODO(), updatedPVC, metav1.UpdateOptions{}) + Update(ctx, updatedPVC, metav1.UpdateOptions{}) Expect(err).ShouldNot(HaveOccurred()) start := time.Now() framework.Logf("Waiting up to %v to be in Resized state", pvc) - return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) { + return wait.PollUntilContextTimeout(ctx, poll, timeout, true, func(ctx context.Context) (bool, error) { framework.Logf("waiting for PVC %s (%d seconds elapsed)", pvcName, int(time.Since(start).Seconds())) updatedPVC, err = c.CoreV1(). PersistentVolumeClaims(pvcNamespace). - Get(context.TODO(), pvcName, metav1.GetOptions{}) + Get(ctx, pvcName, metav1.GetOptions{}) if err != nil { framework.Logf("Error getting pvc in namespace: '%s': %v", pvcNamespace, err) if isRetryableAPIError(err) { diff --git a/e2e/snapshot.go b/e2e/snapshot.go index 45ae7964a..4965cbf98 100644 --- a/e2e/snapshot.go +++ b/e2e/snapshot.go @@ -67,9 +67,10 @@ func createSnapshot(snap *snapapi.VolumeSnapshot, t int) error { return err } + ctx := context.TODO() _, err = sclient. VolumeSnapshots(snap.Namespace). - Create(context.TODO(), snap, metav1.CreateOptions{}) + Create(ctx, snap, metav1.CreateOptions{}) if err != nil { return fmt.Errorf("failed to create volumesnapshot: %w", err) } @@ -80,11 +81,11 @@ func createSnapshot(snap *snapapi.VolumeSnapshot, t int) error { start := time.Now() framework.Logf("waiting for %v to be in ready state", snap) - return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) { + return wait.PollUntilContextTimeout(ctx, poll, timeout, true, func(ctx context.Context) (bool, error) { framework.Logf("waiting for snapshot %s (%d seconds elapsed)", snap.Name, int(time.Since(start).Seconds())) snaps, err := sclient. VolumeSnapshots(snap.Namespace). - Get(context.TODO(), name, metav1.GetOptions{}) + Get(ctx, name, metav1.GetOptions{}) if err != nil { framework.Logf("Error getting snapshot in namespace: '%s': %v", snap.Namespace, err) if isRetryableAPIError(err) { @@ -114,9 +115,10 @@ func deleteSnapshot(snap *snapapi.VolumeSnapshot, t int) error { return err } + ctx := context.TODO() err = sclient. VolumeSnapshots(snap.Namespace). - Delete(context.TODO(), snap.Name, metav1.DeleteOptions{}) + Delete(ctx, snap.Name, metav1.DeleteOptions{}) if err != nil { return fmt.Errorf("failed to delete volumesnapshot: %w", err) } @@ -126,11 +128,11 @@ func deleteSnapshot(snap *snapapi.VolumeSnapshot, t int) error { start := time.Now() framework.Logf("Waiting up to %v to be deleted", snap) - return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) { + return wait.PollUntilContextTimeout(ctx, poll, timeout, true, func(ctx context.Context) (bool, error) { framework.Logf("deleting snapshot %s (%d seconds elapsed)", name, int(time.Since(start).Seconds())) _, err := sclient. VolumeSnapshots(snap.Namespace). - Get(context.TODO(), name, metav1.GetOptions{}) + Get(ctx, name, metav1.GetOptions{}) if err == nil { return false, nil } @@ -223,8 +225,8 @@ func createNFSSnapshotClass(f *framework.Framework) error { timeout := time.Duration(deployTimeout) * time.Minute - return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) { - _, err = sclient.VolumeSnapshotClasses().Create(context.TODO(), &sc, metav1.CreateOptions{}) + return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(ctx context.Context) (bool, error) { + _, err = sclient.VolumeSnapshotClasses().Create(ctx, &sc, metav1.CreateOptions{}) if err != nil { framework.Logf("error creating SnapshotClass %q: %v", sc.Name, err) if apierrs.IsAlreadyExists(err) { @@ -252,8 +254,8 @@ func deleteNFSSnapshotClass() error { timeout := time.Duration(deployTimeout) * time.Minute - return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) { - err = sclient.VolumeSnapshotClasses().Delete(context.TODO(), sc.Name, metav1.DeleteOptions{}) + return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(ctx context.Context) (bool, error) { + err = sclient.VolumeSnapshotClasses().Delete(ctx, sc.Name, metav1.DeleteOptions{}) if err != nil { framework.Logf("error deleting SnapshotClass %q: %v", sc.Name, err) if apierrs.IsNotFound(err) { @@ -276,16 +278,17 @@ func getVolumeSnapshotContent(namespace, snapshotName string) (*snapapi.VolumeSn return nil, err } + ctx := context.TODO() snapshot, err := sclient. VolumeSnapshots(namespace). - Get(context.TODO(), snapshotName, metav1.GetOptions{}) + Get(ctx, snapshotName, metav1.GetOptions{}) if err != nil { return nil, fmt.Errorf("failed to get volumesnapshot: %w", err) } volumeSnapshotContent, err := sclient. VolumeSnapshotContents(). - Get(context.TODO(), *snapshot.Status.BoundVolumeSnapshotContentName, metav1.GetOptions{}) + Get(ctx, *snapshot.Status.BoundVolumeSnapshotContentName, metav1.GetOptions{}) if err != nil { return nil, fmt.Errorf("failed to get volumesnapshotcontent: %w", err) } diff --git a/e2e/utils.go b/e2e/utils.go index bc9cc4f3a..d79d0e2c3 100644 --- a/e2e/utils.go +++ b/e2e/utils.go @@ -248,9 +248,9 @@ func getMons(ns string, c kubernetes.Interface) ([]string, error) { var svcList *v1.ServiceList t := time.Duration(deployTimeout) * time.Minute - err := wait.PollUntilContextTimeout(context.TODO(), poll, t, true, func(_ context.Context) (bool, error) { + err := wait.PollUntilContextTimeout(context.TODO(), poll, t, true, func(ctx context.Context) (bool, error) { var svcErr error - svcList, svcErr = c.CoreV1().Services(ns).List(context.TODO(), opt) + svcList, svcErr = c.CoreV1().Services(ns).List(ctx, opt) if svcErr != nil { if isRetryableAPIError(svcErr) { return false, nil @@ -1560,8 +1560,8 @@ func waitForJobCompletion(c kubernetes.Interface, ns, job string, timeout int) e framework.Logf("waiting for Job %s/%s to be in state %q", ns, job, batch.JobComplete) - return wait.PollUntilContextTimeout(context.TODO(), poll, t, true, func(_ context.Context) (bool, error) { - j, err := c.BatchV1().Jobs(ns).Get(context.TODO(), job, metav1.GetOptions{}) + return wait.PollUntilContextTimeout(context.TODO(), poll, t, true, func(ctx context.Context) (bool, error) { + j, err := c.BatchV1().Jobs(ns).Get(ctx, job, metav1.GetOptions{}) if err != nil { if isRetryableAPIError(err) { return false, nil