Avoid multiple allocations during selfLink generation

This commit is contained in:
Clayton Coleman 2016-05-21 00:05:39 -04:00
parent 25a2a7d25a
commit f670cc4652
No known key found for this signature in database
GPG Key ID: 3D16906B4F1C5CB3
2 changed files with 17 additions and 8 deletions

View File

@ -390,18 +390,26 @@ func (a *APIInstaller) registerResourceHandlers(path string, storage rest.Storag
resourcePath := namespacedPath
resourceParams := namespaceParams
itemPathPrefix := gpath.Join(a.prefix, scope.ParamName()) + "/"
itemPath := namespacedPath + "/{name}"
itemPathMiddle := "/" + resource + "/"
nameParams := append(namespaceParams, nameParam)
proxyParams := append(nameParams, pathParam)
itemPathSuffix := ""
if hasSubresource {
itemPath = itemPath + "/" + subresource
itemPathSuffix = "/" + subresource
itemPath = itemPath + itemPathSuffix
resourcePath = itemPath
resourceParams = nameParams
}
apiResource.Name = path
apiResource.Namespaced = true
apiResource.Kind = resourceKind
namer := scopeNaming{scope, a.group.Linker, gpath.Join(a.prefix, itemPath), false}
itemPathFn := func(name, namespace string) string {
return itemPathPrefix + namespace + itemPathMiddle + name + itemPathSuffix
}
namer := scopeNaming{scope, a.group.Linker, itemPathFn, false}
actions = appendIf(actions, action{"LIST", resourcePath, resourceParams, namer}, isLister)
actions = appendIf(actions, action{"POST", resourcePath, resourceParams, namer}, isCreater)
@ -430,7 +438,7 @@ func (a *APIInstaller) registerResourceHandlers(path string, storage rest.Storag
// For ex: LIST all pods in all namespaces by sending a LIST request at /api/apiVersion/pods.
// TODO: more strongly type whether a resource allows these actions on "all namespaces" (bulk delete)
if !hasSubresource {
namer = scopeNaming{scope, a.group.Linker, gpath.Join(a.prefix, itemPath), true}
namer = scopeNaming{scope, a.group.Linker, itemPathFn, true}
actions = appendIf(actions, action{"LIST", resource, params, namer}, isLister)
actions = appendIf(actions, action{"WATCHLIST", "watch/" + resource, params, namer}, allowWatchList)
}
@ -775,7 +783,7 @@ func (n rootScopeNaming) ObjectName(obj runtime.Object) (namespace, name string,
type scopeNaming struct {
scope meta.RESTScope
runtime.SelfLinker
itemPath string
itemPathFn func(name, namespace string) string
allNamespaces bool
}
@ -822,9 +830,8 @@ func (n scopeNaming) GenerateLink(req *restful.Request, obj runtime.Object) (pat
if len(name) == 0 {
return "", "", errEmptyName
}
path = strings.Replace(n.itemPath, "{name}", name, 1)
path = strings.Replace(path, "{"+n.scope.ArgumentName()+"}", namespace, 1)
return path, "", nil
return n.itemPathFn(name, namespace), "", nil
}
// GenerateListLink returns the appropriate path and query to locate a list by its canonical path.

View File

@ -36,7 +36,9 @@ func TestScopeNamingGenerateLink(t *testing.T) {
s := scopeNaming{
meta.RESTScopeNamespace,
selfLinker,
"/api/v1/namespaces/{namespace}/services/{name}",
func(name, namespace string) string {
return "/api/v1/namespaces/" + namespace + "/services/" + name
},
true,
}
service := &api.Service{