@@ -57,6 +57,9 @@ const (
57
57
// resource if the CARM cache is not synced yet, or if the roleARN is not
58
58
// available.
59
59
roleARNNotAvailableRequeueDelay = 15 * time .Second
60
+ // gracefulShutdownTimeout is the default duration to allow reconciliation
61
+ // to complete during controller shutdown
62
+ gracefulShutdownTimeout = 10 * time .Second
60
63
// adoptOrCreate is an annotation field that decides whether to create the
61
64
// resource if it doesn't exist, or adopt the resource if it exists.
62
65
// value comes from getAdoptionPolicy
@@ -207,7 +210,7 @@ func (r *reconciler) WriteToSecret(
207
210
}
208
211
209
212
// Reconcile implements `controller-runtime.Reconciler` and handles reconciling
210
- // a CR CRUD request
213
+ // a CR CRUD request with graceful shutdown support.
211
214
func (r * resourceReconciler ) Reconcile (ctx context.Context , req ctrlrt.Request ) (ctrlrt.Result , error ) {
212
215
desired , err := r .getAWSResource (ctx , req )
213
216
if err != nil {
@@ -285,8 +288,26 @@ func (r *resourceReconciler) Reconcile(ctx context.Context, req ctrlrt.Request)
285
288
if err != nil {
286
289
return ctrlrt.Result {}, err
287
290
}
291
+
292
+ // Perform normal reconciliation with original context
288
293
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 )
290
311
}
291
312
292
313
func (r * resourceReconciler ) handleCacheError (
0 commit comments