Skip to content

Commit

Permalink
adding ScaledJob finalizer (kedacore#1260)
Browse files Browse the repository at this point in the history
Signed-off-by: Zbynek Roubalik <[email protected]>
  • Loading branch information
zroubalik authored Oct 15, 2020
1 parent d109453 commit 4b4e473
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
20 changes: 20 additions & 0 deletions controllers/scaledjob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ func (r *ScaledJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {

reqLogger.Info("Reconciling ScaledJob")

// Check if the ScaledJob instance is marked to be deleted, which is
// indicated by the deletion timestamp being set.
if scaledJob.GetDeletionTimestamp() != nil {
return ctrl.Result{}, r.finalizeScaledJob(reqLogger, scaledJob)
}

// ensure finalizer is set on this CR
if err := r.ensureFinalizer(reqLogger, scaledJob); err != nil {
return ctrl.Result{}, err
}

var errMsg string
if scaledJob.Spec.JobTargetRef != nil {
reqLogger.Info("Detected ScaleType = Job")
Expand Down Expand Up @@ -132,3 +143,12 @@ func (r *ScaledJobReconciler) requestScaleLoop(logger logr.Logger, scaledJob *ke

return r.scaleHandler.HandleScalableObject(scaledJob)
}

// stopScaleLoop stops ScaleLoop handler for the respective ScaledJob
func (r *ScaledJobReconciler) stopScaleLoop(scaledJob *kedav1alpha1.ScaledJob) error {
if err := r.scaleHandler.DeleteScalableObject(scaledJob); err != nil {
return err
}

return nil
}
53 changes: 53 additions & 0 deletions controllers/scaledjob_finalizer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package controllers

import (
"context"

"github.com/go-logr/logr"

kedav1alpha1 "github.com/kedacore/keda/api/v1alpha1"
"github.com/kedacore/keda/controllers/util"
)

const (
scaledJobFinalizer = "finalizer.keda.sh"
)

// finalizeScaledJob runs finalization logic on ScaledJob if there's finalizer
func (r *ScaledJobReconciler) finalizeScaledJob(logger logr.Logger, scaledJob *kedav1alpha1.ScaledJob) error {
if util.Contains(scaledJob.GetFinalizers(), scaledJobFinalizer) {
// Run finalization logic for scaledJobFinalizer. If the
// finalization logic fails, don't remove the finalizer so
// that we can retry during the next reconciliation.
if err := r.stopScaleLoop(scaledJob); err != nil {
return err
}

// Remove scaledJobFinalizer. Once all finalizers have been
// removed, the object will be deleted.
scaledJob.SetFinalizers(util.Remove(scaledJob.GetFinalizers(), scaledJobFinalizer))
if err := r.Client.Update(context.TODO(), scaledJob); err != nil {
logger.Error(err, "Failed to update ScaledJob after removing a finalizer", "finalizer", scaledJobFinalizer)
return err
}
}

logger.Info("Successfully finalized ScaledJob")
return nil
}

// ensureFinalizer check there is finalizer present on the ScaledJob, if not it adds one
func (r *ScaledJobReconciler) ensureFinalizer(logger logr.Logger, scaledJob *kedav1alpha1.ScaledJob) error {
if !util.Contains(scaledJob.GetFinalizers(), scaledJobFinalizer) {
logger.Info("Adding Finalizer for the ScaledJob")
scaledJob.SetFinalizers(append(scaledJob.GetFinalizers(), scaledJobFinalizer))

// Update CR
err := r.Client.Update(context.TODO(), scaledJob)
if err != nil {
logger.Error(err, "Failed to update ScaledJob with a finalizer", "finalizer", scaledJobFinalizer)
return err
}
}
return nil
}

0 comments on commit 4b4e473

Please sign in to comment.