Skip to content

Commit a13f043

Browse files
authored
Merge pull request #16558 from hakman/automated-cherry-pick-of-#16556-upstream-release-1.29
Automated cherry pick of #16556: cluster-autoscaler: Fix priority expander config
2 parents 75774b9 + ce6f57f commit a13f043

7 files changed

+32
-32
lines changed

tests/integration/update_cluster/cluster-autoscaler-priority-expander-custom/data/aws_s3_object_cas-priority-expander-custom.example.com-addons-bootstrap_content

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ spec:
4141
version: 9.99.0
4242
- id: k8s-1.15
4343
manifest: cluster-autoscaler.addons.k8s.io/k8s-1.15.yaml
44-
manifestHash: b0c764405add27350d8642bbb30352457acd9b7f223cff119a62bb18773fe2e3
44+
manifestHash: 963767d82e85200787e166f3cc6e7b3b9e60d2383c9fef71a562d9e4eedd7086
4545
name: cluster-autoscaler.addons.k8s.io
4646
selector:
4747
k8s-addon: cluster-autoscaler.addons.k8s.io

tests/integration/update_cluster/cluster-autoscaler-priority-expander-custom/data/aws_s3_object_cas-priority-expander-custom.example.com-addons-cluster-autoscaler.addons.k8s.io-k8s-1.15_content

+4-4
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,12 @@ spec:
282282
apiVersion: v1
283283
data:
284284
priorities: |-
285-
"0":
285+
0:
286286
- .*
287-
"50":
288-
- .*low.*
289-
"100":
287+
100:
290288
- .*high.*
289+
50:
290+
- .*low.*
291291
kind: ConfigMap
292292
metadata:
293293
creationTimestamp: null

tests/integration/update_cluster/cluster-autoscaler-priority-expander/data/aws_s3_object_cas-priority-expander.example.com-addons-bootstrap_content

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ spec:
4141
version: 9.99.0
4242
- id: k8s-1.15
4343
manifest: cluster-autoscaler.addons.k8s.io/k8s-1.15.yaml
44-
manifestHash: c4557839138e0ff31d1f98ed22223c98cb99170cf838929e4141ac924758314a
44+
manifestHash: d28605db2e6ade6fee371d462c0e9893871ea73a35ad598fa616a2657290d319
4545
name: cluster-autoscaler.addons.k8s.io
4646
selector:
4747
k8s-addon: cluster-autoscaler.addons.k8s.io

tests/integration/update_cluster/cluster-autoscaler-priority-expander/data/aws_s3_object_cas-priority-expander.example.com-addons-cluster-autoscaler.addons.k8s.io-k8s-1.15_content

+4-4
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,12 @@ spec:
282282
apiVersion: v1
283283
data:
284284
priorities: |-
285-
"0":
285+
0:
286286
- nodes.cas-priority-expander.example.com
287-
"50":
288-
- nodes-low-priority.cas-priority-expander.example.com
289-
"100":
287+
100:
290288
- nodes-high-priority.cas-priority-expander.example.com
289+
50:
290+
- nodes-low-priority.cas-priority-expander.example.com
291291
kind: ConfigMap
292292
metadata:
293293
creationTimestamp: null

upup/models/cloudup/resources/addons/cluster-autoscaler.addons.k8s.io/k8s-1.15.yaml.template

+1-2
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,7 @@ metadata:
266266
k8s-addon: cluster-autoscaler.addons.k8s.io
267267
k8s-app: cluster-autoscaler
268268
data:
269-
priorities: |-
270-
{{- ClusterAutoscalerPriorities | ToYAML | nindent 4 }}
269+
priorities: |- {{ ClusterAutoscalerPriorities | nindent 4 }}
271270
{{ end }}
272271
---
273272
# Source: cluster-autoscaler/templates/deployment.yaml

upup/pkg/fi/cloudup/template_functions.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import (
6969
"k8s.io/kops/upup/pkg/fi/cloudup/openstack"
7070
"k8s.io/kops/upup/pkg/fi/cloudup/scaleway"
7171
"k8s.io/kops/util/pkg/env"
72+
"k8s.io/kops/util/pkg/maps"
7273
"sigs.k8s.io/yaml"
7374
)
7475

@@ -339,19 +340,26 @@ func (tf *TemplateFunctions) AddTo(dest template.FuncMap, secretStore fi.SecretS
339340
dest["UseServiceAccountExternalPermissions"] = tf.UseServiceAccountExternalPermissions
340341

341342
if cluster.Spec.ClusterAutoscaler != nil {
342-
dest["ClusterAutoscalerPriorities"] = func() map[string][]string {
343+
dest["ClusterAutoscalerPriorities"] = func() string {
343344
priorities := make(map[string][]string)
344345
if cluster.Spec.ClusterAutoscaler.CustomPriorityExpanderConfig != nil {
345346
priorities = cluster.Spec.ClusterAutoscaler.CustomPriorityExpanderConfig
346347
} else {
347348
for name, spec := range tf.GetNodeInstanceGroups() {
348-
349349
if spec.Autoscale != nil {
350-
priorities[fmt.Sprint(spec.AutoscalePriority)] = append(priorities[fmt.Sprint(spec.AutoscalePriority)], fmt.Sprintf("%s.%s", name, tf.ClusterName()))
350+
priorities[strconv.Itoa(int(spec.AutoscalePriority))] = append(priorities[strconv.Itoa(int(spec.AutoscalePriority))], fmt.Sprintf("%s.%s", name, tf.ClusterName()))
351351
}
352352
}
353353
}
354-
return priorities
354+
355+
var prioritiesStr []string
356+
for _, prio := range maps.SortedKeys(priorities) {
357+
prioritiesStr = append(prioritiesStr, fmt.Sprintf("%s:", prio))
358+
for _, value := range priorities[prio] {
359+
prioritiesStr = append(prioritiesStr, fmt.Sprintf("- %s", value))
360+
}
361+
}
362+
return strings.Join(prioritiesStr, "\n")
355363
}
356364
dest["CreateClusterAutoscalerPriorityConfig"] = func() bool {
357365
return fi.ValueOf(cluster.Spec.ClusterAutoscaler.CreatePriorityExpenderConfig)

util/pkg/maps/maps.go

+9-16
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,21 @@ limitations under the License.
1717
package maps
1818

1919
import (
20-
"reflect"
21-
"sort"
20+
"cmp"
21+
"slices"
22+
23+
"golang.org/x/exp/maps"
2224
)
2325

2426
// Keys returns the keys of a map
25-
func Keys(m interface{}) []string {
26-
var list []string
27-
28-
v := reflect.ValueOf(m)
29-
if v.Kind() == reflect.Map {
30-
for _, x := range v.MapKeys() {
31-
list = append(list, x.String())
32-
}
33-
}
34-
35-
return list
27+
func Keys[M ~map[K]V, K comparable, V any](m M) []K {
28+
return maps.Keys(m)
3629
}
3730

3831
// SortedKeys returns a list of sorted keys
39-
func SortedKeys(m interface{}) []string {
40-
list := Keys(m)
41-
sort.Strings(list)
32+
func SortedKeys[M ~map[K]V, K cmp.Ordered, V any](m M) []K {
33+
list := maps.Keys(m)
34+
slices.Sort(list)
4235

4336
return list
4437
}

0 commit comments

Comments
 (0)