Skip to content

Commit

Permalink
update the code to add/remove finalizer to dbaasplatform CR
Browse files Browse the repository at this point in the history
  • Loading branch information
xieshenzh committed Dec 15, 2021
1 parent 4821de9 commit 0287819
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions controllers/dbaasplatform_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ import (
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"

dbaasv1alpha1 "github.com/RHEcosystemAppEng/dbaas-operator/api/v1alpha1"
)

const (
DBaaSPlatformFinalizer = "dbaasplaform-cleanup"
DBaaSPlatformFinalizer = "dbaasplatform.dbaas.redhat.com/finalizer"
RequeueDelaySuccess = 10 * time.Second
RequeueDelayError = 5 * time.Second
)
Expand Down Expand Up @@ -90,10 +91,12 @@ func (r *DBaaSPlatformReconciler) Reconcile(ctx context.Context, req ctrl.Reques
return ctrl.Result{}, err
}
// Add a cleanup finalizer if not already present
if cr.DeletionTimestamp == nil && len(cr.Finalizers) == 0 {
cr.Finalizers = append(cr.Finalizers, DBaaSPlatformFinalizer)
err = r.Update(ctx, cr)
return ctrl.Result{}, err
if cr.DeletionTimestamp.IsZero() {
if !contains(cr.GetFinalizers(), DBaaSPlatformFinalizer) {
controllerutil.AddFinalizer(cr, DBaaSPlatformFinalizer)
err = r.Update(ctx, cr)
return ctrl.Result{}, err
}
}
var finished = true

Expand Down Expand Up @@ -149,12 +152,14 @@ func (r *DBaaSPlatformReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}
// Ready for deletion?
// Only remove the finalizer when all platforms were successful
if cr.DeletionTimestamp != nil && finished {
if !cr.DeletionTimestamp.IsZero() && finished {
log.Info("cleanup platforms complete, removing finalizer")
cr.Finalizers = []string{}
err = r.Update(ctx, cr)
r.installComplete = false
return ctrl.Result{}, err
if contains(cr.GetFinalizers(), DBaaSPlatformFinalizer) {
controllerutil.RemoveFinalizer(cr, DBaaSPlatformFinalizer)
err = r.Update(ctx, cr)
r.installComplete = false
return ctrl.Result{}, err
}
}

return r.updateStatus(cr, nextStatus)
Expand Down

0 comments on commit 0287819

Please sign in to comment.