_affinities.tpl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. {{/*
  2. Copyright Broadcom, Inc. All Rights Reserved.
  3. SPDX-License-Identifier: APACHE-2.0
  4. */}}
  5. {{/* vim: set filetype=mustache: */}}
  6. {{/*
  7. Return a soft nodeAffinity definition
  8. {{ include "common.affinities.nodes.soft" (dict "key" "FOO" "values" (list "BAR" "BAZ")) -}}
  9. */}}
  10. {{- define "common.affinities.nodes.soft" -}}
  11. preferredDuringSchedulingIgnoredDuringExecution:
  12. - preference:
  13. matchExpressions:
  14. - key: {{ .key }}
  15. operator: In
  16. values:
  17. {{- range .values }}
  18. - {{ . | quote }}
  19. {{- end }}
  20. weight: 1
  21. {{- end -}}
  22. {{/*
  23. Return a hard nodeAffinity definition
  24. {{ include "common.affinities.nodes.hard" (dict "key" "FOO" "values" (list "BAR" "BAZ")) -}}
  25. */}}
  26. {{- define "common.affinities.nodes.hard" -}}
  27. requiredDuringSchedulingIgnoredDuringExecution:
  28. nodeSelectorTerms:
  29. - matchExpressions:
  30. - key: {{ .key }}
  31. operator: In
  32. values:
  33. {{- range .values }}
  34. - {{ . | quote }}
  35. {{- end }}
  36. {{- end -}}
  37. {{/*
  38. Return a nodeAffinity definition
  39. {{ include "common.affinities.nodes" (dict "type" "soft" "key" "FOO" "values" (list "BAR" "BAZ")) -}}
  40. */}}
  41. {{- define "common.affinities.nodes" -}}
  42. {{- if eq .type "soft" }}
  43. {{- include "common.affinities.nodes.soft" . -}}
  44. {{- else if eq .type "hard" }}
  45. {{- include "common.affinities.nodes.hard" . -}}
  46. {{- end -}}
  47. {{- end -}}
  48. {{/*
  49. Return a topologyKey definition
  50. {{ include "common.affinities.topologyKey" (dict "topologyKey" "BAR") -}}
  51. */}}
  52. {{- define "common.affinities.topologyKey" -}}
  53. {{ .topologyKey | default "kubernetes.io/hostname" -}}
  54. {{- end -}}
  55. {{/*
  56. Return a soft podAffinity/podAntiAffinity definition
  57. {{ include "common.affinities.pods.soft" (dict "component" "FOO" "customLabels" .Values.podLabels "extraMatchLabels" .Values.extraMatchLabels "topologyKey" "BAR" "extraPodAffinityTerms" .Values.extraPodAffinityTerms "extraNamespaces" (list "namespace1" "namespace2") "context" $) -}}
  58. */}}
  59. {{- define "common.affinities.pods.soft" -}}
  60. {{- $component := default "" .component -}}
  61. {{- $customLabels := default (dict) .customLabels -}}
  62. {{- $extraMatchLabels := default (dict) .extraMatchLabels -}}
  63. {{- $extraPodAffinityTerms := default (list) .extraPodAffinityTerms -}}
  64. {{- $extraNamespaces := default (list) .extraNamespaces -}}
  65. preferredDuringSchedulingIgnoredDuringExecution:
  66. - podAffinityTerm:
  67. labelSelector:
  68. matchLabels: {{- (include "common.labels.matchLabels" ( dict "customLabels" $customLabels "context" .context )) | nindent 10 }}
  69. {{- if not (empty $component) }}
  70. {{ printf "app.kubernetes.io/component: %s" $component }}
  71. {{- end }}
  72. {{- range $key, $value := $extraMatchLabels }}
  73. {{ $key }}: {{ $value | quote }}
  74. {{- end }}
  75. {{- if $extraNamespaces }}
  76. namespaces:
  77. - {{ .context.Release.Namespace }}
  78. {{- with $extraNamespaces }}
  79. {{ include "common.tplvalues.render" (dict "value" . "context" $) | nindent 8 }}
  80. {{- end }}
  81. {{- end }}
  82. topologyKey: {{ include "common.affinities.topologyKey" (dict "topologyKey" .topologyKey) }}
  83. weight: 1
  84. {{- range $extraPodAffinityTerms }}
  85. - podAffinityTerm:
  86. labelSelector:
  87. matchLabels: {{- (include "common.labels.matchLabels" ( dict "customLabels" $customLabels "context" $.context )) | nindent 10 }}
  88. {{- if not (empty $component) }}
  89. {{ printf "app.kubernetes.io/component: %s" $component }}
  90. {{- end }}
  91. {{- range $key, $value := .extraMatchLabels }}
  92. {{ $key }}: {{ $value | quote }}
  93. {{- end }}
  94. topologyKey: {{ include "common.affinities.topologyKey" (dict "topologyKey" .topologyKey) }}
  95. weight: {{ .weight | default 1 -}}
  96. {{- end -}}
  97. {{- end -}}
  98. {{/*
  99. Return a hard podAffinity/podAntiAffinity definition
  100. {{ include "common.affinities.pods.hard" (dict "component" "FOO" "customLabels" .Values.podLabels "extraMatchLabels" .Values.extraMatchLabels "topologyKey" "BAR" "extraPodAffinityTerms" .Values.extraPodAffinityTerms "extraNamespaces" (list "namespace1" "namespace2") "context" $) -}}
  101. */}}
  102. {{- define "common.affinities.pods.hard" -}}
  103. {{- $component := default "" .component -}}
  104. {{- $customLabels := default (dict) .customLabels -}}
  105. {{- $extraMatchLabels := default (dict) .extraMatchLabels -}}
  106. {{- $extraPodAffinityTerms := default (list) .extraPodAffinityTerms -}}
  107. {{- $extraNamespaces := default (list) .extraNamespaces -}}
  108. requiredDuringSchedulingIgnoredDuringExecution:
  109. - labelSelector:
  110. matchLabels: {{- (include "common.labels.matchLabels" ( dict "customLabels" $customLabels "context" .context )) | nindent 8 }}
  111. {{- if not (empty $component) }}
  112. {{ printf "app.kubernetes.io/component: %s" $component }}
  113. {{- end }}
  114. {{- range $key, $value := $extraMatchLabels }}
  115. {{ $key }}: {{ $value | quote }}
  116. {{- end }}
  117. {{- if $extraNamespaces }}
  118. namespaces:
  119. - {{ .context.Release.Namespace }}
  120. {{- with $extraNamespaces }}
  121. {{ include "common.tplvalues.render" (dict "value" . "context" $) | nindent 8 }}
  122. {{- end }}
  123. {{- end }}
  124. topologyKey: {{ include "common.affinities.topologyKey" (dict "topologyKey" .topologyKey) }}
  125. {{- range $extraPodAffinityTerms }}
  126. - labelSelector:
  127. matchLabels: {{- (include "common.labels.matchLabels" ( dict "customLabels" $customLabels "context" $.context )) | nindent 8 }}
  128. {{- if not (empty $component) }}
  129. {{ printf "app.kubernetes.io/component: %s" $component }}
  130. {{- end }}
  131. {{- range $key, $value := .extraMatchLabels }}
  132. {{ $key }}: {{ $value | quote }}
  133. {{- end }}
  134. topologyKey: {{ include "common.affinities.topologyKey" (dict "topologyKey" .topologyKey) }}
  135. {{- end -}}
  136. {{- end -}}
  137. {{/*
  138. Return a podAffinity/podAntiAffinity definition
  139. {{ include "common.affinities.pods" (dict "type" "soft" "key" "FOO" "values" (list "BAR" "BAZ")) -}}
  140. */}}
  141. {{- define "common.affinities.pods" -}}
  142. {{- if eq .type "soft" }}
  143. {{- include "common.affinities.pods.soft" . -}}
  144. {{- else if eq .type "hard" }}
  145. {{- include "common.affinities.pods.hard" . -}}
  146. {{- end -}}
  147. {{- end -}}