diff --git a/test/common/crudtester.go b/test/common/crudtester.go index df233a3785..3e3513c721 100644 --- a/test/common/crudtester.go +++ b/test/common/crudtester.go @@ -274,10 +274,10 @@ func (c *FederatedTypeCrudTester) CheckDelete(fedObject *unstructured.Unstructur if orphanDependents { orphanKey := util.OrphanManagedResourcesAnnotation - err := wait.PollImmediate(c.waitInterval, wait.ForeverTestTimeout, func() (bool, error) { + err := wait.PollUntilContextTimeout(context.Background(), c.waitInterval, wait.ForeverTestTimeout, true, func(ctx context.Context) (bool, error) { var err error if fedObject == nil { - fedObject, err = client.Resources(namespace).Get(context.Background(), name, metav1.GetOptions{}) + fedObject, err = client.Resources(namespace).Get(ctx, name, metav1.GetOptions{}) if err != nil { c.tl.Logf("Error retrieving %s %q to add the %q annotation: %v", federatedKind, qualifiedName, orphanKey, err) return false, nil @@ -287,7 +287,7 @@ func (c *FederatedTypeCrudTester) CheckDelete(fedObject *unstructured.Unstructur return true, nil } util.EnableOrphaning(fedObject) - fedObject, err = client.Resources(namespace).Update(context.Background(), fedObject, metav1.UpdateOptions{}) + fedObject, err = client.Resources(namespace).Update(ctx, fedObject, metav1.UpdateOptions{}) if err == nil { return true, nil } @@ -317,8 +317,8 @@ func (c *FederatedTypeCrudTester) CheckDelete(fedObject *unstructured.Unstructur // Wait for deletion. The federated resource will only be removed once managed resources have // been deleted or orphaned. - err = wait.PollImmediate(c.waitInterval, waitTimeout, func() (bool, error) { - _, err := client.Resources(namespace).Get(context.Background(), name, metav1.GetOptions{}) + err = wait.PollUntilContextTimeout(context.Background(), c.waitInterval, waitTimeout, true, func(ctx context.Context) (bool, error) { + _, err := client.Resources(namespace).Get(ctx, name, metav1.GetOptions{}) if apierrors.IsNotFound(err) { return true, nil } @@ -349,8 +349,8 @@ func (c *FederatedTypeCrudTester) CheckDelete(fedObject *unstructured.Unstructur continue } namespace = util.QualifiedNameForCluster(clusterName, qualifiedName).Namespace - err = wait.PollImmediate(c.waitInterval, waitTimeout, func() (bool, error) { - obj, err := testCluster.Client.Resources(namespace).Get(context.Background(), name, metav1.GetOptions{}) + err = wait.PollUntilContextTimeout(context.Background(), c.waitInterval, waitTimeout, true, func(ctx context.Context) (bool, error) { + obj, err := testCluster.Client.Resources(namespace).Get(ctx, name, metav1.GetOptions{}) switch { case !deletingInCluster && apierrors.IsNotFound(err): return false, errors.Errorf("%s %q was unexpectedly deleted from cluster %q", targetKind, qualifiedName, clusterName) @@ -405,16 +405,16 @@ func (c *FederatedTypeCrudTester) CheckReplicaSet(fedObject *unstructured.Unstru clusterConfig := c.testClusters[clusterName].Config kubeClient := kubeclientset.NewForConfigOrDie(clusterConfig) - WaitForNamespaceOrDie(c.tl, kubeClient, clusterName, fedObject.GetNamespace(), + WaitForNamespace(c.tl, kubeClient, clusterName, fedObject.GetNamespace(), c.waitInterval, 30*time.Second) clusterClient := genericclient.NewForConfigOrDie(clusterConfig) c.tl.Log("Checking that the ReplicaSet still exists in every cluster") - err := wait.PollImmediate(c.waitInterval, wait.ForeverTestTimeout, func() (bool, error) { + err := wait.PollUntilContextTimeout(context.TODO(), c.waitInterval, wait.ForeverTestTimeout, true, func(ctx context.Context) (bool, error) { objList := &appsv1.ReplicaSetList{} - err := clusterClient.List(context.TODO(), objList, fedObject.GetNamespace(), matchingLabels) + err := clusterClient.List(ctx, objList, fedObject.GetNamespace(), matchingLabels) if err != nil { return false, errors.Errorf("Error retrieving ReplicatSet: %v", err) } @@ -502,10 +502,9 @@ func (c *FederatedTypeCrudTester) CheckPropagation(fedObject *unstructured.Unstr version, _ := c.expectedVersion(qualifiedName, templateVersion, overrideVersion, clusterName) return version }) - switch { - case err == wait.ErrWaitTimeout: + if wait.Interrupted(err) { c.tl.Fatalf("Timeout verifying %s %q in cluster %q: %v", targetKind, targetName, clusterName, err) - case err != nil: + } else if err != nil { c.tl.Fatalf("Failed to verify %s %q in cluster %q: %v", targetKind, targetName, clusterName, err) } case c.targetIsNamespace && clusterName == primaryClusterName: @@ -517,12 +516,11 @@ func (c *FederatedTypeCrudTester) CheckPropagation(fedObject *unstructured.Unstr }) // Once resource deletion is complete, wait for the status to reflect the deletion - switch { - case err == wait.ErrWaitTimeout: + if wait.Interrupted(err) { if objExpected { c.tl.Fatalf("Timeout verifying deletion of %s %q in cluster %q: %v", targetKind, targetName, clusterName, err) } - case err != nil: + } else if err != nil { c.tl.Fatalf("Failed to verify deletion of %s %q in cluster %q: %v", targetKind, targetName, clusterName, err) } } @@ -530,8 +528,8 @@ func (c *FederatedTypeCrudTester) CheckPropagation(fedObject *unstructured.Unstr // Use a longer wait interval to avoid spamming the test log. waitInterval := 1 * time.Second var waitingForError error - err := wait.PollImmediate(waitInterval, c.clusterWaitTimeout, func() (bool, error) { - ok, err := c.checkFederatedStatus(fedObject, clusterName, objExpected) + err := wait.PollUntilContextTimeout(context.TODO(), waitInterval, c.clusterWaitTimeout, true, func(ctx context.Context) (bool, error) { + ok, err := c.checkFederatedStatus(ctx, fedObject, clusterName, objExpected) if err != nil { // Logging lots of waiting messages would clutter the // logs. Instead, track the most recent message @@ -555,13 +553,13 @@ func (c *FederatedTypeCrudTester) CheckPropagation(fedObject *unstructured.Unstr // checkFederatedStatus ensures that the federated resource status // reflects the expected propagation state. -func (c *FederatedTypeCrudTester) checkFederatedStatus(fedObject *unstructured.Unstructured, clusterName string, objExpected bool) (bool, error) { +func (c *FederatedTypeCrudTester) checkFederatedStatus(ctx context.Context, fedObject *unstructured.Unstructured, clusterName string, objExpected bool) (bool, error) { federatedKind := fedObject.GetKind() qualifiedName := util.NewQualifiedName(fedObject) // Retrieve the resource from the API to ensure the latest status // is considered. - resource, err := GetGenericResource(c.client, fedObject.GroupVersionKind(), qualifiedName) + resource, err := GetGenericResource(ctx, c.client, fedObject.GroupVersionKind(), qualifiedName) if err != nil { return false, err } @@ -620,8 +618,8 @@ func (c *FederatedTypeCrudTester) checkHostNamespaceUnlabeled(client util.Resour // A namespace in the host cluster should end up unlabeled instead of // deleted when it is not targeted by placement. - err := wait.PollImmediate(c.waitInterval, c.clusterWaitTimeout, func() (bool, error) { - hostNamespace, err := client.Resources("").Get(context.Background(), qualifiedName.Name, metav1.GetOptions{}) + err := wait.PollUntilContextTimeout(context.Background(), c.waitInterval, c.clusterWaitTimeout, true, func(ctx context.Context) (bool, error) { + hostNamespace, err := client.Resources("").Get(ctx, qualifiedName.Name, metav1.GetOptions{}) if err != nil { c.tl.Errorf("Error retrieving %s %q in host cluster %q: %v", targetKind, qualifiedName, clusterName, err) return false, nil @@ -635,13 +633,13 @@ func (c *FederatedTypeCrudTester) checkHostNamespaceUnlabeled(client util.Resour } func (c *FederatedTypeCrudTester) waitForResource(client util.ResourceClient, qualifiedName util.QualifiedName, expectedOverrides util.ClusterOverrides, expectedVersionFunc func() string) error { - err := wait.PollImmediate(c.waitInterval, c.clusterWaitTimeout, func() (bool, error) { + err := wait.PollUntilContextTimeout(context.Background(), c.waitInterval, c.clusterWaitTimeout, true, func(ctx context.Context) (bool, error) { expectedVersion := expectedVersionFunc() if len(expectedVersion) == 0 { return false, nil } - clusterObj, err := client.Resources(qualifiedName.Namespace).Get(context.Background(), qualifiedName.Name, metav1.GetOptions{}) + clusterObj, err := client.Resources(qualifiedName.Namespace).Get(ctx, qualifiedName.Name, metav1.GetOptions{}) if err == nil && util.ObjectVersion(clusterObj) == expectedVersion { // Validate that the resource has been labeled properly, // indicating creation or adoption by the sync controller. This @@ -699,8 +697,8 @@ func (c *FederatedTypeCrudTester) TestClusters() map[string]TestCluster { } func (c *FederatedTypeCrudTester) waitForResourceDeletion(client util.ResourceClient, qualifiedName util.QualifiedName, versionRemoved func() bool) error { - err := wait.PollImmediate(c.waitInterval, c.clusterWaitTimeout, func() (bool, error) { - _, err := client.Resources(qualifiedName.Namespace).Get(context.Background(), qualifiedName.Name, metav1.GetOptions{}) + err := wait.PollUntilContextTimeout(context.Background(), c.waitInterval, c.clusterWaitTimeout, true, func(ctx context.Context) (bool, error) { + _, err := client.Resources(qualifiedName.Namespace).Get(ctx, qualifiedName.Name, metav1.GetOptions{}) if apierrors.IsNotFound(err) { if !versionRemoved() { c.tl.Logf("Removal of %q %s successful, but propagated version still exists", c.typeConfig.GetTargetType().Kind, qualifiedName) @@ -719,22 +717,22 @@ func (c *FederatedTypeCrudTester) waitForResourceDeletion(client util.ResourceCl func (c *FederatedTypeCrudTester) updateObject(apiResource metav1.APIResource, obj *unstructured.Unstructured, mutateResourceFunc func(*unstructured.Unstructured)) (*unstructured.Unstructured, error) { client := c.resourceClient(apiResource) var updatedObj *unstructured.Unstructured - err := wait.PollImmediate(c.waitInterval, wait.ForeverTestTimeout, func() (bool, error) { + err := wait.PollUntilContextTimeout(context.TODO(), c.waitInterval, wait.ForeverTestTimeout, true, func(ctx context.Context) (bool, error) { mutateResourceFunc(obj) var err error - updatedObj, err = client.Resources(obj.GetNamespace()).Update(context.Background(), obj, metav1.UpdateOptions{}) + updatedObj, err = client.Resources(obj.GetNamespace()).Update(ctx, obj, metav1.UpdateOptions{}) if apierrors.IsConflict(err) { // The resource was updated by the KubeFed controller. // Get the latest version and retry. - obj, err = client.Resources(obj.GetNamespace()).Get(context.Background(), obj.GetName(), metav1.GetOptions{}) + obj, err = client.Resources(obj.GetNamespace()).Get(ctx, obj.GetName(), metav1.GetOptions{}) return false, err } // Be tolerant of a slow server if apierrors.IsServerTimeout(err) { return false, nil } - return (err == nil), err + return err == nil, err }) return updatedObj, err } @@ -753,9 +751,9 @@ func (c *FederatedTypeCrudTester) expectedVersion(qualifiedName util.QualifiedNa loggedWaiting := false adapter := versionmanager.NewVersionAdapter(c.typeConfig.GetFederatedNamespaced()) var version *fedv1a1.PropagatedVersionStatus - err := wait.PollImmediate(c.waitInterval, wait.ForeverTestTimeout, func() (bool, error) { + err := wait.PollUntilContextTimeout(context.TODO(), c.waitInterval, wait.ForeverTestTimeout, true, func(ctx context.Context) (bool, error) { versionObj := adapter.NewObject() - err := c.client.Get(context.TODO(), versionObj, versionName.Namespace, versionName.Name) + err := c.client.Get(ctx, versionObj, versionName.Namespace, versionName.Name) if apierrors.IsNotFound(err) { if !loggedWaiting { loggedWaiting = true @@ -821,7 +819,7 @@ func (c *FederatedTypeCrudTester) CheckRemoteStatus(fedObject *unstructured.Unst clusterConfig := c.testClusters[clusterName].Config kubeClient := kubeclientset.NewForConfigOrDie(clusterConfig) - WaitForNamespaceOrDie(c.tl, kubeClient, clusterName, targetObject.GetNamespace(), + WaitForNamespace(c.tl, kubeClient, clusterName, targetObject.GetNamespace(), c.waitInterval, 30*time.Second) util.AddManagedLabel(targetObject) @@ -834,10 +832,10 @@ func (c *FederatedTypeCrudTester) CheckRemoteStatus(fedObject *unstructured.Unst c.tl.Log("Checking that the resource has status") var objStatus interface{} - err = wait.PollImmediate(c.waitInterval, wait.ForeverTestTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(context.TODO(), c.waitInterval, wait.ForeverTestTimeout, true, func(ctx context.Context) (bool, error) { obj := &unstructured.Unstructured{} obj.SetGroupVersionKind(labeledObj.GroupVersionKind()) - err := clusterClient.Get(context.TODO(), obj, labeledObj.GetNamespace(), labeledObj.GetName()) + err := clusterClient.Get(ctx, obj, labeledObj.GetNamespace(), labeledObj.GetName()) if err != nil { c.tl.Errorf("Error retrieving kubefed cluster object resource: %v", err) return false, nil @@ -845,7 +843,7 @@ func (c *FederatedTypeCrudTester) CheckRemoteStatus(fedObject *unstructured.Unst objStatus = obj.Object[util.StatusField] c.tl.Logf("Show kubefed cluster object status: %v", objStatus) - return (obj.Object[util.StatusField] != nil), nil + return obj.Object[util.StatusField] != nil, nil }) if err != nil { c.tl.Fatal("Timed out waiting for the resource to have a status field") @@ -877,8 +875,8 @@ func (c *FederatedTypeCrudTester) getRemoteStatus(fedObject *unstructured.Unstru var remoteStatusObj interface{} // The default is normally 30 seconds waitTimeout := 6 * wait.ForeverTestTimeout - err := wait.PollImmediate(c.waitInterval, waitTimeout, func() (bool, error) { - fedObj, err := client.Resources(qualifiedName.Namespace).Get(context.Background(), qualifiedName.Name, metav1.GetOptions{}) + err := wait.PollUntilContextTimeout(context.Background(), c.waitInterval, waitTimeout, true, func(ctx context.Context) (bool, error) { + fedObj, err := client.Resources(qualifiedName.Namespace).Get(ctx, qualifiedName.Name, metav1.GetOptions{}) if err != nil { c.tl.Errorf("An unexpected error occurred while polling for remote status: %v", err) return false, nil @@ -928,12 +926,12 @@ func (c *FederatedTypeCrudTester) CheckStatusCreated(qualifiedName util.Qualifie c.tl.Logf("Checking creation of %s %q", statusKind, qualifiedName) client := c.resourceClient(*statusAPIResource) - err := wait.PollImmediate(c.waitInterval, wait.ForeverTestTimeout, func() (bool, error) { - _, err := client.Resources(qualifiedName.Namespace).Get(context.Background(), qualifiedName.Name, metav1.GetOptions{}) + err := wait.PollUntilContextTimeout(context.Background(), c.waitInterval, wait.ForeverTestTimeout, true, func(ctx context.Context) (bool, error) { + _, err := client.Resources(qualifiedName.Namespace).Get(ctx, qualifiedName.Name, metav1.GetOptions{}) if err != nil && !apierrors.IsNotFound(err) { c.tl.Errorf("An unexpected error occurred while polling for desired status: %v", err) } - return (err == nil), nil + return err == nil, nil }) if err != nil { @@ -944,11 +942,10 @@ func (c *FederatedTypeCrudTester) CheckStatusCreated(qualifiedName util.Qualifie // GetGenericResource retrieves a federated resource and converts it to // the generic resource struct. -func GetGenericResource(client genericclient.Client, gvk schema.GroupVersionKind, - qualifiedName util.QualifiedName) (*status.GenericFederatedResource, error) { +func GetGenericResource(ctx context.Context, client genericclient.Client, gvk schema.GroupVersionKind, qualifiedName util.QualifiedName) (*status.GenericFederatedResource, error) { fedObject := &unstructured.Unstructured{} fedObject.SetGroupVersionKind(gvk) - err := client.Get(context.TODO(), fedObject, qualifiedName.Namespace, qualifiedName.Name) + err := client.Get(ctx, fedObject, qualifiedName.Namespace, qualifiedName.Name) if err != nil { return nil, errors.Wrapf(err, "Failed to retrieve federated resource from the API") } diff --git a/test/common/util.go b/test/common/util.go index 3cde4510b2..82c849c22f 100644 --- a/test/common/util.go +++ b/test/common/util.go @@ -52,9 +52,9 @@ func Equivalent(actual, desired runtimeclient.Object) bool { } // WaitForNamespace waits for namespace to be created in a cluster. -func WaitForNamespaceOrDie(tl TestLogger, client kubeclientset.Interface, clusterName, namespace string, interval, timeout time.Duration) { - err := wait.PollImmediate(interval, timeout, func() (exist bool, err error) { - _, err = client.CoreV1().Namespaces().Get(context.Background(), namespace, metav1.GetOptions{}) +func WaitForNamespace(tl TestLogger, client kubeclientset.Interface, clusterName, namespace string, interval, timeout time.Duration) { + err := wait.PollUntilContextTimeout(context.Background(), interval, timeout, true, func(ctx context.Context) (exist bool, err error) { + _, err = client.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{}) if errors.IsNotFound(err) { return false, nil } diff --git a/test/e2e/crd.go b/test/e2e/crd.go index c114a9dd66..5cb37f227a 100644 --- a/test/e2e/crd.go +++ b/test/e2e/crd.go @@ -127,12 +127,12 @@ func validateCrdCrud(f framework.KubeFedFramework, targetCrdKind string, namespa } targetName := targetAPIResource.Name - err := wait.PollImmediate(framework.PollInterval, framework.TestContext.SingleCallTimeout, func() (bool, error) { + err := wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, framework.TestContext.SingleCallTimeout, true, func(_ context.Context) (bool, error) { _, err := kfenable.LookupAPIResource(hostConfig, targetName, targetAPIResource.Version) if err != nil { tl.Logf("An error was reported while waiting for target type %q to be published as an available resource: %v", targetName, err) } - return (err == nil), nil + return err == nil, nil }) if err != nil { tl.Fatalf("Timed out waiting for target type %q to be published as an available resource", targetName) @@ -239,12 +239,12 @@ func waitForCrd(config *rest.Config, tl common.TestLogger, apiResource metav1.AP if err != nil { tl.Fatalf("Error creating client for crd %q: %v", apiResource.Kind, err) } - err = wait.PollImmediate(framework.PollInterval, framework.TestContext.SingleCallTimeout, func() (bool, error) { - _, err := client.Resources("invalid").Get(context.Background(), "invalid", metav1.GetOptions{}) + err = wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, framework.TestContext.SingleCallTimeout, true, func(ctx context.Context) (bool, error) { + _, err := client.Resources("invalid").Get(ctx, "invalid", metav1.GetOptions{}) if apierrors.IsNotFound(err) { return true, nil } - return (err == nil), err + return err == nil, err }) if err != nil { tl.Fatalf("Error waiting for crd %q to become established: %v", apiResource.Kind, err) diff --git a/test/e2e/crud.go b/test/e2e/crud.go index b8746977f0..17259f8f26 100644 --- a/test/e2e/crud.go +++ b/test/e2e/crud.go @@ -116,8 +116,8 @@ var _ = Describe("Federated", func() { By(fmt.Sprintf("Waiting until the status of the %s %q indicates NamespaceNotFederated", kind, qualifiedName)) client := genericclient.NewForConfigOrDie(f.KubeConfig()) - err := wait.PollImmediate(framework.PollInterval, wait.ForeverTestTimeout, func() (bool, error) { - genericResource, err := common.GetGenericResource(client, fedObject.GroupVersionKind(), qualifiedName) + err := wait.PollUntilContextTimeout(context.TODO(), framework.PollInterval, wait.ForeverTestTimeout, true, func(ctx context.Context) (bool, error) { + genericResource, err := common.GetGenericResource(ctx, client, fedObject.GroupVersionKind(), qualifiedName) if err != nil { tl.Fatalf("An error occurred retrieving the status of the %s %q: %v", kind, qualifiedName, err) } @@ -157,7 +157,7 @@ var _ = Describe("Federated", func() { By("Waiting for the test namespace to be created in the selected cluster") kubeClient := kubeclientset.NewForConfigOrDie(clusterConfig) - common.WaitForNamespaceOrDie(tl, kubeClient, clusterName, targetObject.GetNamespace(), + common.WaitForNamespace(tl, kubeClient, clusterName, targetObject.GetNamespace(), framework.PollInterval, framework.TestContext.SingleCallTimeout) By("Creating a labeled resource in the selected cluster") @@ -175,10 +175,10 @@ var _ = Describe("Federated", func() { }() By("Checking that the labeled resource is unlabeled by the sync controller") - err = wait.PollImmediate(framework.PollInterval, wait.ForeverTestTimeout, func() (bool, error) { + err = wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, wait.ForeverTestTimeout, true, func(ctx context.Context) (bool, error) { obj := &unstructured.Unstructured{} obj.SetGroupVersionKind(labeledObj.GroupVersionKind()) - err := clusterClient.Get(context.TODO(), obj, labeledObj.GetNamespace(), labeledObj.GetName()) + err := clusterClient.Get(ctx, obj, labeledObj.GetNamespace(), labeledObj.GetName()) if err != nil { tl.Errorf("Error retrieving labeled resource: %v", err) return false, nil @@ -206,7 +206,7 @@ var _ = Describe("Federated", func() { By("Waiting for the test namespace to be created in the selected cluster") kubeClient := kubeclientset.NewForConfigOrDie(clusterConfig) - common.WaitForNamespaceOrDie(tl, kubeClient, clusterName, targetObject.GetNamespace(), + common.WaitForNamespace(tl, kubeClient, clusterName, targetObject.GetNamespace(), framework.PollInterval, framework.TestContext.SingleCallTimeout) By("Creating an unlabeled resource in the selected cluster") @@ -248,10 +248,10 @@ var _ = Describe("Federated", func() { waitDuration := 10 * time.Second // Arbitrary amount of time to wait for deletion By(fmt.Sprintf("Checking that the unlabeled resource is not deleted within %v", waitDuration)) - _ = wait.PollImmediate(framework.PollInterval, waitDuration, func() (bool, error) { + _ = wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, waitDuration, true, func(ctx context.Context) (bool, error) { obj := &unstructured.Unstructured{} obj.SetGroupVersionKind(unlabeledObj.GroupVersionKind()) - err := clusterClient.Get(context.TODO(), obj, unlabeledObj.GetNamespace(), unlabeledObj.GetName()) + err := clusterClient.Get(ctx, obj, unlabeledObj.GetNamespace(), unlabeledObj.GetName()) if apierrors.IsNotFound(err) { tl.Fatalf("Unlabeled resource %s %q was deleted", typeConfig.GetTargetType().Kind, util.NewQualifiedName(unlabeledObj)) } diff --git a/test/e2e/federate.go b/test/e2e/federate.go index ef3dc54270..11e8f5a4b7 100644 --- a/test/e2e/federate.go +++ b/test/e2e/federate.go @@ -298,8 +298,8 @@ func deleteResource(tl common.TestLogger, client util.ResourceClient, qualifiedN tl.Fatalf("Error deleting %s %q: %v", kind, qualifiedName, err) } - err = wait.PollImmediate(framework.PollInterval, framework.TestContext.SingleCallTimeout, func() (bool, error) { - _, err := client.Resources(qualifiedName.Namespace).Get(context.Background(), qualifiedName.Name, metav1.GetOptions{}) + err = wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, framework.TestContext.SingleCallTimeout, true, func(ctx context.Context) (bool, error) { + _, err := client.Resources(qualifiedName.Namespace).Get(ctx, qualifiedName.Name, metav1.GetOptions{}) if apierrors.IsNotFound(err) { return true, nil } diff --git a/test/e2e/framework/cluster.go b/test/e2e/framework/cluster.go index 1af15ab14b..671cc435cb 100644 --- a/test/e2e/framework/cluster.go +++ b/test/e2e/framework/cluster.go @@ -56,9 +56,9 @@ func clusterIsReadyOrFail(tl common.TestLogger, client genericclient.Client, if util.IsClusterReady(&cluster.Status) { return } - err := wait.Poll(interval, timeout, func() (bool, error) { + err := wait.PollUntilContextTimeout(context.TODO(), interval, timeout, true, func(ctx context.Context) (bool, error) { cluster := &fedv1b1.KubeFedCluster{} - err := client.Get(context.TODO(), cluster, namespace, clusterName) + err := client.Get(ctx, cluster, namespace, clusterName) if err != nil { return false, err } diff --git a/test/e2e/framework/framework.go b/test/e2e/framework/framework.go index 3157e00439..9de4303e33 100644 --- a/test/e2e/framework/framework.go +++ b/test/e2e/framework/framework.go @@ -311,8 +311,8 @@ func CreateNamespace(client kubeclientset.Interface, generateName string) (strin // Be robust about making the namespace creation call. // TODO(marun) should all api calls be made 'robustly'? var namespaceName string - if err := wait.PollImmediate(PollInterval, TestContext.SingleCallTimeout, func() (bool, error) { - namespace, err := client.CoreV1().Namespaces().Create(context.Background(), namespaceObj, metav1.CreateOptions{}) + if err := wait.PollUntilContextTimeout(context.Background(), PollInterval, TestContext.SingleCallTimeout, true, func(ctx context.Context) (bool, error) { + namespace, err := client.CoreV1().Namespaces().Create(ctx, namespaceObj, metav1.CreateOptions{}) if err != nil { Logf("Unexpected error while creating namespace: %v", err) return false, nil diff --git a/test/e2e/framework/test_context.go b/test/e2e/framework/test_context.go index 74bbd2cb02..0c009dc7ae 100644 --- a/test/e2e/framework/test_context.go +++ b/test/e2e/framework/test_context.go @@ -54,7 +54,7 @@ func (t *TestContextType) NamespaceScopedControlPlane() bool { return t.InMemoryControllers && t.LimitedScopeInMemoryControllers || t.LimitedScope } -var TestContext *TestContextType = &TestContextType{} +var TestContext = &TestContextType{} func registerFlags(t *TestContextType) { flag.BoolVar(&t.InMemoryControllers, "in-memory-controllers", false, diff --git a/test/e2e/framework/unmanaged.go b/test/e2e/framework/unmanaged.go index 5383b40dea..b9fade7072 100644 --- a/test/e2e/framework/unmanaged.go +++ b/test/e2e/framework/unmanaged.go @@ -20,6 +20,7 @@ import ( "context" "fmt" "sort" + "time" "github.com/pkg/errors" @@ -188,8 +189,9 @@ func (f *UnmanagedFramework) ControllerConfig() *util.ControllerConfig { KubeFedNamespace: TestContext.KubeFedSystemNamespace, TargetNamespace: f.inMemoryTargetNamespace(), }, - KubeConfig: f.Config, - MinimizeLatency: true, + KubeConfig: f.Config, + MinimizeLatency: true, + CacheSyncTimeout: 15 * time.Minute, } controllerCfg.RawResourceStatusCollection = true return controllerCfg @@ -276,7 +278,7 @@ func (f *UnmanagedFramework) ClusterConfigs(userAgent string) map[string]common. restclient.AddUserAgent(config, userAgent) clusterConfigs[cluster.Name] = common.TestClusterConfig{ Config: config, - IsPrimary: (cluster.Name == hostClusterName), + IsPrimary: cluster.Name == hostClusterName, } } @@ -366,8 +368,8 @@ func DeleteNamespace(client kubeclientset.Interface, namespaceName string) { } func waitForNamespaceDeletion(client kubeclientset.Interface, namespace string) error { - err := wait.PollImmediate(PollInterval, TestContext.SingleCallTimeout, func() (bool, error) { - if _, err := client.CoreV1().Namespaces().Get(context.Background(), namespace, metav1.GetOptions{}); err != nil { + err := wait.PollUntilContextTimeout(context.Background(), PollInterval, TestContext.SingleCallTimeout, true, func(ctx context.Context) (bool, error) { + if _, err := client.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{}); err != nil { if apierrors.IsNotFound(err) { return true, nil } diff --git a/test/e2e/framework/wait.go b/test/e2e/framework/wait.go index 8d48bb386c..c080d388cc 100644 --- a/test/e2e/framework/wait.go +++ b/test/e2e/framework/wait.go @@ -18,6 +18,7 @@ package framework import ( "bufio" + "context" "io" "strings" "time" @@ -34,7 +35,7 @@ func WaitForObject(tl common.TestLogger, namespace, name string, objectGetter fu var actual runtimeclient.Object interval := PollInterval timeout := TestContext.SingleCallTimeout - err := wait.PollImmediate(interval, timeout, func() (exist bool, err error) { + err := wait.PollUntilContextTimeout(context.Background(), interval, timeout, true, func(ctx context.Context) (exist bool, err error) { actual, err = objectGetter(namespace, name) if err != nil { if apierrors.IsNotFound(err) { diff --git a/test/e2e/ftccontroller.go b/test/e2e/ftccontroller.go index 3443985e73..c60e384cd5 100644 --- a/test/e2e/ftccontroller.go +++ b/test/e2e/ftccontroller.go @@ -178,12 +178,12 @@ func enableResource(f framework.KubeFedFramework, targetAPIResource *metav1.APIR } func waitForTargetCrd(tl common.TestLogger, config *rest.Config, targetName, version string) { - err := wait.PollImmediate(framework.PollInterval, framework.TestContext.SingleCallTimeout, func() (bool, error) { + err := wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, framework.TestContext.SingleCallTimeout, true, func(ctx context.Context) (bool, error) { _, err := kfenable.LookupAPIResource(config, targetName, version) if err != nil { tl.Logf("An error was reported while waiting for target type %q to be published as an available resource: %v", targetName, err) } - return (err == nil), nil + return err == nil, nil }) if err != nil { tl.Fatalf("Timed out waiting for target type %q to be published as an available resource", targetName) @@ -192,9 +192,9 @@ func waitForTargetCrd(tl common.TestLogger, config *rest.Config, targetName, ver // waitForGenerationSynced indicates that sync controller is updated func waitForGenerationSynced(tl common.TestLogger, client genericclient.Client, namespace, name string) { - err := wait.PollImmediate(framework.PollInterval, framework.TestContext.SingleCallTimeout, func() (bool, error) { + err := wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, framework.TestContext.SingleCallTimeout, true, func(ctx context.Context) (bool, error) { ftc := fedv1b1.FederatedTypeConfig{} - err := client.Get(context.TODO(), &ftc, namespace, name) + err := client.Get(ctx, &ftc, namespace, name) if err != nil { tl.Fatalf("Error retrieving status of FederatedTypeConfig %q: %v", util.QualifiedName{Namespace: namespace, Name: name}, err) } diff --git a/test/e2e/not_ready.go b/test/e2e/not_ready.go index b071afb88f..23dffeffd4 100644 --- a/test/e2e/not_ready.go +++ b/test/e2e/not_ready.go @@ -163,9 +163,9 @@ var _ = Describe("[NOT_READY] Simulated not-ready nodes", func() { tl.Fatalf("Failed to patch kubefed cluster: %v", err) } - err = wait.Poll(time.Second*5, time.Second*30, func() (bool, error) { + err = wait.PollUntilContextTimeout(context.TODO(), time.Second*5, time.Second*30, true, func(ctx context.Context) (bool, error) { cluster := &fedv1b1.KubeFedCluster{} - err := hostClient.Get(context.TODO(), cluster, hostNamespace, unhealthyCluster) + err := hostClient.Get(ctx, cluster, hostNamespace, unhealthyCluster) if err != nil { tl.Fatalf("Failed to retrieve unhealthy cluster: %v", err) } diff --git a/test/e2e/placement.go b/test/e2e/placement.go index af0965ff65..827486c7da 100644 --- a/test/e2e/placement.go +++ b/test/e2e/placement.go @@ -128,8 +128,8 @@ var _ = Describe("Placement", func() { if err != nil { tl.Fatalf("Error creating resource client for %q: %v", targetKind, err) } - err = wait.PollImmediate(framework.PollInterval, framework.TestContext.SingleCallTimeout, func() (bool, error) { - _, err := client.Resources(namespace).Get(context.Background(), qualifiedName.Name, metav1.GetOptions{}) + err = wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, framework.TestContext.SingleCallTimeout, true, func(ctx context.Context) (bool, error) { + _, err := client.Resources(namespace).Get(ctx, qualifiedName.Name, metav1.GetOptions{}) if errors.IsNotFound(err) { return true, nil } diff --git a/test/e2e/scale.go b/test/e2e/scale.go index 08865985ef..9a52eb453f 100644 --- a/test/e2e/scale.go +++ b/test/e2e/scale.go @@ -140,9 +140,9 @@ var _ = Describe("Simulated Scale", func() { // serviceaccount token recreate saName := kfutil.ClusterServiceAccountName(memberCluster, hostCluster) var deleteSecret sync.Once - err = wait.PollImmediate(1*time.Second, 10*time.Second, func() (bool, error) { + err = wait.PollUntilContextTimeout(context.Background(), 1*time.Second, 10*time.Second, true, func(ctx context.Context) (bool, error) { sa, err := client.CoreV1().ServiceAccounts(joiningNamespace).Get( - context.Background(), saName, metav1.GetOptions{}, + ctx, saName, metav1.GetOptions{}, ) if err != nil { return false, nil @@ -153,13 +153,13 @@ var _ = Describe("Simulated Scale", func() { for _, objReference := range sa.Secrets { saSecretName := objReference.Name secret, err := client.CoreV1().Secrets(joiningNamespace).Get( - context.Background(), saSecretName, metav1.GetOptions{}, + ctx, saSecretName, metav1.GetOptions{}, ) if err != nil { tl.Fatalf("Error get sa secret %s: %v", saSecretName, err) } if secret.Type == corev1.SecretTypeServiceAccountToken { - if err := client.CoreV1().Secrets(joiningNamespace).Delete(context.TODO(), saSecretName, metav1.DeleteOptions{}); err != nil { + if err := client.CoreV1().Secrets(joiningNamespace).Delete(ctx, saSecretName, metav1.DeleteOptions{}); err != nil { tl.Fatalf("Error delete secret %s: %v", secretName, err) } } @@ -168,7 +168,7 @@ var _ = Describe("Simulated Scale", func() { for _, objReference := range sa.Secrets { saSecretName := objReference.Name secret, err := client.CoreV1().Secrets(joiningNamespace).Get( - context.Background(), saSecretName, metav1.GetOptions{}, + ctx, saSecretName, metav1.GetOptions{}, ) if err != nil { return false, nil diff --git a/test/e2e/schedulermanager.go b/test/e2e/schedulermanager.go index 7468c92b73..078a8614f2 100644 --- a/test/e2e/schedulermanager.go +++ b/test/e2e/schedulermanager.go @@ -17,6 +17,8 @@ limitations under the License. package e2e import ( + "context" + "k8s.io/apimachinery/pkg/util/wait" restclient "k8s.io/client-go/rest" @@ -111,7 +113,7 @@ func GetSchedulingTypes(tl common.TestLogger) map[string]schedulingtypes.Schedul } func waitForSchedulerDeleted(tl common.TestLogger, controller *schedulingmanager.SchedulingManager) { - err := wait.PollImmediate(framework.PollInterval, framework.TestContext.SingleCallTimeout, func() (bool, error) { + err := wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, framework.TestContext.SingleCallTimeout, true, func(_ context.Context) (bool, error) { scheduler := controller.GetScheduler(schedulingtypes.RSPKind) if scheduler != nil { return false, nil @@ -125,7 +127,7 @@ func waitForSchedulerDeleted(tl common.TestLogger, controller *schedulingmanager } func waitForSchedulerStarted(tl common.TestLogger, controller *schedulingmanager.SchedulingManager, schedulingTypes map[string]schedulingtypes.SchedulerFactory) { - err := wait.PollImmediate(framework.PollInterval, framework.TestContext.SingleCallTimeout, func() (bool, error) { + err := wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, framework.TestContext.SingleCallTimeout, true, func(_ context.Context) (bool, error) { scheduler := controller.GetScheduler(schedulingtypes.RSPKind) if scheduler == nil { return false, nil diff --git a/test/e2e/scheduling.go b/test/e2e/scheduling.go index bab30cd32d..6bf4a50f42 100644 --- a/test/e2e/scheduling.go +++ b/test/e2e/scheduling.go @@ -299,8 +299,8 @@ func waitForMatchingFederatedObject(tl common.TestLogger, typeConfig typeconfig. expected64 := int32MapToInt64(expected32) - return wait.PollImmediate(framework.PollInterval, framework.TestContext.SingleCallTimeout, func() (bool, error) { - fedObject, err := client.Resources(namespace).Get(context.Background(), name, metav1.GetOptions{}) + return wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, framework.TestContext.SingleCallTimeout, true, func(ctx context.Context) (bool, error) { + fedObject, err := client.Resources(namespace).Get(ctx, name, metav1.GetOptions{}) if err != nil { if !apierrors.IsNotFound(err) { tl.Errorf("An error occurred while polling for %s %s/%s: %v", kind, namespace, name, err) @@ -348,8 +348,8 @@ func updateClusterLabel(tl common.TestLogger, client genericclient.Client, kubef Version: fedv1b1.SchemeGroupVersion.Version, }) // We retry couple of times on conflict - err := wait.PollImmediate(1*time.Second, 10*time.Second, func() (bool, error) { - err := client.Get(context.Background(), fedCluster, kubefedNamespace, clusterName) + err := wait.PollUntilContextTimeout(context.Background(), 1*time.Second, 10*time.Second, true, func(ctx context.Context) (bool, error) { + err := client.Get(ctx, fedCluster, kubefedNamespace, clusterName) if err != nil { tl.Fatalf("Cannot get KubeFedCluster %q from namespace %q: %v", clusterName, kubefedNamespace, err) } @@ -359,7 +359,7 @@ func updateClusterLabel(tl common.TestLogger, client genericclient.Client, kubef } else { removeLabel(fedCluster, "foo", "bar") } - err = client.Update(context.TODO(), fedCluster) + err = client.Update(ctx, fedCluster) if err == nil { return true, nil } diff --git a/test/e2e/version.go b/test/e2e/version.go index 076a828efe..80f3d72ab2 100644 --- a/test/e2e/version.go +++ b/test/e2e/version.go @@ -454,7 +454,7 @@ var _ = Describe("VersionManager", func() { }) func checkForDeletion(tl common.TestLogger, typeName string, qualifiedName util.QualifiedName, checkFunc func() error) { - err := wait.PollImmediate(framework.PollInterval, framework.TestContext.SingleCallTimeout, func() (bool, error) { + err := wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, framework.TestContext.SingleCallTimeout, true, func(_ context.Context) (bool, error) { err := checkFunc() if errors.IsNotFound(err) { return true, nil @@ -470,9 +470,9 @@ func checkForDeletion(tl common.TestLogger, typeName string, qualifiedName util. } func waitForPropVer(tl common.TestLogger, adapter testVersionAdapter, client genericclient.Client, qualifiedName util.QualifiedName, expectedStatus fedv1a1.PropagatedVersionStatus) { - err := wait.PollImmediate(framework.PollInterval, framework.TestContext.SingleCallTimeout, func() (bool, error) { + err := wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, framework.TestContext.SingleCallTimeout, true, func(ctx context.Context) (bool, error) { propVer := adapter.NewObject() - err := client.Get(context.TODO(), propVer, qualifiedName.Namespace, qualifiedName.Name) + err := client.Get(ctx, propVer, qualifiedName.Namespace, qualifiedName.Name) if errors.IsNotFound(err) { return false, nil }