Skip to content

Commit 3c5edeb

Browse files
committed
Revert "ensure status is patched on adoption (#218)"
This reverts commit 79724c6.
1 parent 79724c6 commit 3c5edeb

File tree

4 files changed

+58
-79
lines changed

4 files changed

+58
-79
lines changed

pkg/errors/error.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,4 @@ func (e TerminalError) Unwrap() error {
123123
return e.err
124124
}
125125

126-
func IsTerminalError(err error) bool {
127-
var terminalErr *TerminalError
128-
return err == Terminal || errors.As(err, &terminalErr)
129-
}
130-
131126
var _ error = &TerminalError{}

pkg/runtime/reconciler.go

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -505,19 +505,21 @@ func (r *resourceReconciler) Sync(
505505
ctx context.Context,
506506
rm acktypes.AWSResourceManager,
507507
desired acktypes.AWSResource,
508-
) (latest acktypes.AWSResource, err error) {
508+
) (acktypes.AWSResource, error) {
509+
var err error
509510
rlog := ackrtlog.FromContext(ctx)
510511
exit := rlog.Trace("r.Sync")
511512
defer func() {
512513
exit(err)
513514
}()
514515

516+
var latest acktypes.AWSResource // the newly created or mutated resource
517+
515518
r.resetConditions(ctx, desired)
516519
defer func() {
517520
r.ensureConditions(ctx, rm, latest, err)
518521
}()
519522

520-
latest = desired.DeepCopy()
521523
isAdopted := IsAdopted(desired)
522524
rlog.WithValues("is_adopted", isAdopted)
523525

@@ -526,7 +528,7 @@ func (r *resourceReconciler) Sync(
526528
needAdoption := NeedAdoption(desired) && !r.rd.IsManaged(desired) && r.cfg.FeatureGates.IsEnabled(featuregate.ResourceAdoption)
527529
adoptionPolicy, err := GetAdoptionPolicy(desired)
528530
if err != nil {
529-
return latest, err
531+
return nil, err
530532
}
531533
if !needAdoption {
532534
adoptionPolicy = ""
@@ -541,9 +543,9 @@ func (r *resourceReconciler) Sync(
541543
rlog.Enter("rm.ResolveReferences")
542544
resolved, hasReferences, err := rm.ResolveReferences(ctx, r.apiReader, desired)
543545
rlog.Exit("rm.ResolveReferences", err)
546+
// TODO (michaelhtm): ignore error only for `adopt` or `read-only`
544547
if err != nil && (adoptionPolicy == "" || adoptionPolicy == AdoptionPolicy_AdoptOrCreate) && !isReadOnly {
545-
latest = ackcondition.WithReferencesResolvedCondition(latest, err)
546-
return latest, err
548+
return ackcondition.WithReferencesResolvedCondition(desired, err), err
547549
}
548550
if hasReferences {
549551
resolved = ackcondition.WithReferencesResolvedCondition(resolved, err)
@@ -552,7 +554,7 @@ func (r *resourceReconciler) Sync(
552554
if needAdoption {
553555
populated, err := r.handlePopulation(ctx, desired)
554556
if err != nil {
555-
return latest, err
557+
return nil, err
556558
}
557559
if adoptionPolicy == AdoptionPolicy_AdoptOrCreate {
558560
// here we assume the spec fields are provided in the spec.
@@ -577,25 +579,25 @@ func (r *resourceReconciler) Sync(
577579
rlog.Exit("rm.ReadOne", err)
578580
if err != nil {
579581
if err != ackerr.NotFound {
580-
return resolved, err
582+
return latest, err
581583
}
582584
if adoptionPolicy == AdoptionPolicy_Adopt || isAdopted {
583-
return resolved, ackerr.AdoptedResourceNotFound
585+
return nil, ackerr.AdoptedResourceNotFound
584586
}
585587
if isReadOnly {
586-
return resolved, ackerr.ReadOnlyResourceNotFound
588+
return nil, ackerr.ReadOnlyResourceNotFound
587589
}
588590
if latest, err = r.createResource(ctx, rm, resolved); err != nil {
589-
return resolved, err
591+
return latest, err
590592
}
591593
} else if adoptionPolicy == AdoptionPolicy_Adopt {
592594
rm.FilterSystemTags(latest, r.cfg.ResourceTagKeys)
593595
if err = r.setResourceManagedAndAdopted(ctx, rm, latest); err != nil {
594-
return resolved, err
596+
return latest, err
595597
}
596598
latest, err = r.patchResourceMetadataAndSpec(ctx, rm, desired, latest)
597599
if err != nil {
598-
return resolved, err
600+
return latest, err
599601
}
600602
} else if isReadOnly {
601603
delta := r.rd.Delta(desired, latest)
@@ -623,11 +625,10 @@ func (r *resourceReconciler) Sync(
623625
}
624626
// Attempt to late initialize the resource. If there are no fields to
625627
// late initialize, this operation will be a no-op.
626-
lateInitialized, err := r.lateInitializeResource(ctx, rm, latest)
627-
if err != nil {
628+
if latest, err = r.lateInitializeResource(ctx, rm, latest); err != nil {
628629
return latest, err
629630
}
630-
return lateInitialized, nil
631+
return latest, nil
631632
}
632633

633634
// resetConditions strips the supplied resource of all objects in its
@@ -669,13 +670,6 @@ func (r *resourceReconciler) ensureConditions(
669670
exit(err)
670671
}()
671672

672-
if ackcondition.Terminal(res) == nil {
673-
if ackerr.IsTerminalError(reconcileErr) {
674-
condReason := reconcileErr.Error()
675-
ackcondition.SetTerminal(res, corev1.ConditionTrue, &condReason, nil)
676-
}
677-
}
678-
679673
// If the ACK.ResourceSynced condition is not set using the custom hooks,
680674
// determine the Synced condition using "rm.IsSynced" method
681675
if ackcondition.Synced(res) == nil {
@@ -694,7 +688,7 @@ func (r *resourceReconciler) ensureConditions(
694688

695689
if reconcileErr != nil {
696690
condReason = reconcileErr.Error()
697-
if ackerr.IsTerminalError(reconcileErr) {
691+
if reconcileErr == ackerr.Terminal {
698692
// A terminal condition is a stable state for a resource.
699693
// Terminal conditions indicate that without changes to the
700694
// desired state of a resource, the resource's desired state
@@ -1325,7 +1319,6 @@ func (r *resourceReconciler) HandleReconcileError(
13251319
latest acktypes.AWSResource,
13261320
err error,
13271321
) (ctrlrt.Result, error) {
1328-
rlog := ackrtlog.FromContext(ctx)
13291322
if ackcompare.IsNotNil(latest) {
13301323
// The reconciliation loop may have returned an error, but if latest is
13311324
// not nil, there may be some changes available in the CR's Status
@@ -1342,11 +1335,10 @@ func (r *resourceReconciler) HandleReconcileError(
13421335
// there is a more robust way to handle failures in the patch operation
13431336
_ = r.patchResourceStatus(ctx, desired, latest)
13441337
}
1345-
1346-
if ackerr.IsTerminalError(err) {
1347-
rlog.Info("resource is terminal")
1338+
if err == nil || err == ackerr.Terminal {
13481339
return ctrlrt.Result{}, nil
13491340
}
1341+
rlog := ackrtlog.FromContext(ctx)
13501342

13511343
var requeueNeededAfter *requeue.RequeueNeededAfter
13521344
if errors.As(err, &requeueNeededAfter) {

pkg/runtime/reconciler_test.go

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ func TestReconcilerCreate_UnmanageResourceOnAWSErrors(t *testing.T) {
252252
latest, latestRTObj, _ := resourceMocks()
253253
latest.On("Identifiers").Return(ids)
254254

255-
desired.On("Conditions").Return([]*ackv1alpha1.Condition{})
256-
desired.On(
255+
latest.On("Conditions").Return([]*ackv1alpha1.Condition{})
256+
latest.On(
257257
"ReplaceConditions",
258258
mock.AnythingOfType("[]*v1alpha1.Condition"),
259259
).Return()
@@ -270,7 +270,7 @@ func TestReconcilerCreate_UnmanageResourceOnAWSErrors(t *testing.T) {
270270
rm.On("Create", ctx, desired).Return(
271271
latest, awsError{},
272272
)
273-
rm.On("IsSynced", ctx, desired).Return(false, nil)
273+
rm.On("IsSynced", ctx, latest).Return(false, nil)
274274
rmf, rd := managedResourceManagerFactoryMocks(desired, latest)
275275
rd.On("IsManaged", desired).Return(false).Twice()
276276
rd.On("IsManaged", desired).Return(true)
@@ -1495,13 +1495,12 @@ func TestReconcilerUpdate_ResourceNotManaged(t *testing.T) {
14951495

14961496
desired, _, _ := resourceMocks()
14971497
desired.On("ReplaceConditions", []*ackv1alpha1.Condition{}).Return()
1498-
1498+
14991499
ids := &ackmocks.AWSResourceIdentifiers{}
15001500
ids.On("ARN").Return(&arn)
1501-
1501+
15021502
latest, _, _ := resourceMocks()
15031503
latest.On("Identifiers").Return(ids)
1504-
desired.On("DeepCopy").Return(latest)
15051504

15061505
terminalCondition := ackv1alpha1.Condition{
15071506
Type: ackv1alpha1.ConditionTypeTerminal,
@@ -1515,7 +1514,6 @@ func TestReconcilerUpdate_ResourceNotManaged(t *testing.T) {
15151514
// These calls will be made from ensureConditions method, which sets
15161515
// ACK.ResourceSynced condition correctly
15171516
latest.On("Conditions").Return([]*ackv1alpha1.Condition{&terminalCondition})
1518-
desired.On("Conditions").Return([]*ackv1alpha1.Condition{&terminalCondition})
15191517

15201518
// Verify once when the terminal condition is set
15211519
latest.On("ReplaceConditions", mock.AnythingOfType("[]*v1alpha1.Condition")).Return([]*ackv1alpha1.Condition{&terminalCondition}).Run(func(args mock.Arguments) {
@@ -1605,7 +1603,7 @@ func TestReconcilerUpdate_ResolveReferencesError(t *testing.T) {
16051603
delta.Add("Spec.A", "val1", "val2")
16061604

16071605
desired, _, _ := resourceMocks()
1608-
desired.On("ReplaceConditions", []*ackv1alpha1.Condition{}).Return().Once()
1606+
desired.On("ReplaceConditions", []*ackv1alpha1.Condition{}).Return()
16091607

16101608
ids := &ackmocks.AWSResourceIdentifiers{}
16111609
ids.On("ARN").Return(&arn)
@@ -1633,35 +1631,30 @@ func TestReconcilerUpdate_ResolveReferencesError(t *testing.T) {
16331631
assert.Equal(t, corev1.ConditionUnknown, cond.Status)
16341632
assert.Equal(t, ackcondition.FailedReferenceResolutionMessage, *cond.Message)
16351633
assert.Equal(t, resolveReferenceError.Error(), *cond.Reason)
1636-
}).Once()
1637-
desired.On(
1638-
"ReplaceConditions",
1639-
mock.AnythingOfType("[]*v1alpha1.Condition"),
1640-
).Return().Run(func(args mock.Arguments) {
1641-
conditions := args.Get(0).([]*ackv1alpha1.Condition)
1642-
assert.Equal(t, 1, len(conditions))
1643-
cond := conditions[0]
1644-
assert.Equal(t, ackv1alpha1.ConditionTypeResourceSynced, cond.Type)
1645-
// The non-terminal reconciler error causes the ReferencesResolved
1646-
// condition to be Unknown
1647-
assert.Equal(t, corev1.ConditionUnknown, cond.Status)
1648-
assert.Equal(t, ackcondition.UnknownSyncedMessage, *cond.Message)
1649-
assert.Equal(t, resolveReferenceError.Error(), *cond.Reason)
1650-
}).Once()
1651-
desired.On(
1652-
"ReplaceConditions",
1653-
mock.AnythingOfType("[]*v1alpha1.Condition"),
1654-
).Return()
1634+
})
16551635

16561636
rm := &ackmocks.AWSResourceManager{}
16571637
rm.On("ResolveReferences", ctx, nil, desired).Return(
16581638
desired, true, resolveReferenceError,
16591639
)
16601640
rm.On("ClearResolvedReferences", desired).Return(desired)
16611641
rm.On("ClearResolvedReferences", latest).Return(latest)
1642+
rm.On("ReadOne", ctx, desired).Return(
1643+
latest, nil,
1644+
)
1645+
rm.On("Update", ctx, desired, latest, delta).Return(
1646+
latest, nil,
1647+
)
16621648

16631649
rmf, rd := managedResourceManagerFactoryMocks(desired, latest)
1664-
rm.On("IsSynced", ctx, desired).Return(true, nil)
1650+
rd.On("Delta", desired, latest).Return(
1651+
delta,
1652+
).Once()
1653+
rd.On("Delta", desired, latest).Return(ackcompare.NewDelta())
1654+
1655+
rm.On("LateInitialize", ctx, latest).Return(latest, nil)
1656+
rm.On("IsSynced", ctx, latest).Return(true, nil)
1657+
rd.On("Delta", latest, latest).Return(ackcompare.NewDelta())
16651658

16661659
r, kc, scmd := reconcilerMocks(rmf)
16671660
rm.On("EnsureTags", ctx, desired, scmd).Return(nil)
@@ -1697,7 +1690,7 @@ func TestReconcilerUpdate_EnsureControllerTagsError(t *testing.T) {
16971690
delta.Add("Spec.A", "val1", "val2")
16981691

16991692
desired, _, _ := resourceMocks()
1700-
desired.On("ReplaceConditions", []*ackv1alpha1.Condition{}).Return().Once()
1693+
desired.On("ReplaceConditions", []*ackv1alpha1.Condition{}).Return()
17011694

17021695
ids := &ackmocks.AWSResourceIdentifiers{}
17031696
ids.On("ARN").Return(&arn)
@@ -1711,8 +1704,8 @@ func TestReconcilerUpdate_EnsureControllerTagsError(t *testing.T) {
17111704
// manager has not set any Conditions on the resource, that at least an
17121705
// ACK.ResourceSynced condition with status Unknown will be set on the
17131706
// resource.
1714-
desired.On("Conditions").Return([]*ackv1alpha1.Condition{})
1715-
desired.On(
1707+
latest.On("Conditions").Return([]*ackv1alpha1.Condition{})
1708+
latest.On(
17161709
"ReplaceConditions",
17171710
mock.AnythingOfType("[]*v1alpha1.Condition"),
17181711
).Return().Run(func(args mock.Arguments) {
@@ -1722,11 +1715,10 @@ func TestReconcilerUpdate_EnsureControllerTagsError(t *testing.T) {
17221715
assert.Equal(t, ackv1alpha1.ConditionTypeResourceSynced, cond.Type)
17231716
// The non-terminal reconciler error causes the ResourceSynced
17241717
// condition to be False
1725-
assert.Equal(t, corev1.ConditionUnknown, cond.Status)
1726-
assert.Equal(t, ackcondition.UnknownSyncedMessage, *cond.Message)
1718+
assert.Equal(t, corev1.ConditionFalse, cond.Status)
1719+
assert.Equal(t, ackcondition.NotSyncedMessage, *cond.Message)
17271720
assert.Equal(t, ensureControllerTagsError.Error(), *cond.Reason)
1728-
}).Once()
1729-
desired.On("ReplaceConditions", mock.AnythingOfType("[]*v1alpha1.Condition")).Return()
1721+
})
17301722

17311723
rm := &ackmocks.AWSResourceManager{}
17321724
rm.On("ResolveReferences", ctx, nil, desired).Return(desired, false, nil)
@@ -1746,7 +1738,7 @@ func TestReconcilerUpdate_EnsureControllerTagsError(t *testing.T) {
17461738
rd.On("Delta", desired, latest).Return(ackcompare.NewDelta())
17471739

17481740
rm.On("LateInitialize", ctx, latest).Return(latest, nil)
1749-
rm.On("IsSynced", ctx, desired).Return(false, nil)
1741+
rm.On("IsSynced", ctx, latest).Return(true, nil)
17501742
rd.On("Delta", latest, latest).Return(ackcompare.NewDelta())
17511743

17521744
r, kc, scmd := reconcilerMocks(rmf)

pkg/runtime/util.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -208,30 +208,30 @@ func NeedAdoption(res acktypes.AWSResource) bool {
208208
}
209209

210210
func ExtractAdoptionFields(res acktypes.AWSResource) (map[string]string, error) {
211-
fields, exists := getAdoptionFields(res)
212-
if !exists {
213-
return nil, fmt.Errorf("%s is not defined", ackv1alpha1.AnnotationAdoptionFields)
214-
}
211+
fields := getAdoptionFields(res)
215212

216-
extractedFields := map[string]string{}
217-
err := json.Unmarshal([]byte(fields), &extractedFields)
213+
extractedFields := &map[string]string{}
214+
err := json.Unmarshal([]byte(fields), extractedFields)
218215
if err != nil {
219216
return nil, err
220217
}
221218

222-
return extractedFields, nil
219+
return *extractedFields, nil
223220
}
224221

225-
func getAdoptionFields(res acktypes.AWSResource) (string, bool) {
222+
func getAdoptionFields(res acktypes.AWSResource) string {
226223
mo := res.MetaObject()
227224
if mo == nil {
228225
// Should never happen... if it does, it's buggy code.
229226
panic("ExtractRequiredFields received resource with nil RuntimeObject")
230227
}
231228

232-
fields, ok := mo.GetAnnotations()[ackv1alpha1.AnnotationAdoptionFields]
233-
234-
return fields, ok
229+
for k, v := range mo.GetAnnotations() {
230+
if k == ackv1alpha1.AnnotationAdoptionFields {
231+
return v
232+
}
233+
}
234+
return ""
235235
}
236236

237237
// patchObject performs a patch operation using context.WithoutCancel to prevent

0 commit comments

Comments
 (0)