Commit 47089157 authored by Vineet Reddy Rajula's avatar Vineet Reddy Rajula
Browse files

Merge branch '37-finalizer-cleanup' into 'master'

Cleanup only if finalizer present

Closes #37

See merge request !24
parents 5e25a1aa f52de83f
Pipeline #2310608 failed with stages
in 3 minutes and 22 seconds
......@@ -23,3 +23,6 @@ bin
*.swo
*~
.vscode
__*
secret.yaml
......@@ -33,6 +33,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
......@@ -93,7 +94,10 @@ func (r *DrupalSiteReconciler) Reconcile(ctx context.Context, req ctrl.Request)
//Handle deletion
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) {
......@@ -156,18 +160,8 @@ func (r *DrupalSiteReconciler) Reconcile(ctx context.Context, req ctrl.Request)
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
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
// 1. check if such resources exist
// - delete them
......@@ -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
// TODO
log.Info("Deleting DrupalSite")
remainingFinalizers := app.GetFinalizers()
for i, finalizer := range remainingFinalizers {
if finalizer == finalizerStr {
remainingFinalizers = append(remainingFinalizers[:i], remainingFinalizers[i+1:]...)
break
}
}
app.SetFinalizers(remainingFinalizers)
return r.updateCRorFailReconcile(ctx, log, app)
controllerutil.RemoveFinalizer(drp, finalizerStr)
return r.updateCRorFailReconcile(ctx, log, drp)
}
func setReady(drp *webservicesv1a1.DrupalSite) (update bool) {
......
......@@ -41,6 +41,7 @@ import (
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
......@@ -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,
// then returns if it needs to be updated.
func ensureSpecFinalizer(drp *webservicesv1a1.DrupalSite, log logr.Logger) (update bool) {
if !contains(drp.GetFinalizers(), finalizerStr) {
drp.SetFinalizers(append(drp.GetFinalizers(), finalizerStr))
if !controllerutil.ContainsFinalizer(drp, finalizerStr) {
log.Info("Adding finalizer")
controllerutil.AddFinalizer(drp, finalizerStr)
update = true
}
update, specErr := assignRouterShard(drp)
......
Markdown is supported
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