Skip to content

Commit

Permalink
remove deprecated poll and waitforError
Browse files Browse the repository at this point in the history
  • Loading branch information
ranakan19 committed Aug 29, 2024
1 parent 9541f3a commit 4f41fbc
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 225 deletions.
4 changes: 2 additions & 2 deletions setup/idlers/update_idler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func UpdateTimeout(cl client.Client, username string, timeout time.Duration) err

func getIdler(cl client.Client, name string) (*toolchainv1alpha1.Idler, error) {
idler := &toolchainv1alpha1.Idler{}
err := k8swait.Poll(cfg.DefaultRetryInterval, cfg.DefaultTimeout, func() (bool, error) {
err := cl.Get(context.TODO(), types.NamespacedName{
err := k8swait.PollUntilContextTimeout(context.TODO(), cfg.DefaultRetryInterval, cfg.DefaultTimeout, true, func(ctx context.Context) (bool, error) {
err := cl.Get(ctx, types.NamespacedName{
Name: name,
}, idler)
if errors.IsNotFound(err) {
Expand Down
3 changes: 2 additions & 1 deletion setup/metrics/gather.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package metrics

import (
"context"
"fmt"
"math"
"strings"
Expand Down Expand Up @@ -104,7 +105,7 @@ func (g *Gatherer) StartGathering() chan struct{} {
for _, q := range g.mqueries {
var metricsErr error
// added retry mechanism since temporary metrics errors have been observed, poll until the query returns a non-error result or the poll times out
err := k8sutil.Poll(cfg.DefaultRetryInterval, cfg.DefaultTimeout, func() (bool, error) {
err := k8sutil.PollUntilContextTimeout(context.TODO(), cfg.DefaultRetryInterval, cfg.DefaultTimeout, true, func(ctx context.Context) (bool, error) {
metricsErr = g.sample(q)
return metricsErr == nil, nil
})
Expand Down
2 changes: 1 addition & 1 deletion setup/templates/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func applyObject(ctx context.Context, applycl *applyclientlib.ApplyClient, obj r

// retry the apply in case it fails due to errors like the following:
// unable to create resource of kind: Deployment, version: v1: Operation cannot be fulfilled on clusterresourcequotas.quota.openshift.io "for-zippy-1882-deployments": the object has been modified; please apply your changes to the latest version and try again
if err := k8swait.Poll(cfg.DefaultRetryInterval, 30*time.Second, func() (bool, error) {
if err := k8swait.PollUntilContextTimeout(context.TODO(), cfg.DefaultRetryInterval, 30*time.Second, true, func(context context.Context) (bool, error) {
if _, applyErr := applycl.ApplyObject(ctx, obj); applyErr != nil {
return false, applyErr
}
Expand Down
4 changes: 2 additions & 2 deletions setup/users/create_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func getMemberClusterName(cl client.Client, hostOperatorNamespace, memberOperato
return memberClusterName, nil
}
var memberCluster toolchainv1alpha1.ToolchainCluster
err := k8swait.Poll(configuration.DefaultRetryInterval, configuration.DefaultTimeout, func() (bool, error) {
err := k8swait.PollUntilContextTimeout(context.TODO(), configuration.DefaultRetryInterval, configuration.DefaultTimeout, true, func(ctx context.Context) (bool, error) {
clusters := &toolchainv1alpha1.ToolchainClusterList{}
if err := cl.List(context.TODO(), clusters, client.InNamespace(hostOperatorNamespace), client.MatchingLabels{
if err := cl.List(ctx, clusters, client.InNamespace(hostOperatorNamespace), client.MatchingLabels{
"namespace": memberOperatorNamespace,
}); err != nil {
return false, err
Expand Down
8 changes: 4 additions & 4 deletions setup/wait/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func ForSpace(cl client.Client, space string) error {
},
}

if err := k8swait.Poll(configuration.DefaultRetryInterval, configuration.DefaultTimeout, func() (bool, error) {
err := cl.Get(context.TODO(), types.NamespacedName{
if err := k8swait.PollUntilContextTimeout(context.TODO(), configuration.DefaultRetryInterval, configuration.DefaultTimeout, true, func(ctx context.Context) (bool, error) {
err := cl.Get(ctx, types.NamespacedName{
Name: space,
Namespace: configuration.HostOperatorNamespace,
}, sp)
Expand Down Expand Up @@ -64,7 +64,7 @@ func HasSubscriptionWithCriteria(cl client.Client, name, namespace string, crite
}

func ForSubscriptionWithCriteria(cl client.Client, name, namespace string, timeout time.Duration, criteria ...subCriteria) error {
if err := k8swait.Poll(configuration.DefaultRetryInterval, timeout, func() (bool, error) {
if err := k8swait.PollUntilContextTimeout(context.TODO(), configuration.DefaultRetryInterval, timeout, true, func(ctx context.Context) (bool, error) {
return HasSubscriptionWithCriteria(cl, name, namespace, criteria...)
}); err != nil {
return errors.Wrapf(err, "could not find a Subscription with name '%s' in namespace '%s' that meets the expected criteria", name, namespace)
Expand All @@ -90,7 +90,7 @@ func HasCSVWithCriteria(cl client.Client, name, namespace string, criteria ...cs
}

func ForCSVWithCriteria(cl client.Client, name, namespace string, timeout time.Duration, criteria ...csvCriteria) error {
if err := k8swait.Poll(configuration.DefaultRetryInterval, timeout, func() (bool, error) {
if err := k8swait.PollUntilContextTimeout(context.TODO(), configuration.DefaultRetryInterval, timeout, true, func(ctx context.Context) (bool, error) {
return HasCSVWithCriteria(cl, name, namespace, criteria...)
}); err != nil {
return errors.Wrapf(err, "could not find a CSV with name '%s' in namespace '%s' that meets the expected criteria", name, namespace)
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/parallel/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ func (u *proxyUser) getWorkspace(t *testing.T, hostAwait *wait.HostAwaitility, w
workspace := &toolchainv1alpha1.Workspace{}
var cause error
// only wait up to 5 seconds because in some test cases the workspace is not expected to be found
_ = kubewait.Poll(wait.DefaultRetryInterval, 5*time.Second, func() (bool, error) {
cause = proxyCl.Get(context.TODO(), types.NamespacedName{Name: workspaceName}, workspace)
_ = kubewait.PollUntilContextTimeout(context.TODO(), wait.DefaultRetryInterval, 5*time.Second, true, func(ctx context.Context) (bool, error) {
cause = proxyCl.Get(ctx, types.NamespacedName{Name: workspaceName}, workspace)
return cause == nil, nil
})

Expand Down Expand Up @@ -1199,7 +1199,7 @@ func (w *wsWatcher) receiveHandler() {

func (w *wsWatcher) WaitForApplication(expectedAppName string) (*appstudiov1.Application, error) {
var foundApp *appstudiov1.Application
err := kubewait.Poll(wait.DefaultRetryInterval, wait.DefaultTimeout, func() (bool, error) {
err := kubewait.PollUntilContextTimeout(context.TODO(), wait.DefaultRetryInterval, wait.DefaultTimeout, true, func(ctx context.Context) (bool, error) {
defer w.mu.RUnlock()
w.mu.RLock()
foundApp = w.receivedApps[expectedAppName]
Expand All @@ -1209,7 +1209,7 @@ func (w *wsWatcher) WaitForApplication(expectedAppName string) (*appstudiov1.App
}

func (w *wsWatcher) WaitForApplicationDeletion(expectedAppName string) error {
err := kubewait.PollImmediate(wait.DefaultRetryInterval, wait.DefaultTimeout, func() (bool, error) {
err := kubewait.PollUntilContextTimeout(context.TODO(), wait.DefaultRetryInterval, wait.DefaultTimeout, true, func(ctx context.Context) (bool, error) {
defer w.mu.RUnlock()
w.mu.RLock()
_, present := w.receivedApps[expectedAppName]
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/parallel/registration_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ func assertRHODSClusterURL(t *testing.T, memberAwait *wait.MemberAwaitility, res
func waitForUserSignupReadyInRegistrationService(t *testing.T, registrationServiceURL, name, bearerToken string) map[string]interface{} {
t.Logf("waiting and verifying that UserSignup '%s' is ready according to registration service", name)
var mp, mpStatus map[string]interface{}
err := k8swait.Poll(time.Second, time.Second*60, func() (done bool, err error) {
err := k8swait.PollUntilContextTimeout(context.TODO(), time.Second, time.Second*60, true, func(ctx context.Context) (done bool, err error) {
mp, mpStatus = ParseSignupResponse(t, NewHTTPRequest(t).InvokeEndpoint("GET", registrationServiceURL+"/api/v1/signup", bearerToken, "", http.StatusOK).UnmarshalMap())
// check if `ready` field is set
if _, ok := mpStatus["ready"]; !ok {
Expand Down
4 changes: 2 additions & 2 deletions testsupport/cleanup/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ func (c *cleanTask) cleanObject() {

// wait until deletion is done
c.t.Logf("waiting until %s: %s is completely deleted", kind, objToClean.GetName())
err := wait.Poll(defaultRetryInterval, defaultTimeout, func() (done bool, err error) {
if err := c.client.Get(context.TODO(), test.NamespacedName(objToClean.GetNamespace(), objToClean.GetName()), objToClean); err != nil {
err := wait.PollUntilContextTimeout(context.TODO(), defaultRetryInterval, defaultTimeout, true, func(ctx context.Context) (done bool, err error) {
if err := c.client.Get(ctx, test.NamespacedName(objToClean.GetNamespace(), objToClean.GetName()), objToClean); err != nil {
if errors.IsNotFound(err) {
// if the object was UserSignup, then let's check that the MUR is deleted as well
if murDeleted, err := c.verifyMurDeleted(isUserSignup, userSignup, false); !murDeleted || err != nil {
Expand Down
46 changes: 23 additions & 23 deletions testsupport/wait/awaitility.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ func (a *Awaitility) baselineKey(t *testing.T, name string, labelAndValues ...st
func (a *Awaitility) WaitForService(t *testing.T, name string) (corev1.Service, error) {
t.Logf("waiting for Service '%s' in namespace '%s'", name, a.Namespace)
var metricsSvc *corev1.Service
err := wait.Poll(a.RetryInterval, a.Timeout, func() (done bool, err error) {
err := wait.PollUntilContextTimeout(context.TODO(), a.RetryInterval, a.Timeout, true, func(ctx context.Context) (done bool, err error) {
metricsSvc = &corev1.Service{}
// retrieve the metrics service from the namespace
err = a.Client.Get(context.TODO(),
err = a.Client.Get(ctx,
types.NamespacedName{
Namespace: a.Namespace,
Name: name,
Expand All @@ -158,7 +158,7 @@ func (a *Awaitility) WaitForToolchainClusterWithCondition(t *testing.T, namespac
t.Logf("waiting for ToolchainCluster in namespace '%s'", namespace)

var c toolchainv1alpha1.ToolchainCluster
err := wait.Poll(a.RetryInterval, a.Timeout, func() (done bool, err error) {
err := wait.PollUntilContextTimeout(context.TODO(), a.RetryInterval, a.Timeout, true, func(ctx context.Context) (done bool, err error) {
var ready bool
if c, ready, err = a.GetToolchainCluster(t, namespace, cdtype); ready {
return true, nil
Expand Down Expand Up @@ -237,8 +237,8 @@ func (a *Awaitility) WaitForRouteToBeAvailable(t *testing.T, ns, name, endpoint
t.Logf("waiting for route '%s' in namespace '%s'", name, ns)
route := routev1.Route{}
// retrieve the route for the registration service
err := wait.Poll(a.RetryInterval, a.Timeout, func() (done bool, err error) {
if err = a.Client.Get(context.TODO(),
err := wait.PollUntilContextTimeout(context.TODO(), a.RetryInterval, a.Timeout, true, func(ctx context.Context) (done bool, err error) {
if err = a.Client.Get(ctx,
types.NamespacedName{
Namespace: ns,
Name: name,
Expand Down Expand Up @@ -328,7 +328,7 @@ func (a *Awaitility) GetMetricValueOrZero(t *testing.T, family string, labelAndV
func (a *Awaitility) WaitUntiltMetricHasValue(t *testing.T, family string, expectedValue float64, labels ...string) {
t.Logf("waiting for metric '%s{%v}' to reach '%v'", family, labels, expectedValue)
var value float64
err := wait.Poll(a.RetryInterval, a.Timeout, func() (done bool, err error) {
err := wait.PollUntilContextTimeout(context.TODO(), a.RetryInterval, a.Timeout, true, func(ctx context.Context) (done bool, err error) {
value, err = metrics.GetMetricValue(a.RestConfig, a.MetricsURL, family, labels)
// if error occurred, ignore and return `false` to keep waiting (may be due to endpoint temporarily unavailable)
// unless the expected value is `0`, in which case the metric is bot exposed (value==0 and err!= nil), but it's fine too.
Expand All @@ -342,7 +342,7 @@ func (a *Awaitility) WaitUntiltMetricHasValue(t *testing.T, family string, expec
func (a *Awaitility) WaitUntilMetricHasValueOrMore(t *testing.T, family string, expectedValue float64, labels ...string) error {
t.Logf("waiting for metric '%s{%v}' to reach '%v' or more", family, labels, expectedValue)
var value float64
err := wait.Poll(a.RetryInterval, a.Timeout, func() (done bool, err error) {
err := wait.PollUntilContextTimeout(context.TODO(), a.RetryInterval, a.Timeout, true, func(ctx context.Context) (done bool, err error) {
value, err = metrics.GetMetricValue(a.RestConfig, a.MetricsURL, family, labels)
// if error occurred, return `false` to keep waiting (may be due to endpoint temporarily unavailable)
return value >= expectedValue && err == nil, nil
Expand All @@ -358,7 +358,7 @@ func (a *Awaitility) WaitUntilMetricHasValueOrMore(t *testing.T, family string,
func (a *Awaitility) WaitUntilMetricHasValueOrLess(t *testing.T, family string, expectedValue float64, labels ...string) error {
t.Logf("waiting for metric '%s{%v}' to reach '%v' or less", family, labels, expectedValue)
var value float64
err := wait.Poll(a.RetryInterval, a.Timeout, func() (done bool, err error) {
err := wait.PollUntilContextTimeout(context.TODO(), a.RetryInterval, a.Timeout, true, func(ctx context.Context) (done bool, err error) {
value, err = metrics.GetMetricValue(a.RestConfig, a.MetricsURL, family, labels)
// if error occurred, return `false` to keep waiting (may be due to endpoint temporarily unavailable)
return value <= expectedValue && err == nil, nil
Expand Down Expand Up @@ -387,9 +387,9 @@ func (a *Awaitility) DeletePods(criteria ...client.ListOption) error {
// GetMemoryUsage retrieves the memory usage (in KB) of a given the pod
func (a *Awaitility) GetMemoryUsage(podname, ns string) (int64, error) {
var containerMetrics k8smetrics.ContainerMetrics
if err := wait.Poll(a.RetryInterval, a.Timeout, func() (done bool, err error) {
if err := wait.PollUntilContextTimeout(context.TODO(), a.RetryInterval, a.Timeout, true, func(ctx context.Context) (done bool, err error) {
podMetrics := k8smetrics.PodMetrics{}
if err := a.Client.Get(context.TODO(), types.NamespacedName{
if err := a.Client.Get(ctx, types.NamespacedName{
Namespace: ns,
Name: podname,
}, &podMetrics); err != nil && !apierrors.IsNotFound(err) {
Expand Down Expand Up @@ -419,9 +419,9 @@ func (a *Awaitility) CreateNamespace(t *testing.T, name string) {
}
err := a.Client.Create(context.TODO(), ns)
require.NoError(t, err)
err = wait.Poll(a.RetryInterval, a.Timeout, func() (done bool, err error) {
err = wait.PollUntilContextTimeout(context.TODO(), a.RetryInterval, a.Timeout, true, func(ctx context.Context) (done bool, err error) {
ns := &corev1.Namespace{}
if err := a.Client.Get(context.TODO(), types.NamespacedName{Name: name}, ns); err != nil && apierrors.IsNotFound(err) {
if err := a.Client.Get(ctx, types.NamespacedName{Name: name}, ns); err != nil && apierrors.IsNotFound(err) {
return false, nil
} else if err != nil {
return false, err
Expand Down Expand Up @@ -504,9 +504,9 @@ func (a *Awaitility) WaitForToolchainCluster(t *testing.T, criteria ...Toolchain
t.Logf("waiting for toolchaincluster in namespace '%s' to match criteria", a.Namespace)
var clusters *toolchainv1alpha1.ToolchainClusterList
var cl *toolchainv1alpha1.ToolchainCluster
err := wait.Poll(a.RetryInterval, a.Timeout, func() (done bool, err error) {
err := wait.PollUntilContextTimeout(context.TODO(), a.RetryInterval, a.Timeout, true, func(ctx context.Context) (done bool, err error) {
clusters = &toolchainv1alpha1.ToolchainClusterList{}
if err := a.Client.List(context.TODO(), clusters, client.InNamespace(a.Namespace)); err != nil {
if err := a.Client.List(ctx, clusters, client.InNamespace(a.Namespace)); err != nil {
return false, err
}
for _, obj := range clusters.Items {
Expand Down Expand Up @@ -588,13 +588,13 @@ func UntilToolchainClusterHasNoTenantLabel() ToolchainClusterWaitCriterion {
// Returns the updated ToolchainCluster
func (a *Awaitility) UpdateToolchainCluster(t *testing.T, toolchainClusterName string, modifyToolchainCluster func(s *toolchainv1alpha1.ToolchainCluster)) (*toolchainv1alpha1.ToolchainCluster, error) {
var tc *toolchainv1alpha1.ToolchainCluster
err := wait.Poll(a.RetryInterval, a.Timeout, func() (done bool, err error) {
err := wait.PollUntilContextTimeout(context.TODO(), a.RetryInterval, a.Timeout, true, func(ctx context.Context) (done bool, err error) {
newToolchainCluster := &toolchainv1alpha1.ToolchainCluster{}
if err := a.Client.Get(context.TODO(), types.NamespacedName{Namespace: a.Namespace, Name: toolchainClusterName}, newToolchainCluster); err != nil {
if err := a.Client.Get(ctx, types.NamespacedName{Namespace: a.Namespace, Name: toolchainClusterName}, newToolchainCluster); err != nil {
return true, err
}
modifyToolchainCluster(newToolchainCluster)
if err := a.Client.Update(context.TODO(), newToolchainCluster); err != nil {
if err := a.Client.Update(ctx, newToolchainCluster); err != nil {
t.Logf("error updating ToolchainCluster '%s': %s. Will retry again...", toolchainClusterName, err.Error())
return false, nil
}
Expand Down Expand Up @@ -690,13 +690,13 @@ func (w *Waiter[T]) FirstThat(predicates ...assertions.Predicate[client.Object])
// match status of each predicate per object
latestResults := map[client.ObjectKey][]bool{}

err := wait.Poll(w.await.RetryInterval, w.await.Timeout, func() (done bool, err error) {
err := wait.PollUntilContextTimeout(context.TODO(), w.await.RetryInterval, w.await.Timeout, true, func(ctx context.Context) (done bool, err error) {
// because there is no generic way of figuring out the list type for some client.Object type, we need to go
// down the low level route and use unstructured to get the list generically and unmarshal and cast the list
// items.
list := &unstructured.UnstructuredList{}
list.SetGroupVersionKind(w.gvk)
if err := w.await.Client.List(context.TODO(), list, client.InNamespace(w.await.Namespace)); err != nil {
if err := w.await.Client.List(ctx, list, client.InNamespace(w.await.Namespace)); err != nil {
return false, err
}
for _, obj := range list.Items {
Expand Down Expand Up @@ -769,10 +769,10 @@ func (w *Waiter[T]) WithNameThat(name string, predicates ...assertions.Predicate
var returnedObject T
latestResults := []bool{}

err := wait.Poll(w.await.RetryInterval, w.await.Timeout, func() (done bool, err error) {
err := wait.PollUntilContextTimeout(context.TODO(), w.await.RetryInterval, w.await.Timeout, true, func(ctx context.Context) (done bool, err error) {
obj := &unstructured.Unstructured{}
obj.SetGroupVersionKind(w.gvk)
if err := w.await.Client.Get(context.TODO(), client.ObjectKey{Name: name, Namespace: w.await.Namespace}, obj); err != nil {
if err := w.await.Client.Get(ctx, client.ObjectKey{Name: name, Namespace: w.await.Namespace}, obj); err != nil {
if apierrors.IsNotFound(err) {
return false, nil
}
Expand Down Expand Up @@ -820,10 +820,10 @@ func (w *Waiter[T]) WithNameThat(name string, predicates ...assertions.Predicate
// WithNameDeleted waits for a single object with the provided name in the namespace of the awaitility to get deleted
func (w *Waiter[T]) WithNameDeleted(name string) error {
w.t.Logf("waiting for object of GVK '%s' with name '%s' in namespace '%s' to be deleted", w.gvk, name, w.await.Namespace)
err := wait.Poll(w.await.RetryInterval, w.await.Timeout, func() (done bool, err error) {
err := wait.PollUntilContextTimeout(context.TODO(), w.await.RetryInterval, w.await.Timeout, true, func(ctx context.Context) (done bool, err error) {
obj := &unstructured.Unstructured{}
obj.SetGroupVersionKind(w.gvk)
if err := w.await.Client.Get(context.TODO(), client.ObjectKey{Name: name, Namespace: w.await.Namespace}, obj); err != nil {
if err := w.await.Client.Get(ctx, client.ObjectKey{Name: name, Namespace: w.await.Namespace}, obj); err != nil {
if apierrors.IsNotFound(err) {
return true, nil
}
Expand Down
Loading

0 comments on commit 4f41fbc

Please sign in to comment.