Merge pull request #87597 from mikedanese/rctx

Refactor context handling in rest.Request

Kubernetes-commit: 60dd5dbd8b2347284b7e9b87a5700f54c185034e
This commit is contained in:
Kubernetes Publisher 2020-01-29 22:11:03 -08:00
commit 63d2c07ac2
9 changed files with 24 additions and 23 deletions

4
Godeps/Godeps.json generated
View File

@ -592,11 +592,11 @@
}, },
{ {
"ImportPath": "k8s.io/apimachinery", "ImportPath": "k8s.io/apimachinery",
"Rev": "845a0cbf0d16" "Rev": "eb4ad4570127"
}, },
{ {
"ImportPath": "k8s.io/client-go", "ImportPath": "k8s.io/client-go",
"Rev": "0fcd5b52ae3f" "Rev": "1ae532aad6ad"
}, },
{ {
"ImportPath": "k8s.io/component-base", "ImportPath": "k8s.io/component-base",

8
go.mod
View File

@ -45,8 +45,8 @@ require (
gopkg.in/yaml.v2 v2.2.8 gopkg.in/yaml.v2 v2.2.8
gotest.tools v2.2.0+incompatible // indirect gotest.tools v2.2.0+incompatible // indirect
k8s.io/api v0.0.0-20200130072251-812149543c99 k8s.io/api v0.0.0-20200130072251-812149543c99
k8s.io/apimachinery v0.0.0-20200130072110-845a0cbf0d16 k8s.io/apimachinery v0.0.0-20200130072111-eb4ad4570127
k8s.io/client-go v0.0.0-20200130072521-0fcd5b52ae3f k8s.io/client-go v0.0.0-20200130072523-1ae532aad6ad
k8s.io/component-base v0.0.0-20200130073007-c1abf0189091 k8s.io/component-base v0.0.0-20200130073007-c1abf0189091
k8s.io/klog v1.0.0 k8s.io/klog v1.0.0
k8s.io/kube-openapi v0.0.0-20200121204235-bf4fb3bd569c k8s.io/kube-openapi v0.0.0-20200121204235-bf4fb3bd569c
@ -59,7 +59,7 @@ replace (
golang.org/x/sys => golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a // pinned to release-branch.go1.13 golang.org/x/sys => golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a // pinned to release-branch.go1.13
golang.org/x/tools => golang.org/x/tools v0.0.0-20190821162956-65e3620a7ae7 // pinned to release-branch.go1.13 golang.org/x/tools => golang.org/x/tools v0.0.0-20190821162956-65e3620a7ae7 // pinned to release-branch.go1.13
k8s.io/api => k8s.io/api v0.0.0-20200130072251-812149543c99 k8s.io/api => k8s.io/api v0.0.0-20200130072251-812149543c99
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20200130072110-845a0cbf0d16 k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20200130072111-eb4ad4570127
k8s.io/client-go => k8s.io/client-go v0.0.0-20200130072521-0fcd5b52ae3f k8s.io/client-go => k8s.io/client-go v0.0.0-20200130072523-1ae532aad6ad
k8s.io/component-base => k8s.io/component-base v0.0.0-20200130073007-c1abf0189091 k8s.io/component-base => k8s.io/component-base v0.0.0-20200130073007-c1abf0189091
) )

4
go.sum
View File

@ -360,8 +360,8 @@ honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
k8s.io/api v0.0.0-20200130072251-812149543c99/go.mod h1:5hz1MDQLlgYnKttI81z5nZ8LGQtHE6oI6Ry3XcXnVAI= k8s.io/api v0.0.0-20200130072251-812149543c99/go.mod h1:5hz1MDQLlgYnKttI81z5nZ8LGQtHE6oI6Ry3XcXnVAI=
k8s.io/apimachinery v0.0.0-20200130072110-845a0cbf0d16/go.mod h1:HJDBSLM+cx1vex4Ye4/ySVjb/6yt+PEBkK8g2alZ6LM= k8s.io/apimachinery v0.0.0-20200130072111-eb4ad4570127/go.mod h1:f7PPp70QzCGGLJHvCtiN2lePa9CdVddeAdL5w7NdrrU=
k8s.io/client-go v0.0.0-20200130072521-0fcd5b52ae3f/go.mod h1:e2/TBKxAJO1u9+kDmNL19FOhAKA4Fp/63kBQ3iOrBRE= k8s.io/client-go v0.0.0-20200130072523-1ae532aad6ad/go.mod h1:1IzWcSqMJwg2THGQsF6Bbx340lRBq6vB2zHCzcnavlo=
k8s.io/component-base v0.0.0-20200130073007-c1abf0189091/go.mod h1:HzmjRJW6Bp7Gk7CjqmoXwm53pONGbanJQz54pUAyLtc= k8s.io/component-base v0.0.0-20200130073007-c1abf0189091/go.mod h1:HzmjRJW6Bp7Gk7CjqmoXwm53pONGbanJQz54pUAyLtc=
k8s.io/gengo v0.0.0-20190128074634-0689ccc1d7d6/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0= k8s.io/gengo v0.0.0-20190128074634-0689ccc1d7d6/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
k8s.io/klog v0.0.0-20181102134211-b9b56d5dfc92/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk= k8s.io/klog v0.0.0-20181102134211-b9b56d5dfc92/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=

View File

@ -24,6 +24,7 @@ import (
"time" "time"
jsonpatch "github.com/evanphx/json-patch" jsonpatch "github.com/evanphx/json-patch"
apiequality "k8s.io/apimachinery/pkg/api/equality" apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/klog" "k8s.io/klog"
@ -235,7 +236,7 @@ func (a *mutatingDispatcher) callAttrMutatingHook(ctx context.Context, h *admiss
defer cancel() 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 the context has a deadline, set it as a parameter to inform the backend
if deadline, hasDeadline := ctx.Deadline(); hasDeadline { if deadline, hasDeadline := ctx.Deadline(); hasDeadline {
@ -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(ctx).Into(response); err != nil {
return false, &webhookutil.ErrCallingWebhook{WebhookName: h.Name, Reason: err} return false, &webhookutil.ErrCallingWebhook{WebhookName: h.Name, Reason: err}
} }
trace.Step("Request completed") trace.Step("Request completed")

View File

@ -22,7 +22,7 @@ import (
"sync" "sync"
"time" "time"
"k8s.io/api/admissionregistration/v1" v1 "k8s.io/api/admissionregistration/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors" apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
utilruntime "k8s.io/apimachinery/pkg/util/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@ -196,7 +196,7 @@ func (d *validatingDispatcher) callHook(ctx context.Context, h *v1.ValidatingWeb
defer cancel() 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 the context has a deadline, set it as a parameter to inform the backend
if deadline, hasDeadline := ctx.Deadline(); hasDeadline { if deadline, hasDeadline := ctx.Deadline(); hasDeadline {
@ -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(ctx).Into(response); err != nil {
return &webhookutil.ErrCallingWebhook{WebhookName: h.Name, Reason: err} return &webhookutil.ErrCallingWebhook{WebhookName: h.Name, Reason: err}
} }
trace.Step("Request completed") trace.Step("Request completed")

View File

@ -37,7 +37,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest" "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd/api/v1" v1 "k8s.io/client-go/tools/clientcmd/api/v1"
) )
const ( const (
@ -397,7 +397,7 @@ func TestTLSConfig(t *testing.T) {
wh, err := NewGenericWebhook(runtime.NewScheme(), scheme.Codecs, configFile, groupVersions, retryBackoff) wh, err := NewGenericWebhook(runtime.NewScheme(), scheme.Codecs, configFile, groupVersions, retryBackoff)
if err == nil { if err == nil {
err = wh.RestClient.Get().Do().Error() err = wh.RestClient.Get().Do(context.TODO()).Error()
} }
if err == nil { if err == nil {
@ -466,7 +466,7 @@ func TestRequestTimeout(t *testing.T) {
resultCh := make(chan rest.Result) resultCh := make(chan rest.Result)
go func() { resultCh <- wh.RestClient.Get().Do() }() go func() { resultCh <- wh.RestClient.Get().Do(context.TODO()) }()
select { select {
case <-time.After(time.Second * 5): case <-time.After(time.Second * 5):
t.Errorf("expected request to timeout after %s", requestTimeout) 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 { result := wh.WithExponentialBackoff(context.Background(), func() rest.Result {
return wh.RestClient.Get().Do() return wh.RestClient.Get().Do(context.TODO())
}) })
var statusCode int var statusCode int
@ -564,7 +564,7 @@ func TestWithExponentialBackoff(t *testing.T) {
} }
result = wh.WithExponentialBackoff(context.Background(), func() rest.Result { result = wh.WithExponentialBackoff(context.Background(), func() rest.Result {
return wh.RestClient.Get().Do() return wh.RestClient.Get().Do(context.TODO())
}) })
result.StatusCode(&statusCode) result.StatusCode(&statusCode)

View File

@ -124,7 +124,7 @@ func (b *backend) processEvents(ev ...*auditinternal.Event) error {
// allow enough time for the serialization/deserialization of audit events, which // allow enough time for the serialization/deserialization of audit events, which
// contain nested request and response objects plus additional event fields. // contain nested request and response objects plus additional event fields.
defer trace.LogIfLong(time.Duration(50+25*len(list.Items)) * time.Millisecond) 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() }).Error()
} }

View File

@ -198,7 +198,7 @@ type tokenReviewV1Client struct {
func (t *tokenReviewV1Client) CreateContext(ctx context.Context, review *authenticationv1.TokenReview) (*authenticationv1.TokenReview, error) { func (t *tokenReviewV1Client) CreateContext(ctx context.Context, review *authenticationv1.TokenReview) (*authenticationv1.TokenReview, error) {
result := &authenticationv1.TokenReview{} result := &authenticationv1.TokenReview{}
err := t.w.RestClient.Post().Context(ctx).Body(review).Do().Into(result) err := t.w.RestClient.Post().Body(review).Do(ctx).Into(result)
return result, err return result, err
} }
@ -209,7 +209,7 @@ type tokenReviewV1beta1Client struct {
func (t *tokenReviewV1beta1Client) CreateContext(ctx context.Context, review *authenticationv1.TokenReview) (*authenticationv1.TokenReview, error) { func (t *tokenReviewV1beta1Client) CreateContext(ctx context.Context, review *authenticationv1.TokenReview) (*authenticationv1.TokenReview, error) {
v1beta1Review := &authenticationv1beta1.TokenReview{Spec: v1SpecToV1beta1Spec(&review.Spec)} v1beta1Review := &authenticationv1beta1.TokenReview{Spec: v1SpecToV1beta1Spec(&review.Spec)}
v1beta1Result := &authenticationv1beta1.TokenReview{} v1beta1Result := &authenticationv1beta1.TokenReview{}
err := t.w.RestClient.Post().Context(ctx).Body(v1beta1Review).Do().Into(v1beta1Result) err := t.w.RestClient.Post().Body(v1beta1Review).Do(ctx).Into(v1beta1Result)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -289,7 +289,7 @@ type subjectAccessReviewV1Client struct {
func (t *subjectAccessReviewV1Client) CreateContext(ctx context.Context, subjectAccessReview *authorizationv1.SubjectAccessReview) (*authorizationv1.SubjectAccessReview, error) { func (t *subjectAccessReviewV1Client) CreateContext(ctx context.Context, subjectAccessReview *authorizationv1.SubjectAccessReview) (*authorizationv1.SubjectAccessReview, error) {
result := &authorizationv1.SubjectAccessReview{} result := &authorizationv1.SubjectAccessReview{}
err := t.w.RestClient.Post().Context(ctx).Body(subjectAccessReview).Do().Into(result) err := t.w.RestClient.Post().Body(subjectAccessReview).Do(ctx).Into(result)
return result, err return result, err
} }
@ -300,7 +300,7 @@ type subjectAccessReviewV1beta1Client struct {
func (t *subjectAccessReviewV1beta1Client) CreateContext(ctx context.Context, subjectAccessReview *authorizationv1.SubjectAccessReview) (*authorizationv1.SubjectAccessReview, error) { func (t *subjectAccessReviewV1beta1Client) CreateContext(ctx context.Context, subjectAccessReview *authorizationv1.SubjectAccessReview) (*authorizationv1.SubjectAccessReview, error) {
v1beta1Review := &authorizationv1beta1.SubjectAccessReview{Spec: v1SpecToV1beta1Spec(&subjectAccessReview.Spec)} v1beta1Review := &authorizationv1beta1.SubjectAccessReview{Spec: v1SpecToV1beta1Spec(&subjectAccessReview.Spec)}
v1beta1Result := &authorizationv1beta1.SubjectAccessReview{} v1beta1Result := &authorizationv1beta1.SubjectAccessReview{}
err := t.w.RestClient.Post().Context(ctx).Body(v1beta1Review).Do().Into(v1beta1Result) err := t.w.RestClient.Post().Body(v1beta1Review).Do(ctx).Into(v1beta1Result)
if err == nil { if err == nil {
subjectAccessReview.Status = v1beta1StatusToV1Status(&v1beta1Result.Status) subjectAccessReview.Status = v1beta1StatusToV1Status(&v1beta1Result.Status)
} }