1
0
mirror of https://github.com/rancher/norman.git synced 2025-07-04 02:56:42 +00:00

Record cluster scoped handlers

Problem: Cluster scoped handlers were not being cleaned up without
manually writing garbage collection functions.

Solution: All cluster scoped handlers will be recorded in a global map
in a generic format so that they can be removed by a generic function.
This commit is contained in:
dax 2019-04-18 19:02:49 -07:00 committed by Craig Jellick
parent 01a9966237
commit 7387aa53a5
2 changed files with 49 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import (
{{.importPackage}}
"github.com/rancher/norman/objectclient"
"github.com/rancher/norman/controller"
"github.com/rancher/norman/resource"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
@ -34,8 +35,18 @@ var (
{{- end }}
Kind: {{.schema.CodeName}}GroupVersionKind.Kind,
}
{{.schema.CodeName}}GroupVersionResource = schema.GroupVersionResource{
Group: GroupName,
Version: Version,
Resource: "{{.schema.PluralName | toLower}}",
}
)
func init() {
resource.Put({{.schema.CodeName}}GroupVersionResource)
}
func New{{.schema.CodeName}}(namespace, name string, obj {{.prefix}}{{.schema.CodeName}}) *{{.prefix}}{{.schema.CodeName}} {
obj.APIVersion, obj.Kind = {{.schema.CodeName}}GroupVersionKind.ToAPIVersionAndKind()
obj.Name = name

38
resource/resource.go Normal file
View File

@ -0,0 +1,38 @@
package resource
import (
"sync"
"k8s.io/apimachinery/pkg/runtime/schema"
)
//rancherTypes is a set of all types generated by rancher
var (
rancherTypes = struct {
sync.RWMutex
m map[schema.GroupVersionResource]bool
}{m: make(map[schema.GroupVersionResource]bool)}
)
//Get returns a copy of the set of rancherTypes
func Get() map[schema.GroupVersionResource]bool {
rancherTypes.RLock()
defer rancherTypes.RUnlock()
targetMap := make(map[schema.GroupVersionResource]bool, len(rancherTypes.m))
for key, value := range rancherTypes.m {
targetMap[key] = value
}
return targetMap
}
//Put adds an object to the set and panic on collision
func Put(key schema.GroupVersionResource) {
rancherTypes.Lock()
defer rancherTypes.Unlock()
_, exists := rancherTypes.m[key]
if exists {
//only used in an init function
panic("key exists in rancherTypes")
}
rancherTypes.m[key] = true
}