Change AfterCreate/Update to take Options

Also adds typedefs for these function signatures for code clarity.
This commit is contained in:
Tim Hockin 2020-12-15 21:40:44 -08:00
parent 9402f48e05
commit 25da6a0660
2 changed files with 33 additions and 27 deletions

View File

@ -56,6 +56,12 @@ type FinishFunc func(ctx context.Context, success bool)
// AfterDeleteFunc is the type used for the Store.AfterDelete hook. // AfterDeleteFunc is the type used for the Store.AfterDelete hook.
type AfterDeleteFunc func(obj runtime.Object, options *metav1.DeleteOptions) type AfterDeleteFunc func(obj runtime.Object, options *metav1.DeleteOptions)
// AfterCreateFunc is the type used for the Store.AfterCreate hook.
type AfterCreateFunc func(obj runtime.Object, options *metav1.CreateOptions)
// AfterUpdateFunc is the type used for the Store.AfterUpdate hook.
type AfterUpdateFunc func(obj runtime.Object, options *metav1.UpdateOptions)
// GenericStore interface can be used for type assertions when we need to access the underlying strategies. // GenericStore interface can be used for type assertions when we need to access the underlying strategies.
type GenericStore interface { type GenericStore interface {
GetCreateStrategy() rest.RESTCreateStrategy GetCreateStrategy() rest.RESTCreateStrategy
@ -159,7 +165,7 @@ type Store struct {
BeginCreate func(ctx context.Context, obj runtime.Object, options *metav1.CreateOptions) (FinishFunc, error) BeginCreate func(ctx context.Context, obj runtime.Object, options *metav1.CreateOptions) (FinishFunc, error)
// AfterCreate implements a further operation to run after a resource is // AfterCreate implements a further operation to run after a resource is
// created and before it is decorated, optional. // created and before it is decorated, optional.
AfterCreate func(runtime.Object) AfterCreate AfterCreateFunc
// UpdateStrategy implements resource-specific behavior during updates. // UpdateStrategy implements resource-specific behavior during updates.
UpdateStrategy rest.RESTUpdateStrategy UpdateStrategy rest.RESTUpdateStrategy
@ -171,7 +177,7 @@ type Store struct {
BeginUpdate func(ctx context.Context, obj, old runtime.Object, options *metav1.UpdateOptions) (FinishFunc, error) BeginUpdate func(ctx context.Context, obj, old runtime.Object, options *metav1.UpdateOptions) (FinishFunc, error)
// AfterUpdate implements a further operation to run after a resource is // AfterUpdate implements a further operation to run after a resource is
// updated and before it is decorated, optional. // updated and before it is decorated, optional.
AfterUpdate func(runtime.Object) AfterUpdate AfterUpdateFunc
// DeleteStrategy implements resource-specific behavior during deletion. // DeleteStrategy implements resource-specific behavior during deletion.
DeleteStrategy rest.RESTDeleteStrategy DeleteStrategy rest.RESTDeleteStrategy
@ -419,7 +425,7 @@ func (e *Store) Create(ctx context.Context, obj runtime.Object, createValidation
fn(ctx, true) fn(ctx, true)
if e.AfterCreate != nil { if e.AfterCreate != nil {
e.AfterCreate(out) e.AfterCreate(out, options)
} }
if e.Decorator != nil { if e.Decorator != nil {
e.Decorator(out) e.Decorator(out)
@ -659,11 +665,11 @@ func (e *Store) Update(ctx context.Context, name string, objInfo rest.UpdatedObj
if creating { if creating {
if e.AfterCreate != nil { if e.AfterCreate != nil {
e.AfterCreate(out) e.AfterCreate(out, newCreateOptionsFromUpdateOptions(options))
} }
} else { } else {
if e.AfterUpdate != nil { if e.AfterUpdate != nil {
e.AfterUpdate(out) e.AfterUpdate(out, options)
} }
} }
if e.Decorator != nil { if e.Decorator != nil {

View File

@ -500,7 +500,7 @@ func TestStoreCreateHooks(t *testing.T) {
name string name string
decorator func(runtime.Object) decorator func(runtime.Object)
beginCreate func(context.Context, runtime.Object, *metav1.CreateOptions) (FinishFunc, error) beginCreate func(context.Context, runtime.Object, *metav1.CreateOptions) (FinishFunc, error)
afterCreate func(runtime.Object) afterCreate AfterCreateFunc
// the TTLFunc is an easy hook to force a failure // the TTLFunc is an easy hook to force a failure
ttl func(obj runtime.Object, existing uint64, update bool) (uint64, error) ttl func(obj runtime.Object, existing uint64, update bool) (uint64, error)
expectError bool expectError bool
@ -516,7 +516,7 @@ func TestStoreCreateHooks(t *testing.T) {
expectAnnotation: "DecoratorWasCalled", expectAnnotation: "DecoratorWasCalled",
}, { }, {
name: "AfterCreate mutation", name: "AfterCreate mutation",
afterCreate: func(obj runtime.Object) { afterCreate: func(obj runtime.Object, opts *metav1.CreateOptions) {
setAnn(obj, "AfterCreateWasCalled") setAnn(obj, "AfterCreateWasCalled")
}, },
expectAnnotation: "AfterCreateWasCalled", expectAnnotation: "AfterCreateWasCalled",
@ -532,7 +532,7 @@ func TestStoreCreateHooks(t *testing.T) {
decorator: func(obj runtime.Object) { decorator: func(obj runtime.Object) {
mile("Decorator") mile("Decorator")
}, },
afterCreate: func(obj runtime.Object) { afterCreate: func(obj runtime.Object, opts *metav1.CreateOptions) {
mile("AfterCreate") mile("AfterCreate")
}, },
beginCreate: func(_ context.Context, obj runtime.Object, _ *metav1.CreateOptions) (FinishFunc, error) { beginCreate: func(_ context.Context, obj runtime.Object, _ *metav1.CreateOptions) (FinishFunc, error) {
@ -547,7 +547,7 @@ func TestStoreCreateHooks(t *testing.T) {
decorator: func(obj runtime.Object) { decorator: func(obj runtime.Object) {
mile("Decorator") mile("Decorator")
}, },
afterCreate: func(obj runtime.Object) { afterCreate: func(obj runtime.Object, opts *metav1.CreateOptions) {
mile("AfterCreate") mile("AfterCreate")
}, },
beginCreate: func(_ context.Context, obj runtime.Object, _ *metav1.CreateOptions) (FinishFunc, error) { beginCreate: func(_ context.Context, obj runtime.Object, _ *metav1.CreateOptions) (FinishFunc, error) {
@ -568,7 +568,7 @@ func TestStoreCreateHooks(t *testing.T) {
decorator: func(obj runtime.Object) { decorator: func(obj runtime.Object) {
mile("Decorator") mile("Decorator")
}, },
afterCreate: func(obj runtime.Object) { afterCreate: func(obj runtime.Object, opts *metav1.CreateOptions) {
mile("AfterCreate") mile("AfterCreate")
}, },
beginCreate: func(_ context.Context, obj runtime.Object, _ *metav1.CreateOptions) (FinishFunc, error) { beginCreate: func(_ context.Context, obj runtime.Object, _ *metav1.CreateOptions) (FinishFunc, error) {
@ -799,9 +799,9 @@ func TestStoreUpdateHooks(t *testing.T) {
decorator func(runtime.Object) decorator func(runtime.Object)
// create-on-update is tested elsewhere, but this proves non-use here // create-on-update is tested elsewhere, but this proves non-use here
beginCreate func(context.Context, runtime.Object, *metav1.CreateOptions) (FinishFunc, error) beginCreate func(context.Context, runtime.Object, *metav1.CreateOptions) (FinishFunc, error)
afterCreate func(runtime.Object) afterCreate AfterCreateFunc
beginUpdate func(context.Context, runtime.Object, runtime.Object, *metav1.UpdateOptions) (FinishFunc, error) beginUpdate func(context.Context, runtime.Object, runtime.Object, *metav1.UpdateOptions) (FinishFunc, error)
afterUpdate func(runtime.Object) afterUpdate AfterUpdateFunc
expectError bool expectError bool
expectAnnotation string // to test object mutations expectAnnotation string // to test object mutations
expectMilestones []string // to test sequence expectMilestones []string // to test sequence
@ -815,7 +815,7 @@ func TestStoreUpdateHooks(t *testing.T) {
expectAnnotation: "DecoratorWasCalled", expectAnnotation: "DecoratorWasCalled",
}, { }, {
name: "AfterUpdate mutation", name: "AfterUpdate mutation",
afterUpdate: func(obj runtime.Object) { afterUpdate: func(obj runtime.Object, opts *metav1.UpdateOptions) {
setAnn(obj, "AfterUpdateWasCalled") setAnn(obj, "AfterUpdateWasCalled")
}, },
expectAnnotation: "AfterUpdateWasCalled", expectAnnotation: "AfterUpdateWasCalled",
@ -831,7 +831,7 @@ func TestStoreUpdateHooks(t *testing.T) {
decorator: func(obj runtime.Object) { decorator: func(obj runtime.Object) {
mile("Decorator") mile("Decorator")
}, },
afterCreate: func(obj runtime.Object) { afterCreate: func(obj runtime.Object, opts *metav1.CreateOptions) {
mile("AfterCreate") mile("AfterCreate")
}, },
beginCreate: func(_ context.Context, obj runtime.Object, _ *metav1.CreateOptions) (FinishFunc, error) { beginCreate: func(_ context.Context, obj runtime.Object, _ *metav1.CreateOptions) (FinishFunc, error) {
@ -840,7 +840,7 @@ func TestStoreUpdateHooks(t *testing.T) {
mile(fmt.Sprintf("FinishCreate(%v)", success)) mile(fmt.Sprintf("FinishCreate(%v)", success))
}, nil }, nil
}, },
afterUpdate: func(obj runtime.Object) { afterUpdate: func(obj runtime.Object, opts *metav1.UpdateOptions) {
mile("AfterUpdate") mile("AfterUpdate")
}, },
beginUpdate: func(_ context.Context, obj, _ runtime.Object, _ *metav1.UpdateOptions) (FinishFunc, error) { beginUpdate: func(_ context.Context, obj, _ runtime.Object, _ *metav1.UpdateOptions) (FinishFunc, error) {
@ -856,7 +856,7 @@ func TestStoreUpdateHooks(t *testing.T) {
decorator: func(obj runtime.Object) { decorator: func(obj runtime.Object) {
mile("Decorator") mile("Decorator")
}, },
afterUpdate: func(obj runtime.Object) { afterUpdate: func(obj runtime.Object, opts *metav1.UpdateOptions) {
mile("AfterUpdate") mile("AfterUpdate")
}, },
beginUpdate: func(_ context.Context, obj, _ runtime.Object, _ *metav1.UpdateOptions) (FinishFunc, error) { beginUpdate: func(_ context.Context, obj, _ runtime.Object, _ *metav1.UpdateOptions) (FinishFunc, error) {
@ -928,9 +928,9 @@ func TestStoreCreateOnUpdateHooks(t *testing.T) {
name string name string
decorator func(runtime.Object) decorator func(runtime.Object)
beginCreate func(context.Context, runtime.Object, *metav1.CreateOptions) (FinishFunc, error) beginCreate func(context.Context, runtime.Object, *metav1.CreateOptions) (FinishFunc, error)
afterCreate func(runtime.Object) afterCreate AfterCreateFunc
beginUpdate func(context.Context, runtime.Object, runtime.Object, *metav1.UpdateOptions) (FinishFunc, error) beginUpdate func(context.Context, runtime.Object, runtime.Object, *metav1.UpdateOptions) (FinishFunc, error)
afterUpdate func(runtime.Object) afterUpdate AfterUpdateFunc
// the TTLFunc is an easy hook to force a failure // the TTLFunc is an easy hook to force a failure
ttl func(obj runtime.Object, existing uint64, update bool) (uint64, error) ttl func(obj runtime.Object, existing uint64, update bool) (uint64, error)
expectError bool expectError bool
@ -942,7 +942,7 @@ func TestStoreCreateOnUpdateHooks(t *testing.T) {
decorator: func(obj runtime.Object) { decorator: func(obj runtime.Object) {
mile("Decorator") mile("Decorator")
}, },
afterCreate: func(obj runtime.Object) { afterCreate: func(obj runtime.Object, opts *metav1.CreateOptions) {
mile("AfterCreate") mile("AfterCreate")
}, },
beginCreate: func(_ context.Context, obj runtime.Object, _ *metav1.CreateOptions) (FinishFunc, error) { beginCreate: func(_ context.Context, obj runtime.Object, _ *metav1.CreateOptions) (FinishFunc, error) {
@ -951,7 +951,7 @@ func TestStoreCreateOnUpdateHooks(t *testing.T) {
mile(fmt.Sprintf("FinishCreate(%v)", success)) mile(fmt.Sprintf("FinishCreate(%v)", success))
}, nil }, nil
}, },
afterUpdate: func(obj runtime.Object) { afterUpdate: func(obj runtime.Object, opts *metav1.UpdateOptions) {
mile("AfterUpdate") mile("AfterUpdate")
}, },
beginUpdate: func(_ context.Context, obj, _ runtime.Object, _ *metav1.UpdateOptions) (FinishFunc, error) { beginUpdate: func(_ context.Context, obj, _ runtime.Object, _ *metav1.UpdateOptions) (FinishFunc, error) {
@ -966,7 +966,7 @@ func TestStoreCreateOnUpdateHooks(t *testing.T) {
decorator: func(obj runtime.Object) { decorator: func(obj runtime.Object) {
mile("Decorator") mile("Decorator")
}, },
afterCreate: func(obj runtime.Object) { afterCreate: func(obj runtime.Object, opts *metav1.CreateOptions) {
mile("AfterCreate") mile("AfterCreate")
}, },
beginCreate: func(_ context.Context, obj runtime.Object, _ *metav1.CreateOptions) (FinishFunc, error) { beginCreate: func(_ context.Context, obj runtime.Object, _ *metav1.CreateOptions) (FinishFunc, error) {
@ -975,7 +975,7 @@ func TestStoreCreateOnUpdateHooks(t *testing.T) {
mile(fmt.Sprintf("FinishCreate(%v)", success)) mile(fmt.Sprintf("FinishCreate(%v)", success))
}, nil }, nil
}, },
afterUpdate: func(obj runtime.Object) { afterUpdate: func(obj runtime.Object, opts *metav1.UpdateOptions) {
mile("AfterUpdate") mile("AfterUpdate")
}, },
beginUpdate: func(_ context.Context, obj, _ runtime.Object, _ *metav1.UpdateOptions) (FinishFunc, error) { beginUpdate: func(_ context.Context, obj, _ runtime.Object, _ *metav1.UpdateOptions) (FinishFunc, error) {
@ -996,7 +996,7 @@ func TestStoreCreateOnUpdateHooks(t *testing.T) {
decorator: func(obj runtime.Object) { decorator: func(obj runtime.Object) {
mile("Decorator") mile("Decorator")
}, },
afterCreate: func(obj runtime.Object) { afterCreate: func(obj runtime.Object, opts *metav1.CreateOptions) {
mile("AfterCreate") mile("AfterCreate")
}, },
beginCreate: func(_ context.Context, obj runtime.Object, _ *metav1.CreateOptions) (FinishFunc, error) { beginCreate: func(_ context.Context, obj runtime.Object, _ *metav1.CreateOptions) (FinishFunc, error) {
@ -1005,7 +1005,7 @@ func TestStoreCreateOnUpdateHooks(t *testing.T) {
mile(fmt.Sprintf("FinishCreate(%v)", success)) mile(fmt.Sprintf("FinishCreate(%v)", success))
}, fmt.Errorf("begin") }, fmt.Errorf("begin")
}, },
afterUpdate: func(obj runtime.Object) { afterUpdate: func(obj runtime.Object, opts *metav1.UpdateOptions) {
mile("AfterUpdate") mile("AfterUpdate")
}, },
beginUpdate: func(_ context.Context, obj, _ runtime.Object, _ *metav1.UpdateOptions) (FinishFunc, error) { beginUpdate: func(_ context.Context, obj, _ runtime.Object, _ *metav1.UpdateOptions) (FinishFunc, error) {
@ -1082,7 +1082,7 @@ func TestStoreUpdateHooksInnerRetry(t *testing.T) {
name string name string
decorator func(runtime.Object) decorator func(runtime.Object)
beginUpdate func(context.Context, runtime.Object, runtime.Object, *metav1.UpdateOptions) (FinishFunc, error) beginUpdate func(context.Context, runtime.Object, runtime.Object, *metav1.UpdateOptions) (FinishFunc, error)
afterUpdate func(runtime.Object) afterUpdate AfterUpdateFunc
// the TTLFunc is an easy hook to force an inner-loop retry // the TTLFunc is an easy hook to force an inner-loop retry
ttl func(obj runtime.Object, existing uint64, update bool) (uint64, error) ttl func(obj runtime.Object, existing uint64, update bool) (uint64, error)
expectError bool expectError bool
@ -1092,7 +1092,7 @@ func TestStoreUpdateHooksInnerRetry(t *testing.T) {
decorator: func(obj runtime.Object) { decorator: func(obj runtime.Object) {
mile("Decorator") mile("Decorator")
}, },
afterUpdate: func(obj runtime.Object) { afterUpdate: func(obj runtime.Object, opts *metav1.UpdateOptions) {
mile("AfterUpdate") mile("AfterUpdate")
}, },
beginUpdate: func(_ context.Context, obj, _ runtime.Object, _ *metav1.UpdateOptions) (FinishFunc, error) { beginUpdate: func(_ context.Context, obj, _ runtime.Object, _ *metav1.UpdateOptions) (FinishFunc, error) {
@ -1108,7 +1108,7 @@ func TestStoreUpdateHooksInnerRetry(t *testing.T) {
decorator: func(obj runtime.Object) { decorator: func(obj runtime.Object) {
mile("Decorator") mile("Decorator")
}, },
afterUpdate: func(obj runtime.Object) { afterUpdate: func(obj runtime.Object, opts *metav1.UpdateOptions) {
mile("AfterUpdate") mile("AfterUpdate")
}, },
beginUpdate: func(_ context.Context, obj, _ runtime.Object, _ *metav1.UpdateOptions) (FinishFunc, error) { beginUpdate: func(_ context.Context, obj, _ runtime.Object, _ *metav1.UpdateOptions) (FinishFunc, error) {