mirror of
https://github.com/rancher/norman.git
synced 2025-08-27 19:38:41 +00:00
Refactor set of cluster scoped handlers
Problem: Cluster scoped gc was taking significantly longer to complete
than before 7387aa5
. This was due to a large number of list calls and
the time needed to iterate over them.
Solution: Instead of requesting every type than Rancher uses, a
seperate map appended to when a cluster scoped handler
is created. The full map of all GroupVersionResources is kept as a
fallback for the use case where user controllers may exist on another
host other than the current leader.
This commit is contained in:
parent
ea122abac5
commit
f5744043a6
@ -157,6 +157,7 @@ func (c *{{.schema.ID}}Controller) AddHandler(ctx context.Context, name string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *{{.schema.ID}}Controller) AddClusterScopedHandler(ctx context.Context, name, cluster string, handler {{.schema.CodeName}}HandlerFunc) {
|
func (c *{{.schema.ID}}Controller) AddClusterScopedHandler(ctx context.Context, name, cluster string, handler {{.schema.CodeName}}HandlerFunc) {
|
||||||
|
resource.PutClusterScoped({{.schema.CodeName}}GroupVersionResource)
|
||||||
c.GenericController.AddHandler(ctx, name, func(key string, obj interface{}) (interface{}, error) {
|
c.GenericController.AddHandler(ctx, name, func(key string, obj interface{}) (interface{}, error) {
|
||||||
if obj == nil {
|
if obj == nil {
|
||||||
return handler(key, nil)
|
return handler(key, nil)
|
||||||
|
@ -7,11 +7,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
//rancherTypes is a set of all types generated by rancher
|
//rancherTypes is a set of all types generated by rancher
|
||||||
|
//clusterScopedTypes is a set of all types that have been added by a clusterScoped handler
|
||||||
var (
|
var (
|
||||||
rancherTypes = struct {
|
rancherTypes = struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
m map[schema.GroupVersionResource]bool
|
m map[schema.GroupVersionResource]bool
|
||||||
}{m: make(map[schema.GroupVersionResource]bool)}
|
}{m: make(map[schema.GroupVersionResource]bool)}
|
||||||
|
|
||||||
|
clusterScopedTypes = struct {
|
||||||
|
sync.RWMutex
|
||||||
|
m map[schema.GroupVersionResource]bool
|
||||||
|
}{m: make(map[schema.GroupVersionResource]bool)}
|
||||||
)
|
)
|
||||||
|
|
||||||
//Get returns a copy of the set of rancherTypes
|
//Get returns a copy of the set of rancherTypes
|
||||||
@ -25,6 +31,16 @@ func Get() map[schema.GroupVersionResource]bool {
|
|||||||
return targetMap
|
return targetMap
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetClusterScopedTypes() map[schema.GroupVersionResource]bool {
|
||||||
|
clusterScopedTypes.Lock()
|
||||||
|
defer clusterScopedTypes.Unlock()
|
||||||
|
targetMap := make(map[schema.GroupVersionResource]bool, len(clusterScopedTypes.m))
|
||||||
|
for key, value := range clusterScopedTypes.m {
|
||||||
|
targetMap[key] = value
|
||||||
|
}
|
||||||
|
return targetMap
|
||||||
|
}
|
||||||
|
|
||||||
//Put adds an object to the set and panic on collision
|
//Put adds an object to the set and panic on collision
|
||||||
func Put(key schema.GroupVersionResource) {
|
func Put(key schema.GroupVersionResource) {
|
||||||
rancherTypes.Lock()
|
rancherTypes.Lock()
|
||||||
@ -36,3 +52,10 @@ func Put(key schema.GroupVersionResource) {
|
|||||||
}
|
}
|
||||||
rancherTypes.m[key] = true
|
rancherTypes.m[key] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//PutClusterScoped adds a object that contains a cluster scoped handler to the set
|
||||||
|
func PutClusterScoped(key schema.GroupVersionResource) {
|
||||||
|
clusterScopedTypes.Lock()
|
||||||
|
defer clusterScopedTypes.Unlock()
|
||||||
|
clusterScopedTypes.m[key] = true
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user