mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 01:06:27 +00:00
Avoid multiple allocations during selfLink generation
This commit is contained in:
parent
25a2a7d25a
commit
f670cc4652
@ -390,18 +390,26 @@ func (a *APIInstaller) registerResourceHandlers(path string, storage rest.Storag
|
|||||||
|
|
||||||
resourcePath := namespacedPath
|
resourcePath := namespacedPath
|
||||||
resourceParams := namespaceParams
|
resourceParams := namespaceParams
|
||||||
|
itemPathPrefix := gpath.Join(a.prefix, scope.ParamName()) + "/"
|
||||||
itemPath := namespacedPath + "/{name}"
|
itemPath := namespacedPath + "/{name}"
|
||||||
|
itemPathMiddle := "/" + resource + "/"
|
||||||
nameParams := append(namespaceParams, nameParam)
|
nameParams := append(namespaceParams, nameParam)
|
||||||
proxyParams := append(nameParams, pathParam)
|
proxyParams := append(nameParams, pathParam)
|
||||||
|
itemPathSuffix := ""
|
||||||
if hasSubresource {
|
if hasSubresource {
|
||||||
itemPath = itemPath + "/" + subresource
|
itemPathSuffix = "/" + subresource
|
||||||
|
itemPath = itemPath + itemPathSuffix
|
||||||
resourcePath = itemPath
|
resourcePath = itemPath
|
||||||
resourceParams = nameParams
|
resourceParams = nameParams
|
||||||
}
|
}
|
||||||
apiResource.Name = path
|
apiResource.Name = path
|
||||||
apiResource.Namespaced = true
|
apiResource.Namespaced = true
|
||||||
apiResource.Kind = resourceKind
|
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{"LIST", resourcePath, resourceParams, namer}, isLister)
|
||||||
actions = appendIf(actions, action{"POST", resourcePath, resourceParams, namer}, isCreater)
|
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.
|
// 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)
|
// TODO: more strongly type whether a resource allows these actions on "all namespaces" (bulk delete)
|
||||||
if !hasSubresource {
|
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{"LIST", resource, params, namer}, isLister)
|
||||||
actions = appendIf(actions, action{"WATCHLIST", "watch/" + resource, params, namer}, allowWatchList)
|
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 {
|
type scopeNaming struct {
|
||||||
scope meta.RESTScope
|
scope meta.RESTScope
|
||||||
runtime.SelfLinker
|
runtime.SelfLinker
|
||||||
itemPath string
|
itemPathFn func(name, namespace string) string
|
||||||
allNamespaces bool
|
allNamespaces bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -822,9 +830,8 @@ func (n scopeNaming) GenerateLink(req *restful.Request, obj runtime.Object) (pat
|
|||||||
if len(name) == 0 {
|
if len(name) == 0 {
|
||||||
return "", "", errEmptyName
|
return "", "", errEmptyName
|
||||||
}
|
}
|
||||||
path = strings.Replace(n.itemPath, "{name}", name, 1)
|
|
||||||
path = strings.Replace(path, "{"+n.scope.ArgumentName()+"}", namespace, 1)
|
return n.itemPathFn(name, namespace), "", nil
|
||||||
return path, "", nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenerateListLink returns the appropriate path and query to locate a list by its canonical path.
|
// GenerateListLink returns the appropriate path and query to locate a list by its canonical path.
|
||||||
|
@ -36,7 +36,9 @@ func TestScopeNamingGenerateLink(t *testing.T) {
|
|||||||
s := scopeNaming{
|
s := scopeNaming{
|
||||||
meta.RESTScopeNamespace,
|
meta.RESTScopeNamespace,
|
||||||
selfLinker,
|
selfLinker,
|
||||||
"/api/v1/namespaces/{namespace}/services/{name}",
|
func(name, namespace string) string {
|
||||||
|
return "/api/v1/namespaces/" + namespace + "/services/" + name
|
||||||
|
},
|
||||||
true,
|
true,
|
||||||
}
|
}
|
||||||
service := &api.Service{
|
service := &api.Service{
|
||||||
|
Loading…
Reference in New Issue
Block a user