Skip to content

Commit 0016367

Browse files
committed
ADdress comments
1 parent 0a84239 commit 0016367

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

pkg/runtime/reconciler.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -303,18 +303,21 @@ func (r *resourceReconciler) handleCacheError(
303303
return r.HandleReconcileError(ctx, desired, latest, requeue.NeededAfter(err, roleARNNotAvailableRequeueDelay))
304304
}
305305

306+
// Handles the population of ACK Resource fields from annotation.
306307
func (r *resourceReconciler) handlePopulation(
307308
rlog acktypes.Logger,
308309
desired acktypes.AWSResource,
309310
adoptionPolicy AdoptionPolicy,
310311
) (acktypes.AWSResource, error) {
311312
// If the resource is being adopted by force, we need to access
312313
// the required field passed by annotation and attempt a read.
313-
314-
rlog.Info("Adopting Resource")
314+
rlog.Debug("Populating Resource")
315315
adoptionFields, err := ExtractAdoptionFields(desired)
316316
if err != nil {
317-
if adoptionPolicy == AdoptOrCreatePolicy {
317+
// if the user does not provide adoption fields and the policy
318+
// is adopt-or-create, we will assume the adoption fields are passed
319+
// in Spec
320+
if adoptionPolicy == AdoptionPolicy_AdoptOrCreate {
318321
return desired, nil
319322
}
320323
return desired, ackerr.NewTerminalError(err)
@@ -323,9 +326,10 @@ func (r *resourceReconciler) handlePopulation(
323326
populated := desired.DeepCopy()
324327
err = populated.PopulateResourceFromAnnotation(adoptionFields)
325328
// maybe don't return errors when it's adopt-or-create?
329+
//
326330
// TODO (michaelhtm) change PopulateResourceFromAnnotation to understand
327331
// adopt-or-create, and validate Spec fields are not nil...
328-
if err != nil && adoptionPolicy != AdoptOrCreatePolicy {
332+
if err != nil && adoptionPolicy != AdoptionPolicy_AdoptOrCreate {
329333
return nil, err
330334
}
331335

@@ -398,7 +402,8 @@ func (r *resourceReconciler) Sync(
398402
isAdopted := IsAdopted(desired)
399403
rlog.WithValues("is_adopted", isAdopted)
400404

401-
// find out if we need to adopt early on
405+
// If AdoptionAnnotation feature gate is enabled, and the resource has an adoption
406+
// annotation and doesn't hold an ACK finalizer, trigger the adoption path.
402407
needAdoption := NeedAdoption(desired) && !r.rd.IsManaged(desired) && r.cfg.FeatureGates.IsEnabled(featuregate.ResourceAdoption)
403408
adoptionPolicy, err := GetAdoptionPolicy(desired)
404409
if err != nil {
@@ -430,7 +435,7 @@ func (r *resourceReconciler) Sync(
430435
if err != nil {
431436
return nil, err
432437
}
433-
if adoptionPolicy == AdoptOrCreatePolicy {
438+
if adoptionPolicy == AdoptionPolicy_AdoptOrCreate {
434439
// here we assume the spec fields are provided in the spec.
435440
resolved.SetStatus(populated)
436441
} else {
@@ -454,7 +459,7 @@ func (r *resourceReconciler) Sync(
454459
if err != ackerr.NotFound {
455460
return latest, err
456461
}
457-
if adoptionPolicy == AdoptPolicy || isAdopted {
462+
if adoptionPolicy == AdoptionPolicy_Adopt || isAdopted {
458463
return nil, ackerr.AdoptedResourceNotFound
459464
}
460465
if isReadOnly {
@@ -463,7 +468,7 @@ func (r *resourceReconciler) Sync(
463468
if latest, err = r.createResource(ctx, rm, resolved); err != nil {
464469
return latest, err
465470
}
466-
} else if adoptionPolicy == AdoptPolicy {
471+
} else if adoptionPolicy == AdoptionPolicy_Adopt {
467472
rm.FilterSystemTags(latest)
468473
if err = r.setResourceManaged(ctx, rm, latest); err != nil {
469474
return latest, err
@@ -473,7 +478,9 @@ func (r *resourceReconciler) Sync(
473478
return latest, err
474479
}
475480
} else if !isReadOnly {
476-
if adoptionPolicy == AdoptOrCreatePolicy {
481+
if adoptionPolicy == AdoptionPolicy_AdoptOrCreate {
482+
// set adopt-or-create resource as managed before attempting
483+
// update
477484
if err = r.setResourceManaged(ctx, rm, latest); err != nil {
478485
return latest, err
479486
}

pkg/runtime/reconciler_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,6 @@ func TestReconcilerAdoptOrCreateResource_Create(t *testing.T) {
324324
require := require.New(t)
325325

326326
ctx := context.TODO()
327-
// arn := ackv1alpha1.AWSResourceName("my-adopt-book-arn")
328327

329328
desired, _, metaObj := resourceMocks()
330329
desired.On("ReplaceConditions", []*ackv1alpha1.Condition{}).Return()
@@ -334,7 +333,6 @@ func TestReconcilerAdoptOrCreateResource_Create(t *testing.T) {
334333
})
335334

336335
ids := &ackmocks.AWSResourceIdentifiers{}
337-
// ids.On("ARN").Return(&arn)
338336

339337
latest, latestRTObj, _ := resourceMocks()
340338
latest.On("Identifiers").Return(ids)
@@ -394,7 +392,6 @@ func TestReconcilerAdoptOrCreateResource_Adopt(t *testing.T) {
394392
require := require.New(t)
395393

396394
ctx := context.TODO()
397-
// arn := ackv1alpha1.AWSResourceName("my-adopt-book-arn")
398395

399396
desired, _, metaObj := resourceMocks()
400397
desired.On("ReplaceConditions", []*ackv1alpha1.Condition{}).Return()
@@ -404,7 +401,6 @@ func TestReconcilerAdoptOrCreateResource_Adopt(t *testing.T) {
404401
})
405402

406403
ids := &ackmocks.AWSResourceIdentifiers{}
407-
// ids.On("ARN").Return(&arn)
408404
delta := ackcompare.NewDelta()
409405
delta.Add("Spec.A", "val1", "val2")
410406

pkg/runtime/util.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@ import (
2929

3030
// AdoptionPolicy stores adoptionPolicy values we expect users to
3131
// provide in the resources `adoption-policy` annotation
32+
//
3233
// TODO(michaelhtm) Maybe we need a different place for this...
3334
// next refactor maybe? 🤷‍♂️
3435
type AdoptionPolicy string
3536

3637
const (
37-
AdoptPolicy AdoptionPolicy = "adopt"
38-
// calling this policy constant `AdoptOrCreatePolicy`
39-
// and see if we need to rename it!!!
40-
AdoptOrCreatePolicy AdoptionPolicy = "adopt-or-create"
38+
// AdoptPolicy is ...
39+
AdoptionPolicy_Adopt AdoptionPolicy = "adopt"
40+
// AdoptPolicy is ...
41+
AdoptionPolicy_AdoptOrCreate AdoptionPolicy = "adopt-or-create"
4142
)
4243

4344
// IsAdopted returns true if the supplied AWSResource was created with a
@@ -100,7 +101,7 @@ func GetAdoptionPolicy(res acktypes.AWSResource) (AdoptionPolicy, error) {
100101
return "", nil
101102
}
102103

103-
if policy != string(AdoptPolicy) && policy != string(AdoptOrCreatePolicy) {
104+
if policy != string(AdoptionPolicy_Adopt) && policy != string(AdoptionPolicy_AdoptOrCreate) {
104105
return "", fmt.Errorf("unrecognized adoption policy")
105106
}
106107

0 commit comments

Comments
 (0)