forked from kedacore/keda
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding ScaledJob finalizer (kedacore#1260)
Signed-off-by: Zbynek Roubalik <[email protected]>
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |