Skip to content

Commit

Permalink
Feat: support proxy server with subpath
Browse files Browse the repository at this point in the history
Signed-off-by: Yin Da <[email protected]>
  • Loading branch information
Somefive authored and yue9944882 committed Dec 14, 2022
1 parent 3208442 commit 6d70297
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
5 changes: 3 additions & 2 deletions pkg/apis/cluster/v1alpha1/clustergateway_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/http"
"net/url"
"os"
gopath "path"
"strings"
"time"

Expand Down Expand Up @@ -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()

Expand All @@ -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,
},
Expand Down
36 changes: 32 additions & 4 deletions pkg/apis/cluster/v1alpha1/clustergateway_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package v1alpha1

import (
"context"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
gopath "path"
"strings"
"testing"

Expand All @@ -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)
}{
Expand Down Expand Up @@ -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 {
Expand All @@ -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),
},
}
Expand All @@ -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)
})
}
}
Expand Down

0 comments on commit 6d70297

Please sign in to comment.