Use Go 1.21 min/max builtins

The `min` and `max` builtins are available since Go 1.21[^1]. The
top-level go.mod file is specifying Go 1.22, so the builtins can be
used.

[^1]: https://go.dev/doc/go1.21#language
This commit is contained in:
Tobias Klauser 2024-08-07 10:40:26 +02:00
parent 453efd7a4b
commit b01b016668
No known key found for this signature in database
GPG Key ID: 6F5040074CCC0D04
3 changed files with 10 additions and 29 deletions

View File

@ -1275,10 +1275,10 @@ func calculateScaleUpLimitWithScalingRules(currentReplicas int32, scaleUpEvents,
return currentReplicas // Scaling is disabled
} else if *scalingRules.SelectPolicy == autoscalingv2.MinChangePolicySelect {
result = math.MaxInt32
selectPolicyFn = min // For scaling up, the lowest change ('min' policy) produces a minimum value
selectPolicyFn = minInt32 // For scaling up, the lowest change ('min' policy) produces a minimum value
} else {
result = math.MinInt32
selectPolicyFn = max // Use the default policy otherwise to produce a highest possible change
selectPolicyFn = maxInt32 // Use the default policy otherwise to produce a highest possible change
}
for _, policy := range scalingRules.Policies {
replicasAddedInCurrentPeriod := getReplicasChangePerPeriod(policy.PeriodSeconds, scaleUpEvents)
@ -1304,10 +1304,10 @@ func calculateScaleDownLimitWithBehaviors(currentReplicas int32, scaleUpEvents,
return currentReplicas // Scaling is disabled
} else if *scalingRules.SelectPolicy == autoscalingv2.MinChangePolicySelect {
result = math.MinInt32
selectPolicyFn = max // For scaling down, the lowest change ('min' policy) produces a maximum value
selectPolicyFn = maxInt32 // For scaling down, the lowest change ('min' policy) produces a maximum value
} else {
result = math.MaxInt32
selectPolicyFn = min // Use the default policy otherwise to produce a highest possible change
selectPolicyFn = minInt32 // Use the default policy otherwise to produce a highest possible change
}
for _, policy := range scalingRules.Policies {
replicasAddedInCurrentPeriod := getReplicasChangePerPeriod(policy.PeriodSeconds, scaleUpEvents)
@ -1434,16 +1434,12 @@ func setConditionInList(inputList []autoscalingv2.HorizontalPodAutoscalerConditi
return resList
}
func max(a, b int32) int32 {
if a >= b {
return a
}
return b
// minInt32 is a wrapper around the min builtin to be used as a function value.
func minInt32(a, b int32) int32 {
return min(a, b)
}
func min(a, b int32) int32 {
if a <= b {
return a
}
return b
// maxInt32 is a wrapper around the max builtin to be used as a function value.
func maxInt32(a, b int32) int32 {
return max(a, b)
}

View File

@ -937,10 +937,3 @@ func TestFilterOut(t *testing.T) {
})
}
}
func max(i, j int) int {
if i > j {
return i
}
return j
}

View File

@ -224,14 +224,6 @@ func TestLexer(t *testing.T) {
}
}
func min(l, r int) (m int) {
m = r
if l < r {
m = l
}
return m
}
func TestLexerSequence(t *testing.T) {
testcases := []struct {
s string