Skip to content

Commit f075d12

Browse files
committed
always patchResourceMetadataAndSpec after rm.LateInitialization() call.
1 parent 2a4ad5c commit f075d12

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

mocks/pkg/types/aws_resource.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/runtime/reconciler.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func (r *resourceReconciler) Sync(
223223
}
224224
// Attempt to late initialize the resource. If there are no fields to
225225
// late initialize, this operation will be a no-op.
226-
if latest, err = r.lateInitializeResource(ctx, rm, desired, latest); err != nil {
226+
if latest, err = r.lateInitializeResource(ctx, rm, latest); err != nil {
227227
return latest, err
228228
}
229229
return r.handleRequeues(ctx, latest)
@@ -356,7 +356,6 @@ func (r *resourceReconciler) updateResource(
356356
func (r *resourceReconciler) lateInitializeResource(
357357
ctx context.Context,
358358
rm acktypes.AWSResourceManager,
359-
desired acktypes.AWSResource,
360359
latest acktypes.AWSResource,
361360
) (acktypes.AWSResource, error) {
362361
var err error
@@ -367,11 +366,15 @@ func (r *resourceReconciler) lateInitializeResource(
367366
rlog.Enter("rm.LateInitialize")
368367
lateInitializedLatest, err := rm.LateInitialize(ctx, latest)
369368
rlog.Exit("rm.LateInitialize", err)
370-
if err != nil {
371-
// If there was error in late initialization, still patch the resource metadata and spec
372-
// to reflect changes in k8s resource.
373-
if ackcompare.IsNotNil(lateInitializedLatest) {
374-
_ = r.patchResourceMetadataAndSpec(ctx, desired, lateInitializedLatest)
369+
// Always patch after late initialize because some fields may have been initialized while
370+
// others require a retry after some delay.
371+
// This patching does not hurt because if there is no diff then 'patchResourceMetadataAndSpec'
372+
// acts as a no-op.
373+
if ackcompare.IsNotNil(lateInitializedLatest) {
374+
patchErr := r.patchResourceMetadataAndSpec(ctx, latest, lateInitializedLatest)
375+
// Throw the patching error if reconciler is unable to patch the resource with late initializations
376+
if patchErr != nil {
377+
err = patchErr
375378
}
376379
}
377380
return lateInitializedLatest, err

pkg/runtime/reconciler_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,16 @@ func TestReconcilerUpdate(t *testing.T) {
149149
rm.On("Update", ctx, desired, latest, delta).Return(
150150
latest, nil,
151151
)
152-
rm.On("LateInitialize", ctx, latest).Return(latest, nil)
153152

154153
rmf, rd := managerFactoryMocks(desired, latest, delta)
155154
rd.On("Delta", desired, latest).Return(
156155
delta,
157156
).Once()
158157
rd.On("Delta", desired, latest).Return(ackcompare.NewDelta())
159158

159+
rm.On("LateInitialize", ctx, latest).Return(latest, nil)
160+
rd.On("Delta", latest, latest).Return(ackcompare.NewDelta())
161+
160162
r, kc := reconcilerMocks(rmf)
161163

162164
kc.On("Patch", ctx, latestRTObj, client.MergeFrom(desiredRTObj)).Return(nil)
@@ -213,6 +215,7 @@ func TestReconcilerUpdate_PatchMetadataAndSpec_DiffInMetadata(t *testing.T) {
213215
latest, nil,
214216
)
215217
rm.On("LateInitialize", ctx, latest).Return(latest, nil)
218+
rd.On("Delta", latest, latest).Return(ackcompare.NewDelta())
216219

217220
r, kc := reconcilerMocks(rmf)
218221

@@ -261,6 +264,7 @@ func TestReconcilerUpdate_PatchMetadataAndSpec_DiffInSpec(t *testing.T) {
261264
latest, nil,
262265
)
263266
rm.On("LateInitialize", ctx, latest).Return(latest, nil)
267+
rd.On("Delta", latest, latest).Return(ackcompare.NewDelta())
264268

265269
r, kc := reconcilerMocks(rmf)
266270

@@ -363,15 +367,17 @@ func TestReconcilerUpdate_ErrorInLateInitialization(t *testing.T) {
363367
rm.On("Update", ctx, desired, latest, delta).Return(
364368
latest, nil,
365369
)
366-
requeueError := requeue.NeededAfter(errors.New("error from late initialization"), time.Duration(0)*time.Second)
367-
rm.On("LateInitialize", ctx, latest).Return(latest, requeueError)
368370

369371
rmf, rd := managerFactoryMocks(desired, latest, delta)
370372
rd.On("Delta", desired, latest).Return(
371373
delta,
372374
).Once()
373375
rd.On("Delta", desired, latest).Return(ackcompare.NewDelta())
374376

377+
requeueError := requeue.NeededAfter(errors.New("error from late initialization"), time.Duration(0)*time.Second)
378+
rm.On("LateInitialize", ctx, latest).Return(latest, requeueError)
379+
rd.On("Delta", latest, latest).Return(ackcompare.NewDelta())
380+
375381
r, kc := reconcilerMocks(rmf)
376382

377383
kc.On("Patch", ctx, latestRTObj, client.MergeFrom(desiredRTObj)).Return(nil)

0 commit comments

Comments
 (0)