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)) 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,
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: