Merge pull request #4768 from nikhiljindal/fixPrefix

APIServer: Updating special verb handlers to accept full request path rather than a trimmed one
pull/6/head
Daniel Smith 2015-02-24 08:04:30 -08:00
commit 6241a211c8
3 changed files with 9 additions and 11 deletions

View File

@ -59,7 +59,6 @@ func (a *APIInstaller) Install() (ws *restful.WebService, errors []error) {
watchHandler := (&WatchHandler{ watchHandler := (&WatchHandler{
storage: a.group.storage, storage: a.group.storage,
codec: a.group.codec, codec: a.group.codec,
prefix: a.group.prefix,
linker: a.group.linker, linker: a.group.linker,
info: a.group.info, info: a.group.info,
}) })
@ -274,7 +273,7 @@ func (a *APIInstaller) registerResourceHandlers(path string, storage RESTStorage
addParams(route, action.Params) addParams(route, action.Params)
ws.Route(route) ws.Route(route)
case "WATCH": // Watch a resource. case "WATCH": // Watch a resource.
route := ws.GET(action.Path).To(restfulStripPrefix(a.prefix+"/watch", watchHandler)). route := ws.GET(action.Path).To(routeFunction(watchHandler)).
Filter(m). Filter(m).
Doc("watch a particular " + kind). Doc("watch a particular " + kind).
Operation("watch" + kind). Operation("watch" + kind).
@ -282,7 +281,7 @@ func (a *APIInstaller) registerResourceHandlers(path string, storage RESTStorage
addParams(route, action.Params) addParams(route, action.Params)
ws.Route(route) ws.Route(route)
case "WATCHLIST": // Watch all resources of a kind. case "WATCHLIST": // Watch all resources of a kind.
route := ws.GET(action.Path).To(restfulStripPrefix(a.prefix+"/watch", watchHandler)). route := ws.GET(action.Path).To(routeFunction(watchHandler)).
Filter(m). Filter(m).
Doc("watch a list of " + kind). Doc("watch a list of " + kind).
Operation("watch" + kind + "list"). Operation("watch" + kind + "list").
@ -290,7 +289,7 @@ func (a *APIInstaller) registerResourceHandlers(path string, storage RESTStorage
addParams(route, action.Params) addParams(route, action.Params)
ws.Route(route) ws.Route(route)
case "REDIRECT": // Get the redirect URL for a resource. case "REDIRECT": // Get the redirect URL for a resource.
route := ws.GET(action.Path).To(restfulStripPrefix(a.prefix+"/redirect", redirectHandler)). route := ws.GET(action.Path).To(routeFunction(redirectHandler)).
Filter(m). Filter(m).
Doc("redirect GET request to " + kind). Doc("redirect GET request to " + kind).
Operation("redirect" + kind). Operation("redirect" + kind).
@ -542,15 +541,15 @@ func appendIf(actions []action, a action, shouldAppend bool) []action {
return actions return actions
} }
// Returns a restful RouteFunction that calls the given handler after stripping prefix from the request path. // Wraps a http.Handler function inside a restful.RouteFunction
func restfulStripPrefix(prefix string, handler http.Handler) restful.RouteFunction { func routeFunction(handler http.Handler) restful.RouteFunction {
return func(restReq *restful.Request, restResp *restful.Response) { return func(restReq *restful.Request, restResp *restful.Response) {
http.StripPrefix(prefix, handler).ServeHTTP(restResp.ResponseWriter, restReq.Request) handler.ServeHTTP(restResp.ResponseWriter, restReq.Request)
} }
} }
func addProxyRoute(ws *restful.WebService, method string, prefix string, path string, proxyHandler http.Handler, kind, resource string, params []*restful.Parameter) { func addProxyRoute(ws *restful.WebService, method string, prefix string, path string, proxyHandler http.Handler, kind, resource string, params []*restful.Parameter) {
proxyRoute := ws.Method(method).Path(path).To(restfulStripPrefix(prefix+"/proxy", proxyHandler)). proxyRoute := ws.Method(method).Path(path).To(routeFunction(proxyHandler)).
Filter(monitorFilter("PROXY", resource)). Filter(monitorFilter("PROXY", resource)).
Doc("proxy " + method + " requests to " + kind). Doc("proxy " + method + " requests to " + kind).
Operation("proxy" + method + kind). Operation("proxy" + method + kind).

View File

@ -141,7 +141,7 @@ func NewAPIGroupVersion(storage map[string]RESTStorage, codec runtime.Codec, roo
admit: admissionControl, admit: admissionControl,
context: contextMapper, context: contextMapper,
mapper: mapper, mapper: mapper,
info: &APIRequestInfoResolver{util.NewStringSet(root), latest.RESTMapper}, info: &APIRequestInfoResolver{util.NewStringSet(strings.TrimPrefix(root, "/")), latest.RESTMapper},
} }
} }

View File

@ -39,7 +39,6 @@ import (
type WatchHandler struct { type WatchHandler struct {
storage map[string]RESTStorage storage map[string]RESTStorage
codec runtime.Codec codec runtime.Codec
prefix string
linker runtime.SelfLinker linker runtime.SelfLinker
info *APIRequestInfoResolver info *APIRequestInfoResolver
} }
@ -51,7 +50,7 @@ func (h *WatchHandler) setSelfLinkAddName(obj runtime.Object, req *http.Request)
return err return err
} }
newURL := *req.URL newURL := *req.URL
newURL.Path = path.Join(h.prefix, req.URL.Path, name) newURL.Path = path.Join(req.URL.Path, name)
newURL.RawQuery = "" newURL.RawQuery = ""
newURL.Fragment = "" newURL.Fragment = ""
return h.linker.SetSelfLink(obj, newURL.String()) return h.linker.SetSelfLink(obj, newURL.String())