statefulset.yaml 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. apiVersion: {{ include "common.capabilities.statefulset.apiVersion" . }}
  2. kind: StatefulSet
  3. metadata:
  4. name: {{ include "mysql.primary.fullname" . }}
  5. namespace: {{ include "common.names.namespace" . | quote }}
  6. labels: {{- include "common.labels.standard" . | nindent 4 }}
  7. app.kubernetes.io/component: primary
  8. {{- if .Values.commonLabels }}
  9. {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
  10. {{- end }}
  11. {{- if .Values.commonAnnotations }}
  12. annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  13. {{- end }}
  14. spec:
  15. replicas: 1
  16. podManagementPolicy: {{ .Values.primary.podManagementPolicy | quote }}
  17. selector:
  18. matchLabels: {{ include "common.labels.matchLabels" . | nindent 6 }}
  19. app.kubernetes.io/component: primary
  20. serviceName: {{ include "mysql.primary.fullname" . }}
  21. {{- if .Values.primary.updateStrategy }}
  22. updateStrategy: {{- toYaml .Values.primary.updateStrategy | nindent 4 }}
  23. {{- end }}
  24. template:
  25. metadata:
  26. annotations:
  27. {{- if (include "mysql.primary.createConfigmap" .) }}
  28. checksum/configuration: {{ include (print $.Template.BasePath "/primary/configmap.yaml") . | sha256sum }}
  29. {{- end }}
  30. {{- if .Values.primary.podAnnotations }}
  31. {{- include "common.tplvalues.render" (dict "value" .Values.primary.podAnnotations "context" $) | nindent 8 }}
  32. {{- end }}
  33. labels: {{- include "common.labels.standard" . | nindent 8 }}
  34. app.kubernetes.io/component: primary
  35. {{- if .Values.primary.podLabels }}
  36. {{- include "common.tplvalues.render" ( dict "value" .Values.primary.podLabels "context" $ ) | nindent 8 }}
  37. {{- end }}
  38. spec:
  39. serviceAccountName: {{ template "mysql.serviceAccountName" . }}
  40. {{- include "mysql.imagePullSecrets" . | nindent 6 }}
  41. {{- if .Values.primary.hostAliases }}
  42. hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.primary.hostAliases "context" $) | nindent 8 }}
  43. {{- end }}
  44. {{- if .Values.primary.affinity }}
  45. affinity: {{- include "common.tplvalues.render" (dict "value" .Values.primary.affinity "context" $) | nindent 8 }}
  46. {{- else }}
  47. affinity:
  48. podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.primary.podAffinityPreset "context" $) | nindent 10 }}
  49. podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.primary.podAntiAffinityPreset "context" $) | nindent 10 }}
  50. nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.primary.nodeAffinityPreset.type "key" .Values.primary.nodeAffinityPreset.key "values" .Values.primary.nodeAffinityPreset.values) | nindent 10 }}
  51. {{- end }}
  52. {{- if .Values.primary.nodeSelector }}
  53. nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.primary.nodeSelector "context" $) | nindent 8 }}
  54. {{- end }}
  55. {{- if .Values.primary.tolerations }}
  56. tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.primary.tolerations "context" $) | nindent 8 }}
  57. {{- end }}
  58. {{- if .Values.primary.priorityClassName }}
  59. priorityClassName: {{ .Values.primary.priorityClassName | quote }}
  60. {{- end }}
  61. {{- if .Values.primary.runtimeClassName }}
  62. runtimeClassName: {{ .Values.primary.runtimeClassName | quote }}
  63. {{- end }}
  64. {{- if .Values.primary.schedulerName }}
  65. schedulerName: {{ .Values.primary.schedulerName | quote }}
  66. {{- end }}
  67. {{- if .Values.primary.topologySpreadConstraints }}
  68. topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.primary.topologySpreadConstraints "context" .) | nindent 8 }}
  69. {{- end }}
  70. {{- if .Values.primary.podSecurityContext.enabled }}
  71. securityContext: {{- omit .Values.primary.podSecurityContext "enabled" | toYaml | nindent 8 }}
  72. {{- end }}
  73. {{- if .Values.primary.terminationGracePeriodSeconds }}
  74. terminationGracePeriodSeconds: {{ .Values.primary.terminationGracePeriodSeconds }}
  75. {{- end }}
  76. initContainers:
  77. {{- if and .Values.primary.podSecurityContext.enabled .Values.volumePermissions.enabled .Values.primary.persistence.enabled }}
  78. - name: volume-permissions
  79. image: {{ include "mysql.volumePermissions.image" . }}
  80. imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
  81. command:
  82. - /bin/bash
  83. - -ec
  84. - |
  85. mkdir -p "/bitnami/mysql"
  86. chown "{{ .Values.primary.containerSecurityContext.runAsUser }}:{{ .Values.primary.podSecurityContext.fsGroup }}" "/bitnami/mysql"
  87. find "/bitnami/mysql" -mindepth 1 -maxdepth 1 -not -name ".snapshot" -not -name "lost+found" | xargs -r chown -R "{{ .Values.primary.containerSecurityContext.runAsUser }}:{{ .Values.primary.podSecurityContext.fsGroup }}"
  88. securityContext:
  89. runAsUser: 0
  90. {{- if .Values.volumePermissions.resources }}
  91. resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
  92. {{- end }}
  93. volumeMounts:
  94. - name: data
  95. mountPath: /bitnami/mysql
  96. {{- if .Values.primary.persistence.subPath }}
  97. subPath: {{ .Values.primary.persistence.subPath }}
  98. {{- end }}
  99. {{- end }}
  100. {{- if .Values.primary.initContainers }}
  101. {{- include "common.tplvalues.render" (dict "value" .Values.primary.initContainers "context" $) | nindent 8 }}
  102. {{- end }}
  103. containers:
  104. - name: mysql
  105. image: {{ include "mysql.image" . }}
  106. imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
  107. {{- if .Values.primary.containerSecurityContext.enabled }}
  108. securityContext: {{- omit .Values.primary.containerSecurityContext "enabled" | toYaml | nindent 12 }}
  109. {{- end }}
  110. {{- if .Values.diagnosticMode.enabled }}
  111. command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
  112. {{- else if .Values.primary.command }}
  113. command: {{- include "common.tplvalues.render" (dict "value" .Values.primary.command "context" $) | nindent 12 }}
  114. {{- end }}
  115. {{- if .Values.diagnosticMode.enabled }}
  116. args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
  117. {{- else if .Values.primary.args }}
  118. args: {{- include "common.tplvalues.render" (dict "value" .Values.primary.args "context" $) | nindent 12 }}
  119. {{- end }}
  120. {{- if .Values.primary.lifecycleHooks }}
  121. lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.primary.lifecycleHooks "context" $) | nindent 12 }}
  122. {{- end }}
  123. env:
  124. - name: BITNAMI_DEBUG
  125. value: {{ ternary "true" "false" (or .Values.image.debug .Values.diagnosticMode.enabled) | quote }}
  126. {{- if .Values.auth.usePasswordFiles }}
  127. - name: MYSQL_ROOT_PASSWORD_FILE
  128. value: {{ default "/opt/bitnami/mysql/secrets/mysql-root-password" .Values.auth.customPasswordFiles.root }}
  129. {{- else }}
  130. - name: MYSQL_ROOT_PASSWORD
  131. valueFrom:
  132. secretKeyRef:
  133. name: {{ template "mysql.secretName" . }}
  134. key: mysql-root-password
  135. {{- end }}
  136. {{- if not (empty .Values.auth.username) }}
  137. - name: MYSQL_USER
  138. value: {{ .Values.auth.username | quote }}
  139. {{- if .Values.auth.usePasswordFiles }}
  140. - name: MYSQL_PASSWORD_FILE
  141. value: {{ default "/opt/bitnami/mysql/secrets/mysql-password" .Values.auth.customPasswordFiles.user }}
  142. {{- else }}
  143. - name: MYSQL_PASSWORD
  144. valueFrom:
  145. secretKeyRef:
  146. name: {{ template "mysql.secretName" . }}
  147. key: mysql-password
  148. {{- end }}
  149. {{- end }}
  150. {{- if and .Values.auth.createDatabase .Values.auth.database }}
  151. - name: MYSQL_DATABASE
  152. value: {{ .Values.auth.database | quote }}
  153. {{- end }}
  154. {{- if eq .Values.architecture "replication" }}
  155. - name: MYSQL_REPLICATION_MODE
  156. value: "master"
  157. - name: MYSQL_REPLICATION_USER
  158. value: {{ .Values.auth.replicationUser | quote }}
  159. {{- if .Values.auth.usePasswordFiles }}
  160. - name: MYSQL_REPLICATION_PASSWORD_FILE
  161. value: {{ default "/opt/bitnami/mysql/secrets/mysql-replication-password" .Values.auth.customPasswordFiles.replicator }}
  162. {{- else }}
  163. - name: MYSQL_REPLICATION_PASSWORD
  164. valueFrom:
  165. secretKeyRef:
  166. name: {{ template "mysql.secretName" . }}
  167. key: mysql-replication-password
  168. {{- end }}
  169. {{- end }}
  170. {{- if .Values.primary.extraFlags }}
  171. - name: MYSQL_EXTRA_FLAGS
  172. value: "{{ .Values.primary.extraFlags }}"
  173. {{- end }}
  174. {{- if .Values.primary.extraEnvVars }}
  175. {{- include "common.tplvalues.render" (dict "value" .Values.primary.extraEnvVars "context" $) | nindent 12 }}
  176. {{- end }}
  177. envFrom:
  178. {{- if .Values.primary.extraEnvVarsCM }}
  179. - configMapRef:
  180. name: {{ include "common.tplvalues.render" (dict "value" .Values.primary.extraEnvVarsCM "context" $) }}
  181. {{- end }}
  182. {{- if .Values.primary.extraEnvVarsSecret }}
  183. - secretRef:
  184. name: {{ include "common.tplvalues.render" (dict "value" .Values.primary.extraEnvVarsSecret "context" $) }}
  185. {{- end }}
  186. ports:
  187. - name: mysql
  188. containerPort: 3306
  189. {{- if .Values.primary.extraPorts }}
  190. {{- include "common.tplvalues.render" (dict "value" .Values.primary.extraPorts "context" $) | nindent 12 }}
  191. {{- end }}
  192. {{- if not .Values.diagnosticMode.enabled }}
  193. {{- if .Values.primary.customLivenessProbe }}
  194. livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.primary.customLivenessProbe "context" $) | nindent 12 }}
  195. {{- else if .Values.primary.livenessProbe.enabled }}
  196. livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.primary.livenessProbe "enabled") "context" $) | nindent 12 }}
  197. exec:
  198. command:
  199. - /bin/bash
  200. - -ec
  201. - |
  202. password_aux="${MYSQL_ROOT_PASSWORD:-}"
  203. if [[ -f "${MYSQL_ROOT_PASSWORD_FILE:-}" ]]; then
  204. password_aux=$(cat "$MYSQL_ROOT_PASSWORD_FILE")
  205. fi
  206. mysqladmin status -uroot -p"${password_aux}"
  207. {{- end }}
  208. {{- if .Values.primary.customReadinessProbe }}
  209. readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.primary.customReadinessProbe "context" $) | nindent 12 }}
  210. {{- else if .Values.primary.readinessProbe.enabled }}
  211. readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.primary.readinessProbe "enabled") "context" $) | nindent 12 }}
  212. exec:
  213. command:
  214. - /bin/bash
  215. - -ec
  216. - |
  217. password_aux="${MYSQL_ROOT_PASSWORD:-}"
  218. if [[ -f "${MYSQL_ROOT_PASSWORD_FILE:-}" ]]; then
  219. password_aux=$(cat "$MYSQL_ROOT_PASSWORD_FILE")
  220. fi
  221. mysqladmin status -uroot -p"${password_aux}"
  222. {{- end }}
  223. {{- if .Values.primary.customStartupProbe }}
  224. startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.primary.customStartupProbe "context" $) | nindent 12 }}
  225. {{- else if .Values.primary.startupProbe.enabled }}
  226. startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.primary.startupProbe "enabled") "context" $) | nindent 12 }}
  227. exec:
  228. command:
  229. - /bin/bash
  230. - -ec
  231. - |
  232. password_aux="${MYSQL_ROOT_PASSWORD:-}"
  233. if [[ -f "${MYSQL_ROOT_PASSWORD_FILE:-}" ]]; then
  234. password_aux=$(cat "$MYSQL_ROOT_PASSWORD_FILE")
  235. fi
  236. mysqladmin status -uroot -p"${password_aux}"
  237. {{- end }}
  238. {{- end }}
  239. {{- if .Values.primary.resources }}
  240. resources: {{ toYaml .Values.primary.resources | nindent 12 }}
  241. {{- end }}
  242. volumeMounts:
  243. - name: data
  244. mountPath: /bitnami/mysql
  245. {{- if .Values.primary.persistence.subPath }}
  246. subPath: {{ .Values.primary.persistence.subPath }}
  247. {{- end }}
  248. {{- if or .Values.initdbScriptsConfigMap .Values.initdbScripts }}
  249. - name: custom-init-scripts
  250. mountPath: /docker-entrypoint-initdb.d
  251. {{- end }}
  252. {{- if or .Values.primary.configuration .Values.primary.existingConfigmap }}
  253. - name: config
  254. mountPath: /opt/bitnami/mysql/conf/my.cnf
  255. subPath: my.cnf
  256. {{- end }}
  257. {{- if and .Values.auth.usePasswordFiles (not .Values.auth.customPasswordFiles) }}
  258. - name: mysql-credentials
  259. mountPath: /opt/bitnami/mysql/secrets/
  260. {{- end }}
  261. {{- if .Values.primary.extraVolumeMounts }}
  262. {{- include "common.tplvalues.render" (dict "value" .Values.primary.extraVolumeMounts "context" $) | nindent 12 }}
  263. {{- end }}
  264. {{- if .Values.metrics.enabled }}
  265. - name: metrics
  266. image: {{ include "mysql.metrics.image" . }}
  267. imagePullPolicy: {{ .Values.metrics.image.pullPolicy | quote }}
  268. {{- if .Values.metrics.containerSecurityContext.enabled }}
  269. securityContext: {{- omit .Values.metrics.containerSecurityContext "enabled" | toYaml | nindent 12 }}
  270. {{- end }}
  271. env:
  272. {{- if .Values.auth.usePasswordFiles }}
  273. - name: MYSQL_ROOT_PASSWORD_FILE
  274. value: {{ default "/opt/bitnami/mysqld-exporter/secrets/mysql-root-password" .Values.auth.customPasswordFiles.root }}
  275. {{- else }}
  276. - name: MYSQL_ROOT_PASSWORD
  277. valueFrom:
  278. secretKeyRef:
  279. name: {{ include "mysql.secretName" . }}
  280. key: mysql-root-password
  281. {{- end }}
  282. {{- if .Values.diagnosticMode.enabled }}
  283. command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
  284. args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
  285. {{- else }}
  286. command:
  287. - /bin/bash
  288. - -ec
  289. - |
  290. password_aux="${MYSQL_ROOT_PASSWORD:-}"
  291. if [[ -f "${MYSQL_ROOT_PASSWORD_FILE:-}" ]]; then
  292. password_aux=$(cat "$MYSQL_ROOT_PASSWORD_FILE")
  293. fi
  294. DATA_SOURCE_NAME="root:${password_aux}@(localhost:3306)/" /bin/mysqld_exporter {{- range .Values.metrics.extraArgs.primary }} {{ . }} {{- end }}
  295. {{- end }}
  296. ports:
  297. - name: metrics
  298. containerPort: 9104
  299. {{- if not .Values.diagnosticMode.enabled }}
  300. {{- if .Values.metrics.livenessProbe.enabled }}
  301. livenessProbe: {{- omit .Values.metrics.livenessProbe "enabled" | toYaml | nindent 12 }}
  302. httpGet:
  303. path: /metrics
  304. port: metrics
  305. {{- end }}
  306. {{- if .Values.metrics.readinessProbe.enabled }}
  307. readinessProbe: {{- omit .Values.metrics.readinessProbe "enabled" | toYaml | nindent 12 }}
  308. httpGet:
  309. path: /metrics
  310. port: metrics
  311. {{- end }}
  312. {{- end }}
  313. {{- if .Values.metrics.resources }}
  314. resources: {{- toYaml .Values.metrics.resources | nindent 12 }}
  315. {{- end }}
  316. {{- if and .Values.auth.usePasswordFiles (not .Values.auth.customPasswordFiles) }}
  317. volumeMounts:
  318. - name: mysql-credentials
  319. mountPath: /opt/bitnami/mysqld-exporter/secrets/
  320. {{- end }}
  321. {{- end }}
  322. {{- if .Values.primary.sidecars }}
  323. {{- include "common.tplvalues.render" (dict "value" .Values.primary.sidecars "context" $) | nindent 8 }}
  324. {{- end }}
  325. volumes:
  326. {{- if or .Values.primary.configuration .Values.primary.existingConfigmap }}
  327. - name: config
  328. configMap:
  329. name: {{ include "mysql.primary.configmapName" . }}
  330. {{- end }}
  331. {{- if or .Values.initdbScriptsConfigMap .Values.initdbScripts }}
  332. - name: custom-init-scripts
  333. configMap:
  334. name: {{ include "mysql.initdbScriptsCM" . }}
  335. {{- end }}
  336. {{- if and .Values.auth.usePasswordFiles (not .Values.auth.customPasswordFiles) }}
  337. - name: mysql-credentials
  338. secret:
  339. secretName: {{ include "mysql.secretName" . }}
  340. items:
  341. - key: mysql-root-password
  342. path: mysql-root-password
  343. - key: mysql-password
  344. path: mysql-password
  345. {{- if eq .Values.architecture "replication" }}
  346. - key: mysql-replication-password
  347. path: mysql-replication-password
  348. {{- end }}
  349. {{- end }}
  350. {{- if .Values.primary.extraVolumes }}
  351. {{- include "common.tplvalues.render" (dict "value" .Values.primary.extraVolumes "context" $) | nindent 8 }}
  352. {{- end }}
  353. {{- if and .Values.primary.persistence.enabled .Values.primary.persistence.existingClaim }}
  354. - name: data
  355. persistentVolumeClaim:
  356. claimName: {{ tpl .Values.primary.persistence.existingClaim . }}
  357. {{- else if not .Values.primary.persistence.enabled }}
  358. - name: data
  359. emptyDir: {}
  360. {{- else if and .Values.primary.persistence.enabled (not .Values.primary.persistence.existingClaim) }}
  361. volumeClaimTemplates:
  362. - metadata:
  363. name: data
  364. labels: {{ include "common.labels.matchLabels" . | nindent 10 }}
  365. app.kubernetes.io/component: primary
  366. {{- if .Values.commonLabels }}
  367. {{- include "common.tplvalues.render" (dict "value" .Values.commonLabels "context" $) | nindent 10 }}
  368. {{- end }}
  369. annotations:
  370. {{- if .Values.primary.persistence.annotations }}
  371. {{- include "common.tplvalues.render" (dict "value" .Values.primary.persistence.annotations "context" $) | nindent 10 }}
  372. {{- end }}
  373. {{- if .Values.commonAnnotations }}
  374. {{- include "common.tplvalues.render" (dict "value" .Values.commonAnnotations "context" $) | nindent 10 }}
  375. {{- end }}
  376. spec:
  377. accessModes:
  378. {{- range .Values.primary.persistence.accessModes }}
  379. - {{ . | quote }}
  380. {{- end }}
  381. resources:
  382. requests:
  383. storage: {{ .Values.primary.persistence.size | quote }}
  384. {{- include "common.storage.class" (dict "persistence" .Values.primary.persistence "global" .Values.global) | nindent 8 }}
  385. {{- if .Values.primary.persistence.selector }}
  386. selector: {{- include "common.tplvalues.render" (dict "value" .Values.primary.persistence.selector "context" $) | nindent 10 }}
  387. {{- end -}}
  388. {{- end }}