_postgres.tpl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. {{- $resources := (required "Postgres - Resources are required" .resources) }}
  15. {{ $name }}:
  16. enabled: true
  17. type: Deployment
  18. podSpec:
  19. containers:
  20. {{ $name }}:
  21. enabled: true
  22. primary: true
  23. imageSelector: postgresImage
  24. securityContext:
  25. runAsUser: 999
  26. runAsGroup: 999
  27. readOnlyRootFilesystem: false
  28. resources:
  29. limits:
  30. cpu: {{ $resources.limits.cpu }}
  31. memory: {{ $resources.limits.memory }}
  32. envFrom:
  33. - secretRef:
  34. name: {{ $secretName }}
  35. probes:
  36. liveness:
  37. enabled: true
  38. type: exec
  39. command:
  40. - sh
  41. - -c
  42. - "until pg_isready -U ${POSTGRES_USER} -h localhost; do sleep 2; done"
  43. readiness:
  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. startup:
  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. initContainers:
  58. {{- include "ix.v1.common.app.permissions" (dict "UID" 999 "GID" 999) | nindent 6 }}
  59. postgresbackup:
  60. enabled: true
  61. type: Job
  62. annotations:
  63. "helm.sh/hook": pre-upgrade
  64. "helm.sh/hook-weight": "1"
  65. "helm.sh/hook-delete-policy": hook-succeeded
  66. podSpec:
  67. restartPolicy: Never
  68. containers:
  69. postgresbackup:
  70. enabled: true
  71. primary: true
  72. imageSelector: postgresImage
  73. securityContext:
  74. runAsUser: 999
  75. runAsGroup: 999
  76. readOnlyRootFilesystem: false
  77. probes:
  78. liveness:
  79. enabled: false
  80. readiness:
  81. enabled: false
  82. startup:
  83. enabled: false
  84. resources:
  85. limits:
  86. cpu: 2000m
  87. memory: 2Gi
  88. envFrom:
  89. - secretRef:
  90. name: {{ $secretName }}
  91. command:
  92. - sh
  93. - -c
  94. - |
  95. until pg_isready -U ${POSTGRES_USER} -h ${POSTGRES_HOST}; do sleep 2; done
  96. echo "Creating backup of ${POSTGRES_DB} database"
  97. pg_dump --dbname=${POSTGRES_URL} --file {{ $backupPath }}/${POSTGRES_DB}_$(date +%Y-%m-%d_%H-%M-%S).sql || echo "Failed to create backup"
  98. echo "Backup finished"
  99. initContainers:
  100. {{- include "ix.v1.common.app.permissions" (dict "UID" 999 "GID" 999 "type" "init") | nindent 6 }}
  101. {{- end -}}