_postgres.tpl 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. {{/* Returns a postgres pod with init container for fixing permissions
  2. and a pre-upgrade job to backup the database */}}
  3. {{/* Call this template:
  4. {{ include "ix.v1.common.app.postgres" (dict "name" "postgres" "secretName" "postgres-creds" "backupPath" "/postgres_backup" "resources" .Values.resources) }}
  5. name (optional): Name of the postgres pod/container (default: postgres)
  6. secretName (required): Name of the secret containing the postgres credentials
  7. backupPath (optional): Path to store the backup, it's the container's path (default: /postgres_backup)
  8. resources (required): Resources for the postgres container
  9. backupChownMode (optional): Whether to chown the backup directory or
  10. check parent directory permissions and fix them if needed.
  11. (default: check) Valid values: always, check
  12. */}}
  13. {{- define "ix.v1.common.app.postgres" -}}
  14. {{- $name := .name | default "postgres" -}}
  15. {{- $imageSelector := .imageSelector | default "postgresImage" -}}
  16. {{- $secretName := (required "Postgres - Secret Name is required" .secretName) -}}
  17. {{- $backupSecretName := .backupSecretName | default $secretName -}}
  18. {{- $backupPath := .backupPath | default "/postgres_backup" -}}
  19. {{- $backupChownMode := .backupChownMode | default "check" -}}
  20. {{- $ixChartContext := .ixChartContext -}}
  21. {{- $preUpgradeTasks := .preUpgradeTasks | default list -}}
  22. {{- $resources := (required "Postgres - Resources are required" .resources) }}
  23. {{ $name }}:
  24. enabled: true
  25. type: Deployment
  26. podSpec:
  27. containers:
  28. {{ $name }}:
  29. enabled: true
  30. primary: true
  31. imageSelector: {{ $imageSelector }}
  32. securityContext:
  33. runAsUser: 999
  34. runAsGroup: 999
  35. readOnlyRootFilesystem: false
  36. resources:
  37. limits:
  38. cpu: {{ $resources.limits.cpu }}
  39. memory: {{ $resources.limits.memory }}
  40. envFrom:
  41. - secretRef:
  42. name: {{ $secretName }}
  43. probes:
  44. liveness:
  45. enabled: true
  46. type: exec
  47. command:
  48. - sh
  49. - -c
  50. - "until pg_isready -U ${POSTGRES_USER} -h localhost; do sleep 2; done"
  51. readiness:
  52. enabled: true
  53. type: exec
  54. command:
  55. - sh
  56. - -c
  57. - "until pg_isready -U ${POSTGRES_USER} -h localhost; do sleep 2; done"
  58. startup:
  59. enabled: true
  60. type: exec
  61. command:
  62. - sh
  63. - -c
  64. - "until pg_isready -U ${POSTGRES_USER} -h localhost; do sleep 2; done"
  65. initContainers:
  66. {{- include "ix.v1.common.app.permissions"
  67. (dict
  68. "UID" 999
  69. "GID" 999
  70. "type" "install"
  71. "containerName" "permissions"
  72. ) | nindent 6 }}
  73. {{- $enableBackupJob := false -}}
  74. {{- if hasKey $ixChartContext "isUpgrade" -}}
  75. {{- if $ixChartContext.isUpgrade -}}
  76. {{- $enableBackupJob = true -}}
  77. {{- if hasKey $ixChartContext "isStopped" -}}
  78. {{- if $ixChartContext.isStopped -}}
  79. {{- fail "Application must be running before upgrade. This is to ensure the database backup will be able to complete." -}}
  80. {{- end -}}
  81. {{- end -}}
  82. {{- end -}}
  83. {{- else -}}
  84. {{/* If the key is not present in ixChartContext, means we
  85. are outside SCALE (Probably CI), let upgrade job run */}}
  86. {{- $enableBackupJob = true -}}
  87. {{- end }}
  88. postgresbackup:
  89. enabled: {{ $enableBackupJob }}
  90. type: Job
  91. annotations:
  92. "helm.sh/hook": pre-upgrade
  93. "helm.sh/hook-weight": "1"
  94. "helm.sh/hook-delete-policy": hook-succeeded
  95. podSpec:
  96. restartPolicy: Never
  97. containers:
  98. postgresbackup:
  99. enabled: true
  100. primary: true
  101. imageSelector: {{ $imageSelector }}
  102. securityContext:
  103. runAsUser: 999
  104. runAsGroup: 999
  105. readOnlyRootFilesystem: false
  106. probes:
  107. liveness:
  108. enabled: false
  109. readiness:
  110. enabled: false
  111. startup:
  112. enabled: false
  113. resources:
  114. limits:
  115. cpu: 2000m
  116. memory: 2Gi
  117. envFrom:
  118. - secretRef:
  119. name: {{ $backupSecretName }}
  120. command:
  121. - sh
  122. - -c
  123. - |
  124. until pg_isready -U ${POSTGRES_USER} -h ${POSTGRES_HOST}; do sleep 2; done
  125. echo "Creating backup of ${POSTGRES_DB} database"
  126. pg_dump --dbname=${POSTGRES_URL} --file {{ $backupPath }}/${POSTGRES_DB}_$(date +%Y-%m-%d_%H-%M-%S).sql || echo "Failed to create backup"
  127. echo "Backup finished"
  128. {{- range $task := $preUpgradeTasks }}
  129. {{ $task }}
  130. {{- end }}
  131. initContainers:
  132. {{- include "ix.v1.common.app.permissions"
  133. (dict
  134. "UID" 999
  135. "GID" 999
  136. "type" "init"
  137. "mode" $backupChownMode
  138. "containerName" "permissions"
  139. ) | nindent 6 }}
  140. {{- end -}}
  141. {{/* Returns a postgres-wait container for waiting for postgres to be ready */}}
  142. {{/* Call this template:
  143. {{ include "ix.v1.common.app.postgresWait" (dict "name" "postgres-wait" "secretName" "postgres-creds") }}
  144. name (optional): Name of the postgres-wait container (default: postgres-wait)
  145. secretName (required): Name of the secret containing the postgres credentials
  146. */}}
  147. {{- define "ix.v1.common.app.postgresWait" -}}
  148. {{- $name := .name | default "postgres-wait" -}}
  149. {{- $secretName := (required "Postgres-Wait - Secret Name is required" .secretName) }}
  150. {{ $name }}:
  151. enabled: true
  152. type: init
  153. imageSelector: postgresImage
  154. envFrom:
  155. - secretRef:
  156. name: {{ $secretName }}
  157. resources:
  158. limits:
  159. cpu: 500m
  160. memory: 256Mi
  161. command: bash
  162. args:
  163. - -c
  164. - |
  165. echo "Waiting for postgres to be ready"
  166. until pg_isready -h ${POSTGRES_HOST} -U ${POSTGRES_USER} -d ${POSTGRES_DB}; do
  167. sleep 2
  168. done
  169. {{- end -}}
  170. {{/* Returns persistence entries for postgres */}}
  171. {{/* Call this template:
  172. {{ include "ix.v1.common.app.postgresPersistence" (dict "pgData" .Values.storage.pgData "pgBackup" .Values.storage.pgBackup) }}
  173. pgData (required): Data persistence configuration
  174. pgBackup (required): Data persistence configuration for backup
  175. */}}
  176. {{- define "ix.v1.common.app.postgresPersistence" -}}
  177. {{- $data := .pgData -}}
  178. {{- $backup := .pgBackup }}
  179. {{- if not $data -}}
  180. {{- fail "Postgres - Data persistence configuration is required" -}}
  181. {{- end -}}
  182. {{- if not $backup -}}
  183. {{- fail "Postgres - Backup persistence configuration is required" -}}
  184. {{- end -}}
  185. postgresdata:
  186. enabled: true
  187. {{- include "ix.v1.common.app.storageOptions" (dict "storage" $data) | nindent 2 }}
  188. targetSelector:
  189. postgres:
  190. postgres:
  191. mountPath: /var/lib/postgresql/data
  192. permissions:
  193. mountPath: /mnt/directories/postgres_data
  194. postgresbackup:
  195. enabled: true
  196. {{- include "ix.v1.common.app.storageOptions" (dict "storage" $backup) | nindent 2 }}
  197. targetSelector:
  198. postgresbackup:
  199. postgresbackup:
  200. mountPath: /postgres_backup
  201. permissions:
  202. mountPath: /mnt/directories/postgres_backup
  203. {{- end -}}
  204. {{/* Returns service entry for postgres */}}
  205. {{/* Call this template:
  206. {{ include "ix.v1.common.app.postgresService" . }}
  207. */}}
  208. {{- define "ix.v1.common.app.postgresService" -}}
  209. postgres:
  210. enabled: true
  211. type: ClusterIP
  212. targetSelector: postgres
  213. ports:
  214. postgres:
  215. enabled: true
  216. primary: true
  217. port: 5432
  218. targetPort: 5432
  219. targetSelector: postgres
  220. {{- end -}}