_postgres.tpl 7.3 KB

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