Skip to content

Commit 275f5cf

Browse files
committed
use the same context for aggregated and proxy requests
1 parent 5521bf2 commit 275f5cf

File tree

2 files changed

+7
-19
lines changed

2 files changed

+7
-19
lines changed

staging/src/k8s.io/apimachinery/pkg/util/proxy/upgradeaware.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package proxy
1919
import (
2020
"bufio"
2121
"bytes"
22-
"context"
2322
"fmt"
2423
"io"
2524
"io/ioutil"
@@ -222,15 +221,8 @@ func (h *UpgradeAwareHandler) ServeHTTP(w http.ResponseWriter, req *http.Request
222221
h.Transport = h.defaultProxyTransport(req.URL, h.Transport)
223222
}
224223

225-
// WithContext creates a shallow clone of the request with the new context.
226-
ctx := context.Background()
227-
// if the original request has a deadline, we should honor that deadline for our proxied request
228-
if deadline, ok := req.Context().Deadline(); ok {
229-
var cancelFn context.CancelFunc
230-
ctx, cancelFn = context.WithDeadline(ctx, deadline)
231-
defer cancelFn()
232-
}
233-
newReq := req.WithContext(ctx)
224+
// WithContext creates a shallow clone of the request with the same context.
225+
newReq := req.WithContext(req.Context())
234226
newReq.Header = utilnet.CloneHeader(req.Header)
235227
if !h.UseRequestLocation {
236228
newReq.URL = &loc

staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,23 +178,19 @@ func (r *proxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
178178

179179
// newRequestForProxy returns a shallow copy of the original request with a context that may include a timeout for discovery requests
180180
func newRequestForProxy(location *url.URL, req *http.Request, enableAggregatedDiscoveryTimeout bool) (*http.Request, context.CancelFunc) {
181-
newCtx := context.Background()
181+
newCtx := req.Context()
182182
cancelFn := func() {}
183183

184-
// if the original request has a deadline, we should honor that deadline for our proxied request
185-
if deadline, ok := req.Context().Deadline(); ok {
186-
newCtx, cancelFn = context.WithDeadline(newCtx, deadline)
187-
188-
// trim leading and trailing slashes. Then "/apis/group/version" requests are for discovery, so if we have exactly three
189-
// segments that we are going to proxy, we have a discovery request.
190-
} else if enableAggregatedDiscoveryTimeout && len(strings.Split(strings.Trim(req.URL.Path, "/"), "/")) == 3 {
184+
// trim leading and trailing slashes. Then "/apis/group/version" requests are for discovery, so if we have exactly three
185+
// segments that we are going to proxy, we have a discovery request.
186+
if enableAggregatedDiscoveryTimeout && len(strings.Split(strings.Trim(req.URL.Path, "/"), "/")) == 3 {
191187
// discovery requests are used by kubectl and others to determine which resources a server has. This is a cheap call that
192188
// should be fast for every aggregated apiserver. Latency for aggregation is expected to be low (as for all extensions)
193189
// so forcing a short timeout here helps responsiveness of all clients.
194190
newCtx, cancelFn = context.WithTimeout(newCtx, aggregatedDiscoveryTimeout)
195191
}
196192

197-
// WithContext creates a shallow clone of the request with the new context.
193+
// WithContext creates a shallow clone of the request with the same context.
198194
newReq := req.WithContext(newCtx)
199195
newReq.Header = utilnet.CloneHeader(req.Header)
200196
newReq.URL = location

0 commit comments

Comments
 (0)