From 34bfdc3635cb621d94eebde5d8f4b9c0b933c68e Mon Sep 17 00:00:00 2001 From: Clayton Coleman Date: Mon, 16 Jan 2023 14:57:57 -0500 Subject: [PATCH] wait: ExponentialBackoffWithContext should take context-aware fn The condition methods will eventually all take a context. Since we have been provided one, alter the accepted condition type and change the four references in tree. Collers of ExponentialBackoffWithContext should use a condition aware function (ConditionWithContextFunc). If the context can be ignored the helper ConditionFunc.WithContext can be used to convert an existing function to the new type. --- staging/src/k8s.io/apimachinery/pkg/util/wait/wait.go | 4 ++-- staging/src/k8s.io/apimachinery/pkg/util/wait/wait_test.go | 2 +- staging/src/k8s.io/apiserver/pkg/util/webhook/webhook.go | 2 +- test/e2e/storage/volume_metrics.go | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/staging/src/k8s.io/apimachinery/pkg/util/wait/wait.go b/staging/src/k8s.io/apimachinery/pkg/util/wait/wait.go index 137627b4050..bdd55b96a4b 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/wait/wait.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/wait/wait.go @@ -729,7 +729,7 @@ func poller(interval, timeout time.Duration) WaitWithContextFunc { // ExponentialBackoffWithContext works with a request context and a Backoff. It ensures that the retry wait never // exceeds the deadline specified by the request context. -func ExponentialBackoffWithContext(ctx context.Context, backoff Backoff, condition ConditionFunc) error { +func ExponentialBackoffWithContext(ctx context.Context, backoff Backoff, condition ConditionWithContextFunc) error { for backoff.Steps > 0 { select { case <-ctx.Done(): @@ -737,7 +737,7 @@ func ExponentialBackoffWithContext(ctx context.Context, backoff Backoff, conditi default: } - if ok, err := runConditionWithCrashProtection(condition); err != nil || ok { + if ok, err := runConditionWithCrashProtectionWithContext(ctx, condition); err != nil || ok { return err } diff --git a/staging/src/k8s.io/apimachinery/pkg/util/wait/wait_test.go b/staging/src/k8s.io/apimachinery/pkg/util/wait/wait_test.go index f42c220830c..4fb3f4d4490 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/wait/wait_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/wait/wait_test.go @@ -875,7 +875,7 @@ func TestExponentialBackoffWithContext(t *testing.T) { } attempts := 0 - err := ExponentialBackoffWithContext(test.ctxGetter(), backoff, func() (bool, error) { + err := ExponentialBackoffWithContext(test.ctxGetter(), backoff, func(_ context.Context) (bool, error) { attempts++ return test.callback(attempts) }) diff --git a/staging/src/k8s.io/apiserver/pkg/util/webhook/webhook.go b/staging/src/k8s.io/apiserver/pkg/util/webhook/webhook.go index 06a74c1cd31..45143bf6efb 100644 --- a/staging/src/k8s.io/apiserver/pkg/util/webhook/webhook.go +++ b/staging/src/k8s.io/apiserver/pkg/util/webhook/webhook.go @@ -121,7 +121,7 @@ func WithExponentialBackoff(ctx context.Context, retryBackoff wait.Backoff, webh // having a webhook error allows us to track the last actual webhook error for requests that // are later cancelled or time out. var webhookErr error - err := wait.ExponentialBackoffWithContext(ctx, retryBackoff, func() (bool, error) { + err := wait.ExponentialBackoffWithContext(ctx, retryBackoff, func(_ context.Context) (bool, error) { webhookErr = webhookFn() if shouldRetry(webhookErr) { return false, nil diff --git a/test/e2e/storage/volume_metrics.go b/test/e2e/storage/volume_metrics.go index fe7af214752..e64882f6a06 100644 --- a/test/e2e/storage/volume_metrics.go +++ b/test/e2e/storage/volume_metrics.go @@ -673,7 +673,7 @@ func waitForDetachAndGrabMetrics(ctx context.Context, oldMetrics *storageControl oldDetachCount = 0 } - verifyMetricFunc := func() (bool, error) { + verifyMetricFunc := func(ctx context.Context) (bool, error) { updatedMetrics, err := metricsGrabber.GrabFromControllerManager(ctx) if err != nil { @@ -821,7 +821,7 @@ func waitForPVControllerSync(ctx context.Context, metricsGrabber *e2emetrics.Gra Factor: 1.2, Steps: 21, } - verifyMetricFunc := func() (bool, error) { + verifyMetricFunc := func(ctx context.Context) (bool, error) { updatedMetrics, err := metricsGrabber.GrabFromControllerManager(ctx) if err != nil { framework.Logf("Error fetching controller-manager metrics") @@ -866,7 +866,7 @@ func waitForADControllerStatesMetrics(ctx context.Context, metricsGrabber *e2eme Factor: 1.2, Steps: 21, } - verifyMetricFunc := func() (bool, error) { + verifyMetricFunc := func(ctx context.Context) (bool, error) { updatedMetrics, err := metricsGrabber.GrabFromControllerManager(ctx) if err != nil { e2eskipper.Skipf("Could not get controller-manager metrics - skipping")