From 275f5cf5a0aae36e92388f60b794a111bd6a9889 Mon Sep 17 00:00:00 2001 From: David Eads Date: Thu, 29 Aug 2019 16:37:06 -0400 Subject: [PATCH] use the same context for aggregated and proxy requests --- .../apimachinery/pkg/util/proxy/upgradeaware.go | 12 ++---------- .../kube-aggregator/pkg/apiserver/handler_proxy.go | 14 +++++--------- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/staging/src/k8s.io/apimachinery/pkg/util/proxy/upgradeaware.go b/staging/src/k8s.io/apimachinery/pkg/util/proxy/upgradeaware.go index 4ce687393dd..fcdc76a0529 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/proxy/upgradeaware.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/proxy/upgradeaware.go @@ -19,7 +19,6 @@ package proxy import ( "bufio" "bytes" - "context" "fmt" "io" "io/ioutil" @@ -222,15 +221,8 @@ func (h *UpgradeAwareHandler) ServeHTTP(w http.ResponseWriter, req *http.Request h.Transport = h.defaultProxyTransport(req.URL, h.Transport) } - // WithContext creates a shallow clone of the request with the new context. - ctx := context.Background() - // if the original request has a deadline, we should honor that deadline for our proxied request - if deadline, ok := req.Context().Deadline(); ok { - var cancelFn context.CancelFunc - ctx, cancelFn = context.WithDeadline(ctx, deadline) - defer cancelFn() - } - newReq := req.WithContext(ctx) + // WithContext creates a shallow clone of the request with the same context. + newReq := req.WithContext(req.Context()) newReq.Header = utilnet.CloneHeader(req.Header) if !h.UseRequestLocation { newReq.URL = &loc diff --git a/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy.go b/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy.go index 6dbe553d2ad..9576cdd84e9 100644 --- a/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy.go +++ b/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy.go @@ -178,23 +178,19 @@ func (r *proxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { // newRequestForProxy returns a shallow copy of the original request with a context that may include a timeout for discovery requests func newRequestForProxy(location *url.URL, req *http.Request, enableAggregatedDiscoveryTimeout bool) (*http.Request, context.CancelFunc) { - newCtx := context.Background() + newCtx := req.Context() cancelFn := func() {} - // if the original request has a deadline, we should honor that deadline for our proxied request - if deadline, ok := req.Context().Deadline(); ok { - newCtx, cancelFn = context.WithDeadline(newCtx, deadline) - - // trim leading and trailing slashes. Then "/apis/group/version" requests are for discovery, so if we have exactly three - // segments that we are going to proxy, we have a discovery request. - } else if enableAggregatedDiscoveryTimeout && len(strings.Split(strings.Trim(req.URL.Path, "/"), "/")) == 3 { + // trim leading and trailing slashes. Then "/apis/group/version" requests are for discovery, so if we have exactly three + // segments that we are going to proxy, we have a discovery request. + if enableAggregatedDiscoveryTimeout && len(strings.Split(strings.Trim(req.URL.Path, "/"), "/")) == 3 { // discovery requests are used by kubectl and others to determine which resources a server has. This is a cheap call that // should be fast for every aggregated apiserver. Latency for aggregation is expected to be low (as for all extensions) // so forcing a short timeout here helps responsiveness of all clients. newCtx, cancelFn = context.WithTimeout(newCtx, aggregatedDiscoveryTimeout) } - // WithContext creates a shallow clone of the request with the new context. + // WithContext creates a shallow clone of the request with the same context. newReq := req.WithContext(newCtx) newReq.Header = utilnet.CloneHeader(req.Header) newReq.URL = location