mirror of
https://github.com/rancher/norman.git
synced 2025-09-13 05:42:37 +00:00
Fix bugs in lifecycle
This commit is contained in:
@@ -349,12 +349,12 @@ func (n *{{.schema.ID}}Client2) Cache() {{.schema.CodeName}}ClientCache {
|
||||
|
||||
func (n *{{.schema.ID}}Client2) OnCreate(ctx context.Context, name string, sync {{.schema.CodeName}}ChangeHandlerFunc) {
|
||||
n.loadController()
|
||||
n.iface.AddLifecycle(ctx, name, &{{.schema.ID}}LifecycleDelegate{create: sync})
|
||||
n.iface.AddLifecycle(ctx, name+"-create", &{{.schema.ID}}LifecycleDelegate{create: sync})
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}Client2) OnChange(ctx context.Context, name string, sync {{.schema.CodeName}}ChangeHandlerFunc) {
|
||||
n.loadController()
|
||||
n.iface.AddLifecycle(ctx, name, &{{.schema.ID}}LifecycleDelegate{update: sync})
|
||||
n.iface.AddLifecycle(ctx, name+"-change", &{{.schema.ID}}LifecycleDelegate{update: sync})
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}Client2) OnRemove(ctx context.Context, name string, sync {{.schema.CodeName}}ChangeHandlerFunc) {
|
||||
@@ -426,10 +426,6 @@ func (n *{{.schema.ID}}LifecycleDelegate) Remove(obj *{{.prefix}}{{.schema.CodeN
|
||||
return n.remove(obj)
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}LifecycleDelegate) HasUpdated() bool {
|
||||
return n.remove != nil
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}LifecycleDelegate) Updated(obj *{{.prefix}}{{.schema.CodeName}}) (runtime.Object, error) {
|
||||
if n.update == nil {
|
||||
return obj, nil
|
||||
|
@@ -19,6 +19,16 @@ type {{.schema.ID}}LifecycleAdapter struct {
|
||||
lifecycle {{.schema.CodeName}}Lifecycle
|
||||
}
|
||||
|
||||
func (w *{{.schema.ID}}LifecycleAdapter) HasCreate() bool {
|
||||
o, ok := w.lifecycle.(lifecycle.ObjectLifecycleCondition)
|
||||
return !ok || o.HasCreate()
|
||||
}
|
||||
|
||||
func (w *{{.schema.ID}}LifecycleAdapter) HasFinalize() bool {
|
||||
o, ok := w.lifecycle.(lifecycle.ObjectLifecycleCondition)
|
||||
return !ok || o.HasFinalize()
|
||||
}
|
||||
|
||||
func (w *{{.schema.ID}}LifecycleAdapter) Create(obj runtime.Object) (runtime.Object, error) {
|
||||
o, err := w.lifecycle.Create(obj.(*{{.prefix}}{{.schema.CodeName}}))
|
||||
if o == nil {
|
||||
|
@@ -26,7 +26,6 @@ type ObjectLifecycle interface {
|
||||
type ObjectLifecycleCondition interface {
|
||||
HasCreate() bool
|
||||
HasFinalize() bool
|
||||
HasUpdated() bool
|
||||
}
|
||||
|
||||
type objectLifecycleAdapter struct {
|
||||
@@ -86,6 +85,10 @@ func (o *objectLifecycleAdapter) update(name string, orig, obj runtime.Object) (
|
||||
}
|
||||
|
||||
func (o *objectLifecycleAdapter) finalize(obj runtime.Object) (runtime.Object, bool, error) {
|
||||
if !o.hasFinalize() {
|
||||
return obj, true, nil
|
||||
}
|
||||
|
||||
metadata, err := meta.Accessor(obj)
|
||||
if err != nil {
|
||||
return obj, false, err
|
||||
@@ -173,9 +176,10 @@ func (o *objectLifecycleAdapter) record(obj runtime.Object, f func(runtime.Objec
|
||||
return obj, err
|
||||
}
|
||||
|
||||
obj = obj.DeepCopyObject()
|
||||
origObj := obj
|
||||
obj = origObj.DeepCopyObject()
|
||||
if newObj, err := f(obj); err != nil {
|
||||
newObj, _ = o.update(metadata.GetName(), obj, newObj)
|
||||
newObj, _ = o.update(metadata.GetName(), origObj, newObj)
|
||||
return newObj, err
|
||||
} else if newObj != nil {
|
||||
newMetadata, err := meta.Accessor(newObj)
|
||||
@@ -184,7 +188,7 @@ func (o *objectLifecycleAdapter) record(obj runtime.Object, f func(runtime.Objec
|
||||
return newObj, nil
|
||||
}
|
||||
if newMetadata.GetResourceVersion() == metadata.GetResourceVersion() {
|
||||
return o.update(metadata.GetName(), obj, newObj)
|
||||
return o.update(metadata.GetName(), origObj, newObj)
|
||||
}
|
||||
return newObj, nil
|
||||
}
|
||||
@@ -213,6 +217,11 @@ func (o *objectLifecycleAdapter) create(obj runtime.Object) (runtime.Object, boo
|
||||
}
|
||||
|
||||
obj, err = o.record(obj, o.lifecycle.Create)
|
||||
if err != nil {
|
||||
return obj, false, err
|
||||
}
|
||||
|
||||
obj, err = o.setInitialized(obj)
|
||||
return obj, false, err
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user