From 120570b3fba0ea3f3c882852e16154483a0919c8 Mon Sep 17 00:00:00 2001 From: KevinKingKong <594849777@qq.com> Date: Wed, 9 Jan 2019 01:05:35 +0800 Subject: [PATCH 1/2] fix apiserver aggregator bug --- .../pkg/apiserver/handler_proxy.go | 1 + .../pkg/apiserver/handler_proxy_test.go | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+) 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 e6e30bbd44b..ee3433212c6 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 @@ -142,6 +142,7 @@ func (r *proxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { newReq := req.WithContext(context.Background()) newReq.Header = utilnet.CloneHeader(req.Header) newReq.URL = location + newReq.Host = location.Host if handlingInfo.proxyRoundTripper == nil { proxyError(w, req, "", http.StatusNotFound) diff --git a/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy_test.go b/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy_test.go index 354c8d8b94a..6133eb64dc5 100644 --- a/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy_test.go +++ b/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy_test.go @@ -42,18 +42,21 @@ type targetHTTPHandler struct { called bool headers map[string][]string path string + host string } func (d *targetHTTPHandler) Reset() { d.path = "" d.called = false d.headers = nil + d.host = "" } func (d *targetHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { d.path = r.URL.Path d.called = true d.headers = r.Header + d.host = r.Host w.WriteHeader(http.StatusOK) } @@ -258,6 +261,38 @@ func TestProxyHandler(t *testing.T) { }, expectedStatusCode: http.StatusServiceUnavailable, }, + "change aggregator http request host": { + user: &user.DefaultInfo{ + Name: "username", + Groups: []string{"one", "two"}, + }, + path: "/request/path", + apiService: &apiregistration.APIService{ + ObjectMeta: metav1.ObjectMeta{Name: "v1.foo"}, + Spec: apiregistration.APIServiceSpec{ + Service: &apiregistration.ServiceReference{}, + Group: "foo", + Version: "v1", + InsecureSkipTLSVerify: true, + }, + Status: apiregistration.APIServiceStatus{ + Conditions: []apiregistration.APIServiceCondition{ + {Type: apiregistration.Available, Status: apiregistration.ConditionTrue}, + }, + }, + }, + expectedStatusCode: http.StatusOK, + expectedCalled: true, + expectedHeaders: map[string][]string{ + "X-Forwarded-Proto": {"https"}, + "X-Forwarded-Uri": {"/request/path"}, + "X-Forwarded-For": {"127.0.0.1"}, + "X-Remote-User": {"username"}, + "User-Agent": {"Go-http-client/1.1"}, + "Accept-Encoding": {"gzip"}, + "X-Remote-Group": {"one", "two"}, + }, + }, } for name, tc := range tests { @@ -313,6 +348,10 @@ func TestProxyHandler(t *testing.T) { t.Errorf("%s: expected %v, got %v", name, e, a) return } + if e, a := targetServer.Listener.Addr().String(), target.host; tc.expectedCalled && !reflect.DeepEqual(e, a) { + t.Errorf("%s: expected %v, got %v", name, e, a) + return + } }() } } From 3a52b9783912bdf19fc249abbd57943cdca07620 Mon Sep 17 00:00:00 2001 From: KevinKingKong <594849777@qq.com> Date: Wed, 9 Jan 2019 02:09:47 +0800 Subject: [PATCH 2/2] remove the unnecessary test case --- .../pkg/apiserver/handler_proxy_test.go | 32 ------------------- 1 file changed, 32 deletions(-) diff --git a/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy_test.go b/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy_test.go index 6133eb64dc5..b4ed4b77bdc 100644 --- a/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy_test.go +++ b/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_proxy_test.go @@ -261,38 +261,6 @@ func TestProxyHandler(t *testing.T) { }, expectedStatusCode: http.StatusServiceUnavailable, }, - "change aggregator http request host": { - user: &user.DefaultInfo{ - Name: "username", - Groups: []string{"one", "two"}, - }, - path: "/request/path", - apiService: &apiregistration.APIService{ - ObjectMeta: metav1.ObjectMeta{Name: "v1.foo"}, - Spec: apiregistration.APIServiceSpec{ - Service: &apiregistration.ServiceReference{}, - Group: "foo", - Version: "v1", - InsecureSkipTLSVerify: true, - }, - Status: apiregistration.APIServiceStatus{ - Conditions: []apiregistration.APIServiceCondition{ - {Type: apiregistration.Available, Status: apiregistration.ConditionTrue}, - }, - }, - }, - expectedStatusCode: http.StatusOK, - expectedCalled: true, - expectedHeaders: map[string][]string{ - "X-Forwarded-Proto": {"https"}, - "X-Forwarded-Uri": {"/request/path"}, - "X-Forwarded-For": {"127.0.0.1"}, - "X-Remote-User": {"username"}, - "User-Agent": {"Go-http-client/1.1"}, - "Accept-Encoding": {"gzip"}, - "X-Remote-Group": {"one", "two"}, - }, - }, } for name, tc := range tests {