_postgres.tpl 7.2 KB

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