-
Couldn't load subscription status.
- Fork 124
support apiserver url rewrite #590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
huiwq1990
wants to merge
1
commit into
clusterpedia-io:main
Choose a base branch
from
huiwq1990:feat-apiserver-rewrite
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package features | ||
|
|
||
| import ( | ||
| "k8s.io/apimachinery/pkg/util/runtime" | ||
| "k8s.io/component-base/featuregate" | ||
|
|
||
| clusterpediafeature "github.com/clusterpedia-io/clusterpedia/pkg/utils/feature" | ||
| ) | ||
|
|
||
| const ( | ||
|
|
||
| // ResourcePathWithoutClusterpediaPrefix is a feature gate for rewrite apiserver request's URL | ||
| // owner: @huiwq1990 | ||
| // alpha: v0.8.0 | ||
| ResourcePathWithoutClusterpediaPrefix featuregate.Feature = "ResourcePathWithoutClusterpediaPrefix" | ||
| ) | ||
|
|
||
| func init() { | ||
| runtime.Must(clusterpediafeature.MutableFeatureGate.Add(defaultResourcePathWithoutClusterpediaPrefixFeatureGates)) | ||
| } | ||
|
|
||
| // defaultResourcePathWithoutClusterpediaPrefixFeatureGates consists of all known apiserver feature keys. | ||
| // To add a new feature, define a key for it above and add it here. | ||
| var defaultResourcePathWithoutClusterpediaPrefixFeatureGates = map[featuregate.Feature]featuregate.FeatureSpec{ | ||
| ResourcePathWithoutClusterpediaPrefix: {Default: false, PreRelease: featuregate.Alpha}, | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package filters | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/url" | ||
| "strings" | ||
|
|
||
| "k8s.io/apimachinery/pkg/util/sets" | ||
| "k8s.io/klog/v2" | ||
| ) | ||
|
|
||
| const OriginPathHeaderKey = "X-Rewrite-Original-Path" | ||
| const ApiServicePrefix = "/apis/clusterpedia.io" | ||
| const OldResourceApiServerPrefixWithoutSlash = "/apis/clusterpedia.io/v1beta1/resources" | ||
| const OldResourceApiServerPrefix = OldResourceApiServerPrefixWithoutSlash + "/" | ||
|
|
||
| var ExcludePrefixPaths = sets.NewString("", "/livez", "/readyz", OldResourceApiServerPrefixWithoutSlash) | ||
|
|
||
| func WithRewriteFilter(handler http.Handler) http.Handler { | ||
| return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
| oldPath := req.URL.EscapedPath() | ||
| if rewritePath, ok := urlPrefixRewrite(oldPath); ok { | ||
| req.URL.Path = rewritePath | ||
| req.Header.Set(OriginPathHeaderKey, oldPath) | ||
| klog.V(5).InfoS("request need rewrite", "oldPath", oldPath, "newPath", req.URL.EscapedPath()) | ||
| } | ||
| handler.ServeHTTP(w, req) | ||
| }) | ||
| } | ||
|
|
||
| func urlPrefixRewrite(oldPath string) (string, bool) { | ||
| if strings.HasPrefix(oldPath, ApiServicePrefix) || ExcludePrefixPaths.Has(oldPath) { | ||
| return "", false | ||
| } | ||
|
|
||
| rewritePath, err := url.JoinPath(OldResourceApiServerPrefix, oldPath) | ||
| if err != nil { | ||
| return "", false | ||
| } | ||
| return rewritePath, true | ||
| } | ||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| package filters | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "net/url" | ||
| "testing" | ||
| ) | ||
|
|
||
| type testCase struct { | ||
| name string | ||
| urls []kubeRequest | ||
| } | ||
|
|
||
| type kubeRequest struct { | ||
| from string | ||
| to string | ||
| rewrite bool | ||
| } | ||
|
|
||
| var tests = []testCase{ | ||
| { | ||
| name: "do rewrite", | ||
| urls: []kubeRequest{ | ||
| { | ||
| from: "/apis/cluster.clusterpedia.io/v1beta1/resourcesany", | ||
| to: "/apis/clusterpedia.io/v1beta1/resources/apis/cluster.clusterpedia.io/v1beta1/resourcesany", | ||
| rewrite: true, | ||
| }, | ||
| { | ||
| from: "/api/v1/namespaces/default/pods?limit=100", | ||
| to: "/apis/clusterpedia.io/v1beta1/resources/api/v1/namespaces/default/pods?limit=100", | ||
| rewrite: true, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "none resources paths", | ||
| urls: []kubeRequest{ | ||
| { | ||
| from: "/livez", | ||
| to: "/livez", | ||
| rewrite: false, | ||
| }, | ||
| { | ||
| from: "/readyz", | ||
| to: "/readyz", | ||
| rewrite: false, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "not need rewrite", | ||
| urls: []kubeRequest{ | ||
| { | ||
| from: "/apis/clusterpedia.io", | ||
| to: "/apis/clusterpedia.io", | ||
| rewrite: false, | ||
| }, | ||
| { | ||
| from: "/apis/clusterpedia.io/", | ||
| to: "/apis/clusterpedia.io/", | ||
| rewrite: false, | ||
| }, | ||
| { | ||
| from: "/apis/clusterpedia.io/any", | ||
| to: "/apis/clusterpedia.io/any", | ||
| rewrite: false, | ||
| }, | ||
| { | ||
| from: "/apis/clusterpedia.io/v1beta1/resources/api/v1/namespaces/default/pods", | ||
| to: "/apis/clusterpedia.io/v1beta1/resources/api/v1/namespaces/default/pods", | ||
| rewrite: false, | ||
| }, | ||
| { | ||
| from: "/apis/clusterpedia.io/v1beta1/resources/apis/clusterpedia.io/v1beta1/clusters", | ||
| to: "/apis/clusterpedia.io/v1beta1/resources/apis/clusterpedia.io/v1beta1/clusters", | ||
| rewrite: false, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "special cases", | ||
| urls: []kubeRequest{ | ||
| { | ||
| from: "/api/v1/namespaces/default/pods?name=abc#xx", | ||
| to: "/apis/clusterpedia.io/v1beta1/resources/api/v1/namespaces/default/pods?name=abc#xx", | ||
| rewrite: true, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| func TestUrlPrefixRewrite(t *testing.T) { | ||
| for _, test := range tests { | ||
| t.Logf("Test - name: %s", test.name) | ||
|
|
||
| for _, tmp := range test.urls { | ||
| fromPath, err := url.Parse(tmp.from) | ||
| if err != nil { | ||
| t.Error(err) | ||
| } | ||
|
|
||
| rewritePath, doRewrite := urlPrefixRewrite(fromPath.EscapedPath()) | ||
| if doRewrite != tmp.rewrite { | ||
| t.Errorf("Test failed \n from : %s \n to : %s \n needRewrite: %v \n doRewrite: %v", | ||
| tmp.from, tmp.to, tmp.rewrite, doRewrite) | ||
| } | ||
|
|
||
| if doRewrite { | ||
| oldURL, err := url.Parse(tmp.to) | ||
| if err != nil { | ||
| t.Error(err) | ||
| } | ||
|
|
||
| if oldURL.EscapedPath() != rewritePath { | ||
| t.Errorf("Test failed \n from : %s \n to : %s \n oldPath: %s \n rewritePath: %s", | ||
| tmp.from, tmp.to, oldURL.EscapedPath(), rewritePath) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestRewrite(t *testing.T) { | ||
| for _, test := range tests { | ||
| t.Logf("Test - name: %s", test.name) | ||
|
|
||
| for _, tmp := range test.urls { | ||
| req, err := http.NewRequest("GET", tmp.from, nil) | ||
| if err != nil { | ||
| t.Fatalf("create HTTP request error: %v", err) | ||
| } | ||
|
|
||
| oldPath := req.URL.EscapedPath() | ||
|
|
||
| h := WithRewriteFilter( | ||
| http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) { | ||
| }), | ||
| ) | ||
|
|
||
| t.Logf("From: %s", req.URL.String()) | ||
|
|
||
| res := httptest.NewRecorder() | ||
| h.ServeHTTP(res, req) | ||
|
|
||
| t.Logf("Rewrited: %s", req.URL.String()) | ||
| if req.URL.String() != tmp.to { | ||
| t.Errorf("Test failed \n from : %s \n to : %s \n result: %s", | ||
| tmp.from, tmp.to, req.URL.RequestURI()) | ||
| } | ||
|
|
||
| if oldHeaderPath := req.Header.Get(OriginPathHeaderKey); oldHeaderPath != "" { | ||
| if oldPath != oldHeaderPath { | ||
| t.Error("incorrect flag") | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.