Skip to content

Commit 106ee38

Browse files
authored
Merge pull request kubernetes#95647 from JoshuaAndrew/master
Horizontal Pod Autoscaler doesn`t automatically scale the number of pods correctly
2 parents 68e4f1f + b19a115 commit 106ee38

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

pkg/controller/podautoscaler/horizontal.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -998,15 +998,17 @@ func calculateScaleUpLimitWithScalingRules(currentReplicas int32, scaleEvents []
998998
if *scalingRules.SelectPolicy == autoscalingv2.DisabledPolicySelect {
999999
return currentReplicas // Scaling is disabled
10001000
} else if *scalingRules.SelectPolicy == autoscalingv2.MinPolicySelect {
1001+
result = math.MaxInt32
10011002
selectPolicyFn = min // For scaling up, the lowest change ('min' policy) produces a minimum value
10021003
} else {
1004+
result = math.MinInt32
10031005
selectPolicyFn = max // Use the default policy otherwise to produce a highest possible change
10041006
}
10051007
for _, policy := range scalingRules.Policies {
10061008
replicasAddedInCurrentPeriod := getReplicasChangePerPeriod(policy.PeriodSeconds, scaleEvents)
10071009
periodStartReplicas := currentReplicas - replicasAddedInCurrentPeriod
10081010
if policy.Type == autoscalingv2.PodsScalingPolicy {
1009-
proposed = int32(periodStartReplicas + policy.Value)
1011+
proposed = periodStartReplicas + policy.Value
10101012
} else if policy.Type == autoscalingv2.PercentScalingPolicy {
10111013
// the proposal has to be rounded up because the proposed change might not increase the replica count causing the target to never scale up
10121014
proposed = int32(math.Ceil(float64(periodStartReplicas) * (1 + float64(policy.Value)/100)))
@@ -1018,14 +1020,16 @@ func calculateScaleUpLimitWithScalingRules(currentReplicas int32, scaleEvents []
10181020

10191021
// calculateScaleDownLimitWithBehavior returns the maximum number of pods that could be deleted for the given HPAScalingRules
10201022
func calculateScaleDownLimitWithBehaviors(currentReplicas int32, scaleEvents []timestampedScaleEvent, scalingRules *autoscalingv2.HPAScalingRules) int32 {
1021-
var result int32 = math.MaxInt32
1023+
var result int32
10221024
var proposed int32
10231025
var selectPolicyFn func(int32, int32) int32
10241026
if *scalingRules.SelectPolicy == autoscalingv2.DisabledPolicySelect {
10251027
return currentReplicas // Scaling is disabled
10261028
} else if *scalingRules.SelectPolicy == autoscalingv2.MinPolicySelect {
1029+
result = math.MinInt32
10271030
selectPolicyFn = max // For scaling down, the lowest change ('min' policy) produces a maximum value
10281031
} else {
1032+
result = math.MaxInt32
10291033
selectPolicyFn = min // Use the default policy otherwise to produce a highest possible change
10301034
}
10311035
for _, policy := range scalingRules.Policies {

pkg/controller/podautoscaler/horizontal_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2993,6 +2993,50 @@ func TestConvertDesiredReplicasWithRules(t *testing.T) {
29932993
}
29942994
}
29952995

2996+
func TestCalculateScaleUpLimitWithScalingRules(t *testing.T) {
2997+
policy := autoscalingv2.MinPolicySelect
2998+
2999+
calculated := calculateScaleUpLimitWithScalingRules(1, []timestampedScaleEvent{}, &autoscalingv2.HPAScalingRules{
3000+
StabilizationWindowSeconds: utilpointer.Int32Ptr(300),
3001+
SelectPolicy: &policy,
3002+
Policies: []autoscalingv2.HPAScalingPolicy{
3003+
{
3004+
Type: autoscalingv2.PodsScalingPolicy,
3005+
Value: 2,
3006+
PeriodSeconds: 60,
3007+
},
3008+
{
3009+
Type: autoscalingv2.PercentScalingPolicy,
3010+
Value: 50,
3011+
PeriodSeconds: 60,
3012+
},
3013+
},
3014+
})
3015+
assert.Equal(t, calculated, int32(2))
3016+
}
3017+
3018+
func TestCalculateScaleDownLimitWithBehaviors(t *testing.T) {
3019+
policy := autoscalingv2.MinPolicySelect
3020+
3021+
calculated := calculateScaleDownLimitWithBehaviors(5, []timestampedScaleEvent{}, &autoscalingv2.HPAScalingRules{
3022+
StabilizationWindowSeconds: utilpointer.Int32Ptr(300),
3023+
SelectPolicy: &policy,
3024+
Policies: []autoscalingv2.HPAScalingPolicy{
3025+
{
3026+
Type: autoscalingv2.PodsScalingPolicy,
3027+
Value: 2,
3028+
PeriodSeconds: 60,
3029+
},
3030+
{
3031+
Type: autoscalingv2.PercentScalingPolicy,
3032+
Value: 50,
3033+
PeriodSeconds: 60,
3034+
},
3035+
},
3036+
})
3037+
assert.Equal(t, calculated, int32(3))
3038+
}
3039+
29963040
func generateScalingRules(pods, podsPeriod, percent, percentPeriod, stabilizationWindow int32) *autoscalingv2.HPAScalingRules {
29973041
policy := autoscalingv2.MaxPolicySelect
29983042
directionBehavior := autoscalingv2.HPAScalingRules{

0 commit comments

Comments
 (0)