From a4cb00e6dd587759f84658dfe3c3ae97da4a41ff Mon Sep 17 00:00:00 2001 From: Yan Bondarenko <202671653+yankawai@users.noreply.github.com> Date: Tue, 20 Oct 2026 13:24:49 +0300 Subject: [PATCH 1/3] fix(monitoring): spread monitoring replicas over nodes The VMCluster, VLCluster, VTCluster and VMAlertmanager the chart renders carry no affinity and no topology spread, and vm-operator copies both fields onto the pods as written without adding any of its own. Nothing in those specs kept two replicas of one tier off the same node. With replicationFactor 2 both copies of a series can then sit on one node, and on a node-local class the volumes keep them there, so losing that node loses both copies. A StatefulSet pod on a lost node is not replaced elsewhere until its Pod or Node object is deleted, which holds for the Alertmanager replicas without a volume as well. The tiers that run as StatefulSets, vmstorage, vmselect, vlstorage, vtstorage and the Alertmanager, now get a per-node spread with whenUnsatisfiable DoNotSchedule and nodeTaintsPolicy Ignore. The tiers that run as Deployments, vminsert, vlinsert, vlselect, vtinsert and vtselect, get the same spread with ScheduleAnyway, since their pods are replaced on another node without help. A platform that sets scheduling.globalAppTopologySpreadConstraints gets that policy for every tier instead, with the tier's own selector added to each constraint that has none. vm-operator rolls a changed StatefulSet one pod at a time. A replica on a node-local volume whose peer shares its node cannot be placed again under the new rule, so its new pod stays Pending and the rollout of that cluster stops there until the replica is moved. Assisted-by: LLM Signed-off-by: Yan Bondarenko <202671653+yankawai@users.noreply.github.com> --- .../monitoring/templates/_scheduling.tpl | 22 ++ .../monitoring/templates/alerta/alerta.yaml | 1 + .../monitoring/templates/vlogs/vlogs.yaml | 3 + .../monitoring/templates/vm/vmcluster.yaml | 3 + .../monitoring/templates/vtraces/vtraces.yaml | 3 + .../tests/topology_spread_test.yaml | 350 ++++++++++++++++++ 6 files changed, 382 insertions(+) create mode 100644 packages/system/monitoring/templates/_scheduling.tpl create mode 100644 packages/system/monitoring/tests/topology_spread_test.yaml diff --git a/packages/system/monitoring/templates/_scheduling.tpl b/packages/system/monitoring/templates/_scheduling.tpl new file mode 100644 index 000000000..6540d4d84 --- /dev/null +++ b/packages/system/monitoring/templates/_scheduling.tpl @@ -0,0 +1,22 @@ +{{- define "monitoring.topologySpreadConstraints" -}} +{{- $root := index . 0 -}} +{{- $selector := dict "matchLabels" (dict "app.kubernetes.io/name" (index . 1) "app.kubernetes.io/instance" (index . 2)) -}} +{{- $global := list -}} +{{- with ($root.Values._cluster | default dict).scheduling -}} +{{- with get . "globalAppTopologySpreadConstraints" -}} +{{- $global = dig "topologySpreadConstraints" (list) (fromYaml .) -}} +{{- end -}} +{{- end -}} +{{- $constraints := list -}} +{{- range $global -}} +{{- $constraints = append $constraints (ternary . (merge (dict "labelSelector" $selector) .) (hasKey . "labelSelector")) -}} +{{- end -}} +{{- if not $constraints -}} +{{- $default := dict "maxSkew" 1 "topologyKey" "kubernetes.io/hostname" "whenUnsatisfiable" (index . 3) "labelSelector" $selector -}} +{{- if eq (index . 3) "DoNotSchedule" -}} +{{- $_ := set $default "nodeTaintsPolicy" "Ignore" -}} +{{- end -}} +{{- $constraints = list $default -}} +{{- end -}} +{{- toYaml (dict "topologySpreadConstraints" $constraints) -}} +{{- end -}} diff --git a/packages/system/monitoring/templates/alerta/alerta.yaml b/packages/system/monitoring/templates/alerta/alerta.yaml index 6376eaa9e..e5bdda356 100644 --- a/packages/system/monitoring/templates/alerta/alerta.yaml +++ b/packages/system/monitoring/templates/alerta/alerta.yaml @@ -421,6 +421,7 @@ spec: apps.cozystack.io/application.name: {{ $.Release.Name }} replicaCount: {{ include "monitoring.alertmanager.replicas" $ }} configSecret: alertmanager + {{- include "monitoring.topologySpreadConstraints" (list $ "vmalertmanager" "alertmanager" "DoNotSchedule") | nindent 2 }} {{- if and $emailEnabled $email.authUsername }} secrets: - {{ $emailPasswordSecret | quote }} diff --git a/packages/system/monitoring/templates/vlogs/vlogs.yaml b/packages/system/monitoring/templates/vlogs/vlogs.yaml index 0e3d6add1..ed70f3215 100644 --- a/packages/system/monitoring/templates/vlogs/vlogs.yaml +++ b/packages/system/monitoring/templates/vlogs/vlogs.yaml @@ -22,13 +22,16 @@ spec: apps.cozystack.io/application.name: {{ $.Release.Name }} vlinsert: replicaCount: 2 + {{- include "monitoring.topologySpreadConstraints" (list $ "vlinsert" .name "ScheduleAnyway") | nindent 4 }} resources: {} vlselect: replicaCount: 2 + {{- include "monitoring.topologySpreadConstraints" (list $ "vlselect" .name "ScheduleAnyway") | nindent 4 }} resources: {} vlstorage: retentionPeriod: {{ .retentionPeriod | quote }} replicaCount: 2 + {{- include "monitoring.topologySpreadConstraints" (list $ "vlstorage" .name "DoNotSchedule") | nindent 4 }} resources: {} storage: volumeClaimTemplate: diff --git a/packages/system/monitoring/templates/vm/vmcluster.yaml b/packages/system/monitoring/templates/vm/vmcluster.yaml index e2962a12a..a89d410fc 100644 --- a/packages/system/monitoring/templates/vm/vmcluster.yaml +++ b/packages/system/monitoring/templates/vm/vmcluster.yaml @@ -18,9 +18,11 @@ spec: # it's usually more than default 30 maxLabelsPerTimeseries: "60" replicaCount: 2 + {{- include "monitoring.topologySpreadConstraints" (list $ "vminsert" .name "ScheduleAnyway") | nindent 4 }} resources: {} vmselect: replicaCount: 2 + {{- include "monitoring.topologySpreadConstraints" (list $ "vmselect" .name "DoNotSchedule") | nindent 4 }} resources: {} extraArgs: search.maxUniqueTimeseries: "600000" @@ -46,6 +48,7 @@ spec: storage: 2Gi vmstorage: replicaCount: 2 + {{- include "monitoring.topologySpreadConstraints" (list $ "vmstorage" .name "DoNotSchedule") | nindent 4 }} resources: {} storage: volumeClaimTemplate: diff --git a/packages/system/monitoring/templates/vtraces/vtraces.yaml b/packages/system/monitoring/templates/vtraces/vtraces.yaml index c7f3d386f..5c03a9dda 100644 --- a/packages/system/monitoring/templates/vtraces/vtraces.yaml +++ b/packages/system/monitoring/templates/vtraces/vtraces.yaml @@ -71,9 +71,11 @@ spec: apps.cozystack.io/application.name: {{ $.Release.Name }} insert: replicaCount: 2 + {{- include "monitoring.topologySpreadConstraints" (list $ "vtinsert" .name "ScheduleAnyway") | nindent 4 }} resources: {} select: replicaCount: 2 + {{- include "monitoring.topologySpreadConstraints" (list $ "vtselect" .name "ScheduleAnyway") | nindent 4 }} resources: {} storage: retentionPeriod: {{ .retentionPeriod | quote }} @@ -84,6 +86,7 @@ spec: retention.maxDiskUsagePercent: "80" {{- end }} replicaCount: 2 + {{- include "monitoring.topologySpreadConstraints" (list $ "vtstorage" .name "DoNotSchedule") | nindent 4 }} resources: {} storage: volumeClaimTemplate: diff --git a/packages/system/monitoring/tests/topology_spread_test.yaml b/packages/system/monitoring/tests/topology_spread_test.yaml new file mode 100644 index 000000000..dc8273fad --- /dev/null +++ b/packages/system/monitoring/tests/topology_spread_test.yaml @@ -0,0 +1,350 @@ +suite: monitoring replicas spread over nodes +templates: + - templates/vm/vmcluster.yaml + - templates/vlogs/vlogs.yaml + - templates/vtraces/vtraces.yaml + - templates/alerta/alerta.yaml + +release: + name: monitoring-system + namespace: tenant-root + +set: + _cluster: + root-host: example.org + _namespace: + host: tenant-root.example.org + ingress: tenant-root + tracingStorages: + - name: generic + mode: cluster + retentionPeriod: "14d" + storage: 10Gi + +tests: + - it: requires separate nodes for vmstorage and vmselect and prefers them for vminsert + template: templates/vm/vmcluster.yaml + documentSelector: + path: metadata.name + value: shortterm + asserts: + - equal: + path: spec.vmstorage.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: DoNotSchedule + nodeTaintsPolicy: Ignore + labelSelector: + matchLabels: + app.kubernetes.io/name: vmstorage + app.kubernetes.io/instance: shortterm + - equal: + path: spec.vmselect.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: DoNotSchedule + nodeTaintsPolicy: Ignore + labelSelector: + matchLabels: + app.kubernetes.io/name: vmselect + app.kubernetes.io/instance: shortterm + - equal: + path: spec.vminsert.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vminsert + app.kubernetes.io/instance: shortterm + + - it: keys each VMCluster's constraints to its own name + template: templates/vm/vmcluster.yaml + documentSelector: + path: metadata.name + value: longterm + asserts: + - equal: + path: spec.vmstorage.topologySpreadConstraints[0].labelSelector.matchLabels + value: + app.kubernetes.io/name: vmstorage + app.kubernetes.io/instance: longterm + - equal: + path: spec.vmselect.topologySpreadConstraints[0].labelSelector.matchLabels + value: + app.kubernetes.io/name: vmselect + app.kubernetes.io/instance: longterm + - equal: + path: spec.vminsert.topologySpreadConstraints[0].labelSelector.matchLabels + value: + app.kubernetes.io/name: vminsert + app.kubernetes.io/instance: longterm + + - it: requires separate nodes for vlstorage and prefers them for vlselect and vlinsert + template: templates/vlogs/vlogs.yaml + documentSelector: + path: metadata.name + value: generic + asserts: + - equal: + path: spec.vlstorage.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: DoNotSchedule + nodeTaintsPolicy: Ignore + labelSelector: + matchLabels: + app.kubernetes.io/name: vlstorage + app.kubernetes.io/instance: generic + - equal: + path: spec.vlselect.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vlselect + app.kubernetes.io/instance: generic + - equal: + path: spec.vlinsert.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vlinsert + app.kubernetes.io/instance: generic + + - it: requires separate nodes for vtstorage and prefers them for vtselect and vtinsert + template: templates/vtraces/vtraces.yaml + documentSelector: + path: kind + value: VTCluster + asserts: + - equal: + path: spec.storage.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: DoNotSchedule + nodeTaintsPolicy: Ignore + labelSelector: + matchLabels: + app.kubernetes.io/name: vtstorage + app.kubernetes.io/instance: generic + - equal: + path: spec.select.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vtselect + app.kubernetes.io/instance: generic + - equal: + path: spec.insert.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vtinsert + app.kubernetes.io/instance: generic + + - it: requires separate nodes for the Alertmanager replicas + template: templates/alerta/alerta.yaml + documentSelector: + path: kind + value: VMAlertmanager + asserts: + - equal: + path: spec.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: DoNotSchedule + nodeTaintsPolicy: Ignore + labelSelector: + matchLabels: + app.kubernetes.io/name: vmalertmanager + app.kubernetes.io/instance: alertmanager + + - it: keeps the default when the platform policy is empty + template: templates/vm/vmcluster.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: "" + documentSelector: + path: metadata.name + value: shortterm + asserts: + - equal: + path: spec.vmstorage.topologySpreadConstraints[0].whenUnsatisfiable + value: DoNotSchedule + - equal: + path: spec.vminsert.topologySpreadConstraints[0].whenUnsatisfiable + value: ScheduleAnyway + + - it: renders the platform policy in place of the default for a VMCluster + template: templates/vm/vmcluster.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + topologySpreadConstraints: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + documentSelector: + path: metadata.name + value: shortterm + asserts: + - equal: + path: spec.vmstorage.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vmstorage + app.kubernetes.io/instance: shortterm + - equal: + path: spec.vmselect.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vmselect + app.kubernetes.io/instance: shortterm + - equal: + path: spec.vminsert.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vminsert + app.kubernetes.io/instance: shortterm + + - it: renders the platform policy in place of the default for the logs store + template: templates/vlogs/vlogs.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + topologySpreadConstraints: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + documentSelector: + path: metadata.name + value: generic + asserts: + - equal: + path: spec.vlstorage.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vlstorage + app.kubernetes.io/instance: generic + + - it: renders the platform policy in place of the default for the traces store + template: templates/vtraces/vtraces.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + topologySpreadConstraints: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + documentSelector: + path: kind + value: VTCluster + asserts: + - equal: + path: spec.storage.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vtstorage + app.kubernetes.io/instance: generic + + - it: renders the platform policy in place of the default for the Alertmanager + template: templates/alerta/alerta.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + topologySpreadConstraints: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + documentSelector: + path: kind + value: VMAlertmanager + asserts: + - equal: + path: spec.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vmalertmanager + app.kubernetes.io/instance: alertmanager + + - it: gives every constraint of the platform policy the component's selector + template: templates/vm/vmcluster.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + topologySpreadConstraints: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: DoNotSchedule + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: ScheduleAnyway + documentSelector: + path: metadata.name + value: shortterm + asserts: + - equal: + path: spec.vmstorage.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: DoNotSchedule + labelSelector: + matchLabels: + app.kubernetes.io/name: vmstorage + app.kubernetes.io/instance: shortterm + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vmstorage + app.kubernetes.io/instance: shortterm From 0dbbb3b157c2e5f3037dccb53b87deba250bc943 Mon Sep 17 00:00:00 2001 From: Yan Bondarenko <202671653+yankawai@users.noreply.github.com> Date: Tue, 20 Oct 2026 13:38:25 +0300 Subject: [PATCH 2/3] fix(monitoring): fail the render on a malformed platform spread policy A globalAppTopologySpreadConstraints value that did not parse, or that held no topologySpreadConstraints list, gave every tier the chart default without any sign, so the platform's policy was silently not applied. The render now fails and names the parse error, as the chart already does for an empty logsStorages. The tests also pin that a selector the policy sets itself is kept as written. Assisted-by: LLM Signed-off-by: Yan Bondarenko <202671653+yankawai@users.noreply.github.com> --- .../monitoring/templates/_scheduling.tpl | 6 +- .../topology_spread_invalid_policy_test.yaml | 59 +++++++++++++++++++ .../tests/topology_spread_test.yaml | 27 +++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 packages/system/monitoring/tests/topology_spread_invalid_policy_test.yaml 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 From 33783b4c5c84e1ed48df024227192ac330e57605 Mon Sep 17 00:00:00 2001 From: Yan Bondarenko <202671653+yankawai@users.noreply.github.com> Date: Tue, 20 Oct 2026 22:15:01 +0300 Subject: [PATCH 3/3] fix(monitoring): stop a policy selector leaking into other constraints The helper gave a platform constraint the tier's selector through a merge inside ternary, and ternary evaluates both branches, so the merge also ran for a constraint that brings a selector of its own. merge writes into its first argument and recurses into the maps both sides hold. The first argument held the shared $selector map, so the labels or matchExpressions of that own selector were written into $selector itself. Every constraint without a selector holds that same map and the list is rendered once at the end, so all of them came out with the foreign terms added, before or after the constraint that brought them. Such a constraint selected only those of the tier's pods that also match the other selector, and on pods without those labels it spread nothing. Only a policy that mixes constraints with and without a selector is affected. The default and other policies render as before. The merge now goes into a deep copy of the constraint, the order the keycloak chart uses for its database constraints, so $selector is never written. Two new cases pin a constraint without a selector after and before one with its own, the first with matchLabels and the second with matchExpressions. Assisted-by: LLM Signed-off-by: Yan Bondarenko <202671653+yankawai@users.noreply.github.com> --- .../monitoring/templates/_scheduling.tpl | 2 +- .../tests/topology_spread_test.yaml | 76 +++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/packages/system/monitoring/templates/_scheduling.tpl b/packages/system/monitoring/templates/_scheduling.tpl index 25ff369ad..e7358c5c6 100644 --- a/packages/system/monitoring/templates/_scheduling.tpl +++ b/packages/system/monitoring/templates/_scheduling.tpl @@ -13,7 +13,7 @@ {{- end -}} {{- $constraints := list -}} {{- range $global -}} -{{- $constraints = append $constraints (ternary . (merge (dict "labelSelector" $selector) .) (hasKey . "labelSelector")) -}} +{{- $constraints = append $constraints (ternary . (merge (deepCopy .) (dict "labelSelector" $selector)) (hasKey . "labelSelector")) -}} {{- end -}} {{- if not $constraints -}} {{- $default := dict "maxSkew" 1 "topologyKey" "kubernetes.io/hostname" "whenUnsatisfiable" (index . 3) "labelSelector" $selector -}} diff --git a/packages/system/monitoring/tests/topology_spread_test.yaml b/packages/system/monitoring/tests/topology_spread_test.yaml index 4ede253ea..14a20da21 100644 --- a/packages/system/monitoring/tests/topology_spread_test.yaml +++ b/packages/system/monitoring/tests/topology_spread_test.yaml @@ -375,3 +375,79 @@ tests: labelSelector: matchLabels: example.org/spread: all + + - it: gives the component's selector to a constraint after one that sets its own + 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 + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: DoNotSchedule + 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 + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: DoNotSchedule + labelSelector: + matchLabels: + app.kubernetes.io/name: vmstorage + app.kubernetes.io/instance: shortterm + + - it: gives the component's selector to a constraint before one that sets its own + template: templates/vm/vmcluster.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + topologySpreadConstraints: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: DoNotSchedule + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchExpressions: + - key: example.org/spread + operator: Exists + documentSelector: + path: metadata.name + value: shortterm + asserts: + - equal: + path: spec.vmstorage.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: DoNotSchedule + labelSelector: + matchLabels: + app.kubernetes.io/name: vmstorage + app.kubernetes.io/instance: shortterm + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchExpressions: + - key: example.org/spread + operator: Exists