From 5737088b7fd0fede7eeda321d7b7167977bfcd0d Mon Sep 17 00:00:00 2001 From: Mike Danese Date: Mon, 27 Jan 2020 18:19:44 -0800 Subject: [PATCH 1/2] refactor Kubernetes-commit: d55d6175f8e2cfdab0b79aac72046a652c2eb515 --- pkg/admission/plugin/webhook/mutating/dispatcher.go | 3 ++- pkg/admission/plugin/webhook/validating/dispatcher.go | 4 ++-- pkg/util/webhook/webhook_test.go | 10 +++++----- plugin/pkg/audit/webhook/webhook.go | 2 +- plugin/pkg/authenticator/token/webhook/webhook.go | 4 ++-- plugin/pkg/authorizer/webhook/webhook.go | 4 ++-- 6 files changed, 14 insertions(+), 13 deletions(-) diff --git a/pkg/admission/plugin/webhook/mutating/dispatcher.go b/pkg/admission/plugin/webhook/mutating/dispatcher.go index 3d6f022e2..19257ff76 100644 --- a/pkg/admission/plugin/webhook/mutating/dispatcher.go +++ b/pkg/admission/plugin/webhook/mutating/dispatcher.go @@ -24,6 +24,7 @@ import ( "time" jsonpatch "github.com/evanphx/json-patch" + apiequality "k8s.io/apimachinery/pkg/api/equality" "k8s.io/klog" @@ -250,7 +251,7 @@ func (a *mutatingDispatcher) callAttrMutatingHook(ctx context.Context, h *admiss } } - if err := r.Do().Into(response); err != nil { + if err := r.Do(context.TODO()).Into(response); err != nil { return false, &webhookutil.ErrCallingWebhook{WebhookName: h.Name, Reason: err} } trace.Step("Request completed") diff --git a/pkg/admission/plugin/webhook/validating/dispatcher.go b/pkg/admission/plugin/webhook/validating/dispatcher.go index 781c4ea3e..e0011a29c 100644 --- a/pkg/admission/plugin/webhook/validating/dispatcher.go +++ b/pkg/admission/plugin/webhook/validating/dispatcher.go @@ -22,7 +22,7 @@ import ( "sync" "time" - "k8s.io/api/admissionregistration/v1" + v1 "k8s.io/api/admissionregistration/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime/schema" utilruntime "k8s.io/apimachinery/pkg/util/runtime" @@ -211,7 +211,7 @@ func (d *validatingDispatcher) callHook(ctx context.Context, h *v1.ValidatingWeb } } - if err := r.Do().Into(response); err != nil { + if err := r.Do(context.TODO()).Into(response); err != nil { return &webhookutil.ErrCallingWebhook{WebhookName: h.Name, Reason: err} } trace.Step("Request completed") diff --git a/pkg/util/webhook/webhook_test.go b/pkg/util/webhook/webhook_test.go index d56599d98..7fda70874 100644 --- a/pkg/util/webhook/webhook_test.go +++ b/pkg/util/webhook/webhook_test.go @@ -37,7 +37,7 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" - "k8s.io/client-go/tools/clientcmd/api/v1" + v1 "k8s.io/client-go/tools/clientcmd/api/v1" ) const ( @@ -397,7 +397,7 @@ func TestTLSConfig(t *testing.T) { wh, err := NewGenericWebhook(runtime.NewScheme(), scheme.Codecs, configFile, groupVersions, retryBackoff) if err == nil { - err = wh.RestClient.Get().Do().Error() + err = wh.RestClient.Get().Do(context.TODO()).Error() } if err == nil { @@ -466,7 +466,7 @@ func TestRequestTimeout(t *testing.T) { resultCh := make(chan rest.Result) - go func() { resultCh <- wh.RestClient.Get().Do() }() + go func() { resultCh <- wh.RestClient.Get().Do(context.TODO()) }() select { case <-time.After(time.Second * 5): t.Errorf("expected request to timeout after %s", requestTimeout) @@ -552,7 +552,7 @@ func TestWithExponentialBackoff(t *testing.T) { } result := wh.WithExponentialBackoff(context.Background(), func() rest.Result { - return wh.RestClient.Get().Do() + return wh.RestClient.Get().Do(context.TODO()) }) var statusCode int @@ -564,7 +564,7 @@ func TestWithExponentialBackoff(t *testing.T) { } result = wh.WithExponentialBackoff(context.Background(), func() rest.Result { - return wh.RestClient.Get().Do() + return wh.RestClient.Get().Do(context.TODO()) }) result.StatusCode(&statusCode) diff --git a/plugin/pkg/audit/webhook/webhook.go b/plugin/pkg/audit/webhook/webhook.go index d0f053643..f73c1fd56 100644 --- a/plugin/pkg/audit/webhook/webhook.go +++ b/plugin/pkg/audit/webhook/webhook.go @@ -124,7 +124,7 @@ func (b *backend) processEvents(ev ...*auditinternal.Event) error { // allow enough time for the serialization/deserialization of audit events, which // contain nested request and response objects plus additional event fields. defer trace.LogIfLong(time.Duration(50+25*len(list.Items)) * time.Millisecond) - return b.w.RestClient.Post().Body(&list).Do() + return b.w.RestClient.Post().Body(&list).Do(context.TODO()) }).Error() } diff --git a/plugin/pkg/authenticator/token/webhook/webhook.go b/plugin/pkg/authenticator/token/webhook/webhook.go index 53c5d2285..0eb56e37f 100644 --- a/plugin/pkg/authenticator/token/webhook/webhook.go +++ b/plugin/pkg/authenticator/token/webhook/webhook.go @@ -198,7 +198,7 @@ type tokenReviewV1Client struct { func (t *tokenReviewV1Client) CreateContext(ctx context.Context, review *authenticationv1.TokenReview) (*authenticationv1.TokenReview, error) { result := &authenticationv1.TokenReview{} - err := t.w.RestClient.Post().Context(ctx).Body(review).Do().Into(result) + err := t.w.RestClient.Post().Context(ctx).Body(review).Do(context.TODO()).Into(result) return result, err } @@ -209,7 +209,7 @@ type tokenReviewV1beta1Client struct { func (t *tokenReviewV1beta1Client) CreateContext(ctx context.Context, review *authenticationv1.TokenReview) (*authenticationv1.TokenReview, error) { v1beta1Review := &authenticationv1beta1.TokenReview{Spec: v1SpecToV1beta1Spec(&review.Spec)} v1beta1Result := &authenticationv1beta1.TokenReview{} - err := t.w.RestClient.Post().Context(ctx).Body(v1beta1Review).Do().Into(v1beta1Result) + err := t.w.RestClient.Post().Context(ctx).Body(v1beta1Review).Do(context.TODO()).Into(v1beta1Result) if err != nil { return nil, err } diff --git a/plugin/pkg/authorizer/webhook/webhook.go b/plugin/pkg/authorizer/webhook/webhook.go index 3839d483e..7a4c5b172 100644 --- a/plugin/pkg/authorizer/webhook/webhook.go +++ b/plugin/pkg/authorizer/webhook/webhook.go @@ -289,7 +289,7 @@ type subjectAccessReviewV1Client struct { func (t *subjectAccessReviewV1Client) CreateContext(ctx context.Context, subjectAccessReview *authorizationv1.SubjectAccessReview) (*authorizationv1.SubjectAccessReview, error) { result := &authorizationv1.SubjectAccessReview{} - err := t.w.RestClient.Post().Context(ctx).Body(subjectAccessReview).Do().Into(result) + err := t.w.RestClient.Post().Context(ctx).Body(subjectAccessReview).Do(context.TODO()).Into(result) return result, err } @@ -300,7 +300,7 @@ type subjectAccessReviewV1beta1Client struct { func (t *subjectAccessReviewV1beta1Client) CreateContext(ctx context.Context, subjectAccessReview *authorizationv1.SubjectAccessReview) (*authorizationv1.SubjectAccessReview, error) { v1beta1Review := &authorizationv1beta1.SubjectAccessReview{Spec: v1SpecToV1beta1Spec(&subjectAccessReview.Spec)} v1beta1Result := &authorizationv1beta1.SubjectAccessReview{} - err := t.w.RestClient.Post().Context(ctx).Body(v1beta1Review).Do().Into(v1beta1Result) + err := t.w.RestClient.Post().Context(ctx).Body(v1beta1Review).Do(context.TODO()).Into(v1beta1Result) if err == nil { subjectAccessReview.Status = v1beta1StatusToV1Status(&v1beta1Result.Status) } From f7c2e267155038ad9a04ce215dc16f37d474e02d Mon Sep 17 00:00:00 2001 From: Mike Danese Date: Mon, 27 Jan 2020 18:52:27 -0800 Subject: [PATCH 2/2] cleanup req.Context() and ResponseWrapper Kubernetes-commit: 968adfa99362f733ef82f4aabb34a59dbbd6e56a --- pkg/admission/plugin/webhook/mutating/dispatcher.go | 4 ++-- pkg/admission/plugin/webhook/validating/dispatcher.go | 4 ++-- plugin/pkg/authenticator/token/webhook/webhook.go | 4 ++-- plugin/pkg/authorizer/webhook/webhook.go | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pkg/admission/plugin/webhook/mutating/dispatcher.go b/pkg/admission/plugin/webhook/mutating/dispatcher.go index 19257ff76..1d337fb43 100644 --- a/pkg/admission/plugin/webhook/mutating/dispatcher.go +++ b/pkg/admission/plugin/webhook/mutating/dispatcher.go @@ -236,7 +236,7 @@ func (a *mutatingDispatcher) callAttrMutatingHook(ctx context.Context, h *admiss defer cancel() } - r := client.Post().Context(ctx).Body(request) + r := client.Post().Body(request) // if the context has a deadline, set it as a parameter to inform the backend if deadline, hasDeadline := ctx.Deadline(); hasDeadline { @@ -251,7 +251,7 @@ func (a *mutatingDispatcher) callAttrMutatingHook(ctx context.Context, h *admiss } } - if err := r.Do(context.TODO()).Into(response); err != nil { + if err := r.Do(ctx).Into(response); err != nil { return false, &webhookutil.ErrCallingWebhook{WebhookName: h.Name, Reason: err} } trace.Step("Request completed") diff --git a/pkg/admission/plugin/webhook/validating/dispatcher.go b/pkg/admission/plugin/webhook/validating/dispatcher.go index e0011a29c..ab6b03924 100644 --- a/pkg/admission/plugin/webhook/validating/dispatcher.go +++ b/pkg/admission/plugin/webhook/validating/dispatcher.go @@ -196,7 +196,7 @@ func (d *validatingDispatcher) callHook(ctx context.Context, h *v1.ValidatingWeb defer cancel() } - r := client.Post().Context(ctx).Body(request) + r := client.Post().Body(request) // if the context has a deadline, set it as a parameter to inform the backend if deadline, hasDeadline := ctx.Deadline(); hasDeadline { @@ -211,7 +211,7 @@ func (d *validatingDispatcher) callHook(ctx context.Context, h *v1.ValidatingWeb } } - if err := r.Do(context.TODO()).Into(response); err != nil { + if err := r.Do(ctx).Into(response); err != nil { return &webhookutil.ErrCallingWebhook{WebhookName: h.Name, Reason: err} } trace.Step("Request completed") diff --git a/plugin/pkg/authenticator/token/webhook/webhook.go b/plugin/pkg/authenticator/token/webhook/webhook.go index 0eb56e37f..1912959e5 100644 --- a/plugin/pkg/authenticator/token/webhook/webhook.go +++ b/plugin/pkg/authenticator/token/webhook/webhook.go @@ -198,7 +198,7 @@ type tokenReviewV1Client struct { func (t *tokenReviewV1Client) CreateContext(ctx context.Context, review *authenticationv1.TokenReview) (*authenticationv1.TokenReview, error) { result := &authenticationv1.TokenReview{} - err := t.w.RestClient.Post().Context(ctx).Body(review).Do(context.TODO()).Into(result) + err := t.w.RestClient.Post().Body(review).Do(ctx).Into(result) return result, err } @@ -209,7 +209,7 @@ type tokenReviewV1beta1Client struct { func (t *tokenReviewV1beta1Client) CreateContext(ctx context.Context, review *authenticationv1.TokenReview) (*authenticationv1.TokenReview, error) { v1beta1Review := &authenticationv1beta1.TokenReview{Spec: v1SpecToV1beta1Spec(&review.Spec)} v1beta1Result := &authenticationv1beta1.TokenReview{} - err := t.w.RestClient.Post().Context(ctx).Body(v1beta1Review).Do(context.TODO()).Into(v1beta1Result) + err := t.w.RestClient.Post().Body(v1beta1Review).Do(ctx).Into(v1beta1Result) if err != nil { return nil, err } diff --git a/plugin/pkg/authorizer/webhook/webhook.go b/plugin/pkg/authorizer/webhook/webhook.go index 7a4c5b172..fecf7aab8 100644 --- a/plugin/pkg/authorizer/webhook/webhook.go +++ b/plugin/pkg/authorizer/webhook/webhook.go @@ -289,7 +289,7 @@ type subjectAccessReviewV1Client struct { func (t *subjectAccessReviewV1Client) CreateContext(ctx context.Context, subjectAccessReview *authorizationv1.SubjectAccessReview) (*authorizationv1.SubjectAccessReview, error) { result := &authorizationv1.SubjectAccessReview{} - err := t.w.RestClient.Post().Context(ctx).Body(subjectAccessReview).Do(context.TODO()).Into(result) + err := t.w.RestClient.Post().Body(subjectAccessReview).Do(ctx).Into(result) return result, err } @@ -300,7 +300,7 @@ type subjectAccessReviewV1beta1Client struct { func (t *subjectAccessReviewV1beta1Client) CreateContext(ctx context.Context, subjectAccessReview *authorizationv1.SubjectAccessReview) (*authorizationv1.SubjectAccessReview, error) { v1beta1Review := &authorizationv1beta1.SubjectAccessReview{Spec: v1SpecToV1beta1Spec(&subjectAccessReview.Spec)} v1beta1Result := &authorizationv1beta1.SubjectAccessReview{} - err := t.w.RestClient.Post().Context(ctx).Body(v1beta1Review).Do(context.TODO()).Into(v1beta1Result) + err := t.w.RestClient.Post().Body(v1beta1Review).Do(ctx).Into(v1beta1Result) if err == nil { subjectAccessReview.Status = v1beta1StatusToV1Status(&v1beta1Result.Status) }