diff --git a/packages/system/monitoring/templates/_scheduling.tpl b/packages/system/monitoring/templates/_scheduling.tpl index 6540d4d84..25ff369ad 100644 --- a/packages/system/monitoring/templates/_scheduling.tpl +++ b/packages/system/monitoring/templates/_scheduling.tpl @@ -4,7 +4,11 @@ {{- $global := list -}} {{- with ($root.Values._cluster | default dict).scheduling -}} {{- with get . "globalAppTopologySpreadConstraints" -}} -{{- $global = dig "topologySpreadConstraints" (list) (fromYaml .) -}} +{{- $parsed := fromYaml . -}} +{{- $global = dig "topologySpreadConstraints" (list) $parsed -}} +{{- if or (not (kindIs "slice" $global)) (not $global) -}} +{{- fail (trim (printf "monitoring: _cluster.scheduling.globalAppTopologySpreadConstraints must be YAML with a non-empty topologySpreadConstraints list. %s" (get $parsed "Error"))) -}} +{{- end -}} {{- end -}} {{- end -}} {{- $constraints := list -}} diff --git a/packages/system/monitoring/tests/topology_spread_invalid_policy_test.yaml b/packages/system/monitoring/tests/topology_spread_invalid_policy_test.yaml new file mode 100644 index 000000000..6558f6764 --- /dev/null +++ b/packages/system/monitoring/tests/topology_spread_invalid_policy_test.yaml @@ -0,0 +1,59 @@ +suite: monitoring refuses a malformed platform spread policy +templates: + - templates/vm/vmcluster.yaml + +release: + name: monitoring-system + namespace: tenant-root + +tests: + - it: fails on a platform policy that does not parse + template: templates/vm/vmcluster.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + topologySpreadConstraints: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + asserts: + - failedTemplate: + errorPattern: "globalAppTopologySpreadConstraints must be YAML with a non-empty topologySpreadConstraints list\\. error converting YAML to JSON" + + - it: fails on a platform policy without a topologySpreadConstraints list + template: templates/vm/vmcluster.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: ScheduleAnyway + asserts: + - failedTemplate: + errorPattern: "globalAppTopologySpreadConstraints must be YAML with a non-empty topologySpreadConstraints list" + + - it: fails on a platform policy with an empty topologySpreadConstraints list + template: templates/vm/vmcluster.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + topologySpreadConstraints: [] + asserts: + - failedTemplate: + errorMessage: "monitoring: _cluster.scheduling.globalAppTopologySpreadConstraints must be YAML with a non-empty topologySpreadConstraints list." + + - it: fails on a platform policy whose topologySpreadConstraints is not a list + template: templates/vm/vmcluster.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + topologySpreadConstraints: + maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: ScheduleAnyway + asserts: + - failedTemplate: + errorMessage: "monitoring: _cluster.scheduling.globalAppTopologySpreadConstraints must be YAML with a non-empty topologySpreadConstraints list." diff --git a/packages/system/monitoring/tests/topology_spread_test.yaml b/packages/system/monitoring/tests/topology_spread_test.yaml index dc8273fad..4ede253ea 100644 --- a/packages/system/monitoring/tests/topology_spread_test.yaml +++ b/packages/system/monitoring/tests/topology_spread_test.yaml @@ -348,3 +348,30 @@ tests: matchLabels: app.kubernetes.io/name: vmstorage app.kubernetes.io/instance: shortterm + + - it: keeps a selector the platform policy sets itself + template: templates/vm/vmcluster.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + topologySpreadConstraints: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + example.org/spread: all + documentSelector: + path: metadata.name + value: shortterm + asserts: + - equal: + path: spec.vmstorage.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + example.org/spread: all