@@ -101,6 +101,11 @@ func (r *RuntimeComponentReconciler) Reconcile(ctx context.Context, req ctrl.Req
101101 ns = r .watchNamespaces [0 ]
102102 }
103103
104+ // This reconcile context is cancelled right before the Reconcile function terminates or when the parent context ctx is cancelled (such as in operator leader election), whichever happens first.
105+ // Resources that require cleanup may use this context to hook into program execution before a function return.
106+ recCtx , cancel := context .WithCancel (ctx )
107+ defer cancel ()
108+
104109 configMap , err := r .GetOpConfigMap (OperatorName , ns )
105110 if err != nil {
106111 reqLogger .Info ("Failed to find runtime-component-operator config map" )
@@ -238,7 +243,7 @@ func (r *RuntimeComponentReconciler) Reconcile(ctx context.Context, req ctrl.Req
238243 if serviceAccountName == "" {
239244 serviceAccount := & corev1.ServiceAccount {ObjectMeta : defaultMeta }
240245 err = r .CreateOrUpdate (serviceAccount , instance , func () error {
241- return appstacksutils .CustomizeServiceAccount (serviceAccount , instance , r .GetClient ())
246+ return appstacksutils .CustomizeServiceAccount (recCtx , serviceAccount , instance , r .GetClient ())
242247 })
243248 if err != nil {
244249 reqLogger .Error (err , "Failed to reconcile ServiceAccount" )
@@ -257,7 +262,7 @@ func (r *RuntimeComponentReconciler) Reconcile(ctx context.Context, req ctrl.Req
257262
258263 // Check if the ServiceAccount has a valid pull secret before creating the deployment/statefulset
259264 // or setting up knative. Otherwise the pods can go into an ImagePullBackOff loop
260- saErr := appstacksutils .ServiceAccountPullSecretExists (instance , r .GetClient ())
265+ saErr := appstacksutils .ServiceAccountPullSecretExists (recCtx , instance , r .GetClient ())
261266 if saErr != nil {
262267 return r .ManageError (saErr , common .StatusConditionTypeReconciled , instance )
263268 }
@@ -319,7 +324,7 @@ func (r *RuntimeComponentReconciler) Reconcile(ctx context.Context, req ctrl.Req
319324 }
320325 }
321326
322- useCertmanager , err := r .GenerateSvcCertSecret (ba , "rco" , "Runtime Component Operator" , "runtime-component-operator" )
327+ useCertmanager , err := r .GenerateSvcCertSecret (recCtx , ba , "rco" , "Runtime Component Operator" , "runtime-component-operator" )
323328 if err != nil {
324329 reqLogger .Error (err , "Failed to reconcile CertManager Certificate" )
325330 return r .ManageError (err , common .StatusConditionTypeReconciled , instance )
@@ -365,7 +370,7 @@ func (r *RuntimeComponentReconciler) Reconcile(ctx context.Context, req ctrl.Req
365370 }
366371 }
367372
368- err = r .ReconcileBindings (instance )
373+ err = r .ReconcileBindings (recCtx , instance )
369374 if err != nil {
370375 return r .ManageError (err , common .StatusConditionTypeReconciled , ba )
371376 }
@@ -395,7 +400,7 @@ func (r *RuntimeComponentReconciler) Reconcile(ctx context.Context, req ctrl.Req
395400 err = r .CreateOrUpdate (statefulSet , instance , func () error {
396401 appstacksutils .CustomizeStatefulSet (statefulSet , instance )
397402 appstacksutils .CustomizePodSpec (& statefulSet .Spec .Template , instance )
398- if err := appstacksutils .CustomizePodWithSVCCertificate (& statefulSet .Spec .Template , instance , r .GetClient ()); err != nil {
403+ if err := appstacksutils .CustomizePodWithSVCCertificate (recCtx , & statefulSet .Spec .Template , instance , r .GetClient ()); err != nil {
399404 return err
400405 }
401406 appstacksutils .CustomizePersistence (statefulSet , instance )
@@ -427,7 +432,7 @@ func (r *RuntimeComponentReconciler) Reconcile(ctx context.Context, req ctrl.Req
427432 err = r .CreateOrUpdate (deploy , instance , func () error {
428433 appstacksutils .CustomizeDeployment (deploy , instance )
429434 appstacksutils .CustomizePodSpec (& deploy .Spec .Template , instance )
430- if err := appstacksutils .CustomizePodWithSVCCertificate (& deploy .Spec .Template , instance , r .GetClient ()); err != nil {
435+ if err := appstacksutils .CustomizePodWithSVCCertificate (recCtx , & deploy .Spec .Template , instance , r .GetClient ()); err != nil {
431436 return err
432437 }
433438 return nil
@@ -529,7 +534,7 @@ func (r *RuntimeComponentReconciler) Reconcile(ctx context.Context, req ctrl.Req
529534 } else if ok {
530535 if instance .Spec .Monitoring != nil && (instance .Spec .CreateKnativeService == nil || ! * instance .Spec .CreateKnativeService ) {
531536 // Validate the monitoring endpoints' configuration before creating/updating the ServiceMonitor
532- if err := appstacksutils .ValidatePrometheusMonitoringEndpoints (instance , r .GetClient (), instance .GetNamespace ()); err != nil {
537+ if err := appstacksutils .ValidatePrometheusMonitoringEndpoints (recCtx , instance , r .GetClient (), instance .GetNamespace ()); err != nil {
533538 return r .ManageError (err , common .StatusConditionTypeReconciled , instance )
534539 }
535540 sm := & prometheusv1.ServiceMonitor {ObjectMeta : defaultMeta }
0 commit comments