Skip to content

Commit 352e53c

Browse files
committed
add a go-routime
1 parent dc7be0d commit 352e53c

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

pkg/runtime/reconciler.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ const (
5757
// resource if the CARM cache is not synced yet, or if the roleARN is not
5858
// available.
5959
roleARNNotAvailableRequeueDelay = 15 * time.Second
60+
// gracefulShutdownTimeout is the default duration to allow reconciliation
61+
// to complete during controller shutdown
62+
gracefulShutdownTimeout = 10 * time.Second
6063
// adoptOrCreate is an annotation field that decides whether to create the
6164
// resource if it doesn't exist, or adopt the resource if it exists.
6265
// value comes from getAdoptionPolicy
@@ -207,7 +210,7 @@ func (r *reconciler) WriteToSecret(
207210
}
208211

209212
// Reconcile implements `controller-runtime.Reconciler` and handles reconciling
210-
// a CR CRUD request
213+
// a CR CRUD request with graceful shutdown support.
211214
func (r *resourceReconciler) Reconcile(ctx context.Context, req ctrlrt.Request) (ctrlrt.Result, error) {
212215
desired, err := r.getAWSResource(ctx, req)
213216
if err != nil {
@@ -285,8 +288,26 @@ func (r *resourceReconciler) Reconcile(ctx context.Context, req ctrlrt.Request)
285288
if err != nil {
286289
return ctrlrt.Result{}, err
287290
}
291+
292+
// Perform normal reconciliation with original context
288293
latest, err := r.reconcile(ctx, rm, desired)
289-
return r.HandleReconcileError(ctx, desired, latest, err)
294+
295+
// Use graceful context for final status patch only if original context is cancelled
296+
finalCtx := ctx
297+
if ctx.Err() != nil {
298+
// Original context cancelled - create graceful context for final status patch
299+
gracefulCtx, cancel := context.WithTimeout(context.Background(), gracefulShutdownTimeout)
300+
defer cancel()
301+
302+
// Transfer important values from the original context to the graceful context
303+
gracefulCtx = context.WithValue(gracefulCtx, ackrtlog.ContextKey, rlog)
304+
gracefulCtx = context.WithValue(gracefulCtx, "resourceNamespace", req.Namespace)
305+
306+
finalCtx = gracefulCtx
307+
rlog.Info("using graceful context for final status patch due to controller shutdown")
308+
}
309+
310+
return r.HandleReconcileError(finalCtx, desired, latest, err)
290311
}
291312

292313
func (r *resourceReconciler) handleCacheError(

0 commit comments

Comments
 (0)