_helpers.tpl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. {{/* vim: set filetype=mustache: */}}
  2. {{/*
  3. Create a default fully qualified app name for PostgreSQL Primary objects
  4. We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
  5. */}}
  6. {{- define "postgresql.primary.fullname" -}}
  7. {{- if eq .Values.architecture "replication" }}
  8. {{- printf "%s-%s" (include "common.names.fullname" .) .Values.primary.name | trunc 63 | trimSuffix "-" -}}
  9. {{- else -}}
  10. {{- include "common.names.fullname" . -}}
  11. {{- end -}}
  12. {{- end -}}
  13. {{/*
  14. Create a default fully qualified app name for PostgreSQL read-only replicas objects
  15. We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
  16. */}}
  17. {{- define "postgresql.readReplica.fullname" -}}
  18. {{- printf "%s-%s" (include "common.names.fullname" .) .Values.readReplicas.name | trunc 63 | trimSuffix "-" -}}
  19. {{- end -}}
  20. {{/*
  21. Create the default FQDN for PostgreSQL primary headless service
  22. We truncate at 63 chars because of the DNS naming spec.
  23. */}}
  24. {{- define "postgresql.primary.svc.headless" -}}
  25. {{- printf "%s-hl" (include "postgresql.primary.fullname" .) | trunc 63 | trimSuffix "-" }}
  26. {{- end -}}
  27. {{/*
  28. Create the default FQDN for PostgreSQL read-only replicas headless service
  29. We truncate at 63 chars because of the DNS naming spec.
  30. */}}
  31. {{- define "postgresql.readReplica.svc.headless" -}}
  32. {{- printf "%s-hl" (include "postgresql.readReplica.fullname" .) | trunc 63 | trimSuffix "-" }}
  33. {{- end -}}
  34. {{/*
  35. Return the proper PostgreSQL image name
  36. */}}
  37. {{- define "postgresql.image" -}}
  38. {{ include "common.images.image" (dict "imageRoot" .Values.image "global" .Values.global) }}
  39. {{- end -}}
  40. {{/*
  41. Return the proper PostgreSQL metrics image name
  42. */}}
  43. {{- define "postgresql.metrics.image" -}}
  44. {{ include "common.images.image" (dict "imageRoot" .Values.metrics.image "global" .Values.global) }}
  45. {{- end -}}
  46. {{/*
  47. Return the proper image name (for the init container volume-permissions image)
  48. */}}
  49. {{- define "postgresql.volumePermissions.image" -}}
  50. {{ include "common.images.image" (dict "imageRoot" .Values.volumePermissions.image "global" .Values.global) }}
  51. {{- end -}}
  52. {{/*
  53. Return the proper Docker Image Registry Secret Names
  54. */}}
  55. {{- define "postgresql.imagePullSecrets" -}}
  56. {{ include "common.images.pullSecrets" (dict "images" (list .Values.image .Values.metrics.image .Values.volumePermissions.image) "global" .Values.global) }}
  57. {{- end -}}
  58. {{/*
  59. Return the name for a custom user to create
  60. */}}
  61. {{- define "postgresql.username" -}}
  62. {{- if .Values.global.postgresql.auth.username }}
  63. {{- .Values.global.postgresql.auth.username -}}
  64. {{- else -}}
  65. {{- .Values.auth.username -}}
  66. {{- end -}}
  67. {{- end -}}
  68. {{/*
  69. Return the name for a custom database to create
  70. */}}
  71. {{- define "postgresql.database" -}}
  72. {{- if .Values.global.postgresql.auth.database }}
  73. {{- .Values.global.postgresql.auth.database -}}
  74. {{- else if .Values.auth.database -}}
  75. {{- .Values.auth.database -}}
  76. {{- end -}}
  77. {{- end -}}
  78. {{/*
  79. Get the password secret.
  80. */}}
  81. {{- define "postgresql.secretName" -}}
  82. {{- if .Values.global.postgresql.auth.existingSecret }}
  83. {{- printf "%s" (tpl .Values.global.postgresql.auth.existingSecret $) -}}
  84. {{- else if .Values.auth.existingSecret -}}
  85. {{- printf "%s" (tpl .Values.auth.existingSecret $) -}}
  86. {{- else -}}
  87. {{- printf "%s" (include "common.names.fullname" .) -}}
  88. {{- end -}}
  89. {{- end -}}
  90. {{/*
  91. Get the replication-password key.
  92. */}}
  93. {{- define "postgresql.replicationPasswordKey" -}}
  94. {{- if or .Values.global.postgresql.auth.existingSecret .Values.auth.existingSecret }}
  95. {{- if .Values.global.postgresql.auth.secretKeys.replicationPasswordKey }}
  96. {{- printf "%s" (tpl .Values.global.postgresql.auth.secretKeys.replicationPasswordKey $) -}}
  97. {{- else if .Values.auth.secretKeys.replicationPasswordKey -}}
  98. {{- printf "%s" (tpl .Values.auth.secretKeys.replicationPasswordKey $) -}}
  99. {{- else -}}
  100. {{- "replication-password" -}}
  101. {{- end -}}
  102. {{- else -}}
  103. {{- "replication-password" -}}
  104. {{- end -}}
  105. {{- end -}}
  106. {{/*
  107. Get the admin-password key.
  108. */}}
  109. {{- define "postgresql.adminPasswordKey" -}}
  110. {{- if or .Values.global.postgresql.auth.existingSecret .Values.auth.existingSecret }}
  111. {{- if .Values.global.postgresql.auth.secretKeys.adminPasswordKey }}
  112. {{- printf "%s" (tpl .Values.global.postgresql.auth.secretKeys.adminPasswordKey $) -}}
  113. {{- else if .Values.auth.secretKeys.adminPasswordKey -}}
  114. {{- printf "%s" (tpl .Values.auth.secretKeys.adminPasswordKey $) -}}
  115. {{- end -}}
  116. {{- else -}}
  117. {{- "postgres-password" -}}
  118. {{- end -}}
  119. {{- end -}}
  120. {{/*
  121. Get the user-password key.
  122. */}}
  123. {{- define "postgresql.userPasswordKey" -}}
  124. {{- if or .Values.global.postgresql.auth.existingSecret .Values.auth.existingSecret }}
  125. {{- if or (empty (include "postgresql.username" .)) (eq (include "postgresql.username" .) "postgres") }}
  126. {{- printf "%s" (include "postgresql.adminPasswordKey" .) -}}
  127. {{- else -}}
  128. {{- if .Values.global.postgresql.auth.secretKeys.userPasswordKey }}
  129. {{- printf "%s" (tpl .Values.global.postgresql.auth.secretKeys.userPasswordKey $) -}}
  130. {{- else if .Values.auth.secretKeys.userPasswordKey -}}
  131. {{- printf "%s" (tpl .Values.auth.secretKeys.userPasswordKey $) -}}
  132. {{- end -}}
  133. {{- end -}}
  134. {{- else -}}
  135. {{- ternary "password" "postgres-password" (and (not (empty (include "postgresql.username" .))) (ne (include "postgresql.username" .) "postgres")) -}}
  136. {{- end -}}
  137. {{- end -}}
  138. {{/*
  139. Return true if a secret object should be created
  140. */}}
  141. {{- define "postgresql.createSecret" -}}
  142. {{- if not (or .Values.global.postgresql.auth.existingSecret .Values.auth.existingSecret) -}}
  143. {{- true -}}
  144. {{- end -}}
  145. {{- end -}}
  146. {{/*
  147. Return PostgreSQL service port
  148. */}}
  149. {{- define "postgresql.service.port" -}}
  150. {{- if .Values.global.postgresql.service.ports.postgresql }}
  151. {{- .Values.global.postgresql.service.ports.postgresql -}}
  152. {{- else -}}
  153. {{- .Values.primary.service.ports.postgresql -}}
  154. {{- end -}}
  155. {{- end -}}
  156. {{/*
  157. Return PostgreSQL service port
  158. */}}
  159. {{- define "postgresql.readReplica.service.port" -}}
  160. {{- if .Values.global.postgresql.service.ports.postgresql }}
  161. {{- .Values.global.postgresql.service.ports.postgresql -}}
  162. {{- else -}}
  163. {{- .Values.readReplicas.service.ports.postgresql -}}
  164. {{- end -}}
  165. {{- end -}}
  166. {{/*
  167. Get the PostgreSQL primary configuration ConfigMap name.
  168. */}}
  169. {{- define "postgresql.primary.configmapName" -}}
  170. {{- if .Values.primary.existingConfigmap -}}
  171. {{- printf "%s" (tpl .Values.primary.existingConfigmap $) -}}
  172. {{- else -}}
  173. {{- printf "%s-configuration" (include "postgresql.primary.fullname" .) -}}
  174. {{- end -}}
  175. {{- end -}}
  176. {{/*
  177. Return true if a configmap object should be created for PostgreSQL primary with the configuration
  178. */}}
  179. {{- define "postgresql.primary.createConfigmap" -}}
  180. {{- if and (or .Values.primary.configuration .Values.primary.pgHbaConfiguration) (not .Values.primary.existingConfigmap) }}
  181. {{- true -}}
  182. {{- else -}}
  183. {{- end -}}
  184. {{- end -}}
  185. {{/*
  186. Get the PostgreSQL primary extended configuration ConfigMap name.
  187. */}}
  188. {{- define "postgresql.primary.extendedConfigmapName" -}}
  189. {{- if .Values.primary.existingExtendedConfigmap -}}
  190. {{- printf "%s" (tpl .Values.primary.existingExtendedConfigmap $) -}}
  191. {{- else -}}
  192. {{- printf "%s-extended-configuration" (include "postgresql.primary.fullname" .) -}}
  193. {{- end -}}
  194. {{- end -}}
  195. {{/*
  196. Get the PostgreSQL read replica extended configuration ConfigMap name.
  197. */}}
  198. {{- define "postgresql.readReplicas.extendedConfigmapName" -}}
  199. {{- printf "%s-extended-configuration" (include "postgresql.readReplica.fullname" .) -}}
  200. {{- end -}}
  201. {{/*
  202. Return true if a configmap object should be created for PostgreSQL primary with the extended configuration
  203. */}}
  204. {{- define "postgresql.primary.createExtendedConfigmap" -}}
  205. {{- if and .Values.primary.extendedConfiguration (not .Values.primary.existingExtendedConfigmap) }}
  206. {{- true -}}
  207. {{- else -}}
  208. {{- end -}}
  209. {{- end -}}
  210. {{/*
  211. Return true if a configmap object should be created for PostgreSQL read replica with the extended configuration
  212. */}}
  213. {{- define "postgresql.readReplicas.createExtendedConfigmap" -}}
  214. {{- if .Values.readReplicas.extendedConfiguration }}
  215. {{- true -}}
  216. {{- else -}}
  217. {{- end -}}
  218. {{- end -}}
  219. {{/*
  220. Create the name of the service account to use
  221. */}}
  222. {{- define "postgresql.serviceAccountName" -}}
  223. {{- if .Values.serviceAccount.create -}}
  224. {{ default (include "common.names.fullname" .) .Values.serviceAccount.name }}
  225. {{- else -}}
  226. {{ default "default" .Values.serviceAccount.name }}
  227. {{- end -}}
  228. {{- end -}}
  229. {{/*
  230. Return true if a configmap should be mounted with PostgreSQL configuration
  231. */}}
  232. {{- define "postgresql.mountConfigurationCM" -}}
  233. {{- if or .Values.primary.configuration .Values.primary.pgHbaConfiguration .Values.primary.existingConfigmap }}
  234. {{- true -}}
  235. {{- end -}}
  236. {{- end -}}
  237. {{/*
  238. Get the initialization scripts ConfigMap name.
  239. */}}
  240. {{- define "postgresql.initdb.scriptsCM" -}}
  241. {{- if .Values.primary.initdb.scriptsConfigMap -}}
  242. {{- printf "%s" (tpl .Values.primary.initdb.scriptsConfigMap $) -}}
  243. {{- else -}}
  244. {{- printf "%s-init-scripts" (include "postgresql.primary.fullname" .) -}}
  245. {{- end -}}
  246. {{- end -}}
  247. {/*
  248. Return true if TLS is enabled for LDAP connection
  249. */}}
  250. {{- define "postgresql.ldap.tls.enabled" -}}
  251. {{- if and (kindIs "string" .Values.ldap.tls) (not (empty .Values.ldap.tls)) }}
  252. {{- true -}}
  253. {{- else if and (kindIs "map" .Values.ldap.tls) .Values.ldap.tls.enabled }}
  254. {{- true -}}
  255. {{- end -}}
  256. {{- end -}}
  257. {{/*
  258. Get the readiness probe command
  259. */}}
  260. {{- define "postgresql.readinessProbeCommand" -}}
  261. {{- $customUser := include "postgresql.username" . }}
  262. - |
  263. {{- if (include "postgresql.database" .) }}
  264. exec pg_isready -U {{ default "postgres" $customUser | quote }} -d "dbname={{ include "postgresql.database" . }} {{- if .Values.tls.enabled }} sslcert={{ include "postgresql.tlsCert" . }} sslkey={{ include "postgresql.tlsCertKey" . }}{{- end }}" -h 127.0.0.1 -p {{ .Values.containerPorts.postgresql }}
  265. {{- else }}
  266. exec pg_isready -U {{ default "postgres" $customUser | quote }} {{- if .Values.tls.enabled }} -d "sslcert={{ include "postgresql.tlsCert" . }} sslkey={{ include "postgresql.tlsCertKey" . }}"{{- end }} -h 127.0.0.1 -p {{ .Values.containerPorts.postgresql }}
  267. {{- end }}
  268. {{- if contains "bitnami/" .Values.image.repository }}
  269. [ -f /opt/bitnami/postgresql/tmp/.initialized ] || [ -f /bitnami/postgresql/.initialized ]
  270. {{- end -}}
  271. {{- end -}}
  272. {{/*
  273. Compile all warnings into a single message, and call fail.
  274. */}}
  275. {{- define "postgresql.validateValues" -}}
  276. {{- $messages := list -}}
  277. {{- $messages := append $messages (include "postgresql.validateValues.ldapConfigurationMethod" .) -}}
  278. {{- $messages := append $messages (include "postgresql.validateValues.psp" .) -}}
  279. {{- $messages := without $messages "" -}}
  280. {{- $message := join "\n" $messages -}}
  281. {{- if $message -}}
  282. {{- printf "\nVALUES VALIDATION:\n%s" $message | fail -}}
  283. {{- end -}}
  284. {{- end -}}
  285. {{/*
  286. Validate values of Postgresql - If ldap.url is used then you don't need the other settings for ldap
  287. */}}
  288. {{- define "postgresql.validateValues.ldapConfigurationMethod" -}}
  289. {{- if and .Values.ldap.enabled (and (not (empty .Values.ldap.url)) (not (empty .Values.ldap.server))) }}
  290. postgresql: ldap.url, ldap.server
  291. You cannot set both `ldap.url` and `ldap.server` at the same time.
  292. Please provide a unique way to configure LDAP.
  293. More info at https://www.postgresql.org/docs/current/auth-ldap.html
  294. {{- end -}}
  295. {{- end -}}
  296. {{/*
  297. Validate values of Postgresql - If PSP is enabled RBAC should be enabled too
  298. */}}
  299. {{- define "postgresql.validateValues.psp" -}}
  300. {{- if and .Values.psp.create (not .Values.rbac.create) }}
  301. postgresql: psp.create, rbac.create
  302. RBAC should be enabled if PSP is enabled in order for PSP to work.
  303. More info at https://kubernetes.io/docs/concepts/policy/pod-security-policy/#authorizing-policies
  304. {{- end -}}
  305. {{- end -}}
  306. {{/*
  307. Return the path to the cert file.
  308. */}}
  309. {{- define "postgresql.tlsCert" -}}
  310. {{- if .Values.tls.autoGenerated }}
  311. {{- printf "/opt/bitnami/postgresql/certs/tls.crt" -}}
  312. {{- else -}}
  313. {{- required "Certificate filename is required when TLS in enabled" .Values.tls.certFilename | printf "/opt/bitnami/postgresql/certs/%s" -}}
  314. {{- end -}}
  315. {{- end -}}
  316. {{/*
  317. Return the path to the cert key file.
  318. */}}
  319. {{- define "postgresql.tlsCertKey" -}}
  320. {{- if .Values.tls.autoGenerated }}
  321. {{- printf "/opt/bitnami/postgresql/certs/tls.key" -}}
  322. {{- else -}}
  323. {{- required "Certificate Key filename is required when TLS in enabled" .Values.tls.certKeyFilename | printf "/opt/bitnami/postgresql/certs/%s" -}}
  324. {{- end -}}
  325. {{- end -}}
  326. {{/*
  327. Return the path to the CA cert file.
  328. */}}
  329. {{- define "postgresql.tlsCACert" -}}
  330. {{- if .Values.tls.autoGenerated }}
  331. {{- printf "/opt/bitnami/postgresql/certs/ca.crt" -}}
  332. {{- else -}}
  333. {{- printf "/opt/bitnami/postgresql/certs/%s" .Values.tls.certCAFilename -}}
  334. {{- end -}}
  335. {{- end -}}
  336. {{/*
  337. Return the path to the CRL file.
  338. */}}
  339. {{- define "postgresql.tlsCRL" -}}
  340. {{- if .Values.tls.crlFilename -}}
  341. {{- printf "/opt/bitnami/postgresql/certs/%s" .Values.tls.crlFilename -}}
  342. {{- end -}}
  343. {{- end -}}
  344. {{/*
  345. Return true if a TLS credentials secret object should be created
  346. */}}
  347. {{- define "postgresql.createTlsSecret" -}}
  348. {{- if and .Values.tls.autoGenerated (not .Values.tls.certificatesSecret) }}
  349. {{- true -}}
  350. {{- end -}}
  351. {{- end -}}
  352. {{/*
  353. Return the path to the CA cert file.
  354. */}}
  355. {{- define "postgresql.tlsSecretName" -}}
  356. {{- if .Values.tls.autoGenerated }}
  357. {{- printf "%s-crt" (include "common.names.fullname" .) -}}
  358. {{- else -}}
  359. {{ required "A secret containing TLS certificates is required when TLS is enabled" .Values.tls.certificatesSecret }}
  360. {{- end -}}
  361. {{- end -}}