From 6d7029785f34f69412ea5061b38e100fe8c1484d Mon Sep 17 00:00:00 2001 From: Yin Da Date: Wed, 14 Dec 2022 16:21:51 +0800 Subject: [PATCH] Feat: support proxy server with subpath Signed-off-by: Yin Da --- .../cluster/v1alpha1/clustergateway_proxy.go | 5 +-- .../v1alpha1/clustergateway_proxy_test.go | 36 ++++++++++++++++--- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/pkg/apis/cluster/v1alpha1/clustergateway_proxy.go b/pkg/apis/cluster/v1alpha1/clustergateway_proxy.go index ace7937f..c4ebb10e 100644 --- a/pkg/apis/cluster/v1alpha1/clustergateway_proxy.go +++ b/pkg/apis/cluster/v1alpha1/clustergateway_proxy.go @@ -21,6 +21,7 @@ import ( "net/http" "net/url" "os" + gopath "path" "strings" "time" @@ -213,7 +214,7 @@ func (p *proxyHandler) ServeHTTP(writer http.ResponseWriter, request *http.Reque host, _, _ := net.SplitHostPort(urlAddr.Host) path := strings.TrimPrefix(request.URL.Path, apiPrefix+p.parentName+apiSuffix) newReq.Host = host - newReq.URL.Path = path + newReq.URL.Path = gopath.Join(urlAddr.Path, path) newReq.URL.RawQuery = request.URL.RawQuery newReq.RequestURI = newReq.URL.RequestURI() @@ -233,7 +234,7 @@ func (p *proxyHandler) ServeHTTP(writer http.ResponseWriter, request *http.Reque proxy := apiproxy.NewUpgradeAwareHandler( &url.URL{ Scheme: urlAddr.Scheme, - Path: path, + Path: newReq.URL.Path, Host: urlAddr.Host, RawQuery: request.URL.RawQuery, }, diff --git a/pkg/apis/cluster/v1alpha1/clustergateway_proxy_test.go b/pkg/apis/cluster/v1alpha1/clustergateway_proxy_test.go index 1a7c6a1a..5e485dcd 100644 --- a/pkg/apis/cluster/v1alpha1/clustergateway_proxy_test.go +++ b/pkg/apis/cluster/v1alpha1/clustergateway_proxy_test.go @@ -2,9 +2,10 @@ package v1alpha1 import ( "context" - "io/ioutil" + "io" "net/http" "net/http/httptest" + gopath "path" "strings" "testing" @@ -31,6 +32,7 @@ func TestProxyHandler(t *testing.T) { objName string inputOption *ClusterGatewayProxyOptions reqInfo request.RequestInfo + endpointPath string expectedFailure bool errorAssertFunc func(t *testing.T, err error) }{ @@ -76,6 +78,32 @@ func TestProxyHandler(t *testing.T) { assert.True(t, strings.HasPrefix(err.Error(), "no such cluster")) }, }, + { + name: "normal proxy with sub-path in endpoint should work", + parent: &fakeParentStorage{ + obj: &ClusterGateway{ + ObjectMeta: metav1.ObjectMeta{ + Name: "myName", + }, + Spec: ClusterGatewaySpec{ + Access: ClusterAccess{ + Credential: &ClusterAccessCredential{ + Type: CredentialTypeServiceAccountToken, + ServiceAccountToken: "myToken", + }, + }, + }, + }, + }, + endpointPath: "/extra", + objName: "myName", + inputOption: &ClusterGatewayProxyOptions{ + Path: "/abc", + }, + reqInfo: request.RequestInfo{ + Verb: "get", + }, + }, } for _, c := range cases { @@ -95,7 +123,7 @@ func TestProxyHandler(t *testing.T) { c.parent.obj.Spec.Access.Endpoint = &ClusterEndpoint{ Type: ClusterEndpointTypeConst, Const: &ClusterEndpointConst{ - Address: endpointSvr.URL, + Address: endpointSvr.URL + c.endpointPath, Insecure: pointer.Bool(true), }, } @@ -120,11 +148,11 @@ func TestProxyHandler(t *testing.T) { targetPath := apiPrefix + c.objName + apiSuffix + path resp, err := svr.Client().Get(svr.URL + targetPath) assert.NoError(t, err) - data, err := ioutil.ReadAll(resp.Body) + data, err := io.ReadAll(resp.Body) require.NoError(t, err) assert.Equal(t, text, string(data)) assert.Equal(t, 200, resp.StatusCode) - assert.Equal(t, path, receivingReq.URL.Path) + assert.Equal(t, gopath.Join(c.endpointPath, path), receivingReq.URL.Path) }) } }