_helpers.tpl 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. {{- define "vm.namespace" -}}
  2. {{- $Release := (.helm).Release | default .Release -}}
  3. {{- $Values := (.helm).Values | default .Values -}}
  4. {{- $Capabilities := (.helm).Capabilities | default .Capabilities -}}
  5. {{- if semverCompare "<3.14.0" $Capabilities.HelmVersion.Version }}
  6. {{- fail "This chart requires helm version 3.14.0 or higher" }}
  7. {{- end }}
  8. {{- $Values.namespaceOverride | default ($Values.global).namespaceOverride | default $Release.Namespace -}}
  9. {{- end -}}
  10. {{- define "vm.validate.args" -}}
  11. {{- $Chart := (.helm).Chart | default .Chart -}}
  12. {{- if empty $Chart -}}
  13. {{- fail "invalid template data" -}}
  14. {{- end -}}
  15. {{- end -}}
  16. {{- /* Expand the name of the chart. */ -}}
  17. {{- define "vm.name" -}}
  18. {{- include "vm.validate.args" . -}}
  19. {{- $Chart := (.helm).Chart | default .Chart -}}
  20. {{- $Values := (.helm).Values | default .Values -}}
  21. {{- $nameOverride := $Values.nameOverride | default ($Values.global).nameOverride | default $Chart.Name -}}
  22. {{- if or ($Values.global).disableNameTruncation $Values.disableNameTruncation -}}
  23. {{- $nameOverride -}}
  24. {{- else -}}
  25. {{- $nameOverride | trunc 63 | trimSuffix "-" -}}
  26. {{- end -}}
  27. {{- end -}}
  28. {{- /*
  29. Create a default fully qualified app name.
  30. We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
  31. If release name contains chart name it will be used as a full name.
  32. */ -}}
  33. {{- define "vm.fullname" -}}
  34. {{- include "vm.validate.args" . -}}
  35. {{- $Values := (.helm).Values | default .Values -}}
  36. {{- $Chart := (.helm).Chart | default .Chart -}}
  37. {{- $Release := (.helm).Release | default .Release -}}
  38. {{- $fullname := "" -}}
  39. {{- $appendDefault := true -}}
  40. {{- if .appKey -}}
  41. {{- $appKey := ternary (list .appKey) .appKey (kindIs "string" .appKey) -}}
  42. {{- $values := $Values -}}
  43. {{- range $ak := $appKey }}
  44. {{- if $values -}}
  45. {{- $values = (index $values $ak) | default dict -}}
  46. {{- end -}}
  47. {{- if and (kindIs "map" $values) $values.fullnameOverride -}}
  48. {{- $fullname = $values.fullnameOverride -}}
  49. {{- $appendDefault = false -}}
  50. {{- else if and (kindIs "map" $values) $values.name -}}
  51. {{- $fullname = $values.name -}}
  52. {{- end -}}
  53. {{- end }}
  54. {{- end -}}
  55. {{- if empty $fullname -}}
  56. {{- if $Values.fullnameOverride -}}
  57. {{- $fullname = $Values.fullnameOverride -}}
  58. {{- else if ($Values.global).fullnameOverride -}}
  59. {{- $fullname = $Values.global.fullnameOverride -}}
  60. {{- else -}}
  61. {{- $name := default $Chart.Name $Values.nameOverride -}}
  62. {{- if contains $name $Release.Name -}}
  63. {{- $fullname = $Release.Name -}}
  64. {{- else -}}
  65. {{- $fullname = (printf "%s-%s" $Release.Name $name) }}
  66. {{- end -}}
  67. {{- end -}}
  68. {{- end -}}
  69. {{- if $appendDefault -}}
  70. {{- $alias := .alias -}}
  71. {{- with .prefix -}}
  72. {{- $fullname = printf "%s-%s" (ternary . $alias (empty $alias)) $fullname -}}
  73. {{- end -}}
  74. {{- with .suffix -}}
  75. {{- $fullname = printf "%s-%s" $fullname (ternary . $alias (empty $alias)) }}
  76. {{- end -}}
  77. {{- end -}}
  78. {{- if or ($Values.global).disableNameTruncation $Values.disableNameTruncation -}}
  79. {{- $fullname -}}
  80. {{- else -}}
  81. {{- $fullname | trunc 63 | trimSuffix "-" -}}
  82. {{- end -}}
  83. {{- end }}
  84. {{- define "vm.managed.fullname" -}}
  85. {{- $prefix := .appKey -}}
  86. {{- if kindIs "slice" $prefix -}}
  87. {{- $prefix = last $prefix -}}
  88. {{- end -}}
  89. {{- $prefix = ternary $prefix (printf "vm%s" $prefix) (or (hasPrefix "vm" $prefix) (hasPrefix "vl" $prefix)) -}}
  90. {{- if $prefix -}}
  91. {{- $_ := set $ "prefix" $prefix -}}
  92. {{- end -}}
  93. {{- include "vm.fullname" . -}}
  94. {{- $_ := unset . "prefix" }}
  95. {{- end -}}
  96. {{- define "vm.plain.fullname" -}}
  97. {{- $suffix := .appKey -}}
  98. {{- if kindIs "slice" $suffix -}}
  99. {{- $suffix = last $suffix }}
  100. {{- end -}}
  101. {{- if $suffix -}}
  102. {{- $_ := set . "suffix" $suffix -}}
  103. {{- end -}}
  104. {{- include "vm.fullname" . -}}
  105. {{- $_ := unset . "suffix" }}
  106. {{- end -}}
  107. {{- /* Create chart name and version as used by the chart label. */ -}}
  108. {{- define "vm.chart" -}}
  109. {{- include "vm.validate.args" . -}}
  110. {{- $Values := (.helm).Values | default .Values -}}
  111. {{- $Chart := (.helm).Chart | default .Chart -}}
  112. {{- $chart := printf "%s-%s" $Chart.Name $Chart.Version | replace "+" "_" -}}
  113. {{- if or ($Values.global).disableNameTruncation $Values.disableNameTruncation -}}
  114. {{- $chart -}}
  115. {{- else -}}
  116. {{- $chart | trunc 63 | trimSuffix "-" -}}
  117. {{- end }}
  118. {{- end }}
  119. {{- /* Create the name of the service account to use */ -}}
  120. {{- define "vm.sa.name" -}}
  121. {{- include "vm.validate.args" . -}}
  122. {{- $Values := (.helm).Values | default .Values -}}
  123. {{- if $Values.serviceAccount.create }}
  124. {{- default (include "vm.fullname" .) $Values.serviceAccount.name }}
  125. {{- else -}}
  126. {{- default "default" $Values.serviceAccount.name -}}
  127. {{- end }}
  128. {{- end }}
  129. {{- define "vm.metaLabels" -}}
  130. {{- include "vm.validate.args" . -}}
  131. {{- $Release := (.helm).Release | default .Release -}}
  132. {{- $labels := .extraLabels | default dict -}}
  133. {{- $_ := set $labels "helm.sh/chart" (include "vm.chart" .) -}}
  134. {{- $_ := set $labels "app.kubernetes.io/managed-by" $Release.Service -}}
  135. {{- toYaml $labels -}}
  136. {{- end -}}
  137. {{- define "vm.podLabels" -}}
  138. {{- include "vm.validate.args" . -}}
  139. {{- $Release := (.helm).Release | default .Release -}}
  140. {{- $labels := fromYaml (include "vm.selectorLabels" .) -}}
  141. {{- $labels = mergeOverwrite $labels (.extraLabels | default dict) -}}
  142. {{- $_ := set $labels "app.kubernetes.io/managed-by" $Release.Service -}}
  143. {{- toYaml $labels -}}
  144. {{- end -}}
  145. {{- /* Common labels */ -}}
  146. {{- define "vm.labels" -}}
  147. {{- include "vm.validate.args" . -}}
  148. {{- $Chart := (.helm).Chart | default .Chart -}}
  149. {{- $labels := fromYaml (include "vm.selectorLabels" .) -}}
  150. {{- $labels = mergeOverwrite $labels (fromYaml (include "vm.metaLabels" .)) -}}
  151. {{- with $Chart.AppVersion -}}
  152. {{- $_ := set $labels "app.kubernetes.io/version" ($Chart.AppVersion) -}}
  153. {{- end -}}
  154. {{- toYaml $labels -}}
  155. {{- end -}}
  156. {{- define "vm.release" -}}
  157. {{- include "vm.validate.args" . -}}
  158. {{- $Release := (.helm).Release | default .Release -}}
  159. {{- $Values := (.helm).Values | default .Values -}}
  160. {{- $release := default $Release.Name $Values.argocdReleaseOverride -}}
  161. {{- if or ($Values.global).disableNameTruncation $Values.disableNameTruncation -}}
  162. {{- $release -}}
  163. {{- else -}}
  164. {{- $release | trunc 63 | trimSuffix "-" -}}
  165. {{- end -}}
  166. {{- end -}}
  167. {{- define "vm.app.name" -}}
  168. {{- if .appKey -}}
  169. {{- $Values := (.helm).Values | default .Values -}}
  170. {{- $values := $Values -}}
  171. {{- $appKey := ternary (list .appKey) .appKey (kindIs "string" .appKey) -}}
  172. {{- $name := last $appKey }}
  173. {{- range $ak := $appKey }}
  174. {{- $values = (index $values $ak) | default dict -}}
  175. {{- if $values.name -}}
  176. {{- $name = $values.name -}}
  177. {{- end -}}
  178. {{- end -}}
  179. {{- $name -}}
  180. {{- end -}}
  181. {{- end -}}
  182. {{- /* Selector labels */ -}}
  183. {{- define "vm.selectorLabels" -}}
  184. {{- $labels := .extraLabels | default dict -}}
  185. {{- $_ := set $labels "app.kubernetes.io/name" (include "vm.name" .) -}}
  186. {{- $_ := set $labels "app.kubernetes.io/instance" (include "vm.release" .) -}}
  187. {{- with (include "vm.app.name" .) -}}
  188. {{- if eq $.style "managed" -}}
  189. {{- $_ := set $labels "app.kubernetes.io/component" (printf "%s-%s" (include "vm.name" $) .) -}}
  190. {{- else -}}
  191. {{- $_ := set $labels "app" . -}}
  192. {{- end -}}
  193. {{- end -}}
  194. {{- toYaml $labels -}}
  195. {{- end }}