_affinities.tpl 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. {{/*
  2. Copyright VMware, Inc.
  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 "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. preferredDuringSchedulingIgnoredDuringExecution:
  65. - podAffinityTerm:
  66. labelSelector:
  67. matchLabels: {{- (include "common.labels.matchLabels" ( dict "customLabels" $customLabels "context" .context )) | nindent 10 }}
  68. {{- if not (empty $component) }}
  69. {{ printf "app.kubernetes.io/component: %s" $component }}
  70. {{- end }}
  71. {{- range $key, $value := $extraMatchLabels }}
  72. {{ $key }}: {{ $value | quote }}
  73. {{- end }}
  74. topologyKey: {{ include "common.affinities.topologyKey" (dict "topologyKey" .topologyKey) }}
  75. weight: 1
  76. {{- range $extraPodAffinityTerms }}
  77. - podAffinityTerm:
  78. labelSelector:
  79. matchLabels: {{- (include "common.labels.matchLabels" ( dict "customLabels" $customLabels "context" $.context )) | nindent 10 }}
  80. {{- if not (empty $component) }}
  81. {{ printf "app.kubernetes.io/component: %s" $component }}
  82. {{- end }}
  83. {{- range $key, $value := .extraMatchLabels }}
  84. {{ $key }}: {{ $value | quote }}
  85. {{- end }}
  86. topologyKey: {{ include "common.affinities.topologyKey" (dict "topologyKey" .topologyKey) }}
  87. weight: {{ .weight | default 1 -}}
  88. {{- end -}}
  89. {{- end -}}
  90. {{/*
  91. Return a hard podAffinity/podAntiAffinity definition
  92. {{ include "common.affinities.pods.hard" (dict "component" "FOO" "customLabels" .Values.podLabels "extraMatchLabels" .Values.extraMatchLabels "topologyKey" "BAR" "extraPodAffinityTerms" .Values.extraPodAffinityTerms "context" $) -}}
  93. */}}
  94. {{- define "common.affinities.pods.hard" -}}
  95. {{- $component := default "" .component -}}
  96. {{- $customLabels := default (dict) .customLabels -}}
  97. {{- $extraMatchLabels := default (dict) .extraMatchLabels -}}
  98. {{- $extraPodAffinityTerms := default (list) .extraPodAffinityTerms -}}
  99. requiredDuringSchedulingIgnoredDuringExecution:
  100. - labelSelector:
  101. matchLabels: {{- (include "common.labels.matchLabels" ( dict "customLabels" $customLabels "context" .context )) | nindent 8 }}
  102. {{- if not (empty $component) }}
  103. {{ printf "app.kubernetes.io/component: %s" $component }}
  104. {{- end }}
  105. {{- range $key, $value := $extraMatchLabels }}
  106. {{ $key }}: {{ $value | quote }}
  107. {{- end }}
  108. topologyKey: {{ include "common.affinities.topologyKey" (dict "topologyKey" .topologyKey) }}
  109. {{- range $extraPodAffinityTerms }}
  110. - labelSelector:
  111. matchLabels: {{- (include "common.labels.matchLabels" ( dict "customLabels" $customLabels "context" $.context )) | nindent 8 }}
  112. {{- if not (empty $component) }}
  113. {{ printf "app.kubernetes.io/component: %s" $component }}
  114. {{- end }}
  115. {{- range $key, $value := .extraMatchLabels }}
  116. {{ $key }}: {{ $value | quote }}
  117. {{- end }}
  118. topologyKey: {{ include "common.affinities.topologyKey" (dict "topologyKey" .topologyKey) }}
  119. {{- end -}}
  120. {{- end -}}
  121. {{/*
  122. Return a podAffinity/podAntiAffinity definition
  123. {{ include "common.affinities.pods" (dict "type" "soft" "key" "FOO" "values" (list "BAR" "BAZ")) -}}
  124. */}}
  125. {{- define "common.affinities.pods" -}}
  126. {{- if eq .type "soft" }}
  127. {{- include "common.affinities.pods.soft" . -}}
  128. {{- else if eq .type "hard" }}
  129. {{- include "common.affinities.pods.hard" . -}}
  130. {{- end -}}
  131. {{- end -}}