Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(common): add support for pod affinity and set default affinity #31536

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion charts/library/common-test/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ apiVersion: v2
appVersion: unknown
dependencies:
- name: common
version: ~26.0.0
version: ~26.1.0
repository: file://../common/
condition: ""
alias: ""
Expand Down
2 changes: 1 addition & 1 deletion charts/library/common/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ sources:
- https://hub.docker.com/_/
- https://hub.docker.com/r/mikefarah/yq
type: library
version: 26.0.1
version: 26.1.0
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
{{- fail "PodLabels - Template used in a place that is not designed to be used" -}}
{{- end }}
pod.lifecycle: {{ $label }}
{{include "tc.v1.common.lib.metadata.volumeLabels" (dict "rootCtx" $rootCtx "objectData" $objectData)}}
{{- end -}}
66 changes: 66 additions & 0 deletions charts/library/common/templates/lib/metadata/_volumeLabels.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{{/* Labels that are added to podSpec */}}
{{/* Call this template:
{{ include "tc.v1.common.lib.metadata.volumeLabels" $ }}
*/}}
{{- define "tc.v1.common.lib.metadata.volumeLabels" -}}

{{- $rootCtx := .rootCtx -}}
{{- $objectData := .objectData -}}
{{- $pvcNames := "" -}}

{{- range $name, $persistenceValues := $rootCtx.Values.persistence -}}
{{- $enabled := (include "tc.v1.common.lib.util.enabled" (dict
"rootCtx" $rootCtx "objectData" $persistenceValues
"name" $name "caller" "Volumes"
"key" "persistence")) -}}
{{- if (eq $enabled "true") -}}
{{- $persistence := (mustDeepCopy $persistenceValues) -}}
{{- $_ := set $persistence "shortName" $name -}}

{{- $selected := false -}}

{{/* If set to true, define volume */}}
{{- if $persistence.targetSelectAll -}}
{{- $selected = true -}}

{{/* If the pod is the autopermission */}}
{{- else if eq $objectData.shortName "autopermissions" -}}
{{- if $persistence.autoPermissions -}}
{{- if $persistence.autoPermissions.enabled -}}
{{- $selected = true -}}
{{- end -}}
{{- end -}}

{{/* If targetSelector is set, check if pod is selected */}}
{{- else if $persistence.targetSelector -}}
{{- if not (kindIs "map" $persistence.targetSelector) -}}
{{- fail (printf "Persistence - Expected [targetSelector] to be [dict], but got [%s]" (kindOf $persistence.targetSelector)) -}}
{{- end -}}

{{- if (mustHas $objectData.shortName (keys $persistence.targetSelector)) -}}
{{- $selected = true -}}
{{- end -}}

{{/* If no targetSelector is set or targetSelectAll, check if pod is primary */}}
{{- else if $objectData.primary -}}
{{- $selected = true -}}
{{- end -}}

{{/* If pod selected */}}
{{- if $selected -}}
{{/* Define the volume based on type */}}
{{- $type := ($persistence.type | default $rootCtx.Values.global.fallbackDefaults.persistenceType) -}}

{{- if eq "pvc" $type }}
{{- if $pvcNames -}}
{{ $pvcNames = ( printf "%s+%s" $pvcNames $name ) }}
{{- else -}}
{{ $pvcNames = $name }}
{{- end -}}
{{ end -}}
{{- end -}}

{{- end -}}
{{- end -}}
"truecharts.org/pvc": {{ $pvcNames }}
{{- end -}}
42 changes: 42 additions & 0 deletions charts/library/common/templates/lib/pod/_affinity.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{{/* Returns pod affinity */}}
{{/* Call this template:
{{ include "tc.v1.common.lib.pod.affinity" (dict "rootCtx" $ "objectData" $objectData) }}
rootCtx: The root context of the chart.
objectData: The object data to be used to render the Pod.
*/}}
{{- define "tc.v1.common.lib.pod.affinity" -}}
{{- $rootCtx := .rootCtx -}}
{{- $objectData := .objectData -}}

{{- $affinity := list -}}

{{/* Initialize from the "global" option */}}
{{- with $rootCtx.Values.podOptions.affinity -}}
{{- $affinity = . -}}
{{- end -}}

{{/* Override with pods option */}}
{{- with $objectData.podSpec.affinity -}}
{{- $affinity = . -}}
{{- end -}}

{{- $validTypes := (list "Deployment" "StatefulSet") -}}
{{/* TODO: We need to merge default with user input */}}
{{- $pvcLabels := (include "tc.v1.common.lib.metadata.volumeLabels" (dict "rootCtx" $rootCtx "objectData" $objectData)) -}}
{{- if and (mustHas $objectData.type $validTypes) $pvcLabels $rootCtx.Values.podOptions.defaultAffinity }}
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- {{ $pvcLabels }}
topologyKey: "kubernetes.io/hostname"
{{- 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 -}}
4 changes: 4 additions & 0 deletions charts/library/common/templates/lib/workload/_pod.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
1 change: 1 addition & 0 deletions charts/library/common/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ""
Expand Down
Loading