Skip to content

Fix replicaCount calculation exceeding max int32 #126979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pkg/controller/podautoscaler/replica_calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
20 changes: 20 additions & 0 deletions pkg/controller/podautoscaler/replica_calculator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
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