_postgres.tpl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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" (dict "UID" 999 "GID" 999) | nindent 6 }}
  64. {{- $enableBackupJob := false -}}
  65. {{- if hasKey $ixChartContext "isUpgrade" -}}
  66. {{- if $ixChartContext.isUpgrade -}}
  67. {{- $enableBackupJob = true -}}
  68. {{- end -}}
  69. {{- else -}}
  70. {{/*
  71. If the key is not present in ixChartContext,
  72. means we are outside SCALE (Probably CI),
  73. let upgrade job run
  74. */}}
  75. {{- $enableBackupJob = true -}}
  76. {{- end }}
  77. postgresbackup:
  78. enabled: {{ $enableBackupJob }}
  79. type: Job
  80. annotations:
  81. "helm.sh/hook": pre-upgrade
  82. "helm.sh/hook-weight": "1"
  83. "helm.sh/hook-delete-policy": hook-succeeded
  84. podSpec:
  85. restartPolicy: Never
  86. containers:
  87. postgresbackup:
  88. enabled: true
  89. primary: true
  90. imageSelector: postgresImage
  91. securityContext:
  92. runAsUser: 999
  93. runAsGroup: 999
  94. readOnlyRootFilesystem: false
  95. probes:
  96. liveness:
  97. enabled: false
  98. readiness:
  99. enabled: false
  100. startup:
  101. enabled: false
  102. resources:
  103. limits:
  104. cpu: 2000m
  105. memory: 2Gi
  106. envFrom:
  107. - secretRef:
  108. name: {{ $secretName }}
  109. command:
  110. - sh
  111. - -c
  112. - |
  113. until pg_isready -U ${POSTGRES_USER} -h ${POSTGRES_HOST}; do sleep 2; done
  114. echo "Creating backup of ${POSTGRES_DB} database"
  115. pg_dump --dbname=${POSTGRES_URL} --file {{ $backupPath }}/${POSTGRES_DB}_$(date +%Y-%m-%d_%H-%M-%S).sql || echo "Failed to create backup"
  116. echo "Backup finished"
  117. initContainers:
  118. {{- include "ix.v1.common.app.permissions" (dict "UID" 999 "GID" 999 "type" "init" "mode" $backupChownMode) | nindent 6 }}
  119. {{- end -}}
  120. {{/* Returns a postgres-wait container for waiting for postgres to be ready */}}
  121. {{/* Call this template:
  122. {{ include "ix.v1.common.app.postgresWait" (dict "name" "postgres-wait" "secretName" "postgres-creds") }}
  123. name (optional): Name of the postgres-wait container (default: postgres-wait)
  124. secretName (required): Name of the secret containing the postgres credentials
  125. */}}
  126. {{- define "ix.v1.common.app.postgresWait" -}}
  127. {{- $name := .name | default "postgres-wait" -}}
  128. {{- $secretName := (required "Postgres-Wait - Secret Name is required" .secretName) }}
  129. {{ $name }}:
  130. enabled: true
  131. type: init
  132. imageSelector: postgresImage
  133. envFrom:
  134. - secretRef:
  135. name: {{ $secretName }}
  136. resources:
  137. limits:
  138. cpu: 500m
  139. memory: 256Mi
  140. command: bash
  141. args:
  142. - -c
  143. - |
  144. echo "Waiting for postgres to be ready"
  145. until pg_isready -h ${POSTGRES_HOST} -U ${POSTGRES_USER} -d ${POSTGRES_DB}; do
  146. sleep 2
  147. done
  148. {{- end -}}