From 3c4512c6ccca066d590a33b6333198b5ed813da2 Mon Sep 17 00:00:00 2001 From: Josselin Costanzi Date: Fri, 6 Oct 2023 21:21:01 +0000 Subject: [PATCH] hpa: always update status metrics when updating the replica count Have hpa always update both the metrics and replica count. This fix an edge case behavior bug where the metrics would not be updated if a custom metrics was unavailable. --- pkg/controller/podautoscaler/horizontal.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/controller/podautoscaler/horizontal.go b/pkg/controller/podautoscaler/horizontal.go index a814a6c8069..282d54462ce 100644 --- a/pkg/controller/podautoscaler/horizontal.go +++ b/pkg/controller/podautoscaler/horizontal.go @@ -836,7 +836,7 @@ func (a *HorizontalController) reconcileAutoscaler(ctx context.Context, hpaShare // computeReplicasForMetrics may return both non-zero metricDesiredReplicas and an error. // That means some metrics still work and HPA should perform scaling based on them. if err != nil && metricDesiredReplicas == -1 { - a.setCurrentReplicasInStatus(hpa, currentReplicas) + a.setCurrentReplicasAndMetricsInStatus(hpa, currentReplicas, metricStatuses) if err := a.updateStatusIfNeeded(ctx, hpaStatusOriginal, hpa); err != nil { utilruntime.HandleError(err) } @@ -879,7 +879,7 @@ func (a *HorizontalController) reconcileAutoscaler(ctx context.Context, hpaShare if err != nil { a.eventRecorder.Eventf(hpa, v1.EventTypeWarning, "FailedRescale", "New size: %d; reason: %s; error: %v", desiredReplicas, rescaleReason, err.Error()) setCondition(hpa, autoscalingv2.AbleToScale, v1.ConditionFalse, "FailedUpdateScale", "the HPA controller was unable to update the target scale: %v", err) - a.setCurrentReplicasInStatus(hpa, currentReplicas) + a.setCurrentReplicasAndMetricsInStatus(hpa, currentReplicas, metricStatuses) if err := a.updateStatusIfNeeded(ctx, hpaStatusOriginal, hpa); err != nil { utilruntime.HandleError(err) } @@ -1357,9 +1357,9 @@ func (a *HorizontalController) scaleForResourceMappings(ctx context.Context, nam return nil, schema.GroupResource{}, firstErr } -// setCurrentReplicasInStatus sets the current replica count in the status of the HPA. -func (a *HorizontalController) setCurrentReplicasInStatus(hpa *autoscalingv2.HorizontalPodAutoscaler, currentReplicas int32) { - a.setStatus(hpa, currentReplicas, hpa.Status.DesiredReplicas, hpa.Status.CurrentMetrics, false) +// setCurrentReplicasAndMetricsInStatus sets the current replica count and metrics in the status of the HPA. +func (a *HorizontalController) setCurrentReplicasAndMetricsInStatus(hpa *autoscalingv2.HorizontalPodAutoscaler, currentReplicas int32, metricStatuses []autoscalingv2.MetricStatus) { + a.setStatus(hpa, currentReplicas, hpa.Status.DesiredReplicas, metricStatuses, false) } // setStatus recreates the status of the given HPA, updating the current and