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.
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.
type GenericStore interface {
GetCreateStrategy() rest.RESTCreateStrategy
@ -159,7 +165,7 @@ type Store struct {
BeginCreate func(ctx context.Context, obj runtime.Object, options *metav1.CreateOptions) (FinishFunc, error)
// AfterCreate implements a further operation to run after a resource is
// created and before it is decorated, optional.
AfterCreate func(runtime.Object)
AfterCreate AfterCreateFunc
// UpdateStrategy implements resource-specific behavior during updates.
UpdateStrategy rest.RESTUpdateStrategy
@ -171,7 +177,7 @@ type Store struct {
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
// updated and before it is decorated, optional.
AfterUpdate func(runtime.Object)
AfterUpdate AfterUpdateFunc
// DeleteStrategy implements resource-specific behavior during deletion.
DeleteStrategy rest.RESTDeleteStrategy
@ -419,7 +425,7 @@ func (e *Store) Create(ctx context.Context, obj runtime.Object, createValidation
fn(ctx, true)
if e.AfterCreate != nil {
e.AfterCreate(out)
e.AfterCreate(out, options)
}
if e.Decorator != nil {
e.Decorator(out)
@ -659,11 +665,11 @@ func (e *Store) Update(ctx context.Context, name string, objInfo rest.UpdatedObj
if creating {
if e.AfterCreate != nil {
e.AfterCreate(out)
e.AfterCreate(out, newCreateOptionsFromUpdateOptions(options))
}
} else {
if e.AfterUpdate != nil {
e.AfterUpdate(out)
e.AfterUpdate(out, options)
}
}
if e.Decorator != nil {

View File

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