fix(redis, valkey, harbor): add the selector to every platform spread constraint

A globalAppTopologySpreadConstraints policy can hold several
constraints, one per zone and one per node for example. The selector
was added under the last one only, and kube-scheduler counts no pods
for a constraint without a selector, so the earlier constraints did not
spread these pods at all. A last constraint with a selector of its own
got a second labelSelector key, which a strict YAML parser rejects and
Helm resolves to the chart's selector, dropping the policy's.

Each constraint without a selector now gets the one of its component,
and a constraint with its own keeps it, the rule the keycloak chart
already applies to its database constraints. The policy is parsed up
front, so one that does not parse or holds no non-empty
topologySpreadConstraints list fails the render with a message. Before,
such a policy failed only because the appended selector broke the YAML,
and a map in place of the list rendered without an error.

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 21:44:09 +03:00
1 parent 902051e780
commit 2119b7ac80
6 files changed
+430 -39

No files matched your search

@@ -1,7 +1,14 @@
{{- /* One image for the data pods and the Sentinels, resolved once so they cannot drift apart. */}}
{{- $redisImage := printf "redis:%s" (include "redis.versionMap" $) }}
{{- $tlsEnabled := (include "redis.tls.enabled" .) | eq "true" -}}
{{- $spreadPolicy := dig "scheduling" "globalAppTopologySpreadConstraints" "" (.Values._cluster | default dict) -}}
{{- $spreadConstraints := list -}}
{{- with dig "scheduling" "globalAppTopologySpreadConstraints" "" (.Values._cluster | default dict) -}}
{{- $policy := fromYaml . -}}
{{- $spreadConstraints = dig "topologySpreadConstraints" (list) $policy -}}
{{- if or (not (kindIs "slice" $spreadConstraints)) (not $spreadConstraints) -}}
{{- fail (trim (printf "redis: _cluster.scheduling.globalAppTopologySpreadConstraints must be YAML with a non-empty topologySpreadConstraints list. %s" (get $policy "Error"))) -}}
{{- end -}}
{{- end -}}
{{- /*
A Sentinel holds only its configuration and failover state, so the Redis
sizing over-provisions it. An empty sentinel block still falls back to the
@@ -67,12 +74,11 @@ spec:
# on every start, so a chart rollback to an older engine is safe here.
image: {{ $redisImage | quote }}
replicas: 3
{{- if $spreadPolicy }}
{{- $spreadPolicy | fromYaml | toYaml | nindent 4 }}
labelSelector:
matchLabels:
app.kubernetes.io/name: {{ .Release.Name }}
app.kubernetes.io/component: sentinel
{{- if $spreadConstraints }}
topologySpreadConstraints:
{{- range $spreadConstraints }}
- {{- toYaml (ternary . (merge (deepCopy .) (dict "labelSelector" (dict "matchLabels" (dict "app.kubernetes.io/name" $.Release.Name "app.kubernetes.io/component" "sentinel")))) (hasKey . "labelSelector")) | nindent 6 }}
{{- end }}
{{- else }}
topologySpreadConstraints:
- maxSkew: 1
@@ -91,12 +97,11 @@ spec:
image: {{ $redisImage | quote }}
resources: {{- include "cozy-lib.resources.defaultingSanitize" (list .Values.resourcesPreset .Values.resources $) | nindent 6 }}
replicas: {{ .Values.replicas }}
{{- if $spreadPolicy }}
{{- $spreadPolicy | fromYaml | toYaml | nindent 4 }}
labelSelector:
matchLabels:
app.kubernetes.io/name: {{ .Release.Name }}
app.kubernetes.io/component: redis
{{- if $spreadConstraints }}
topologySpreadConstraints:
{{- range $spreadConstraints }}
- {{- toYaml (ternary . (merge (deepCopy .) (dict "labelSelector" (dict "matchLabels" (dict "app.kubernetes.io/name" $.Release.Name "app.kubernetes.io/component" "redis")))) (hasKey . "labelSelector")) | nindent 6 }}
{{- end }}
{{- else }}
topologySpreadConstraints:
- maxSkew: 1
@@ -97,3 +97,153 @@ tests:
matchLabels:
app.kubernetes.io/name: my-redis
app.kubernetes.io/component: redis
- it: gives every constraint of a cluster-wide policy the selector
set:
_cluster:
scheduling:
globalAppTopologySpreadConstraints: |
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
documentSelector:
path: kind
value: RedisFailover
asserts:
- equal:
path: spec.sentinel.topologySpreadConstraints
value:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app.kubernetes.io/name: my-redis
app.kubernetes.io/component: sentinel
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app.kubernetes.io/name: my-redis
app.kubernetes.io/component: sentinel
- equal:
path: spec.redis.topologySpreadConstraints
value:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app.kubernetes.io/name: my-redis
app.kubernetes.io/component: redis
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app.kubernetes.io/name: my-redis
app.kubernetes.io/component: redis
- it: keeps a selector the cluster-wide policy sets itself
set:
_cluster:
scheduling:
globalAppTopologySpreadConstraints: |
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
example.org/spread: all
documentSelector:
path: kind
value: RedisFailover
asserts:
- equal:
path: spec.sentinel.topologySpreadConstraints
value:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app.kubernetes.io/name: my-redis
app.kubernetes.io/component: sentinel
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
example.org/spread: all
- equal:
path: spec.redis.topologySpreadConstraints
value:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app.kubernetes.io/name: my-redis
app.kubernetes.io/component: redis
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
example.org/spread: all
- it: fails on a cluster-wide policy that does not parse
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 cluster-wide policy without a topologySpreadConstraints key
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 cluster-wide policy with an empty topologySpreadConstraints list
set:
_cluster:
scheduling:
globalAppTopologySpreadConstraints: |
topologySpreadConstraints: []
asserts:
- failedTemplate:
errorMessage: "redis: _cluster.scheduling.globalAppTopologySpreadConstraints must be YAML with a non-empty topologySpreadConstraints list."
- it: fails on a cluster-wide policy whose topologySpreadConstraints is not a list
set:
_cluster:
scheduling:
globalAppTopologySpreadConstraints: |
topologySpreadConstraints:
maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
asserts:
- failedTemplate:
errorMessage: "redis: _cluster.scheduling.globalAppTopologySpreadConstraints must be YAML with a non-empty topologySpreadConstraints list."
@@ -1,4 +1,11 @@
{{- $spreadPolicy := dig "scheduling" "globalAppTopologySpreadConstraints" "" (.Values._cluster | default dict) }}
{{- $spreadConstraints := list }}
{{- with dig "scheduling" "globalAppTopologySpreadConstraints" "" (.Values._cluster | default dict) }}
{{- $policy := fromYaml . }}
{{- $spreadConstraints = dig "topologySpreadConstraints" (list) $policy }}
{{- if or (not (kindIs "slice" $spreadConstraints)) (not $spreadConstraints) }}
{{- fail (trim (printf "valkey: _cluster.scheduling.globalAppTopologySpreadConstraints must be YAML with a non-empty topologySpreadConstraints list. %s" (get $policy "Error"))) }}
{{- end }}
{{- end }}
{{- if .Values.authEnabled }}
{{- $existingPassword := lookup "v1" "Secret" .Release.Namespace (printf "%s-auth" .Release.Name) }}
{{- $password := randAlphaNum 32 | b64enc }}
@@ -32,12 +39,11 @@ spec:
engine: Valkey
sentinel:
replicas: 3
{{- if $spreadPolicy }}
{{- $spreadPolicy | fromYaml | toYaml | nindent 4 }}
labelSelector:
matchLabels:
app.kubernetes.io/name: {{ .Release.Name }}
app.kubernetes.io/component: sentinel
{{- if $spreadConstraints }}
topologySpreadConstraints:
{{- range $spreadConstraints }}
- {{- toYaml (ternary . (merge (deepCopy .) (dict "labelSelector" (dict "matchLabels" (dict "app.kubernetes.io/name" $.Release.Name "app.kubernetes.io/component" "sentinel")))) (hasKey . "labelSelector")) | nindent 6 }}
{{- end }}
{{- else }}
topologySpreadConstraints:
- maxSkew: 1
@@ -56,12 +62,11 @@ spec:
image: "valkey/valkey:{{ include "valkey.versionMap" $ }}"
resources: {{- include "cozy-lib.resources.defaultingSanitize" (list .Values.resourcesPreset .Values.resources $) | nindent 6 }}
replicas: {{ .Values.replicas }}
{{- if $spreadPolicy }}
{{- $spreadPolicy | fromYaml | toYaml | nindent 4 }}
labelSelector:
matchLabels:
app.kubernetes.io/name: {{ .Release.Name }}
app.kubernetes.io/component: redis
{{- if $spreadConstraints }}
topologySpreadConstraints:
{{- range $spreadConstraints }}
- {{- toYaml (ternary . (merge (deepCopy .) (dict "labelSelector" (dict "matchLabels" (dict "app.kubernetes.io/name" $.Release.Name "app.kubernetes.io/component" "redis")))) (hasKey . "labelSelector")) | nindent 6 }}
{{- end }}
{{- else }}
topologySpreadConstraints:
- maxSkew: 1
@@ -78,3 +78,116 @@ tests:
matchLabels:
app.kubernetes.io/name: my-valkey
app.kubernetes.io/component: redis
- it: gives every constraint of a cluster-wide policy the selector
set:
_cluster:
scheduling:
globalAppTopologySpreadConstraints: |
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
documentSelector:
path: kind
value: RedisFailover
asserts:
- equal:
path: spec.sentinel.topologySpreadConstraints
value:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app.kubernetes.io/name: my-valkey
app.kubernetes.io/component: sentinel
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app.kubernetes.io/name: my-valkey
app.kubernetes.io/component: sentinel
- equal:
path: spec.redis.topologySpreadConstraints
value:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app.kubernetes.io/name: my-valkey
app.kubernetes.io/component: redis
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app.kubernetes.io/name: my-valkey
app.kubernetes.io/component: redis
- it: keeps a selector the cluster-wide policy sets itself
set:
_cluster:
scheduling:
globalAppTopologySpreadConstraints: |
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
example.org/spread: all
documentSelector:
path: kind
value: RedisFailover
asserts:
- equal:
path: spec.sentinel.topologySpreadConstraints
value:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app.kubernetes.io/name: my-valkey
app.kubernetes.io/component: sentinel
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
example.org/spread: all
- equal:
path: spec.redis.topologySpreadConstraints
value:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app.kubernetes.io/name: my-valkey
app.kubernetes.io/component: redis
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
example.org/spread: all
- it: fails on a cluster-wide policy with an empty topologySpreadConstraints list
set:
_cluster:
scheduling:
globalAppTopologySpreadConstraints: |
topologySpreadConstraints: []
asserts:
- failedTemplate:
errorMessage: "valkey: _cluster.scheduling.globalAppTopologySpreadConstraints must be YAML with a non-empty topologySpreadConstraints list."
+18 -13
View File
@@ -1,4 +1,11 @@
{{- $spreadPolicy := dig "scheduling" "globalAppTopologySpreadConstraints" "" (.Values._cluster | default dict) }}
{{- $spreadConstraints := list }}
{{- with dig "scheduling" "globalAppTopologySpreadConstraints" "" (.Values._cluster | default dict) }}
{{- $policy := fromYaml . }}
{{- $spreadConstraints = dig "topologySpreadConstraints" (list) $policy }}
{{- if or (not (kindIs "slice" $spreadConstraints)) (not $spreadConstraints) }}
{{- fail (trim (printf "harbor: _cluster.scheduling.globalAppTopologySpreadConstraints must be YAML with a non-empty topologySpreadConstraints list. %s" (get $policy "Error"))) }}
{{- end }}
{{- end }}
---
apiVersion: v1
kind: Secret
@@ -28,12 +35,11 @@ spec:
# bump it as a deliberate, separate change.
image: redis:6.2.6-alpine
replicas: 3
{{- if $spreadPolicy }}
{{- $spreadPolicy | fromYaml | toYaml | nindent 4 }}
labelSelector:
matchLabels:
app.kubernetes.io/name: {{ .Values.harbor.fullnameOverride }}-redis
app.kubernetes.io/component: sentinel
{{- if $spreadConstraints }}
topologySpreadConstraints:
{{- range $spreadConstraints }}
- {{- toYaml (ternary . (merge (deepCopy .) (dict "labelSelector" (dict "matchLabels" (dict "app.kubernetes.io/name" (printf "%s-redis" $.Values.harbor.fullnameOverride) "app.kubernetes.io/component" "sentinel")))) (hasKey . "labelSelector")) | nindent 6 }}
{{- end }}
{{- else }}
topologySpreadConstraints:
- maxSkew: 1
@@ -59,12 +65,11 @@ spec:
# Harbor's dump.rdb into a 7.x format a platform rollback can no longer load.
image: redis:6.2.6-alpine
replicas: {{ .Values.redis.replicas }}
{{- if $spreadPolicy }}
{{- $spreadPolicy | fromYaml | toYaml | nindent 4 }}
labelSelector:
matchLabels:
app.kubernetes.io/name: {{ .Values.harbor.fullnameOverride }}-redis
app.kubernetes.io/component: redis
{{- if $spreadConstraints }}
topologySpreadConstraints:
{{- range $spreadConstraints }}
- {{- toYaml (ternary . (merge (deepCopy .) (dict "labelSelector" (dict "matchLabels" (dict "app.kubernetes.io/name" (printf "%s-redis" $.Values.harbor.fullnameOverride) "app.kubernetes.io/component" "redis")))) (hasKey . "labelSelector")) | nindent 6 }}
{{- end }}
{{- else }}
topologySpreadConstraints:
- maxSkew: 1
@@ -79,3 +79,116 @@ tests:
matchLabels:
app.kubernetes.io/name: harbor-test-redis
app.kubernetes.io/component: redis
- it: gives every constraint of a cluster-wide policy the selector
set:
_cluster:
scheduling:
globalAppTopologySpreadConstraints: |
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
documentSelector:
path: kind
value: RedisFailover
asserts:
- equal:
path: spec.sentinel.topologySpreadConstraints
value:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app.kubernetes.io/name: harbor-test-redis
app.kubernetes.io/component: sentinel
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app.kubernetes.io/name: harbor-test-redis
app.kubernetes.io/component: sentinel
- equal:
path: spec.redis.topologySpreadConstraints
value:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app.kubernetes.io/name: harbor-test-redis
app.kubernetes.io/component: redis
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app.kubernetes.io/name: harbor-test-redis
app.kubernetes.io/component: redis
- it: keeps a selector the cluster-wide policy sets itself
set:
_cluster:
scheduling:
globalAppTopologySpreadConstraints: |
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
example.org/spread: all
documentSelector:
path: kind
value: RedisFailover
asserts:
- equal:
path: spec.sentinel.topologySpreadConstraints
value:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app.kubernetes.io/name: harbor-test-redis
app.kubernetes.io/component: sentinel
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
example.org/spread: all
- equal:
path: spec.redis.topologySpreadConstraints
value:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app.kubernetes.io/name: harbor-test-redis
app.kubernetes.io/component: redis
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
example.org/spread: all
- it: fails on a cluster-wide policy with an empty topologySpreadConstraints list
set:
_cluster:
scheduling:
globalAppTopologySpreadConstraints: |
topologySpreadConstraints: []
asserts:
- failedTemplate:
errorMessage: "harbor: _cluster.scheduling.globalAppTopologySpreadConstraints must be YAML with a non-empty topologySpreadConstraints list."