1
0
mirror of https://github.com/rancher/norman.git synced 2025-07-05 19:47:42 +00:00
norman/generator/lifecycle_template.go
Craig Jellick 905797b50f Add suport for cluster scoped controllers
Some worload controllers need to watch resoruces in the mangement plane
and react to them. But, they should only react to resources that
correspond to their cluster. This adds framework support for that.
2018-01-16 09:04:15 -07:00

56 lines
1.7 KiB
Go

package generator
var lifecycleTemplate = `package {{.schema.Version.Version}}
import (
{{.importPackage}}
"k8s.io/apimachinery/pkg/runtime"
"github.com/rancher/norman/lifecycle"
)
type {{.schema.CodeName}}Lifecycle interface {
Create(obj *{{.prefix}}{{.schema.CodeName}}) (*{{.prefix}}{{.schema.CodeName}}, error)
Remove(obj *{{.prefix}}{{.schema.CodeName}}) (*{{.prefix}}{{.schema.CodeName}}, error)
Updated(obj *{{.prefix}}{{.schema.CodeName}}) (*{{.prefix}}{{.schema.CodeName}}, error)
}
type {{.schema.ID}}LifecycleAdapter struct {
lifecycle {{.schema.CodeName}}Lifecycle
}
func (w *{{.schema.ID}}LifecycleAdapter) Create(obj runtime.Object) (runtime.Object, error) {
o, err := w.lifecycle.Create(obj.(*{{.prefix}}{{.schema.CodeName}}))
if o == nil {
return nil, err
}
return o, err
}
func (w *{{.schema.ID}}LifecycleAdapter) Finalize(obj runtime.Object) (runtime.Object, error) {
o, err := w.lifecycle.Remove(obj.(*{{.prefix}}{{.schema.CodeName}}))
if o == nil {
return nil, err
}
return o, err
}
func (w *{{.schema.ID}}LifecycleAdapter) Updated(obj runtime.Object) (runtime.Object, error) {
o, err := w.lifecycle.Updated(obj.(*{{.prefix}}{{.schema.CodeName}}))
if o == nil {
return nil, err
}
return o, err
}
func New{{.schema.CodeName}}LifecycleAdapter(name string, clusterScoped bool, client {{.schema.CodeName}}Interface, l {{.schema.CodeName}}Lifecycle) {{.schema.CodeName}}HandlerFunc {
adapter := &{{.schema.ID}}LifecycleAdapter{lifecycle: l}
syncFn := lifecycle.NewObjectLifecycleAdapter(name, clusterScoped, adapter, client.ObjectClient())
return func(key string, obj *{{.prefix}}{{.schema.CodeName}}) error {
if obj == nil {
return syncFn(key, nil)
}
return syncFn(key, obj)
}
}
`