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>
This commit is contained in:
Yan Bondarenko committed 2026-10-20 22:15:01 +03:00
1 parent 0dbbb3b157
commit 33783b4c5c
2 files changed
+77 -1

No files matched your search

@@ -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 -}}
@@ -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