@@ -179,6 +179,8 @@ func labels(environment *aliecsv1alpha1.Environment, nodename string) map[string
179179 }
180180}
181181
182+ const environmentFinalizer = "aliecs.alice.cern/environment-finalizer"
183+
182184// +kubebuilder:rbac:groups=aliecs.alice.cern,resources=environments,verbs=get;list;watch;create;update;patch;delete
183185// +kubebuilder:rbac:groups=aliecs.alice.cern,resources=environments/status,verbs=get;update;patch
184186// +kubebuilder:rbac:groups=aliecs.alice.cern,resources=environments/finalizers,verbs=update
@@ -194,6 +196,42 @@ func labels(environment *aliecsv1alpha1.Environment, nodename string) map[string
194196//
195197// For more details, check Reconcile and its Result here:
196198// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.23.1/pkg/reconcile
199+ func (r * EnvironmentReconciler ) handleFinalizer (ctx context.Context , environment * aliecsv1alpha1.Environment , log logr.Logger ) (ctrl.Result , bool , error ) {
200+ if environment .DeletionTimestamp .IsZero () {
201+ if ! controllerutil .ContainsFinalizer (environment , environmentFinalizer ) {
202+ controllerutil .AddFinalizer (environment , environmentFinalizer )
203+ if err := r .Update (ctx , environment ); err != nil {
204+ return ctrl.Result {}, true , err
205+ }
206+ return ctrl.Result {}, true , nil
207+ }
208+ } else {
209+ if controllerutil .ContainsFinalizer (environment , environmentFinalizer ) {
210+ tasks := & aliecsv1alpha1.TaskList {}
211+ if err := r .List (ctx , tasks , client .InNamespace (environment .Namespace ), client.MatchingLabels {"environment" : environment .Name }); err != nil {
212+ return ctrl.Result {}, true , err
213+ }
214+ if len (tasks .Items ) > 0 {
215+ for i := range tasks .Items {
216+ if tasks .Items [i ].DeletionTimestamp .IsZero () {
217+ if err := r .Delete (ctx , & tasks .Items [i ]); err != nil && ! k8serrors .IsNotFound (err ) {
218+ return ctrl.Result {}, true , err
219+ }
220+ }
221+ }
222+ log .Info ("waiting for tasks to be deleted before removing environment" , "remaining" , len (tasks .Items ))
223+ return ctrl.Result {}, true , nil
224+ }
225+ controllerutil .RemoveFinalizer (environment , environmentFinalizer )
226+ if err := r .Update (ctx , environment ); err != nil {
227+ return ctrl.Result {}, true , err
228+ }
229+ }
230+ return ctrl.Result {}, true , nil
231+ }
232+ return ctrl.Result {}, false , nil
233+ }
234+
197235func (r * EnvironmentReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
198236 log := logf .FromContext (ctx )
199237
@@ -202,6 +240,10 @@ func (r *EnvironmentReconciler) Reconcile(ctx context.Context, req ctrl.Request)
202240 return ctrl.Result {}, client .IgnoreNotFound (err )
203241 }
204242
243+ if res , stop , err := r .handleFinalizer (ctx , environment , log ); err != nil || stop {
244+ return res , err
245+ }
246+
205247 if environment .Status .State == "" {
206248 for nodename , tasksReferences := range environment .TaskTemplates .Tasks {
207249 log .Info ("creating tasks for hostname from references" , "hostname" , nodename , "number of tasks" , len (tasksReferences ))
0 commit comments