Skip to content

Commit 764adb9

Browse files
committed
fix: stop scale loop for pause Scaledbject (issue #4253). fix get hpa name and paused condition
Signed-off-by: Tobo Atchou <[email protected]>
1 parent 4e326f1 commit 764adb9

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

controllers/keda/hpa.go

-3
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,6 @@ func updateHealthStatus(scaledObject *kedav1alpha1.ScaledObject, externalMetricN
269269

270270
// getHPAName returns generated HPA name for ScaledObject specified in the parameter
271271
func getHPAName(scaledObject *kedav1alpha1.ScaledObject) string {
272-
if scaledObject.Status.HpaName != "" {
273-
return scaledObject.Status.HpaName
274-
}
275272
if scaledObject.Spec.Advanced != nil && scaledObject.Spec.Advanced.HorizontalPodAutoscalerConfig != nil && scaledObject.Spec.Advanced.HorizontalPodAutoscalerConfig.Name != "" {
276273
return scaledObject.Spec.Advanced.HorizontalPodAutoscalerConfig.Name
277274
}

controllers/keda/scaledobject_controller.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ func (r *ScaledObjectReconciler) Reconcile(ctx context.Context, req ctrl.Request
172172
}
173173
}
174174

175-
// reconcile ScaledObject and set status appropriately
176-
msg, err := r.reconcileScaledObject(ctx, reqLogger, scaledObject)
177175
conditions := scaledObject.Status.Conditions.DeepCopy()
176+
// reconcile ScaledObject and set status appropriately
177+
msg, err := r.reconcileScaledObject(ctx, reqLogger, scaledObject, &conditions)
178178
if err != nil {
179179
reqLogger.Error(err, msg)
180180
conditions.SetReadyCondition(metav1.ConditionFalse, "ScaledObjectCheckFailed", msg)
@@ -197,14 +197,13 @@ func (r *ScaledObjectReconciler) Reconcile(ctx context.Context, req ctrl.Request
197197
}
198198

199199
// reconcileScaledObject implements reconciler logic for ScaledObject
200-
func (r *ScaledObjectReconciler) reconcileScaledObject(ctx context.Context, logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject) (string, error) {
200+
func (r *ScaledObjectReconciler) reconcileScaledObject(ctx context.Context, logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject, conditions *kedav1alpha1.Conditions) (string, error) {
201201
// Check the presence of "autoscaling.keda.sh/paused-replicas" annotation on the scaledObject (since the presence of this annotation will pause
202202
// autoscaling no matter what number of replicas is provided), and if so, stop the scale loop and delete the HPA on the scaled object.
203203
_, paused := scaledObject.GetAnnotations()[kedacontrollerutil.PausedReplicasAnnotation]
204204
if paused {
205205
logger.Info("ScaledObject is paused, so skipping the request.")
206206
msg := kedav1alpha1.ScaledObjectConditionPausedMessage
207-
conditions := scaledObject.Status.Conditions.DeepCopy()
208207
if err := r.stopScaleLoop(ctx, logger, scaledObject); err != nil {
209208
msg = "failed to stop the scale loop for paused ScaledObject"
210209
conditions.SetPausedCondition(metav1.ConditionFalse, "ScaledObjectStopScaleLoopFailed", msg)
@@ -416,7 +415,7 @@ func (r *ScaledObjectReconciler) checkReplicaCountBoundsAreValid(scaledObject *k
416415

417416
// ensureHPAForScaledObjectExists ensures that in cluster exist up-to-date HPA for specified ScaledObject, returns true if a new HPA was created
418417
func (r *ScaledObjectReconciler) ensureHPAForScaledObjectExists(ctx context.Context, logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject, gvkr *kedav1alpha1.GroupVersionKindResource) (bool, error) {
419-
hpaName := getHPAName(scaledObject)
418+
hpaName := getHPANameOnEnsure(scaledObject)
420419
foundHpa := &autoscalingv2.HorizontalPodAutoscaler{}
421420
// Check if HPA for this ScaledObject already exists
422421
err := r.Client.Get(ctx, types.NamespacedName{Name: hpaName, Namespace: scaledObject.Namespace}, foundHpa)
@@ -456,7 +455,7 @@ func (r *ScaledObjectReconciler) ensureHPAForScaledObjectExists(ctx context.Cont
456455

457456
// ensureHPAForScaledObjectIsDeleted ensures that in cluster any HPA for specified ScaledObject is deleted, returns true if no HPA exists
458457
func (r *ScaledObjectReconciler) ensureHPAForScaledObjectIsDeleted(ctx context.Context, logger logr.Logger, scaledObject *kedav1alpha1.ScaledObject) (bool, error) {
459-
hpaName := getHPAName(scaledObject)
458+
hpaName := getHPANameOnEnsure(scaledObject)
460459
foundHpa := &autoscalingv2.HorizontalPodAutoscaler{}
461460
// Check if HPA for this ScaledObject already exists
462461
err := r.Client.Get(ctx, types.NamespacedName{Name: hpaName, Namespace: scaledObject.Namespace}, foundHpa)
@@ -474,6 +473,13 @@ func (r *ScaledObjectReconciler) ensureHPAForScaledObjectIsDeleted(ctx context.C
474473
return true, nil
475474
}
476475

476+
func getHPANameOnEnsure(scaledObject *kedav1alpha1.ScaledObject) string {
477+
if scaledObject.Status.HpaName != "" {
478+
return scaledObject.Status.HpaName
479+
}
480+
return getHPAName(scaledObject)
481+
}
482+
477483
func isHpaRenamed(scaledObject *kedav1alpha1.ScaledObject, foundHpa *autoscalingv2.HorizontalPodAutoscaler) bool {
478484
// if HPA name defined in SO -> check if equals to the found HPA
479485
if scaledObject.Spec.Advanced != nil && scaledObject.Spec.Advanced.HorizontalPodAutoscalerConfig != nil && scaledObject.Spec.Advanced.HorizontalPodAutoscalerConfig.Name != "" {

0 commit comments

Comments
 (0)