_helpers.tpl 14 KB

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