Skip to content

Commit

Permalink
Use Go 1.21 min/max builtins
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tklauser committed May 23, 2024
1 parent c9a1a0a commit d14d6ec
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 29 deletions.
24 changes: 10 additions & 14 deletions pkg/controller/podautoscaler/horizontal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 = maxInt32 // Use the default policy otherwise to produce a highest possible change
}
for _, policy := range scalingRules.Policies {
replicasAddedInCurrentPeriod := getReplicasChangePerPeriod(policy.PeriodSeconds, scaleUpEvents)
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -937,10 +937,3 @@ func TestFilterOut(t *testing.T) {
})
}
}

func max(i, j int) int {
if i > j {
return i
}
return j
}
8 changes: 0 additions & 8 deletions staging/src/k8s.io/apimachinery/pkg/labels/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d14d6ec

Please sign in to comment.