Created
September 11, 2019 14:20
-
-
Save sttts/ac21ef3a5d270ada7573ef508ad77c6e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/vendor/k8s.io/kubernetes/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy.go b/vendor/k8s.io/kubernetes/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy.go | |
index 99b7b4b82e..83a43dbddf 100644 | |
--- a/vendor/k8s.io/kubernetes/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy.go | |
+++ b/vendor/k8s.io/kubernetes/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy.go | |
@@ -189,9 +189,14 @@ 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 := req.Context() | |
+ newCtx := req.Context() // context.Background() | |
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) | |
+ //} | |
+ | |
if requestInfo, ok := genericapirequest.RequestInfoFrom(req.Context()); ok { | |
// 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. | |
diff --git a/vendor/k8s.io/kubernetes/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy_test.go b/vendor/k8s.io/kubernetes/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy_test.go | |
index 75345b8911..91c3f979af 100644 | |
--- a/vendor/k8s.io/kubernetes/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy_test.go | |
+++ b/vendor/k8s.io/kubernetes/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy_test.go | |
@@ -20,6 +20,7 @@ import ( | |
"crypto/tls" | |
"fmt" | |
"io/ioutil" | |
+ "net" | |
"net/http" | |
"net/http/httptest" | |
"net/http/httputil" | |
@@ -535,19 +536,29 @@ VYsTwo5YgV1HzDkV+BNmBCw1GYcGXAElhJI+dCsgQuuU6TKzgl8= | |
func TestGetContextForNewRequest(t *testing.T) { | |
done := make(chan struct{}) | |
- server := httptest.NewTLSServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { | |
+ incoming := make(chan *http.Request, 10) | |
+ | |
+ server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | |
+ incoming <- req | |
<-done // never return so that we're certain to return base on timeout | |
})) | |
defer server.Close() | |
defer close(done) | |
- proxyServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | |
+ l, err := net.Listen("tcp", "localhost:0") | |
+ if err != nil { | |
+ t.Fatal(err) | |
+ } | |
+ go http.Serve(l, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | |
location, err := url.Parse(server.URL) | |
if err != nil { | |
t.Fatal(err) | |
} | |
location.Path = req.URL.Path | |
+ // simulate a real HTTP server | |
+ //ctx := context.WithValue(req.Context(), http.ServerContextKey, nil) | |
+ | |
nestedReq := req.WithContext(genericapirequest.WithRequestInfo(req.Context(), &genericapirequest.RequestInfo{Path: req.URL.Path})) | |
newReq, cancelFn := newRequestForProxy(location, nestedReq, true) | |
defer cancelFn() | |
@@ -555,10 +566,11 @@ func TestGetContextForNewRequest(t *testing.T) { | |
theproxy := proxy.NewUpgradeAwareHandler(location, server.Client().Transport, true, false, &responder{w: w}) | |
theproxy.ServeHTTP(w, newReq) | |
})) | |
- defer proxyServer.Close() | |
+ defer l.Close() | |
// normal clients will not be setting a timeout, don't set one here. Our proxy logic should construct this for us | |
- resp, err := proxyServer.Client().Get(proxyServer.URL + "/apis/group/version") | |
+ t.Logf("Getting /apis/group/version with a blocking backend, running into timeout") | |
+ resp, err := http.Get("http://" + l.Addr().String() + "/apis/group/version") | |
if err != nil { | |
t.Fatal(err) | |
} | |
@@ -572,5 +584,19 @@ func TestGetContextForNewRequest(t *testing.T) { | |
if !strings.Contains(string(body), "Error trying to reach service: 'context deadline exceeded'") { | |
t.Error(string(body)) | |
} | |
- | |
+ <-incoming | |
+ | |
+ // try again but close the server half-way, checking that | |
+ t.Logf("Getting /apis/group/version with a failing backend") | |
+ errCh := make(chan error) | |
+ go func() { | |
+ _, err := http.Get("http://" + l.Addr().String() + "/apis/group/version") | |
+ errCh <- err | |
+ }() | |
+ <-incoming | |
+ server.CloseClientConnections() | |
+ if err := <-errCh; err == nil { | |
+ t.Fatal("expected request error") | |
+ } | |
+ t.Logf("got error as expected: %v", err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment