_postgres.tpl 4.8 KB

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