From 6c330967652215ae5b266e1602475ce9f844a20c Mon Sep 17 00:00:00 2001 From: Kjeld Schouten Date: Thu, 6 Feb 2025 11:45:21 +0100 Subject: [PATCH] Create default affinity rule, to keep pods with same PVCs on same nodes --- charts/library/common/templates/lib/pod/_affinity.tpl | 11 +++++------ charts/library/common/templates/lib/workload/_pod.tpl | 4 ++++ charts/library/common/values.yaml | 1 + 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/charts/library/common/templates/lib/pod/_affinity.tpl b/charts/library/common/templates/lib/pod/_affinity.tpl index 6756a679b1c03..c038b032b1272 100644 --- a/charts/library/common/templates/lib/pod/_affinity.tpl +++ b/charts/library/common/templates/lib/pod/_affinity.tpl @@ -21,10 +21,8 @@ objectData: The object data to be used to render the Pod. {{- end -}} {{- $validTypes := (list "Deployment" "StatefulSet") -}} + {{/* TODO: We need to merge default with user input */}} {{- if and (mustHas $objectData.type $validTypes) $rootCtx.Values.podOptions.defaultAffinity }} - {{/* -TODO: We need to label pods with the PVCs that are attached, then use those labels here -EXAMPLE FORMAT: podAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: @@ -32,11 +30,12 @@ EXAMPLE FORMAT: - key: app operator: In values: - - PERSITENCE NAME LABEL + - {{include "tc.v1.common.lib.metadata.volumeLabels" (dict "rootCtx" $rootCtx "objectData" $objectData)}} topologyKey: "kubernetes.io/hostname" - */}} - {{- end -}} + {{- else -}} {{- with $affinity -}} {{/* TODO: Template this, so we can add some validation around easy to make mistakes. Low Prio */}} {{- . | toYaml | nindent 0 }} {{- end -}} + {{- end -}} + {{- end -}} diff --git a/charts/library/common/templates/lib/workload/_pod.tpl b/charts/library/common/templates/lib/workload/_pod.tpl index 31e414cf77a3d..cb4b5219814e4 100644 --- a/charts/library/common/templates/lib/workload/_pod.tpl +++ b/charts/library/common/templates/lib/workload/_pod.tpl @@ -30,6 +30,10 @@ priorityClassName: {{ . }} nodeSelector: {{- . | nindent 2 }} {{- end -}} + {{- with (include "tc.v1.common.lib.pod.affinity" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim) }} +affinity: + {{- . | nindent 2 }} + {{- end -}} {{- with (include "tc.v1.common.lib.pod.topologySpreadConstraints" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim) }} topologySpreadConstraints: {{- . | nindent 2 }} diff --git a/charts/library/common/values.yaml b/charts/library/common/values.yaml index e5c5e49a31d5e..1b61459fc92dd 100644 --- a/charts/library/common/values.yaml +++ b/charts/library/common/values.yaml @@ -150,6 +150,7 @@ podOptions: kubernetes.io/arch: "amd64" # -- Used to enforce a good spread for Deployments and StatefulSets by default defaultSpread: true + defaultAffinity: true topologySpreadConstraints: [] tolerations: [] schedulerName: ""