# helm_charts_master-statefulset.yaml
apiVersion: {{ template "mariadb.statefulset.apiVersion" . }}
kind: StatefulSet
metadata:
  name: {{ template "master.fullname" . }}
  labels:
    app: {{ template "mariadb.name" . }}
    chart: {{ template "mariadb.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
    component: master
spec:
  selector:
    matchLabels:
      app: {{ template "mariadb.name" . }}
      release: {{ .Release.Name }}
      component: master
  serviceName: {{ template "master.fullname" . }}
  replicas: 1
  updateStrategy:
    type: {{ .Values.master.updateStrategy.type }}
    {{- if (eq "Recreate" .Values.master.updateStrategy.type) }}
    rollingUpdate: null
    {{- end }}
  template:
    metadata:
      {{- with .Values.master.annotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        app: {{ template "mariadb.name" . }}
        chart: {{ template "mariadb.chart" . }}
        release: {{ .Release.Name }}
        component: master
    spec:
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName | quote }}
      {{- end }}
      serviceAccountName: {{ template "mariadb.serviceAccountName" . }}
      {{- if .Values.securityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.securityContext.fsGroup }}
        runAsUser: {{ .Values.securityContext.runAsUser }}
      {{- end }}
      {{- if eq .Values.master.antiAffinity "hard" }}
      affinity:
      {{- with .Values.master.affinity  }}
{{ toYaml . | indent 8 }}
      {{- end }}
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - topologyKey: "kubernetes.io/hostname"
              labelSelector:
                matchLabels:
                  app: {{ template "mariadb.name" . }}
                  release: {{ .Release.Name }}
      {{- else if eq .Values.master.antiAffinity "soft" }}
      affinity:
      {{- with .Values.master.affinity  }}
{{ toYaml . | indent 8 }}
      {{- end }}
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 1
              podAffinityTerm:
                topologyKey: kubernetes.io/hostname
                labelSelector:
                  matchLabels:
                    app: {{ template "mariadb.name" . }}
                    release: {{ .Release.Name }}
      {{- else}}
      {{- with .Values.master.affinity }}
      affinity: {{ toYaml . | nindent 8 }}
      {{- end }}
      {{- end }}
      {{- if .Values.master.nodeSelector }}
      nodeSelector: {{ toYaml .Values.master.nodeSelector | nindent 8 }}
      {{- end -}}
      {{- with .Values.master.tolerations }}
      tolerations: {{ toYaml . | nindent 8 }}
      {{- end }}
{{- include "mariadb.imagePullSecrets" . | indent 6 }}
      initContainers:
        {{- if .Values.master.extraInitContainers }}
{{ tpl .Values.master.extraInitContainers . | indent 8 }}
        {{- end }}
        {{- if and .Values.volumePermissions.enabled .Values.master.persistence.enabled }}
        - name: volume-permissions
          image: {{ template "mariadb.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command: ["chown", "-R", "{{ .Values.securityContext.runAsUser }}:{{ .Values.securityContext.fsGroup }}", "{{ .Values.master.persistence.mountPath }}"]
          securityContext:
            runAsUser: 0
          resources: {{ toYaml .Values.volumePermissions.resources | nindent 12 }}
          volumeMounts:
            - name: data
              mountPath: {{ .Values.master.persistence.mountPath }}
        {{- end }}
      containers:
        - name: "mariadb"
          image: {{ template "mariadb.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          env:
            {{- if .Values.image.debug}}
            - name: BITNAMI_DEBUG
              value: "true"
            {{- end }}
            {{- if .Values.master.extraFlags }}
            - name: MARIADB_EXTRA_FLAGS
              value: "{{ .Values.master.extraFlags }}"
            {{- end }}
            {{- if .Values.rootUser.injectSecretsAsVolume }}
            - name: MARIADB_ROOT_PASSWORD_FILE
              value: "/opt/bitnami/mariadb/secrets/mariadb-root-password"
            {{- else }}
            - name: MARIADB_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "mariadb.secretName" . }}
                  key: mariadb-root-password
            {{- end }}
            {{- if not (empty .Values.db.user) }}
            - name: MARIADB_USER
              value: "{{ .Values.db.user }}"
            {{- if .Values.db.injectSecretsAsVolume }}
            - name: MARIADB_PASSWORD_FILE
              value: "/opt/bitnami/mariadb/secrets/mariadb-password"
            {{- else }}
            - name: MARIADB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "mariadb.secretName" . }}
                  key: mariadb-password
            {{- end }}
            {{- end }}
            - name: MARIADB_DATABASE
              value: "{{ .Values.db.name }}"
            {{- if .Values.replication.enabled }}
            - name: MARIADB_REPLICATION_MODE
              value: "master"
            - name: MARIADB_REPLICATION_USER
              value: "{{ .Values.replication.user }}"
            {{- if .Values.replication.injectSecretsAsVolume }}
            - name: MARIADB_REPLICATION_PASSWORD_FILE
              value: "/opt/bitnami/mariadb/secrets/mariadb-replication-password"
            {{- else }}
            - name: MARIADB_REPLICATION_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "mariadb.secretName" . }}
                  key: mariadb-replication-password
            {{- end }}
            {{- end }}
            {{- if .Values.master.extraEnvVars }}
            {{- tpl (toYaml .Values.master.extraEnvVars) $ | nindent 12 }}
            {{- end }}
          ports:
            - name: mysql
              containerPort: 3306
          {{- if .Values.master.livenessProbe.enabled }}
          livenessProbe:
            exec:
              command:
                - sh
                - -c
                - |
                  password_aux="${MARIADB_ROOT_PASSWORD:-}"
                  if [ -f "${MARIADB_ROOT_PASSWORD_FILE:-}" ]; then
                      password_aux=$(cat $MARIADB_ROOT_PASSWORD_FILE)
                  fi
                  mysqladmin status -uroot -p$password_aux
            initialDelaySeconds: {{ .Values.master.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.master.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.master.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.master.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.master.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.master.readinessProbe.enabled }}
          readinessProbe:
            exec:
              command:
                - sh
                - -c
                - |
                  password_aux="${MARIADB_ROOT_PASSWORD:-}"
                  if [ -f "${MARIADB_ROOT_PASSWORD_FILE:-}" ]; then
                      password_aux=$(cat $MARIADB_ROOT_PASSWORD_FILE)
                  fi
                  mysqladmin status -uroot -p$password_aux
            initialDelaySeconds: {{ .Values.master.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.master.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.master.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.master.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.master.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.master.resources }}
          resources: {{ toYaml .Values.master.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: data
              mountPath: {{ .Values.master.persistence.mountPath }}
              {{- if .Values.master.persistence.subPath }}
              subPath: {{ .Values.master.persistence.subPath }}
              {{- end }}
            {{- if or (.Files.Glob "files/docker-entrypoint-initdb.d/*.{sh,sql,sql.gz}") .Values.initdbScriptsConfigMap .Values.initdbScripts }}
            - name: custom-init-scripts
              mountPath: /docker-entrypoint-initdb.d
            {{- end }}
            {{- if .Values.master.config }}
            - name: config
              mountPath: /opt/bitnami/mariadb/conf/my.cnf
              subPath: my.cnf
            {{- end }}
            {{- if or .Values.rootUser.injectSecretsAsVolume .Values.db.injectSecretsAsVolume .Values.replication.injectSecretsAsVolume }}
            - name: mariadb-credentials
              mountPath: /opt/bitnami/mariadb/secrets/
            {{- end }}
        {{- if .Values.metrics.enabled }}
        - name: metrics
          image: {{ template "mariadb.metrics.image" . }}
          imagePullPolicy: {{ .Values.metrics.image.pullPolicy | quote }}
          env:
            {{- if .Values.rootUser.injectSecretsAsVolume }}
            - name: MARIADB_ROOT_PASSWORD_FILE
              value: "/opt/bitnami/mysqld-exporter/secrets/mariadb-root-password"
            {{- else }}
            - name: MARIADB_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "mariadb.secretName" . }}
                  key: mariadb-root-password
            {{- end }}
          command:
            - sh
            - -c
            - |
              password_aux="${MARIADB_ROOT_PASSWORD:-}"
              if [ -f "${MARIADB_ROOT_PASSWORD_FILE:-}" ]; then
                  password_aux=$(cat $MARIADB_ROOT_PASSWORD_FILE)
              fi
              DATA_SOURCE_NAME="root:${password_aux}@(localhost:3306)/" /bin/mysqld_exporter
              {{- range .Values.metrics.extraArgs.master }}
              {{ . }}
              {{- end }}
          ports:
            - name: metrics
              containerPort: 9104
          {{- if .Values.metrics.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /metrics
              port: metrics
            initialDelaySeconds: {{ .Values.metrics.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.metrics.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.metrics.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.metrics.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.metrics.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.metrics.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /metrics
              port: metrics
            initialDelaySeconds: {{ .Values.metrics.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.metrics.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.metrics.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.metrics.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.metrics.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.metrics.resources }}
          resources: {{ toYaml .Values.metrics.resources | nindent 12 }}
          {{- end }}
          {{- if .Values.rootUser.injectSecretsAsVolume }}
          volumeMounts:
            - name: mariadb-credentials
              mountPath: /opt/bitnami/mysqld-exporter/secrets/
          {{- end }}
        {{- end }}
      volumes:
        {{- if .Values.master.config }}
        - name: config
          configMap:
            name: {{ template "master.fullname" . }}
        {{- end }}
        {{- if or (.Files.Glob "files/docker-entrypoint-initdb.d/*.{sh,sql,sql.gz}") .Values.initdbScriptsConfigMap .Values.initdbScripts }}
        - name: custom-init-scripts
          configMap:
            name: {{ template "mariadb.initdbScriptsCM" . }}
        {{- end }}
        {{- if or .Values.rootUser.injectSecretsAsVolume .Values.db.injectSecretsAsVolume .Values.replication.injectSecretsAsVolume }}
        - name: mariadb-credentials
          secret:
            secretName: {{ template "mariadb.fullname" . }}
            items:
              {{- if .Values.rootUser.injectSecretsAsVolume }}
              - key: mariadb-root-password
                path: mariadb-root-password
              {{- end }}
              {{- if .Values.db.injectSecretsAsVolume }}
              - key: mariadb-password
                path: mariadb-password
              {{- end }}
              {{- if and .Values.replication.enabled .Values.replication.injectSecretsAsVolume }}
              - key: mariadb-replication-password
                path: mariadb-replication-password
              {{- end }}
        {{- end }}
{{- if and .Values.master.persistence.enabled .Values.master.persistence.existingClaim }}
        - name: data
          persistentVolumeClaim:
            claimName: {{ .Values.master.persistence.existingClaim }}
{{- else if not .Values.master.persistence.enabled }}
        - name: data
          emptyDir: {}
{{- else if and .Values.master.persistence.enabled (not .Values.master.persistence.existingClaim) }}
  volumeClaimTemplates:
    - metadata:
        name: data
        labels:
          app: "{{ template "mariadb.name" . }}"
          component: "master"
          release: {{ .Release.Name | quote }}
          heritage: {{ .Release.Service | quote }}
      spec:
        accessModes:
        {{- range .Values.master.persistence.accessModes }}
          - {{ . | quote }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.master.persistence.size | quote }}
        {{ include "mariadb.master.storageClass" . }}
{{- end }}