diff --git a/controllers/hpa.go b/controllers/hpa.go index 33548899fed..1fd645f9324 100644 --- a/controllers/hpa.go +++ b/controllers/hpa.go @@ -3,6 +3,7 @@ package controllers import ( "context" "fmt" + "sort" "github.com/go-logr/logr" version "github.com/kedacore/keda/v2/version" @@ -161,6 +162,12 @@ func (r *ScaledObjectReconciler) getScaledObjectMetricSpecs(logger logr.Logger, scaler.Close() } + // sort metrics in ScaledObject, this way we always check the same resource in Reconcile loop and we can prevent unnecessary HPA updates, + // see https://github.com/kedacore/keda/issues/1531 for details + sort.Slice(scaledObjectMetricSpecs, func(i, j int) bool { + return scaledObjectMetricSpecs[i].Type < scaledObjectMetricSpecs[j].Type + }) + // store External.MetricNames,Resource.MetricsNames used by scalers defined in the ScaledObject status := scaledObject.Status.DeepCopy() status.ExternalMetricNames = externalMetricNames