From dbd0d566edbf9708a569ade4deca3b158e42c2ac Mon Sep 17 00:00:00 2001 From: shibataka000 Date: Wed, 25 Mar 2020 15:36:45 +0900 Subject: [PATCH] Fix bug about unintentional scale out during updating deployment. This commit fix bug in calcPlainMetricReplicas function. Same bug in GetResourceReplicas function was fixed in #85027. --- .../podautoscaler/replica_calculator.go | 8 +++++++- .../podautoscaler/replica_calculator_test.go | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/pkg/controller/podautoscaler/replica_calculator.go b/pkg/controller/podautoscaler/replica_calculator.go index 02b15c9add4..40f7f084f1d 100644 --- a/pkg/controller/podautoscaler/replica_calculator.go +++ b/pkg/controller/podautoscaler/replica_calculator.go @@ -235,9 +235,15 @@ func (c *ReplicaCalculator) calcPlainMetricReplicas(metrics metricsclient.PodMet return currentReplicas, utilization, nil } + newReplicas := int32(math.Ceil(newUsageRatio * float64(len(metrics)))) + if (newUsageRatio < 1.0 && newReplicas > currentReplicas) || (newUsageRatio > 1.0 && newReplicas < currentReplicas) { + // return the current replicas if the change of metrics length would cause a change in scale direction + return currentReplicas, utilization, nil + } + // return the result, where the number of replicas considered is // however many replicas factored into our calculation - return int32(math.Ceil(newUsageRatio * float64(len(metrics)))), utilization, nil + return newReplicas, utilization, nil } // GetObjectMetricReplicas calculates the desired replica count based on a target metric utilization (as a milli-value) diff --git a/pkg/controller/podautoscaler/replica_calculator_test.go b/pkg/controller/podautoscaler/replica_calculator_test.go index 7fd85ab88b4..1f22cef6083 100644 --- a/pkg/controller/podautoscaler/replica_calculator_test.go +++ b/pkg/controller/podautoscaler/replica_calculator_test.go @@ -1265,6 +1265,22 @@ func TestReplicaCalcDuringRollingUpdateWithMaxSurge(t *testing.T) { tc.runTest(t) } +func TestReplicaCalcDuringRollingUpdateWithMaxSurgeCM(t *testing.T) { + tc := replicaCalcTestCase{ + currentReplicas: 2, + expectedReplicas: 2, + podPhase: []v1.PodPhase{v1.PodRunning, v1.PodRunning, v1.PodRunning}, + metric: &metricInfo{ + name: "qps", + levels: []int64{10000, 10000}, + targetUtilization: 17000, + expectedUtilization: 10000, + metricType: podMetric, + }, + } + tc.runTest(t) +} + // TestComputedToleranceAlgImplementation is a regression test which // back-calculates a minimal percentage for downscaling based on a small percentage // increase in pod utilization which is calibrated against the tolerance value.