diff --git a/mocks/pkg/types/aws_resource_manager.go b/mocks/pkg/types/aws_resource_manager.go index 0ee82e5e..d49123f4 100644 --- a/mocks/pkg/types/aws_resource_manager.go +++ b/mocks/pkg/types/aws_resource_manager.go @@ -294,6 +294,36 @@ func (_m *AWSResourceManager) Update(_a0 context.Context, _a1 types.AWSResource, return r0, r1 } +// OnAdopted provides a mock function with given fields: _a0, _a1 +func (_m *AWSResourceManager) OnAdopted(_a0 context.Context, _a1 types.AWSResource) (types.AWSResource, error) { + ret := _m.Called(_a0, _a1) + + if len(ret) == 0 { + panic("no return value specified for OnAdopted") + } + + var r0 types.AWSResource + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, types.AWSResource) (types.AWSResource, error)); ok { + return rf(_a0, _a1) + } + if rf, ok := ret.Get(0).(func(context.Context, types.AWSResource) types.AWSResource); ok { + r0 = rf(_a0, _a1) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(types.AWSResource) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, types.AWSResource) error); ok { + r1 = rf(_a0, _a1) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + // NewAWSResourceManager creates a new instance of AWSResourceManager. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. // The first argument is typically a *testing.T value. func NewAWSResourceManager(t interface { diff --git a/pkg/runtime/reconciler.go b/pkg/runtime/reconciler.go index 2abf836d..b80da124 100644 --- a/pkg/runtime/reconciler.go +++ b/pkg/runtime/reconciler.go @@ -599,6 +599,10 @@ func (r *resourceReconciler) Sync( if err != nil { return latest, err } + latest, err = rm.OnAdopted(ctx, latest) + if err != nil { + return latest, err + } } else if isReadOnly { delta := r.rd.Delta(desired, latest) if delta.DifferentAt("Spec") { @@ -616,6 +620,9 @@ func (r *resourceReconciler) Sync( if err = r.setResourceManagedAndAdopted(ctx, rm, latest); err != nil { return latest, err } + if latest, err = rm.OnAdopted(ctx, latest); err != nil { + return latest, err + } err = requeue.Needed(fmt.Errorf("adopted resource, requeuing to check for updates")) return latest, err } diff --git a/pkg/runtime/reconciler_test.go b/pkg/runtime/reconciler_test.go index 9dd9cac5..e96e7d30 100644 --- a/pkg/runtime/reconciler_test.go +++ b/pkg/runtime/reconciler_test.go @@ -397,6 +397,7 @@ func TestReconcilerAdoptResource(t *testing.T) { rm.On("FilterSystemTags", latest, []string{}) rd.On("MarkAdopted", latest).Return() rm.On("EnsureTags", ctx, desired, scmd).Return(nil) + rm.On("OnAdopted", ctx, latest).Return(latest, nil) statusWriter := &ctrlrtclientmock.SubResourceWriter{} kc.On("Patch", withoutCancelContextMatcher, latestRTObj, mock.AnythingOfType("*client.mergeFromPatch")).Return(nil) kc.On("Status").Return(statusWriter) @@ -559,6 +560,7 @@ func TestReconcilerAdoptOrCreateResource_Adopt(t *testing.T) { r, kc, scmd := reconcilerMocks(rmf) rm.On("EnsureTags", ctx, desired, scmd).Return(nil) + rm.On("OnAdopted", ctx, latest).Return(latest, nil) statusWriter := &ctrlrtclientmock.SubResourceWriter{} kc.On("Status").Return(statusWriter) kc.On("Patch", withoutCancelContextMatcher, latestRTObj, mock.AnythingOfType("*client.mergeFromPatch")).Return(nil) diff --git a/pkg/types/aws_resource_manager.go b/pkg/types/aws_resource_manager.go index 6ca943e6..55aa4bc7 100644 --- a/pkg/types/aws_resource_manager.go +++ b/pkg/types/aws_resource_manager.go @@ -91,6 +91,10 @@ type AWSResourceManager interface { // Eg. resources created with cloudformation have tags that cannot be //removed by an ACK controller FilterSystemTags(AWSResource, []string) + // OnAdopted is called after a resource has been successfully adopted + // and marked as managed. Controllers can use this hook to perform + // custom post-adoption logic. + OnAdopted(context.Context, AWSResource) (AWSResource, error) } // AWSResourceManagerFactory returns an AWSResourceManager that can be used to