Commit f52de83f authored by Konstantinos Samaras-Tsakiris's avatar Konstantinos Samaras-Tsakiris
Browse files

Cleanup only if finalizer present

parent 5e25a1aa
Pipeline #2309820 passed with stage
in 2 minutes and 11 seconds
...@@ -23,3 +23,6 @@ bin ...@@ -23,3 +23,6 @@ bin
*.swo *.swo
*~ *~
.vscode .vscode
__*
secret.yaml
...@@ -33,6 +33,7 @@ import ( ...@@ -33,6 +33,7 @@ import (
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime" ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/controller-runtime/pkg/reconcile"
) )
...@@ -93,7 +94,10 @@ func (r *DrupalSiteReconciler) Reconcile(ctx context.Context, req ctrl.Request) ...@@ -93,7 +94,10 @@ func (r *DrupalSiteReconciler) Reconcile(ctx context.Context, req ctrl.Request)
//Handle deletion //Handle deletion
if drupalSite.GetDeletionTimestamp() != nil { if drupalSite.GetDeletionTimestamp() != nil {
return r.cleanupDrupalSite(ctx, log, drupalSite) if controllerutil.ContainsFinalizer(drupalSite, finalizerStr) {
return r.cleanupDrupalSite(ctx, log, drupalSite)
}
return ctrl.Result{}, nil
} }
handleTransientErr := func(transientErr reconcileError, logstrFmt string) (reconcile.Result, error) { handleTransientErr := func(transientErr reconcileError, logstrFmt string) (reconcile.Result, error) {
...@@ -156,18 +160,8 @@ func (r *DrupalSiteReconciler) Reconcile(ctx context.Context, req ctrl.Request) ...@@ -156,18 +160,8 @@ func (r *DrupalSiteReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, nil return ctrl.Result{}, nil
} }
// Add watches for other resources controller.Watch check API Use filters with labels to pick up the resource
// Structure
// Fetch the CR
// Add clean up and delete https://gitlab.cern.ch:8443/paas-tools/operators/authz-operator/blob/24717af14e0792a416168eaea99dd4bbd9b83b9b/pkg/controller/applicationregistration/applicationregistration_controller.go#L117-118
// Actions on initialization
// Actions on maintaining https://gitlab.cern.ch:8443/paas-tools/operators/authz-operator/blob/24717af14e0792a416168eaea99dd4bbd9b83b9b/pkg/controller/applicationregistration/applicationregistration_controller.go#L152
// Status - creation complete
// Transient Error Reconcile Error
// cleanupDrupalSite checks and removes if a finalizer exists on the resource // cleanupDrupalSite checks and removes if a finalizer exists on the resource
func (r *DrupalSiteReconciler) cleanupDrupalSite(ctx context.Context, log logr.Logger, app *webservicesv1a1.DrupalSite) (ctrl.Result, error) { func (r *DrupalSiteReconciler) cleanupDrupalSite(ctx context.Context, log logr.Logger, drp *webservicesv1a1.DrupalSite) (ctrl.Result, error) {
// finalizer: dependentResources // finalizer: dependentResources
// 1. check if such resources exist // 1. check if such resources exist
// - delete them // - delete them
...@@ -175,15 +169,8 @@ func (r *DrupalSiteReconciler) cleanupDrupalSite(ctx context.Context, log logr.L ...@@ -175,15 +169,8 @@ func (r *DrupalSiteReconciler) cleanupDrupalSite(ctx context.Context, log logr.L
// 1. if not, delete the finalizer key manually and let Kubernetes delete the resource cleanly // 1. if not, delete the finalizer key manually and let Kubernetes delete the resource cleanly
// TODO // TODO
log.Info("Deleting DrupalSite") log.Info("Deleting DrupalSite")
remainingFinalizers := app.GetFinalizers() controllerutil.RemoveFinalizer(drp, finalizerStr)
for i, finalizer := range remainingFinalizers { return r.updateCRorFailReconcile(ctx, log, drp)
if finalizer == finalizerStr {
remainingFinalizers = append(remainingFinalizers[:i], remainingFinalizers[i+1:]...)
break
}
}
app.SetFinalizers(remainingFinalizers)
return r.updateCRorFailReconcile(ctx, log, app)
} }
func setReady(drp *webservicesv1a1.DrupalSite) (update bool) { func setReady(drp *webservicesv1a1.DrupalSite) (update bool) {
......
...@@ -41,6 +41,7 @@ import ( ...@@ -41,6 +41,7 @@ import (
"k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/pointer" "k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/controller-runtime/pkg/reconcile"
) )
...@@ -97,8 +98,9 @@ func contains(a []string, x string) bool { ...@@ -97,8 +98,9 @@ func contains(a []string, x string) bool {
// ensureSpecFinalizer ensures that the spec is valid, adding extra info if necessary, and that the finalizer is there, // ensureSpecFinalizer ensures that the spec is valid, adding extra info if necessary, and that the finalizer is there,
// then returns if it needs to be updated. // then returns if it needs to be updated.
func ensureSpecFinalizer(drp *webservicesv1a1.DrupalSite, log logr.Logger) (update bool) { func ensureSpecFinalizer(drp *webservicesv1a1.DrupalSite, log logr.Logger) (update bool) {
if !contains(drp.GetFinalizers(), finalizerStr) { if !controllerutil.ContainsFinalizer(drp, finalizerStr) {
drp.SetFinalizers(append(drp.GetFinalizers(), finalizerStr)) log.Info("Adding finalizer")
controllerutil.AddFinalizer(drp, finalizerStr)
update = true update = true
} }
update, specErr := assignRouterShard(drp) update, specErr := assignRouterShard(drp)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment