From 7f0abef295086875956b2e43c2b7a96b3f503acc Mon Sep 17 00:00:00 2001 From: Omer Aplatony Date: Wed, 28 Aug 2024 22:54:15 +0300 Subject: [PATCH 1/2] Fix replicaCount calculation exceeding max int32 Signed-off-by: Omer Aplatony --- pkg/controller/podautoscaler/replica_calculator.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/controller/podautoscaler/replica_calculator.go b/pkg/controller/podautoscaler/replica_calculator.go index 1acd9c439e803..e54e65c977a1f 100644 --- a/pkg/controller/podautoscaler/replica_calculator.go +++ b/pkg/controller/podautoscaler/replica_calculator.go @@ -275,7 +275,14 @@ func (c *ReplicaCalculator) getUsageRatioReplicaCount(currentReplicas int32, usa if err != nil { return 0, time.Time{}, fmt.Errorf("unable to calculate ready pods: %s", err) } - replicaCount = int32(math.Ceil(usageRatio * float64(readyPodCount))) + // Calculate replicaCount as float64 first + replicaCountFloat := usageRatio * float64(readyPodCount) + // Check if replicaCount exceeds max int32 + if replicaCountFloat > math.MaxInt32 { + replicaCount = math.MaxInt32 + } else { + replicaCount = int32(math.Ceil(replicaCountFloat)) + } } else { // Scale to zero or n pods depending on usageRatio replicaCount = int32(math.Ceil(usageRatio)) From d619e52e2fb5dab0f91db31a0de6b007805f466d Mon Sep 17 00:00:00 2001 From: Omer Aplatony Date: Thu, 29 Aug 2024 07:11:04 +0300 Subject: [PATCH 2/2] Add test for scaling up with overflow Signed-off-by: Omer Aplatony --- .../podautoscaler/replica_calculator_test.go | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pkg/controller/podautoscaler/replica_calculator_test.go b/pkg/controller/podautoscaler/replica_calculator_test.go index 81d5a98139ac4..db31ab31b33f6 100644 --- a/pkg/controller/podautoscaler/replica_calculator_test.go +++ b/pkg/controller/podautoscaler/replica_calculator_test.go @@ -513,6 +513,26 @@ func TestReplicaCalcScaleUpUnreadyLessScale(t *testing.T) { tc.runTest(t) } +func TestReplicaCalcScaleUpOverflow(t *testing.T) { + tc := replicaCalcTestCase{ + currentReplicas: 3, + expectedReplicas: math.MaxInt32, + metric: &metricInfo{ + name: "qps", + levels: []int64{math.MaxInt64}, // Use MaxInt64 to ensure a very large value + targetUsage: 1, // Set a very low target to force high scaling + metricType: objectMetric, + expectedUsage: math.MaxInt64, + singleObject: &autoscalingv2.CrossVersionObjectReference{ + Kind: "Deployment", + APIVersion: "apps/v1", + Name: "some-deployment", + }, + }, + } + tc.runTest(t) +} + func TestReplicaCalcScaleUpContainerHotCpuLessScale(t *testing.T) { tc := replicaCalcTestCase{ currentReplicas: 3, pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy