Skip to content

Commit

Permalink
use templates to merge default and user provided affinity
Browse files Browse the repository at this point in the history
Signed-off-by: Marius Kimmina <[email protected]>
  • Loading branch information
mariuskimmina committed Jul 24, 2024
1 parent 092e199 commit 18e1ef3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
30 changes: 30 additions & 0 deletions charts/prometheus-node-exporter/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,33 @@ labelValueLengthLimit: {{ . }}
{{- end }}
{{- end }}
{{- end }}

{{/*
The default node affinity to exclude Fargate nodes and merge with user-defined affinity
*/}}
{{- define "prometheus-node-exporter.defaultAffinity" -}}
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: eks.amazonaws.com/compute-type
operator: NotIn
values:
- fargate
{{- end -}}
{{- define "prometheus-node-exporter.mergeAffinities" -}}
{{- $defaultAffinity := include "prometheus-node-exporter.defaultAffinity" . | fromYaml -}}
{{- $userAffinity := .Values.affinity -}}
{{- if and $userAffinity $userAffinity.nodeAffinity -}}
{{- if $userAffinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution -}}
{{- $defaultTerms := $defaultAffinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms -}}
{{- $userTerms := $userAffinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms -}}
{{- $mergedTerms := concat $defaultTerms $userTerms -}}
{{- $_ := set $defaultAffinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution "nodeSelectorTerms" $mergedTerms -}}
{{- end -}}
{{- $mergedNodeAffinity := merge $defaultAffinity.nodeAffinity $userAffinity.nodeAffinity -}}
{{- $_ := set $defaultAffinity "nodeAffinity" $mergedNodeAffinity -}}
{{- end -}}
{{- toYaml $defaultAffinity -}}
{{- end -}}

12 changes: 1 addition & 11 deletions charts/prometheus-node-exporter/templates/daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,7 @@ spec:
hostNetwork: {{ .Values.hostNetwork }}
hostPID: {{ .Values.hostPID }}
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: eks.amazonaws.com/compute-type
operator: NotIn
values:
- fargate
{{- with .Values.affinity }}
{{- toYaml . | nindent 8 }}
{{- end }}
{{- include "prometheus-node-exporter.mergeAffinities" . | nindent 8 }}
{{- with .Values.dnsConfig }}
dnsConfig:
{{- toYaml . | nindent 8 }}
Expand Down
9 changes: 9 additions & 0 deletions charts/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: node-type
operator: In
values:
- compute
10 changes: 10 additions & 0 deletions charts/test2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: custom-key
operator: In
values:
- custom-value

0 comments on commit 18e1ef3

Please sign in to comment.