_postgres.tpl 6.9 KB

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