_affinities.tpl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. {{/* vim: set filetype=mustache: */}}
  2. {{/*
  3. Return a soft nodeAffinity definition
  4. {{ include "common.affinities.nodes.soft" (dict "key" "FOO" "values" (list "BAR" "BAZ")) -}}
  5. */}}
  6. {{- define "common.affinities.nodes.soft" -}}
  7. preferredDuringSchedulingIgnoredDuringExecution:
  8. - preference:
  9. matchExpressions:
  10. - key: {{ .key }}
  11. operator: In
  12. values:
  13. {{- range .values }}
  14. - {{ . | quote }}
  15. {{- end }}
  16. weight: 1
  17. {{- end -}}
  18. {{/*
  19. Return a hard nodeAffinity definition
  20. {{ include "common.affinities.nodes.hard" (dict "key" "FOO" "values" (list "BAR" "BAZ")) -}}
  21. */}}
  22. {{- define "common.affinities.nodes.hard" -}}
  23. requiredDuringSchedulingIgnoredDuringExecution:
  24. nodeSelectorTerms:
  25. - matchExpressions:
  26. - key: {{ .key }}
  27. operator: In
  28. values:
  29. {{- range .values }}
  30. - {{ . | quote }}
  31. {{- end }}
  32. {{- end -}}
  33. {{/*
  34. Return a nodeAffinity definition
  35. {{ include "common.affinities.nodes" (dict "type" "soft" "key" "FOO" "values" (list "BAR" "BAZ")) -}}
  36. */}}
  37. {{- define "common.affinities.nodes" -}}
  38. {{- if eq .type "soft" }}
  39. {{- include "common.affinities.nodes.soft" . -}}
  40. {{- else if eq .type "hard" }}
  41. {{- include "common.affinities.nodes.hard" . -}}
  42. {{- end -}}
  43. {{- end -}}
  44. {{/*
  45. Return a topologyKey definition
  46. {{ include "common.affinities.topologyKey" (dict "topologyKey" "BAR") -}}
  47. */}}
  48. {{- define "common.affinities.topologyKey" -}}
  49. {{ .topologyKey | default "kubernetes.io/hostname" -}}
  50. {{- end -}}
  51. {{/*
  52. Return a soft podAffinity/podAntiAffinity definition
  53. {{ include "common.affinities.pods.soft" (dict "component" "FOO" "extraMatchLabels" .Values.extraMatchLabels "topologyKey" "BAR" "context" $) -}}
  54. */}}
  55. {{- define "common.affinities.pods.soft" -}}
  56. {{- $component := default "" .component -}}
  57. {{- $extraMatchLabels := default (dict) .extraMatchLabels -}}
  58. preferredDuringSchedulingIgnoredDuringExecution:
  59. - podAffinityTerm:
  60. labelSelector:
  61. matchLabels: {{- (include "common.labels.matchLabels" .context) | nindent 10 }}
  62. {{- if not (empty $component) }}
  63. {{ printf "app.kubernetes.io/component: %s" $component }}
  64. {{- end }}
  65. {{- range $key, $value := $extraMatchLabels }}
  66. {{ $key }}: {{ $value | quote }}
  67. {{- end }}
  68. topologyKey: {{ include "common.affinities.topologyKey" (dict "topologyKey" .topologyKey) }}
  69. weight: 1
  70. {{- end -}}
  71. {{/*
  72. Return a hard podAffinity/podAntiAffinity definition
  73. {{ include "common.affinities.pods.hard" (dict "component" "FOO" "extraMatchLabels" .Values.extraMatchLabels "topologyKey" "BAR" "context" $) -}}
  74. */}}
  75. {{- define "common.affinities.pods.hard" -}}
  76. {{- $component := default "" .component -}}
  77. {{- $extraMatchLabels := default (dict) .extraMatchLabels -}}
  78. requiredDuringSchedulingIgnoredDuringExecution:
  79. - labelSelector:
  80. matchLabels: {{- (include "common.labels.matchLabels" .context) | nindent 8 }}
  81. {{- if not (empty $component) }}
  82. {{ printf "app.kubernetes.io/component: %s" $component }}
  83. {{- end }}
  84. {{- range $key, $value := $extraMatchLabels }}
  85. {{ $key }}: {{ $value | quote }}
  86. {{- end }}
  87. topologyKey: {{ include "common.affinities.topologyKey" (dict "topologyKey" .topologyKey) }}
  88. {{- end -}}
  89. {{/*
  90. Return a podAffinity/podAntiAffinity definition
  91. {{ include "common.affinities.pods" (dict "type" "soft" "key" "FOO" "values" (list "BAR" "BAZ")) -}}
  92. */}}
  93. {{- define "common.affinities.pods" -}}
  94. {{- if eq .type "soft" }}
  95. {{- include "common.affinities.pods.soft" . -}}
  96. {{- else if eq .type "hard" }}
  97. {{- include "common.affinities.pods.hard" . -}}
  98. {{- end -}}
  99. {{- end -}}