Skip to content

Commit

Permalink
fix: unnecessary HPA updates when cpu utilization trigger is used
Browse files Browse the repository at this point in the history
  • Loading branch information
DuanYunpeng committed May 23, 2024
1 parent 4fa1079 commit b6362fc
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion controllers/keda/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,19 @@ func (r *ScaledObjectReconciler) getScaledObjectMetricSpecs(ctx context.Context,
}

metricSpecs := cache.GetMetricSpecForScaling(ctx)
var mSpecs []autoscalingv2.MetricSpec
var cpuUtilizationMetric int

for _, metricSpec := range metricSpecs {
if metricSpec.Resource != nil {
target := metricSpec.Resource.Target
if target.Type == autoscalingv2.UtilizationMetricType && string(metricSpec.Resource.Name) == "cpu" {
cpuUtilizationMetric++
if cpuUtilizationMetric > 1 {
continue
}
}

resourceMetricNames = append(resourceMetricNames, string(metricSpec.Resource.Name))
}

Expand All @@ -238,8 +248,9 @@ func (r *ScaledObjectReconciler) getScaledObjectMetricSpecs(ctx context.Context,
metricSpec.External.Metric.Selector.MatchLabels[kedav1alpha1.ScaledObjectOwnerAnnotation] = scaledObject.Name
externalMetricNames = append(externalMetricNames, externalMetricName)
}
mSpecs = append(mSpecs, metricSpec)
}
scaledObjectMetricSpecs = append(scaledObjectMetricSpecs, metricSpecs...)
scaledObjectMetricSpecs = append(scaledObjectMetricSpecs, mSpecs...)

// 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
Expand Down

0 comments on commit b6362fc

Please sign in to comment.