Skip to content

Commit 6b98059

Browse files
authored
Merge pull request #16559 from hakman/automated-cherry-pick-of-#16556-upstream-release-1.28
Automated cherry pick of #16556: cluster-autoscaler: Fix priority expander config
2 parents 199545f + 0a72efb commit 6b98059

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: 41dc4453160d8f9ae80a1b9beb288252649912296ca56dc3f70dda6cf3cbfaa1
44+
manifestHash: 8170c20f6d8184dff873231d153b5f6d93f5d8dd75f739963f6d091b6b7f2187
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: 9b04006b3916dbc0e5fe22f2a23a1fa72d1665e12fe1b96f4cd7707cb823f7d7
44+
manifestHash: f63778b4b540b89fff019defd1eec7e2320233b6eb54b46897a00589f3fceb92
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
@@ -270,8 +270,7 @@ metadata:
270270
k8s-addon: cluster-autoscaler.addons.k8s.io
271271
k8s-app: cluster-autoscaler
272272
data:
273-
priorities: |-
274-
{{- ClusterAutoscalerPriorities | ToYAML | nindent 4 }}
273+
priorities: |- {{ ClusterAutoscalerPriorities | nindent 4 }}
275274
{{ end }}
276275
---
277276
# Source: cluster-autoscaler/templates/deployment.yaml

upup/pkg/fi/cloudup/template_functions.go

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

@@ -337,19 +338,26 @@ func (tf *TemplateFunctions) AddTo(dest template.FuncMap, secretStore fi.SecretS
337338
dest["UseServiceAccountExternalPermissions"] = tf.UseServiceAccountExternalPermissions
338339

339340
if cluster.Spec.ClusterAutoscaler != nil {
340-
dest["ClusterAutoscalerPriorities"] = func() map[string][]string {
341+
dest["ClusterAutoscalerPriorities"] = func() string {
341342
priorities := make(map[string][]string)
342343
if cluster.Spec.ClusterAutoscaler.CustomPriorityExpanderConfig != nil {
343344
priorities = cluster.Spec.ClusterAutoscaler.CustomPriorityExpanderConfig
344345
} else {
345346
for name, spec := range tf.GetNodeInstanceGroups() {
346-
347347
if spec.Autoscale != nil {
348-
priorities[fmt.Sprint(spec.AutoscalePriority)] = append(priorities[fmt.Sprint(spec.AutoscalePriority)], fmt.Sprintf("%s.%s", name, tf.ClusterName()))
348+
priorities[strconv.Itoa(int(spec.AutoscalePriority))] = append(priorities[strconv.Itoa(int(spec.AutoscalePriority))], fmt.Sprintf("%s.%s", name, tf.ClusterName()))
349349
}
350350
}
351351
}
352-
return priorities
352+
353+
var prioritiesStr []string
354+
for _, prio := range maps.SortedKeys(priorities) {
355+
prioritiesStr = append(prioritiesStr, fmt.Sprintf("%s:", prio))
356+
for _, value := range priorities[prio] {
357+
prioritiesStr = append(prioritiesStr, fmt.Sprintf("- %s", value))
358+
}
359+
}
360+
return strings.Join(prioritiesStr, "\n")
353361
}
354362
dest["CreateClusterAutoscalerPriorityConfig"] = func() bool {
355363
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)