From 56f7683cab971ca2dae4049c9489232e4a01e3ae Mon Sep 17 00:00:00 2001 From: Charith Ellawala Date: Thu, 5 Oct 2023 16:47:08 +0100 Subject: [PATCH] enhancement(helm): Add support for defining topology spread constraints (#1821) * Add topologySpreadConstraints to the helm chart Signed-off-by: Piotr Solarczyk * Use a template function Signed-off-by: Charith Ellawala --------- Signed-off-by: Piotr Solarczyk Signed-off-by: Charith Ellawala Co-authored-by: Piotr Solarczyk --- deploy/charts/cerbos/templates/_helpers.tpl | 20 +++++++++++++++++++ .../charts/cerbos/templates/deployment.yaml | 1 + deploy/charts/cerbos/values.yaml | 6 ++++++ 3 files changed, 27 insertions(+) diff --git a/deploy/charts/cerbos/templates/_helpers.tpl b/deploy/charts/cerbos/templates/_helpers.tpl index ba94e3735..c3c1db9a3 100644 --- a/deploy/charts/cerbos/templates/_helpers.tpl +++ b/deploy/charts/cerbos/templates/_helpers.tpl @@ -167,3 +167,23 @@ The image reference to use in pods {{- end -}} " {{- end }} + +{{/* +Topology spread constraints with label selector injected +*/}} +{{- define "cerbos.topologySpreadConstraints" -}} +{{- if .Values.topologySpreadConstraints }} +{{- $defaultLabels := (fromYaml (include "cerbos.selectorLabels" $)) }} +{{- $defaultLabelSelector := (dict "labelSelector" (dict "matchLabels" $defaultLabels)) }} +{{- $constraints := list }} +{{- range $c := .Values.topologySpreadConstraints }} +{{- if (hasKey $c "labelSelector") }} +{{- $constraints = (append $constraints $c) }} +{{- else }} +{{- $constraints = (append $constraints (mergeOverwrite $c $defaultLabelSelector)) }} +{{- end }} +{{- end }} +topologySpreadConstraints: +{{ toYaml $constraints | indent 2 }} +{{- end }} +{{- end }} diff --git a/deploy/charts/cerbos/templates/deployment.yaml b/deploy/charts/cerbos/templates/deployment.yaml index ee1fedc31..0fe2df64b 100644 --- a/deploy/charts/cerbos/templates/deployment.yaml +++ b/deploy/charts/cerbos/templates/deployment.yaml @@ -132,3 +132,4 @@ spec: tolerations: {{- toYaml . | nindent 8 }} {{- end }} + {{- include "cerbos.topologySpreadConstraints" . | nindent 6}} diff --git a/deploy/charts/cerbos/values.yaml b/deploy/charts/cerbos/values.yaml index bec899da6..9bc418713 100644 --- a/deploy/charts/cerbos/values.yaml +++ b/deploy/charts/cerbos/values.yaml @@ -79,6 +79,12 @@ tolerations: [] # Pod affinity rules. affinity: {} +# Topology Spread Constraints rules. +topologySpreadConstraints: [] + # - topologyKey: topology.kubernetes.io/zone + # maxSkew: 1 + # whenUnsatisfiable: ScheduleAnyway + # Volumes to add to the pod. volumes: []