From 6694a45542f68b91c6ec250ac9a34ee73a1a8268 Mon Sep 17 00:00:00 2001 From: derekwaynecarr Date: Thu, 11 Dec 2014 13:59:51 -0500 Subject: [PATCH] Add doc for {namespace} path param, fixup and verify proxy paths --- pkg/apiserver/apiserver.go | 13 ++++++++++--- pkg/apiserver/proxy.go | 5 +++-- pkg/apiserver/proxy_test.go | 3 +++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/pkg/apiserver/apiserver.go b/pkg/apiserver/apiserver.go index cd9cdfa559a..ad5609bb554 100644 --- a/pkg/apiserver/apiserver.go +++ b/pkg/apiserver/apiserver.go @@ -100,7 +100,7 @@ func indirectArbitraryPointer(ptrToObject interface{}) interface{} { return reflect.Indirect(reflect.ValueOf(ptrToObject)).Interface() } -func registerResourceHandlers(ws *restful.WebService, version string, path string, storage RESTStorage, kinds map[string]reflect.Type, h restful.RouteFunction) { +func registerResourceHandlers(ws *restful.WebService, version string, path string, storage RESTStorage, kinds map[string]reflect.Type, h restful.RouteFunction, namespaceScope bool) { glog.V(3).Infof("Installing /%s/%s\n", version, path) object := storage.New() _, kind, err := api.Scheme.ObjectVersionAndKind(object) @@ -118,6 +118,11 @@ func registerResourceHandlers(ws *restful.WebService, version string, path strin // See github.com/emicklei/go-restful/blob/master/jsr311.go for routing logic // and status-code behavior + if namespaceScope { + path = "ns/{namespace}/" + path + ws.Param(ws.PathParameter("namespace", "object name and auth scope, such as for teams and projects").DataType("string")) + } + glog.V(3).Infof("Installing version=/%s, kind=/%s, path=/%s\n", version, kind, path) ws.Route(ws.POST(path).To(h). Doc("create a " + kind). @@ -221,8 +226,10 @@ func (g *APIGroupVersion) InstallREST(container *restful.Container, root string, } for path, storage := range g.handler.storage { - registerResourceHandlers(ws, version, path, storage, kinds, h) - registerResourceHandlers(ws, version, "ns/{namespace}/"+path, storage, kinds, h) + // register legacy patterns where namespace is optional in path + registerResourceHandlers(ws, version, path, storage, kinds, h, false) + // register pattern where namespace is required in path + registerResourceHandlers(ws, version, path, storage, kinds, h, true) } // TODO: port the rest of these. Sadly, if we don't, we'll have inconsistent diff --git a/pkg/apiserver/proxy.go b/pkg/apiserver/proxy.go index 1b2f26ce234..e358ffe2aa4 100644 --- a/pkg/apiserver/proxy.go +++ b/pkg/apiserver/proxy.go @@ -90,8 +90,9 @@ func (r *ProxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { } id := parts[1] rest := "" - if len(parts) == 3 { - rest = parts[2] + if len(parts) > 2 { + proxyParts := parts[2:] + rest = strings.Join(proxyParts, "/") } storage, ok := r.storage[kind] if !ok { diff --git a/pkg/apiserver/proxy_test.go b/pkg/apiserver/proxy_test.go index 109bde3f72f..9ab96a32134 100644 --- a/pkg/apiserver/proxy_test.go +++ b/pkg/apiserver/proxy_test.go @@ -154,6 +154,9 @@ func TestProxy(t *testing.T) { if e, a := item.reqBody, string(gotBody); e != a { t.Errorf("%v - expected %v, got %v", item.method, e, a) } + if e, a := item.path, req.URL.Path; e != a { + t.Errorf("%v - expected %v, got %v", item.method, e, a) + } fmt.Fprint(w, item.respBody) })) defer proxyServer.Close()