Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Provide support for explicitly pausing autoscaling of ScaledJobs #3828

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ To learn more about active deprecations, we recommend checking [GitHub Discussio
- [v1.2.0](#v120)
- [v1.1.0](#v110)
- [v1.0.0](#v100)
- **General:** Introduce autoscaling is paused annotation for ScaledJobs ([#3303](https://github.com/kedacore/keda/issues/3303))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this to new section, just above - General: Introduce new Loki Scaler


### New

Expand Down
2 changes: 2 additions & 0 deletions apis/keda/v1alpha1/scaledjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ type ScaledJobStatus struct {
LastActiveTime *metav1.Time `json:"lastActiveTime,omitempty"`
// +optional
Conditions Conditions `json:"conditions,omitempty"`
// +optional
Paused string `json:"isPaused,omitempty"`
}

// ScaledJobList contains a list of ScaledJob
Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/keda.sh_scaledjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7743,6 +7743,8 @@ spec:
lastActiveTime:
format: date-time
type: string
Paused:
type: string
type: object
type: object
served: true
Expand Down
21 changes: 20 additions & 1 deletion controllers/keda/scaledjob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ func (r *ScaledJobReconciler) SetupWithManager(mgr ctrl.Manager, options control
WithOptions(options).
// Ignore updates to ScaledJob Status (in this case metadata.Generation does not change)
// so reconcile loop is not started on Status updates
For(&kedav1alpha1.ScaledJob{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
For(&kedav1alpha1.ScaledJob{}, builder.WithPredicates(
predicate.Or(
kedacontrollerutil.PausedPredicate{},
predicate.GenerationChangedPredicate{},
))).
Complete(r)
}

Expand Down Expand Up @@ -121,6 +125,11 @@ func (r *ScaledJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
return ctrl.Result{}, err
}
}
// check if scaledJob.Status.IsPaused constant is not an empty string, if not an empty string then stopScaleLoop
if scaledJob.Status.Paused != "" {
reqLogger.Info("ScaledJob is paused, stopping scale loop")
return ctrl.Result{}, r.Pause(ctx, reqLogger, scaledJob)
}

// Check jobTargetRef is specified
if scaledJob.Spec.JobTargetRef == nil {
Expand Down Expand Up @@ -322,3 +331,13 @@ func (r *ScaledJobReconciler) updateTriggerTotalsOnDelete(namespacedName string)

delete(scaledJobTriggers, namespacedName)
}
//stopScaleLoop when scaledJob.Status.IsPaused constant is set
func (r *ScaledJobReconciler) Pause(ctx context.Context, logger logr.Logger, scaledJob *kedav1alpha1.ScaledJob) error {
if scaledJob.Annotations["scaledjob.keda.sh/paused"] == "true" {
logger.V(1).Info("Stopping a ScaleLoop when scaledJob is paused")
// stopScaleLoop
return r.stopScaleLoop(ctx, logger, scaledJob)
//return r.scaleHandler.DeleteScalableObject(ctx, scaledJob)
Comment on lines +338 to +340
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we delete these comments?

}
return nil
}
22 changes: 22 additions & 0 deletions controllers/keda/util/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

const PausedReplicasAnnotation = "autoscaling.keda.sh/paused-replicas"

const Paused = "autoscaling.keda.sh/paused"

type PausedReplicasPredicate struct {
predicate.Funcs
}
Expand Down Expand Up @@ -61,3 +63,23 @@ func (ScaleObjectReadyConditionPredicate) Update(e event.UpdateEvent) bool {

return false
}
type PausedPredicate struct {
predicate.Funcs
}
func (PausedPredicate) Update(e event.UpdateEvent) bool {
if e.ObjectOld == nil || e.ObjectNew == nil {
return false
}

newAnnotations := e.ObjectNew.GetAnnotations()
oldAnnotations := e.ObjectOld.GetAnnotations()
if newAnnotations != nil && oldAnnotations != nil {
if newVal, ok1 := newAnnotations[Paused]; ok1 {
if oldVal, ok2 := oldAnnotations[Paused]; ok2 {
return newVal != oldVal
}
return true
}
}
return false
}
7 changes: 7 additions & 0 deletions pkg/scaling/executor/scale_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,10 @@ func min(x, y int64) int64 {
}
return x
}
//check if scaledJob has Paused annotation, if it does, pause the scaledjob
func (e *scaleExecutor) Pause(ctx context.Context, logger logr.Logger, scaledJob *kedav1alpha1.ScaledJob) error {
if scaledJob.Annotations["scaledjob.keda.sh/paused"] == "true" {
return e.client.Status().Update(ctx, scaledJob)
}
return nil
}