Skip to content

Commit 4c686dd

Browse files
committed
Propagate context to ExponentialBackoff
1 parent 92eb072 commit 4c686dd

File tree

6 files changed

+20
-11
lines changed

6 files changed

+20
-11
lines changed

plugin/pkg/admission/imagepolicy/admission.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,21 @@ func (a *Plugin) Validate(ctx context.Context, attributes admission.Attributes,
160160
Namespace: attributes.GetNamespace(),
161161
},
162162
}
163-
if err := a.admitPod(pod, attributes, &imageReview); err != nil {
163+
if err := a.admitPod(ctx, pod, attributes, &imageReview); err != nil {
164164
return admission.NewForbidden(attributes, err)
165165
}
166166
return nil
167167
}
168168

169-
func (a *Plugin) admitPod(pod *api.Pod, attributes admission.Attributes, review *v1alpha1.ImageReview) error {
169+
func (a *Plugin) admitPod(ctx context.Context, pod *api.Pod, attributes admission.Attributes, review *v1alpha1.ImageReview) error {
170170
cacheKey, err := json.Marshal(review.Spec)
171171
if err != nil {
172172
return err
173173
}
174174
if entry, ok := a.responseCache.Get(string(cacheKey)); ok {
175175
review.Status = entry.(v1alpha1.ImageReviewStatus)
176176
} else {
177-
result := a.webhook.WithExponentialBackoff(func() rest.Result {
177+
result := a.webhook.WithExponentialBackoff(ctx, func() rest.Result {
178178
return a.webhook.RestClient.Post().Body(review).Do()
179179
})
180180

staging/src/k8s.io/apiserver/pkg/util/webhook/webhook.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818
package webhook
1919

2020
import (
21+
"context"
2122
"fmt"
2223
"time"
2324

@@ -81,9 +82,9 @@ func newGenericWebhook(scheme *runtime.Scheme, codecFactory serializer.CodecFact
8182

8283
// WithExponentialBackoff will retry webhookFn() up to 5 times with exponentially increasing backoff when
8384
// it returns an error for which apierrors.SuggestsClientDelay() or apierrors.IsInternalError() returns true.
84-
func (g *GenericWebhook) WithExponentialBackoff(webhookFn func() rest.Result) rest.Result {
85+
func (g *GenericWebhook) WithExponentialBackoff(ctx context.Context, webhookFn func() rest.Result) rest.Result {
8586
var result rest.Result
86-
WithExponentialBackoff(g.InitialBackoff, func() error {
87+
WithExponentialBackoff(ctx, g.InitialBackoff, func() error {
8788
result = webhookFn()
8889
return result.Error()
8990
})
@@ -92,7 +93,7 @@ func (g *GenericWebhook) WithExponentialBackoff(webhookFn func() rest.Result) re
9293

9394
// WithExponentialBackoff will retry webhookFn() up to 5 times with exponentially increasing backoff when
9495
// it returns an error for which apierrors.SuggestsClientDelay() or apierrors.IsInternalError() returns true.
95-
func WithExponentialBackoff(initialBackoff time.Duration, webhookFn func() error) error {
96+
func WithExponentialBackoff(ctx context.Context, initialBackoff time.Duration, webhookFn func() error) error {
9697
backoff := wait.Backoff{
9798
Duration: initialBackoff,
9899
Factor: 1.5,
@@ -103,6 +104,12 @@ func WithExponentialBackoff(initialBackoff time.Duration, webhookFn func() error
103104
var err error
104105
wait.ExponentialBackoff(backoff, func() (bool, error) {
105106
err = webhookFn()
107+
108+
if ctx.Err() != nil {
109+
// we timed out or were cancelled, we should not retry
110+
return true, err
111+
}
112+
106113
// these errors indicate a transient error that should be retried.
107114
if net.IsConnectionReset(err) || apierrors.IsInternalError(err) || apierrors.IsTimeout(err) || apierrors.IsTooManyRequests(err) {
108115
return false, nil

staging/src/k8s.io/apiserver/pkg/util/webhook/webhook_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package webhook
1818

1919
import (
20+
"context"
2021
"crypto/tls"
2122
"crypto/x509"
2223
"encoding/json"
@@ -550,7 +551,7 @@ func TestWithExponentialBackoff(t *testing.T) {
550551
t.Fatalf("failed to create the webhook: %v", err)
551552
}
552553

553-
result := wh.WithExponentialBackoff(func() rest.Result {
554+
result := wh.WithExponentialBackoff(context.Background(), func() rest.Result {
554555
return wh.RestClient.Get().Do()
555556
})
556557

@@ -562,7 +563,7 @@ func TestWithExponentialBackoff(t *testing.T) {
562563
t.Errorf("unexpected status code: %d", statusCode)
563564
}
564565

565-
result = wh.WithExponentialBackoff(func() rest.Result {
566+
result = wh.WithExponentialBackoff(context.Background(), func() rest.Result {
566567
return wh.RestClient.Get().Do()
567568
})
568569

staging/src/k8s.io/apiserver/plugin/pkg/audit/webhook/webhook.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818
package webhook
1919

2020
import (
21+
"context"
2122
"fmt"
2223
"time"
2324

@@ -95,7 +96,7 @@ func (b *backend) processEvents(ev ...*auditinternal.Event) error {
9596
for _, e := range ev {
9697
list.Items = append(list.Items, *e)
9798
}
98-
return b.w.WithExponentialBackoff(func() rest.Result {
99+
return b.w.WithExponentialBackoff(context.Background(), func() rest.Result {
99100
trace := utiltrace.New("Call Audit Events webhook",
100101
utiltrace.Field{"name", b.name},
101102
utiltrace.Field{"event-count", len(list.Items)})

staging/src/k8s.io/apiserver/plugin/pkg/authenticator/token/webhook/webhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (w *WebhookTokenAuthenticator) AuthenticateToken(ctx context.Context, token
9898
err error
9999
auds authenticator.Audiences
100100
)
101-
webhook.WithExponentialBackoff(w.initialBackoff, func() error {
101+
webhook.WithExponentialBackoff(ctx, w.initialBackoff, func() error {
102102
result, err = w.tokenReview.Create(r)
103103
return err
104104
})

staging/src/k8s.io/apiserver/plugin/pkg/authorizer/webhook/webhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func (w *WebhookAuthorizer) Authorize(ctx context.Context, attr authorizer.Attri
188188
result *authorization.SubjectAccessReview
189189
err error
190190
)
191-
webhook.WithExponentialBackoff(w.initialBackoff, func() error {
191+
webhook.WithExponentialBackoff(ctx, w.initialBackoff, func() error {
192192
result, err = w.subjectAccessReview.Create(r)
193193
return err
194194
})

0 commit comments

Comments
 (0)