_mariadb.tpl 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. {{- end -}}
  71. {{- else -}}
  72. {{/*
  73. If the key is not present in ixChartContext,
  74. means we are outside SCALE (Probably CI),
  75. let upgrade job run
  76. */}}
  77. {{- $enableBackupJob = true -}}
  78. {{- end }}
  79. mariadbbackup:
  80. enabled: {{ $enableBackupJob }}
  81. type: Job
  82. annotations:
  83. "helm.sh/hook": pre-upgrade
  84. "helm.sh/hook-weight": "1"
  85. "helm.sh/hook-delete-policy": hook-succeeded
  86. podSpec:
  87. restartPolicy: Never
  88. containers:
  89. mariadbbackup:
  90. enabled: true
  91. primary: true
  92. imageSelector: mariadbImage
  93. securityContext:
  94. runAsUser: 999
  95. runAsGroup: 999
  96. readOnlyRootFilesystem: false
  97. probes:
  98. liveness:
  99. enabled: false
  100. readiness:
  101. enabled: false
  102. startup:
  103. enabled: false
  104. resources:
  105. limits:
  106. cpu: 2000m
  107. memory: 2Gi
  108. envFrom:
  109. - secretRef:
  110. name: {{ $secretName }}
  111. command:
  112. - sh
  113. - -c
  114. - |
  115. until mariadb-admin --user=root --host="${MARIADB_HOST}" --password="${MARIADB_ROOT_PASSWORD}" --connect-timeout=5 ping
  116. do
  117. echo "Waiting for mariadb to be ready. Sleeping 2 seconds"
  118. sleep 2s
  119. done
  120. until mariadb-admin --user=root --host="${MARIADB_HOST}" --password="${MARIADB_ROOT_PASSWORD}" --connect-timeout=5 status
  121. do
  122. echo "Waiting for mariadb to be alive. Sleeping 2 seconds"
  123. sleep 2s
  124. done
  125. echo "Creating backup of ${MARIADB_DATABASE} database"
  126. mariadb-dump ${MARIADB_DATABASE} --host="${MARIADB_HOST}" \
  127. --user=root --password="${MARIADB_ROOT_PASSWORD}" \
  128. > {{ $backupPath }}/${MARIADB_DATABASE}_$(date +%Y-%m-%d_%H-%M-%S).sql \
  129. || echo "Failed to create backup"
  130. echo "Backup finished"
  131. initContainers:
  132. {{- include "ix.v1.common.app.permissions" (dict "UID" 999 "GID" 999 "type" "init" "mode" $backupChownMode) | nindent 6 }}
  133. {{- end -}}
  134. {{/* Returns a mariadb-wait container for waiting for mariadb to be ready */}}
  135. {{/* Call this template:
  136. {{ include "ix.v1.common.app.mariadbWait" (dict "name" "mariadb-wait" "secretName" "mariadb-creds") }}
  137. name (optional): Name of the mariadb-wait container (default: mariadb-wait)
  138. secretName (required): Name of the secret containing the mariadb credentials
  139. */}}
  140. {{- define "ix.v1.common.app.mariadbWait" -}}
  141. {{- $name := .name | default "mariadb-wait" -}}
  142. {{- $secretName := (required "Mariadb-Wait - Secret Name is required" .secretName) }}
  143. {{ $name }}:
  144. enabled: true
  145. type: init
  146. imageSelector: mariadbImage
  147. envFrom:
  148. - secretRef:
  149. name: {{ $secretName }}
  150. resources:
  151. limits:
  152. cpu: 500m
  153. memory: 256Mi
  154. command: bash
  155. args:
  156. - -c
  157. - |
  158. echo "Waiting for mariadb to be ready"
  159. until mariadb-admin --user=root --host="${MARIADB_HOST}" --password="${MARIADB_ROOT_PASSWORD}" --connect-timeout=5 ping
  160. do
  161. echo "Waiting for mariadb to be ready. Sleeping 2 seconds"
  162. sleep 2s
  163. done
  164. until mariadb-admin --user=root --host="${MARIADB_HOST}" --password="${MARIADB_ROOT_PASSWORD}" --connect-timeout=5 status
  165. do
  166. echo "Waiting for mariadb to be alive. Sleeping 2 seconds"
  167. sleep 2s
  168. done
  169. {{- end -}}