_mariadb.tpl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. {{/* Returns a mariadb 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.mariadb" (dict "name" "mariadb" "secretName" "mariadb-creds" "backupPath" "/mariadb_backup" "resources" .Values.resources) }}
  5. name (optional): Name of the mariadb pod/container (default: mariadb)
  6. secretName (required): Name of the secret containing the mariadb credentials
  7. backupPath (optional): Path to store the backup, it's the container's path (default: /mariadb_backup)
  8. resources (required): Resources for the mariadb 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.mariadb" -}}
  14. {{- $name := .name | default "mariadb" -}}
  15. {{- $secretName := (required "MariaDB - Secret Name is required" .secretName) -}}
  16. {{- $backupPath := .backupPath | default "/mariadb_backup" -}}
  17. {{- $backupChownMode := .backupChownMode | default "check" -}}
  18. {{- $ixChartContext := .ixChartContext -}}
  19. {{- $resources := (required "MariadDB - 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: mariadbImage
  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. {{- $args := "--user=root --host=localhost --password=$MARIADB_ROOT_PASSWORD" }}
  42. liveness:
  43. enabled: true
  44. type: exec
  45. command:
  46. - sh
  47. - -c
  48. - "until mariadb-admin {{ $args }} ping && mariadb-admin {{ $args }} status; do sleep 2; done"
  49. readiness:
  50. enabled: true
  51. type: exec
  52. command:
  53. - sh
  54. - -c
  55. - "until mariadb-admin {{ $args }} ping && mariadb-admin {{ $args }} status; do sleep 2; done"
  56. startup:
  57. enabled: true
  58. type: exec
  59. command:
  60. - sh
  61. - -c
  62. - "until mariadb-admin {{ $args }} ping && mariadb-admin {{ $args }} status; do sleep 2; done"
  63. initContainers:
  64. {{- include "ix.v1.common.app.permissions" (dict "UID" 999 "GID" 999) | nindent 6 }}
  65. {{/* Backup Job */}}
  66. {{- $enableBackupJob := false -}}
  67. {{- if hasKey $ixChartContext "isUpgrade" -}}
  68. {{- if $ixChartContext.isUpgrade -}}
  69. {{- $enableBackupJob = true -}}
  70. {{- if hasKey $ixChartContext "isStopped" -}}
  71. {{- if $ixChartContext.isStopped -}}
  72. {{- fail "Application must be running before upgrade. This is to ensure the database backup will be able to complete." -}}
  73. {{- end -}}
  74. {{- end -}}
  75. {{- end -}}
  76. {{- else -}}
  77. {{/*
  78. If the key is not present in ixChartContext,
  79. means we are outside SCALE (Probably CI),
  80. let upgrade job run
  81. */}}
  82. {{- $enableBackupJob = true -}}
  83. {{- end }}
  84. mariadbbackup:
  85. enabled: {{ $enableBackupJob }}
  86. type: Job
  87. annotations:
  88. "helm.sh/hook": pre-upgrade
  89. "helm.sh/hook-weight": "1"
  90. "helm.sh/hook-delete-policy": hook-succeeded
  91. podSpec:
  92. restartPolicy: Never
  93. containers:
  94. mariadbbackup:
  95. enabled: true
  96. primary: true
  97. imageSelector: mariadbImage
  98. securityContext:
  99. runAsUser: 999
  100. runAsGroup: 999
  101. readOnlyRootFilesystem: false
  102. probes:
  103. liveness:
  104. enabled: false
  105. readiness:
  106. enabled: false
  107. startup:
  108. enabled: false
  109. resources:
  110. limits:
  111. cpu: 2000m
  112. memory: 2Gi
  113. envFrom:
  114. - secretRef:
  115. name: {{ $secretName }}
  116. command:
  117. - sh
  118. - -c
  119. - |
  120. until mariadb-admin --user=root --host="${MARIADB_HOST}" --password="${MARIADB_ROOT_PASSWORD}" --connect-timeout=5 ping
  121. do
  122. echo "Waiting for mariadb to be ready. Sleeping 2 seconds"
  123. sleep 2s
  124. done
  125. until mariadb-admin --user=root --host="${MARIADB_HOST}" --password="${MARIADB_ROOT_PASSWORD}" --connect-timeout=5 status
  126. do
  127. echo "Waiting for mariadb to be alive. Sleeping 2 seconds"
  128. sleep 2s
  129. done
  130. echo "Creating backup of ${MARIADB_DATABASE} database"
  131. mariadb-dump ${MARIADB_DATABASE} --host="${MARIADB_HOST}" \
  132. --user=root --password="${MARIADB_ROOT_PASSWORD}" \
  133. > {{ $backupPath }}/${MARIADB_DATABASE}_$(date +%Y-%m-%d_%H-%M-%S).sql \
  134. || echo "Failed to create backup"
  135. echo "Backup finished"
  136. initContainers:
  137. {{- include "ix.v1.common.app.permissions" (dict "UID" 999 "GID" 999 "type" "init" "mode" $backupChownMode) | nindent 6 }}
  138. {{- end -}}
  139. {{/* Returns a mariadb-wait container for waiting for mariadb to be ready */}}
  140. {{/* Call this template:
  141. {{ include "ix.v1.common.app.mariadbWait" (dict "name" "mariadb-wait" "secretName" "mariadb-creds") }}
  142. name (optional): Name of the mariadb-wait container (default: mariadb-wait)
  143. secretName (required): Name of the secret containing the mariadb credentials
  144. */}}
  145. {{- define "ix.v1.common.app.mariadbWait" -}}
  146. {{- $name := .name | default "mariadb-wait" -}}
  147. {{- $secretName := (required "Mariadb-Wait - Secret Name is required" .secretName) }}
  148. {{ $name }}:
  149. enabled: true
  150. type: init
  151. imageSelector: mariadbImage
  152. envFrom:
  153. - secretRef:
  154. name: {{ $secretName }}
  155. resources:
  156. limits:
  157. cpu: 500m
  158. memory: 256Mi
  159. command: bash
  160. args:
  161. - -c
  162. - |
  163. echo "Waiting for mariadb to be ready"
  164. until mariadb-admin --user=root --host="${MARIADB_HOST}" --password="${MARIADB_ROOT_PASSWORD}" --connect-timeout=5 ping
  165. do
  166. echo "Waiting for mariadb to be ready. Sleeping 2 seconds"
  167. sleep 2s
  168. done
  169. until mariadb-admin --user=root --host="${MARIADB_HOST}" --password="${MARIADB_ROOT_PASSWORD}" --connect-timeout=5 status
  170. do
  171. echo "Waiting for mariadb to be alive. Sleeping 2 seconds"
  172. sleep 2s
  173. done
  174. {{- end -}}