-
Notifications
You must be signed in to change notification settings - Fork 19
fix(prow-job-executor): retry Key Vault prow-token lookup on transient failures (AROSLSRE-1228) #252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
geoberle
merged 5 commits into
Azure:main
from
raelga:fix/prowtoken-keyvault-retry-1228
Jun 23, 2026
Merged
fix(prow-job-executor): retry Key Vault prow-token lookup on transient failures (AROSLSRE-1228) #252
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f658d1b
fix(prow-job-executor): retry Key Vault prow-token lookup on transien…
raelga 434e42f
fix(prow-job-executor): address review feedback on KV retry
raelga 94ac9f5
refactor(prow-job-executor): extract generic retry helper; fail fast …
raelga f1da9fd
fix(prow-job-executor): fail fast on context cancellation in retry he…
raelga 3447238
docs(prow-job-executor): correct GetJobStatus Steps comment to "Maxim…
raelga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| // Copyright 2025 Microsoft Corporation | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // Package retry provides a small, generic exponential-backoff retry helper shared | ||
| // by the prow-job-executor's transient-failure paths (Gangway job submission, job | ||
| // status polling, and the Key Vault prow-token lookup). | ||
| package retry | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/go-logr/logr" | ||
|
|
||
| "k8s.io/apimachinery/pkg/util/wait" | ||
| ) | ||
|
|
||
| // WithValue invokes fn with exponential backoff and returns its value once it | ||
| // succeeds, retrying only errors that isRetryable classifies as transient. | ||
| // | ||
| // Behavior: | ||
| // - fn is called at least once. On success its value is returned immediately. | ||
| // - When fn returns an error that isRetryable reports as false (a permanent or | ||
| // deterministic failure), WithValue stops immediately and propagates that error | ||
| // unchanged, without consuming the remaining backoff budget. | ||
| // - When isRetryable reports true, the error is logged at info level (if a logr | ||
| // logger is present on ctx) and the call is retried until the backoff budget is | ||
| // exhausted, after which the last transient error is wrapped and returned. | ||
| // - A cancelled or expired parent context always takes precedence: its error is | ||
| // returned as-is rather than masked behind the last transient error. | ||
| // | ||
| // fn must respect ctx for cancellation. The parent context bounds the total runtime | ||
| // regardless of the backoff schedule. | ||
| func WithValue[T any](ctx context.Context, backoff wait.Backoff, isRetryable func(error) bool, fn func(ctx context.Context) (T, error)) (T, error) { | ||
| logger, err := logr.FromContext(ctx) | ||
| if err != nil { | ||
| logger = logr.Discard() | ||
| } | ||
|
|
||
| var result T | ||
| var lastErr error | ||
| condition := func(ctx context.Context) (bool, error) { | ||
| v, err := fn(ctx) | ||
| if err != nil { | ||
| // A cancelled/expired parent context is terminal: stop immediately, | ||
| // without logging a misleading "will retry" or recording it as the last | ||
| // transient error. (Some callers' isRetryable does not special-case | ||
| // context errors, e.g. GetJobStatus.) | ||
| if ctxErr := ctx.Err(); ctxErr != nil { | ||
| return false, ctxErr | ||
| } | ||
|
|
||
| // Permanent/deterministic failures surface immediately instead of | ||
| // after a long backoff. | ||
| if !isRetryable(err) { | ||
| return false, err // Stop retrying and propagate the error as-is. | ||
| } | ||
|
|
||
| lastErr = err | ||
| logger.Info("Operation failed with a transient error, will retry", "error", err.Error()) | ||
| return false, nil | ||
| } | ||
|
|
||
| result = v | ||
| return true, nil // Success, stop retrying. | ||
| } | ||
|
|
||
| if err := wait.ExponentialBackoffWithContext(ctx, backoff, condition); err != nil { | ||
| // A cancelled/expired parent context takes precedence: report it as-is | ||
| // rather than masking it behind the last transient error. | ||
| if ctxErr := ctx.Err(); ctxErr != nil { | ||
| var zero T | ||
| return zero, ctxErr | ||
| } | ||
| // Retries were exhausted: surface the last transient error for context. | ||
| if lastErr != nil { | ||
| var zero T | ||
| return zero, fmt.Errorf("retry budget exhausted after transient errors: %w", lastErr) | ||
| } | ||
| // A permanent error returned by the condition propagates unchanged. | ||
| var zero T | ||
| return zero, err | ||
| } | ||
|
|
||
| return result, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| // Copyright 2025 Microsoft Corporation | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package retry | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/go-logr/logr" | ||
|
|
||
| "k8s.io/apimachinery/pkg/util/wait" | ||
| ) | ||
|
|
||
| // fastBackoff returns a backoff with negligible delays so retry tests run quickly | ||
| // while still exercising a deterministic number of attempts. No Cap is set so the | ||
| // attempt count is exactly Steps (apimachinery's Cap would also halt retries early). | ||
| func fastBackoff(steps int) wait.Backoff { | ||
| return wait.Backoff{ | ||
| Duration: time.Millisecond, | ||
| Factor: 2.0, | ||
| Jitter: 0.0, | ||
| Steps: steps, | ||
| } | ||
| } | ||
|
|
||
| func testContext() context.Context { | ||
| return logr.NewContext(context.Background(), logr.Discard()) | ||
| } | ||
|
|
||
| // retryAll treats every error as transient. | ||
| func retryAll(error) bool { return true } | ||
|
|
||
| func TestWithValueSucceedsFirstAttempt(t *testing.T) { | ||
| calls := 0 | ||
| got, err := WithValue(testContext(), fastBackoff(4), retryAll, func(context.Context) (int, error) { | ||
| calls++ | ||
| return 42, nil | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if got != 42 { | ||
| t.Fatalf("got %d, want 42", got) | ||
| } | ||
| if calls != 1 { | ||
| t.Fatalf("fn called %d times, want 1", calls) | ||
| } | ||
| } | ||
|
|
||
| func TestWithValueRetriesTransientThenSucceeds(t *testing.T) { | ||
| calls := 0 | ||
| got, err := WithValue(testContext(), fastBackoff(4), retryAll, func(context.Context) (string, error) { | ||
| calls++ | ||
| if calls < 3 { | ||
| return "", errors.New("transient") | ||
| } | ||
| return "ok", nil | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if got != "ok" { | ||
| t.Fatalf("got %q, want %q", got, "ok") | ||
| } | ||
| if calls != 3 { | ||
| t.Fatalf("fn called %d times, want 3", calls) | ||
| } | ||
| } | ||
|
|
||
| func TestWithValueFailsFastOnNonRetryable(t *testing.T) { | ||
| sentinel := errors.New("permanent") | ||
| calls := 0 | ||
| _, err := WithValue(testContext(), fastBackoff(4), func(error) bool { return false }, func(context.Context) (int, error) { | ||
| calls++ | ||
| return 0, sentinel | ||
| }) | ||
| if !errors.Is(err, sentinel) { | ||
| t.Fatalf("error = %v, want it to wrap sentinel", err) | ||
| } | ||
| if calls != 1 { | ||
| t.Fatalf("fn called %d times, want 1 (no retries on permanent error)", calls) | ||
| } | ||
| // A permanent error must propagate unchanged, not behind the "retry budget" wrapper. | ||
| if strings.Contains(err.Error(), "retry budget exhausted") { | ||
| t.Fatalf("permanent error should propagate as-is, got %q", err.Error()) | ||
| } | ||
| } | ||
|
|
||
| func TestWithValueWrapsLastErrorWhenExhausted(t *testing.T) { | ||
| sentinel := errors.New("still failing") | ||
| calls := 0 | ||
| _, err := WithValue(testContext(), fastBackoff(3), retryAll, func(context.Context) (int, error) { | ||
| calls++ | ||
| return 0, sentinel | ||
| }) | ||
| if err == nil { | ||
| t.Fatal("expected error, got nil") | ||
| } | ||
| if !errors.Is(err, sentinel) { | ||
| t.Fatalf("error = %v, want it to wrap the last transient error", err) | ||
| } | ||
| if !strings.Contains(err.Error(), "retry budget exhausted") { | ||
| t.Fatalf("exhausted error %q should mention the retry budget", err.Error()) | ||
| } | ||
| if calls != 3 { | ||
| t.Fatalf("fn called %d times, want 3", calls) | ||
| } | ||
| } | ||
|
|
||
| // TestWithValueContextCanceledBeforeStart verifies a context cancelled before the | ||
| // first attempt surfaces as-is and never invokes fn (ExponentialBackoffWithContext | ||
| // checks the context before the first condition call). | ||
| func TestWithValueContextCanceledBeforeStart(t *testing.T) { | ||
| ctx, cancel := context.WithCancel(testContext()) | ||
| cancel() | ||
|
|
||
| calls := 0 | ||
| _, err := WithValue(ctx, fastBackoff(4), retryAll, func(ctx context.Context) (int, error) { | ||
| calls++ | ||
| return 0, ctx.Err() | ||
| }) | ||
| if !errors.Is(err, context.Canceled) { | ||
| t.Fatalf("error = %v, want context.Canceled", err) | ||
| } | ||
| if strings.Contains(err.Error(), "retry budget exhausted") { | ||
| t.Fatalf("context error should surface as-is, got %q", err.Error()) | ||
| } | ||
| if calls != 0 { | ||
| t.Fatalf("fn called %d times, want 0 (context checked before first attempt)", calls) | ||
| } | ||
| } | ||
|
|
||
| // TestWithValueContextCanceledDuringCall verifies that when the parent context is | ||
| // cancelled while fn is running, the next iteration fails fast on the context error | ||
| // without logging a "will retry" or wrapping it as an exhausted-budget error — even | ||
| // though fn returned an otherwise-retryable error. | ||
| func TestWithValueContextCanceledDuringCall(t *testing.T) { | ||
| ctx, cancel := context.WithCancel(testContext()) | ||
|
|
||
| calls := 0 | ||
| _, err := WithValue(ctx, fastBackoff(4), retryAll, func(ctx context.Context) (int, error) { | ||
| calls++ | ||
| cancel() // cancel the parent mid-call | ||
| return 0, errors.New("transient") // a normally-retryable error | ||
| }) | ||
| if !errors.Is(err, context.Canceled) { | ||
| t.Fatalf("error = %v, want context.Canceled", err) | ||
| } | ||
| if strings.Contains(err.Error(), "retry budget exhausted") { | ||
| t.Fatalf("cancelled context should not be wrapped as exhausted budget, got %q", err.Error()) | ||
| } | ||
| if calls != 1 { | ||
| t.Fatalf("fn called %d times, want 1 (no retry after cancellation)", calls) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.