-
Notifications
You must be signed in to change notification settings - Fork 38.7k
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
remove go-restful from namer for rest handling #43767
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ import ( | |
"math/rand" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/http/httputil" | ||
"net/url" | ||
"reflect" | ||
"strconv" | ||
|
@@ -331,7 +332,17 @@ func handleInternal(storage map[string]rest.Storage, admissionControl admission. | |
} | ||
} | ||
|
||
return &defaultAPIServer{mux, container} | ||
handler := genericapifilters.WithRequestInfo(mux, testRequestInfoResolver(), requestContextMapper) | ||
handler = request.WithRequestContext(handler, requestContextMapper) | ||
|
||
return &defaultAPIServer{handler, container} | ||
} | ||
|
||
func testRequestInfoResolver() *request.RequestInfoFactory { | ||
return &request.RequestInfoFactory{ | ||
APIPrefixes: sets.NewString("api", "apis"), | ||
GrouplessAPIPrefixes: sets.NewString("api"), | ||
} | ||
} | ||
|
||
func TestSimpleSetupRight(t *testing.T) { | ||
|
@@ -746,7 +757,7 @@ func TestNotFound(t *testing.T) { | |
"groupless root DELETE with extra segment": {"DELETE", "/" + grouplessPrefix + "/" + grouplessGroupVersion.Version + "/simpleroots/bar/baz", http.StatusNotFound}, | ||
"groupless root PUT without extra segment": {"PUT", "/" + grouplessPrefix + "/" + grouplessGroupVersion.Version + "/simpleroots", http.StatusMethodNotAllowed}, | ||
"groupless root PUT with extra segment": {"PUT", "/" + grouplessPrefix + "/" + grouplessGroupVersion.Version + "/simpleroots/bar/baz", http.StatusNotFound}, | ||
"groupless root watch missing storage": {"GET", "/" + grouplessPrefix + "/" + grouplessGroupVersion.Version + "/watch/", http.StatusNotFound}, | ||
"groupless root watch missing storage": {"GET", "/" + grouplessPrefix + "/" + grouplessGroupVersion.Version + "/watch/", http.StatusInternalServerError}, | ||
|
||
"groupless namespaced PATCH method": {"PATCH", "/" + grouplessPrefix + "/" + grouplessGroupVersion.Version + "/namespaces/ns/simples", http.StatusMethodNotAllowed}, | ||
"groupless namespaced GET long prefix": {"GET", "/" + grouplessPrefix + "/", http.StatusNotFound}, | ||
|
@@ -757,7 +768,7 @@ func TestNotFound(t *testing.T) { | |
"groupless namespaced DELETE with extra segment": {"DELETE", "/" + grouplessPrefix + "/" + grouplessGroupVersion.Version + "/namespaces/ns/simples/bar/baz", http.StatusNotFound}, | ||
"groupless namespaced PUT without extra segment": {"PUT", "/" + grouplessPrefix + "/" + grouplessGroupVersion.Version + "/namespaces/ns/simples", http.StatusMethodNotAllowed}, | ||
"groupless namespaced PUT with extra segment": {"PUT", "/" + grouplessPrefix + "/" + grouplessGroupVersion.Version + "/namespaces/ns/simples/bar/baz", http.StatusNotFound}, | ||
"groupless namespaced watch missing storage": {"GET", "/" + grouplessPrefix + "/" + grouplessGroupVersion.Version + "/watch/", http.StatusNotFound}, | ||
"groupless namespaced watch missing storage": {"GET", "/" + grouplessPrefix + "/" + grouplessGroupVersion.Version + "/watch/", http.StatusInternalServerError}, | ||
"groupless namespaced watch with bad method": {"POST", "/" + grouplessPrefix + "/" + grouplessGroupVersion.Version + "/watch/namespaces/ns/simples/bar", http.StatusMethodNotAllowed}, | ||
"groupless namespaced watch param with bad method": {"POST", "/" + grouplessPrefix + "/" + grouplessGroupVersion.Version + "/namespaces/ns/simples/bar?watch=true", http.StatusMethodNotAllowed}, | ||
|
||
|
@@ -777,7 +788,7 @@ func TestNotFound(t *testing.T) { | |
"root DELETE with extra segment": {"DELETE", "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/simpleroots/bar/baz", http.StatusNotFound}, | ||
"root PUT without extra segment": {"PUT", "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/simpleroots", http.StatusMethodNotAllowed}, | ||
"root PUT with extra segment": {"PUT", "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/simpleroots/bar/baz", http.StatusNotFound}, | ||
"root watch missing storage": {"GET", "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/watch/", http.StatusNotFound}, | ||
"root watch missing storage": {"GET", "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/watch/", http.StatusInternalServerError}, | ||
// TODO: JTL: "root watch with bad method": {"POST", "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/watch/simpleroot/bar", http.StatusMethodNotAllowed}, | ||
|
||
"namespaced PATCH method": {"PATCH", "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/namespaces/ns/simples", http.StatusMethodNotAllowed}, | ||
|
@@ -789,7 +800,7 @@ func TestNotFound(t *testing.T) { | |
"namespaced DELETE with extra segment": {"DELETE", "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/namespaces/ns/simples/bar/baz", http.StatusNotFound}, | ||
"namespaced PUT without extra segment": {"PUT", "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/namespaces/ns/simples", http.StatusMethodNotAllowed}, | ||
"namespaced PUT with extra segment": {"PUT", "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/namespaces/ns/simples/bar/baz", http.StatusNotFound}, | ||
"namespaced watch missing storage": {"GET", "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/watch/", http.StatusNotFound}, | ||
"namespaced watch missing storage": {"GET", "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/watch/", http.StatusInternalServerError}, | ||
"namespaced watch with bad method": {"POST", "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/watch/namespaces/ns/simples/bar", http.StatusMethodNotAllowed}, | ||
"namespaced watch param with bad method": {"POST", "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/namespaces/ns/simples/bar?watch=true", http.StatusMethodNotAllowed}, | ||
} | ||
|
@@ -1081,7 +1092,7 @@ func TestList(t *testing.T) { | |
if !simpleStorage.namespacePresent { | ||
t.Errorf("%d: namespace not set", i) | ||
} else if simpleStorage.actualNamespace != testCase.namespace { | ||
t.Errorf("%d: unexpected resource namespace: %s", i, simpleStorage.actualNamespace) | ||
t.Errorf("%d: %q unexpected resource namespace: %s", i, testCase.url, simpleStorage.actualNamespace) | ||
} | ||
if simpleStorage.requestedLabelSelector == nil || simpleStorage.requestedLabelSelector.String() != testCase.label { | ||
t.Errorf("%d: unexpected label selector: %v", i, simpleStorage.requestedLabelSelector) | ||
|
@@ -1169,6 +1180,7 @@ func TestNonEmptyList(t *testing.T) { | |
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
t.Log(body) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. debug output There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Super useful when the test goes poorly. |
||
|
||
if len(listOut.Items) != 1 { | ||
t.Errorf("Unexpected response: %#v", listOut) | ||
|
@@ -2220,10 +2232,12 @@ func TestPatch(t *testing.T) { | |
client := http.Client{} | ||
request, err := http.NewRequest("PATCH", server.URL+"/"+prefix+"/"+testGroupVersion.Group+"/"+testGroupVersion.Version+"/namespaces/default/simple/"+ID, bytes.NewReader([]byte(`{"labels":{"foo":"bar"}}`))) | ||
request.Header.Set("Content-Type", "application/merge-patch+json; charset=UTF-8") | ||
_, err = client.Do(request) | ||
response, err := client.Do(request) | ||
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
dump, _ := httputil.DumpResponse(response, true) | ||
t.Log(string(dump)) | ||
|
||
if simpleStorage.updated == nil || simpleStorage.updated.Labels["foo"] != "bar" { | ||
t.Errorf("Unexpected update value %#v, expected %#v.", simpleStorage.updated, item) | ||
|
@@ -2292,10 +2306,12 @@ func TestUpdate(t *testing.T) { | |
|
||
client := http.Client{} | ||
request, err := http.NewRequest("PUT", server.URL+"/"+prefix+"/"+testGroupVersion.Group+"/"+testGroupVersion.Version+"/namespaces/default/simple/"+ID, bytes.NewReader(body)) | ||
_, err = client.Do(request) | ||
response, err := client.Do(request) | ||
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
dump, _ := httputil.DumpResponse(response, true) | ||
t.Log(string(dump)) | ||
|
||
if simpleStorage.updated == nil || simpleStorage.updated.Name != item.Name { | ||
t.Errorf("Unexpected update value %#v, expected %#v.", simpleStorage.updated, item) | ||
|
@@ -2333,6 +2349,9 @@ func TestUpdateInvokesAdmissionControl(t *testing.T) { | |
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
dump, _ := httputil.DumpResponse(response, true) | ||
t.Log(string(dump)) | ||
|
||
if response.StatusCode != http.StatusForbidden { | ||
t.Errorf("Unexpected response %#v", response) | ||
} | ||
|
@@ -2343,7 +2362,7 @@ func TestUpdateRequiresMatchingName(t *testing.T) { | |
simpleStorage := SimpleRESTStorage{} | ||
ID := "id" | ||
storage["simple"] = &simpleStorage | ||
handler := handleDeny(storage) | ||
handler := handle(storage) | ||
server := httptest.NewServer(handler) | ||
defer server.Close() | ||
|
||
|
@@ -2363,6 +2382,8 @@ func TestUpdateRequiresMatchingName(t *testing.T) { | |
t.Errorf("unexpected error: %v", err) | ||
} | ||
if response.StatusCode != http.StatusBadRequest { | ||
dump, _ := httputil.DumpResponse(response, true) | ||
t.Log(string(dump)) | ||
t.Errorf("Unexpected response %#v", response) | ||
} | ||
} | ||
|
@@ -2394,13 +2415,16 @@ func TestUpdateAllowsMissingNamespace(t *testing.T) { | |
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
dump, _ := httputil.DumpResponse(response, true) | ||
t.Log(string(dump)) | ||
|
||
if response.StatusCode != http.StatusOK { | ||
t.Errorf("Unexpected response %#v", response) | ||
} | ||
} | ||
|
||
// when the object name and namespace can't be retrieved, skip name checking | ||
func TestUpdateAllowsMismatchedNamespaceOnError(t *testing.T) { | ||
// when the object name and namespace can't be retrieved, don't update. It isn't safe. | ||
func TestUpdateDisallowsMismatchedNamespaceOnError(t *testing.T) { | ||
storage := map[string]rest.Storage{} | ||
simpleStorage := SimpleRESTStorage{} | ||
ID := "id" | ||
|
@@ -2428,13 +2452,15 @@ func TestUpdateAllowsMismatchedNamespaceOnError(t *testing.T) { | |
|
||
client := http.Client{} | ||
request, err := http.NewRequest("PUT", server.URL+"/"+prefix+"/"+testGroupVersion.Group+"/"+testGroupVersion.Version+"/namespaces/default/simple/"+ID, bytes.NewReader(body)) | ||
_, err = client.Do(request) | ||
response, err := client.Do(request) | ||
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
dump, _ := httputil.DumpResponse(response, true) | ||
t.Log(string(dump)) | ||
|
||
if simpleStorage.updated == nil || simpleStorage.updated.Name != item.Name { | ||
t.Errorf("Unexpected update value %#v, expected %#v.", simpleStorage.updated, item) | ||
if simpleStorage.updated != nil { | ||
t.Errorf("Unexpected update value %#v.", simpleStorage.updated) | ||
} | ||
if selfLinker.called { | ||
t.Errorf("self link ignored") | ||
|
@@ -2605,14 +2631,17 @@ func TestUpdateREST(t *testing.T) { | |
} | ||
|
||
testREST := func(t *testing.T, container *restful.Container, barCode int) { | ||
handler := genericapifilters.WithRequestInfo(container, newTestRequestInfoResolver(), requestContextMapper) | ||
handler = request.WithRequestContext(handler, requestContextMapper) | ||
|
||
w := httptest.NewRecorder() | ||
container.ServeHTTP(w, &http.Request{Method: "GET", URL: &url.URL{Path: "/" + prefix + "/" + newGroupVersion.Group + "/" + newGroupVersion.Version + "/namespaces/test/foo/test"}}) | ||
handler.ServeHTTP(w, &http.Request{Method: "GET", URL: &url.URL{Path: "/" + prefix + "/" + newGroupVersion.Group + "/" + newGroupVersion.Version + "/namespaces/test/foo/test"}}) | ||
if w.Code != http.StatusOK { | ||
t.Fatalf("expected OK: %#v", w) | ||
} | ||
|
||
w = httptest.NewRecorder() | ||
container.ServeHTTP(w, &http.Request{Method: "GET", URL: &url.URL{Path: "/" + prefix + "/" + newGroupVersion.Group + "/" + newGroupVersion.Version + "/namespaces/test/bar/test"}}) | ||
handler.ServeHTTP(w, &http.Request{Method: "GET", URL: &url.URL{Path: "/" + prefix + "/" + newGroupVersion.Group + "/" + newGroupVersion.Version + "/namespaces/test/bar/test"}}) | ||
if w.Code != barCode { | ||
t.Errorf("expected response code %d for GET to bar but received %d", barCode, w.Code) | ||
} | ||
|
@@ -2716,16 +2745,19 @@ func TestParentResourceIsRequired(t *testing.T) { | |
t.Fatal(err) | ||
} | ||
|
||
handler := genericapifilters.WithRequestInfo(container, newTestRequestInfoResolver(), requestContextMapper) | ||
handler = request.WithRequestContext(handler, requestContextMapper) | ||
|
||
// resource is NOT registered in the root scope | ||
w := httptest.NewRecorder() | ||
container.ServeHTTP(w, &http.Request{Method: "GET", URL: &url.URL{Path: "/" + prefix + "/simple/test/sub"}}) | ||
handler.ServeHTTP(w, &http.Request{Method: "GET", URL: &url.URL{Path: "/" + prefix + "/simple/test/sub"}}) | ||
if w.Code != http.StatusNotFound { | ||
t.Errorf("expected not found: %#v", w) | ||
} | ||
|
||
// resource is registered in the namespace scope | ||
w = httptest.NewRecorder() | ||
container.ServeHTTP(w, &http.Request{Method: "GET", URL: &url.URL{Path: "/" + prefix + "/" + newGroupVersion.Group + "/" + newGroupVersion.Version + "/namespaces/test/simple/test/sub"}}) | ||
handler.ServeHTTP(w, &http.Request{Method: "GET", URL: &url.URL{Path: "/" + prefix + "/" + newGroupVersion.Group + "/" + newGroupVersion.Version + "/namespaces/test/simple/test/sub"}}) | ||
if w.Code != http.StatusOK { | ||
t.Fatalf("expected OK: %#v", w) | ||
} | ||
|
147 changes: 147 additions & 0 deletions
147
staging/src/k8s.io/apiserver/pkg/endpoints/handlers/namer.go
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
Copyright 2017 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package handlers | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
|
||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apiserver/pkg/endpoints/request" | ||
) | ||
|
||
// ContextFunc returns a Context given a request - a context must be returned | ||
type ContextFunc func(req *http.Request) request.Context | ||
|
||
// ScopeNamer handles accessing names from requests and objects | ||
type ScopeNamer interface { | ||
// Namespace returns the appropriate namespace value from the request (may be empty) or an | ||
// error. | ||
Namespace(req *http.Request) (namespace string, err error) | ||
// Name returns the name from the request, and an optional namespace value if this is a namespace | ||
// scoped call. An error is returned if the name is not available. | ||
Name(req *http.Request) (namespace, name string, err error) | ||
// ObjectName returns the namespace and name from an object if they exist, or an error if the object | ||
// does not support names. | ||
ObjectName(obj runtime.Object) (namespace, name string, err error) | ||
// SetSelfLink sets the provided URL onto the object. The method should return nil if the object | ||
// does not support selfLinks. | ||
SetSelfLink(obj runtime.Object, url string) error | ||
// GenerateLink creates an encoded URI for a given runtime object that represents the canonical path | ||
// and query. | ||
GenerateLink(req *http.Request, obj runtime.Object) (uri string, err error) | ||
// GenerateLink creates an encoded URI for a list that represents the canonical path and query. | ||
GenerateListLink(req *http.Request) (uri string, err error) | ||
} | ||
|
||
type ContextBasedNaming struct { | ||
GetContext ContextFunc | ||
SelfLinker runtime.SelfLinker | ||
ClusterScoped bool | ||
|
||
SelfLinkPathPrefix string | ||
SelfLinkPathSuffix string | ||
} | ||
|
||
// ContextBasedNaming implements ScopeNamer | ||
var _ ScopeNamer = ContextBasedNaming{} | ||
|
||
func (n ContextBasedNaming) SetSelfLink(obj runtime.Object, url string) error { | ||
return n.SelfLinker.SetSelfLink(obj, url) | ||
} | ||
|
||
func (n ContextBasedNaming) Namespace(req *http.Request) (namespace string, err error) { | ||
requestInfo, ok := request.RequestInfoFrom(n.GetContext(req)) | ||
if !ok { | ||
return "", fmt.Errorf("missing requestInfo") | ||
} | ||
return requestInfo.Namespace, nil | ||
} | ||
|
||
func (n ContextBasedNaming) Name(req *http.Request) (namespace, name string, err error) { | ||
requestInfo, ok := request.RequestInfoFrom(n.GetContext(req)) | ||
if !ok { | ||
return "", "", fmt.Errorf("missing requestInfo") | ||
} | ||
ns, err := n.Namespace(req) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
if len(requestInfo.Name) == 0 { | ||
return "", "", errEmptyName | ||
} | ||
return ns, requestInfo.Name, nil | ||
} | ||
|
||
func (n ContextBasedNaming) GenerateLink(req *http.Request, obj runtime.Object) (uri string, err error) { | ||
namespace, name, err := n.ObjectName(obj) | ||
if err != nil { | ||
return "", err | ||
} | ||
requestInfo, ok := request.RequestInfoFrom(n.GetContext(req)) | ||
if !ok { | ||
return "", fmt.Errorf("missing requestInfo") | ||
} | ||
|
||
if len(namespace) == 0 && len(name) == 0 { | ||
if len(requestInfo.Name) == 0 { | ||
return "", errEmptyName | ||
} | ||
|
||
namespace = requestInfo.Namespace | ||
name = requestInfo.Name | ||
} | ||
|
||
if n.ClusterScoped { | ||
return n.SelfLinkPathPrefix + url.QueryEscape(name) + n.SelfLinkPathSuffix, nil | ||
} | ||
|
||
return n.SelfLinkPathPrefix + | ||
url.QueryEscape(namespace) + | ||
"/" + url.QueryEscape(requestInfo.Resource) + "/" + | ||
url.QueryEscape(name) + | ||
n.SelfLinkPathSuffix, | ||
nil | ||
} | ||
|
||
func (n ContextBasedNaming) GenerateListLink(req *http.Request) (uri string, err error) { | ||
if len(req.URL.RawPath) > 0 { | ||
return req.URL.RawPath, nil | ||
} | ||
return req.URL.EscapedPath(), nil | ||
} | ||
|
||
func (n ContextBasedNaming) ObjectName(obj runtime.Object) (namespace, name string, err error) { | ||
name, err = n.SelfLinker.Name(obj) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
if len(name) == 0 { | ||
return "", "", errEmptyName | ||
} | ||
namespace, err = n.SelfLinker.Namespace(obj) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
return namespace, name, err | ||
} | ||
|
||
// errEmptyName is returned when API requests do not fill the name section of the path. | ||
var errEmptyName = errors.NewBadRequest("name must be provided") |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do these change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This used to test the cases without the "normal" handler chain. Once you add a requestInfoResolver into the chain, it fails on these URLs because they aren't well formed. The same thing would happen in our real chain.