_helpers.tpl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. {{/* vim: set filetype=mustache: */}}
  2. {{/*
  3. Expand the name of the chart.
  4. */}}
  5. {{- define "kafka.name" -}}
  6. {{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
  7. {{- end -}}
  8. {{/*
  9. Create a default fully qualified app name.
  10. We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
  11. If release name contains chart name it will be used as a full name.
  12. */}}
  13. {{- define "kafka.fullname" -}}
  14. {{- include "common.names.fullname" . -}}
  15. {{- end -}}
  16. {{/*
  17. Create a default fully qualified zookeeper name.
  18. We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
  19. */}}
  20. {{- define "kafka.zookeeper.fullname" -}}
  21. {{- if .Values.zookeeper.fullnameOverride -}}
  22. {{- .Values.zookeeper.fullnameOverride | trunc 63 | trimSuffix "-" -}}
  23. {{- else -}}
  24. {{- $name := default "zookeeper" .Values.zookeeper.nameOverride -}}
  25. {{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
  26. {{- end -}}
  27. {{- end -}}
  28. {{/*
  29. Create the name of the service account to use
  30. */}}
  31. {{- define "kafka.serviceAccountName" -}}
  32. {{- if .Values.serviceAccount.create -}}
  33. {{ default (include "kafka.fullname" .) .Values.serviceAccount.name }}
  34. {{- else -}}
  35. {{ default "default" .Values.serviceAccount.name }}
  36. {{- end -}}
  37. {{- end -}}
  38. {{/*
  39. Return the proper Kafka image name
  40. */}}
  41. {{- define "kafka.image" -}}
  42. {{ include "common.images.image" (dict "imageRoot" .Values.image "global" .Values.global) }}
  43. {{- end -}}
  44. {{/*
  45. Return the proper Kafka provisioning image name
  46. */}}
  47. {{- define "kafka.provisioning.image" -}}
  48. {{ include "common.images.image" (dict "imageRoot" .Values.provisioning.image "global" .Values.global) }}
  49. {{- end -}}
  50. {{/*
  51. Return the proper image name (for the init container auto-discovery image)
  52. */}}
  53. {{- define "kafka.externalAccess.autoDiscovery.image" -}}
  54. {{ include "common.images.image" (dict "imageRoot" .Values.externalAccess.autoDiscovery.image "global" .Values.global) }}
  55. {{- end -}}
  56. {{/*
  57. Return the proper image name (for the init container volume-permissions image)
  58. */}}
  59. {{- define "kafka.volumePermissions.image" -}}
  60. {{ include "common.images.image" (dict "imageRoot" .Values.volumePermissions.image "global" .Values.global) }}
  61. {{- end -}}
  62. {{/*
  63. Return the proper Kafka exporter image name
  64. */}}
  65. {{- define "kafka.metrics.kafka.image" -}}
  66. {{ include "common.images.image" (dict "imageRoot" .Values.metrics.kafka.image "global" .Values.global) }}
  67. {{- end -}}
  68. {{/*
  69. Return the proper JMX exporter image name
  70. */}}
  71. {{- define "kafka.metrics.jmx.image" -}}
  72. {{ include "common.images.image" (dict "imageRoot" .Values.metrics.jmx.image "global" .Values.global) }}
  73. {{- end -}}
  74. {{/*
  75. Return the proper Docker Image Registry Secret Names
  76. */}}
  77. {{- define "kafka.imagePullSecrets" -}}
  78. {{ include "common.images.pullSecrets" (dict "images" (list .Values.image .Values.externalAccess.autoDiscovery.image .Values.volumePermissions.image .Values.metrics.kafka.image .Values.metrics.jmx.image) "global" .Values.global) }}
  79. {{- end -}}
  80. {{/*
  81. Return the proper Storage Class
  82. */}}
  83. {{- define "kafka.storageClass" -}}
  84. {{/*
  85. Helm 2.11 supports the assignment of a value to a variable defined in a different scope,
  86. but Helm 2.9 and 2.10 does not support it, so we need to implement this if-else logic.
  87. */}}
  88. {{- if .Values.global -}}
  89. {{- if .Values.global.storageClass -}}
  90. {{- if (eq "-" .Values.global.storageClass) -}}
  91. {{- printf "storageClassName: \"\"" -}}
  92. {{- else }}
  93. {{- printf "storageClassName: %s" .Values.global.storageClass -}}
  94. {{- end -}}
  95. {{- else -}}
  96. {{- if .Values.persistence.storageClass -}}
  97. {{- if (eq "-" .Values.persistence.storageClass) -}}
  98. {{- printf "storageClassName: \"\"" -}}
  99. {{- else }}
  100. {{- printf "storageClassName: %s" .Values.persistence.storageClass -}}
  101. {{- end -}}
  102. {{- end -}}
  103. {{- end -}}
  104. {{- else -}}
  105. {{- if .Values.persistence.storageClass -}}
  106. {{- if (eq "-" .Values.persistence.storageClass) -}}
  107. {{- printf "storageClassName: \"\"" -}}
  108. {{- else }}
  109. {{- printf "storageClassName: %s" .Values.persistence.storageClass -}}
  110. {{- end -}}
  111. {{- end -}}
  112. {{- end -}}
  113. {{- end -}}
  114. {{/*
  115. Return true if authentication via SASL should be configured for client communications
  116. */}}
  117. {{- define "kafka.client.saslAuthentication" -}}
  118. {{- $saslProtocols := list "sasl" "sasl_tls" -}}
  119. {{- if has .Values.auth.clientProtocol $saslProtocols -}}
  120. {{- true -}}
  121. {{- end -}}
  122. {{- end -}}
  123. {{/*
  124. Return true if authentication via SASL should be configured for inter-broker communications
  125. */}}
  126. {{- define "kafka.interBroker.saslAuthentication" -}}
  127. {{- $saslProtocols := list "sasl" "sasl_tls" -}}
  128. {{- if has .Values.auth.interBrokerProtocol $saslProtocols -}}
  129. {{- true -}}
  130. {{- end -}}
  131. {{- end -}}
  132. {{/*
  133. Return true if encryption via TLS for client connections should be configured
  134. */}}
  135. {{- define "kafka.client.tlsEncryption" -}}
  136. {{- $tlsProtocols := list "tls" "mtls" "sasl_tls" -}}
  137. {{- if (has .Values.auth.clientProtocol $tlsProtocols) -}}
  138. {{- true -}}
  139. {{- end -}}
  140. {{- end -}}
  141. {{/*
  142. Return true if encryption via TLS for inter broker communication connections should be configured
  143. */}}
  144. {{- define "kafka.interBroker.tlsEncryption" -}}
  145. {{- $tlsProtocols := list "tls" "mtls" "sasl_tls" -}}
  146. {{- if (has .Values.auth.interBrokerProtocol $tlsProtocols) -}}
  147. {{- true -}}
  148. {{- end -}}
  149. {{- end -}}
  150. {{/*
  151. Return true if encryption via TLS should be configured
  152. */}}
  153. {{- define "kafka.tlsEncryption" -}}
  154. {{- if or (include "kafka.client.tlsEncryption" .) (include "kafka.interBroker.tlsEncryption" .) -}}
  155. {{- true -}}
  156. {{- end -}}
  157. {{- end -}}
  158. {{/*
  159. Return the type of listener
  160. Usage:
  161. {{ include "kafka.listenerType" ( dict "protocol" .Values.path.to.the.Value ) }}
  162. */}}
  163. {{- define "kafka.listenerType" -}}
  164. {{- if eq .protocol "plaintext" -}}
  165. PLAINTEXT
  166. {{- else if or (eq .protocol "tls") (eq .protocol "mtls") -}}
  167. SSL
  168. {{- else if eq .protocol "sasl_tls" -}}
  169. SASL_SSL
  170. {{- else if eq .protocol "sasl" -}}
  171. SASL_PLAINTEXT
  172. {{- end -}}
  173. {{- end -}}
  174. {{/*
  175. Return the Kafka JAAS credentials secret
  176. */}}
  177. {{- define "kafka.jaasSecretName" -}}
  178. {{- $secretName := coalesce .Values.auth.sasl.jaas.existingSecret .Values.auth.jaas.existingSecret -}}
  179. {{- if $secretName -}}
  180. {{- printf "%s" (tpl $secretName $) -}}
  181. {{- else -}}
  182. {{- printf "%s-jaas" (include "kafka.fullname" .) -}}
  183. {{- end -}}
  184. {{- end -}}
  185. {{/*
  186. Return true if a JAAS credentials secret object should be created
  187. */}}
  188. {{- define "kafka.createJaasSecret" -}}
  189. {{- $secretName := coalesce .Values.auth.sasl.jaas.existingSecret .Values.auth.jaas.existingSecret -}}
  190. {{- if and (or (include "kafka.client.saslAuthentication" .) (include "kafka.interBroker.saslAuthentication" .) (and .Values.zookeeper.auth.enabled .Values.auth.jaas.zookeeperUser)) (empty $secretName) -}}
  191. {{- true -}}
  192. {{- end -}}
  193. {{- end -}}
  194. {{/*
  195. Return the Kafka TLS credentials secret
  196. */}}
  197. {{- define "kafka.tlsSecretName" -}}
  198. {{- $secretName := coalesce .Values.auth.tls.existingSecret .Values.auth.jksSecret -}}
  199. {{- if $secretName -}}
  200. {{- printf "%s" (tpl $secretName $) -}}
  201. {{- else -}}
  202. {{- printf "%s-tls" (include "kafka.fullname" .) -}}
  203. {{- end -}}
  204. {{- end -}}
  205. {{/*
  206. Return true if a TLS credentials secret object should be created
  207. */}}
  208. {{- define "kafka.createTlsSecret" -}}
  209. {{- $secretName := coalesce .Values.auth.tls.existingSecret .Values.auth.jksSecret -}}
  210. {{- if and (include "kafka.tlsEncryption" .) (empty $secretName) (eq .Values.auth.tls.type "jks") (.Files.Glob "files/tls/*.jks") }}
  211. {{- true -}}
  212. {{- else if and (include "kafka.tlsEncryption" .) (empty $secretName) (eq .Values.auth.tls.type "pem") (or (.Files.Glob "files/tls/*.{crt,pem}") .Values.auth.tls.autoGenerated) }}
  213. {{- true -}}
  214. {{- end -}}
  215. {{- end -}}
  216. {{/*
  217. Return the Kafka configuration configmap
  218. */}}
  219. {{- define "kafka.configmapName" -}}
  220. {{- if .Values.existingConfigmap -}}
  221. {{- printf "%s" (tpl .Values.existingConfigmap $) -}}
  222. {{- else -}}
  223. {{- printf "%s-configuration" (include "kafka.fullname" .) -}}
  224. {{- end -}}
  225. {{- end -}}
  226. {{/*
  227. Return true if a configmap object should be created
  228. */}}
  229. {{- define "kafka.createConfigmap" -}}
  230. {{- if and .Values.config (not .Values.existingConfigmap) }}
  231. {{- true -}}
  232. {{- end -}}
  233. {{- end -}}
  234. {{/*
  235. Return the Kafka log4j ConfigMap name.
  236. */}}
  237. {{- define "kafka.log4j.configMapName" -}}
  238. {{- if .Values.existingLog4jConfigMap -}}
  239. {{- printf "%s" (tpl .Values.existingLog4jConfigMap $) -}}
  240. {{- else -}}
  241. {{- printf "%s-log4j-configuration" (include "kafka.fullname" .) -}}
  242. {{- end -}}
  243. {{- end -}}
  244. {{/*
  245. Return true if a log4j ConfigMap object should be created.
  246. */}}
  247. {{- define "kafka.log4j.createConfigMap" -}}
  248. {{- if and .Values.log4j (not .Values.existingLog4jConfigMap) }}
  249. {{- true -}}
  250. {{- end -}}
  251. {{- end -}}
  252. {{/*
  253. Return the Kafka configuration configmap
  254. */}}
  255. {{- define "kafka.metrics.jmx.configmapName" -}}
  256. {{- if .Values.metrics.jmx.existingConfigmap -}}
  257. {{- printf "%s" (tpl .Values.metrics.jmx.existingConfigmap $) -}}
  258. {{- else -}}
  259. {{- printf "%s-jmx-configuration" (include "kafka.fullname" .) -}}
  260. {{- end -}}
  261. {{- end -}}
  262. {{/*
  263. Return true if a configmap object should be created
  264. */}}
  265. {{- define "kafka.metrics.jmx.createConfigmap" -}}
  266. {{- if and .Values.metrics.jmx.enabled .Values.metrics.jmx.config (not .Values.metrics.jmx.existingConfigmap) }}
  267. {{- true -}}
  268. {{- end -}}
  269. {{- end -}}
  270. {{/*
  271. Compile all warnings into a single message, and call fail.
  272. */}}
  273. {{- define "kafka.validateValues" -}}
  274. {{- $messages := list -}}
  275. {{- $messages := append $messages (include "kafka.validateValues.authProtocols" .) -}}
  276. {{- $messages := append $messages (include "kafka.validateValues.nodePortListLength" .) -}}
  277. {{- $messages := append $messages (include "kafka.validateValues.externalAccessServiceType" .) -}}
  278. {{- $messages := append $messages (include "kafka.validateValues.externalAccessAutoDiscoveryRBAC" .) -}}
  279. {{- $messages := append $messages (include "kafka.validateValues.saslMechanisms" .) -}}
  280. {{- $messages := append $messages (include "kafka.validateValues.tlsSecret" .) -}}
  281. {{- $messages := without $messages "" -}}
  282. {{- $message := join "\n" $messages -}}
  283. {{- if $message -}}
  284. {{- printf "\nVALUES VALIDATION:\n%s" $message | fail -}}
  285. {{- end -}}
  286. {{- end -}}
  287. {{/* Validate values of Kafka - Authentication protocols for Kafka */}}
  288. {{- define "kafka.validateValues.authProtocols" -}}
  289. {{- $authProtocols := list "plaintext" "tls" "mtls" "sasl" "sasl_tls" -}}
  290. {{- if or (not (has .Values.auth.clientProtocol $authProtocols)) (not (has .Values.auth.interBrokerProtocol $authProtocols)) -}}
  291. kafka: auth.clientProtocol auth.interBrokerProtocol
  292. Available authentication protocols are "plaintext", "tls", "mtls", "sasl" and "sasl_tls"
  293. {{- end -}}
  294. {{- end -}}
  295. {{/* Validate values of Kafka - number of replicas must be the same than NodePort list */}}
  296. {{- define "kafka.validateValues.nodePortListLength" -}}
  297. {{- $replicaCount := int .Values.replicaCount }}
  298. {{- $nodePortListLength := len .Values.externalAccess.service.nodePorts }}
  299. {{- if and .Values.externalAccess.enabled (not .Values.externalAccess.autoDiscovery.enabled) (not (eq $replicaCount $nodePortListLength )) (eq .Values.externalAccess.service.type "NodePort") -}}
  300. kafka: .Values.externalAccess.service.nodePorts
  301. Number of replicas and nodePort array length must be the same. Currently: replicaCount = {{ $replicaCount }} and nodePorts = {{ $nodePortListLength }}
  302. {{- end -}}
  303. {{- end -}}
  304. {{/* Validate values of Kafka - service type for external access */}}
  305. {{- define "kafka.validateValues.externalAccessServiceType" -}}
  306. {{- if and (not (eq .Values.externalAccess.service.type "NodePort")) (not (eq .Values.externalAccess.service.type "LoadBalancer")) -}}
  307. kafka: externalAccess.service.type
  308. Available service type for external access are NodePort or LoadBalancer.
  309. {{- end -}}
  310. {{- end -}}
  311. {{/* Validate values of Kafka - RBAC should be enabled when autoDiscovery is enabled */}}
  312. {{- define "kafka.validateValues.externalAccessAutoDiscoveryRBAC" -}}
  313. {{- if and .Values.externalAccess.enabled .Values.externalAccess.autoDiscovery.enabled (not .Values.rbac.create )}}
  314. kafka: rbac.create
  315. By specifying "externalAccess.enabled=true" and "externalAccess.autoDiscovery.enabled=true"
  316. an initContainer will be used to autodetect the external IPs/ports by querying the
  317. K8s API. Please note this initContainer requires specific RBAC resources. You can create them
  318. by specifying "--set rbac.create=true".
  319. {{- end -}}
  320. {{- end -}}
  321. {{/* Validate values of Kafka - SASL mechanisms must be provided when using SASL */}}
  322. {{- define "kafka.validateValues.saslMechanisms" -}}
  323. {{- if and (or (.Values.auth.clientProtocol | regexFind "sasl") (.Values.auth.interBrokerProtocol | regexFind "sasl") (and .Values.zookeeper.auth.enabled .Values.auth.jaas.zookeeperUser)) (not .Values.auth.saslMechanisms) }}
  324. kafka: auth.saslMechanisms
  325. The SASL mechanisms are required when either auth.clientProtocol or auth.interBrokerProtocol use SASL or Zookeeper user is provided.
  326. {{- end }}
  327. {{- if not (contains .Values.auth.saslInterBrokerMechanism .Values.auth.saslMechanisms) }}
  328. kafka: auth.saslMechanisms
  329. auth.saslInterBrokerMechanism must be provided and it should be one of the specified mechanisms at auth.saslMechanisms
  330. {{- end -}}
  331. {{- end -}}
  332. {{/* Validate values of Kafka - A secret containing TLS certs must be provided when TLS authentication is enabled */}}
  333. {{- define "kafka.validateValues.tlsSecret" -}}
  334. {{- $secretName := coalesce .Values.auth.tls.existingSecret .Values.auth.jksSecret -}}
  335. {{- if and (include "kafka.tlsEncryption" .) (eq .Values.auth.tls.type "jks") (empty $secretName) (not (.Files.Glob "files/tls/*.jks}")) }}
  336. kafka: auth.tls.existingSecret
  337. A secret containing the Kafka JKS keystores and truststore is required
  338. when TLS encryption in enabled and TLS format is "JKS"
  339. {{- else if and (include "kafka.tlsEncryption" .) (eq .Values.auth.tls.type "pem") (empty $secretName) (not (.Files.Glob "files/tls/*.{crt,pem}")) (not .Values.auth.tls.autoGenerated) }}
  340. kafka: auth.tls.existingSecret
  341. A secret containing the Kafka TLS certificates and keys is required
  342. when TLS encryption in enabled and TLS format is "PEM"
  343. {{- end -}}
  344. {{- end -}}