Skip to content

Properly handle CARM errors and requeues #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/condition/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (
NotSyncedMessage = "Resource not synced"
SyncedMessage = "Resource synced successfully"
FailedReferenceResolutionMessage = "Reference resolution failed"
UnavailableIAMRoleMessage = "IAM Role is not available"
)

// Synced returns the Condition in the resource's Conditions collection that is
Expand Down
1 change: 1 addition & 0 deletions pkg/runtime/adoption_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func (r *adoptionReconciler) reconcile(ctx context.Context, req ctrlrt.Request)
// is not available.
roleARN, err = r.getRoleARN(acctID)
if err != nil {
ackrtlog.InfoAdoptedResource(r.log, res, fmt.Sprintf("Unable to start adoption reconcilliation %s: %v", acctID, err))
// r.getRoleARN errors are not terminal, we should requeue.
return requeue.NeededAfter(err, roleARNNotAvailableRequeueDelay)
Copy link
Member Author

@a-hilaly a-hilaly Feb 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This requeue works fine, because r.reconcile is wrapped by handleReconcileErrors

}
Expand Down
31 changes: 20 additions & 11 deletions pkg/runtime/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ func (r *resourceReconciler) Reconcile(ctx context.Context, req ctrlrt.Request)
return ctrlrt.Result{}, err
}

rlog := ackrtlog.NewResourceLogger(
r.log, desired,
// All the fields for a resource that do not change during reconciliation
// can be initialized during resourceLogger creation
"kind", r.rd.GroupVersionKind().Kind,
"namespace", req.Namespace,
"name", req.Name,
)
// We're storing a logger pointer in the context, so that any changes to the logger
// will be reflected in the context.
ctx = context.WithValue(ctx, ackrtlog.ContextKey, rlog)

// If a user has specified a namespace that is annotated with the
// an owner account ID, we need an appropriate role ARN to assume
// in order to perform the reconciliation. The roles ARN are typically
Expand All @@ -174,12 +186,16 @@ func (r *resourceReconciler) Reconcile(ctx context.Context, req ctrlrt.Request)
// is not available.
roleARN, err = r.getRoleARN(acctID)
if err != nil {
// r.getRoleARN errors are not terminal, we should requeue.
return ctrlrt.Result{}, requeue.NeededAfter(err, roleARNNotAvailableRequeueDelay)
// TODO(a-hilaly): Refactor all the reconcile function to make it
// easier to understand and maintain.
reason := err.Error()
latest := desired.DeepCopy()
// set ResourceSynced condition to false with proper error message
condition.SetSynced(latest, corev1.ConditionFalse, &condition.UnavailableIAMRoleMessage, &reason)
return r.HandleReconcileError(ctx, desired, latest, requeue.NeededAfter(err, roleARNNotAvailableRequeueDelay))
}
}
region := r.getRegion(desired)

endpointURL := r.getEndpointURL(desired)
gvk := r.rd.GroupVersionKind()
// New session will only pivot to the roleARN if it is not empty.
Expand All @@ -188,18 +204,11 @@ func (r *resourceReconciler) Reconcile(ctx context.Context, req ctrlrt.Request)
return ctrlrt.Result{}, err
}

rlog := ackrtlog.NewResourceLogger(
r.log, desired,
rlog.WithValues(
"account", acctID,
"role", roleARN,
"region", region,
// All the fields for a resource that do not change during reconciliation
// can be initialized during resourceLogger creation
"kind", r.rd.GroupVersionKind().Kind,
"namespace", req.Namespace,
"name", req.Name,
)
ctx = context.WithValue(ctx, ackrtlog.ContextKey, rlog)

rm, err := r.rmf.ManagerFor(
r.cfg, r.log, r.metrics, r, sess, acctID, region,
Expand Down