{{- if .Values.replication.enabled }}
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: {{ template "slave.fullname" . }}
  labels:
    app: "{{ template "mariadb.name" . }}"
    chart: "{{ template "mariadb.chart" . }}"
    component: "slave"
    release: {{ .Release.Name | quote }}
    heritage: {{ .Release.Service | quote }}
spec:
  selector:
    matchLabels:
      release: "{{ .Release.Name }}"
      component: "slave"
      app: "{{ template "mariadb.name" . }}"
  serviceName: "{{ template "slave.fullname" . }}"
  replicas: {{ .Values.slave.replicas }}
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      {{- if .Values.slave.annotations }}
      annotations:
        {{- range .Values.slave.annotations }}
        {{ .key }}: '{{ .value }}'
        {{- end }}
      {{- end }}
      labels:
        app: "{{ template "mariadb.name" . }}"
        component: "slave"
        release: "{{ .Release.Name }}"
        chart: "{{ template "mariadb.chart" . }}"
    spec:
      serviceAccountName: "{{ template "mariadb.serviceAccountName" . }}"
      {{- if .Values.securityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.securityContext.fsGroup }}
        runAsUser: {{ .Values.securityContext.runAsUser }}
      {{- end }}
      {{- if eq .Values.slave.antiAffinity "hard" }}
      affinity:
      {{- with .Values.slave.affinity  }}
{{ toYaml . | indent 8 }}
      {{- end }}
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - topologyKey: "kubernetes.io/hostname"
              labelSelector:
                matchLabels:
                  app: "{{ template "mariadb.name" . }}"
                  release: "{{ .Release.Name }}"
      {{- else if eq .Values.slave.antiAffinity "soft" }}
      affinity:
      {{- with .Values.slave.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.slave.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
      {{- end }}
      {{- end }}
      {{- with .Values.slave.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
      {{- end }}
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
      {{- range .Values.image.pullSecrets }}
        - name: {{ . }}
      {{- end}}
      {{- end }}
      {{- if .Values.master.extraInitContainers }}
      initContainers:
{{ tpl .Values.master.extraInitContainers . | indent 6}}
      {{- end }}
      containers:
      - name: "mariadb"
        image: {{ template "mariadb.image" . }}
        imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
        env:
        {{- if .Values.image.debug}}
        - name: BITNAMI_DEBUG
          value: "true"
        {{- end }}
        - name: MARIADB_REPLICATION_MODE
          value: "slave"
        - name: MARIADB_MASTER_HOST
          value: {{ template "mariadb.fullname" . }}
        - name: MARIADB_MASTER_PORT_NUMBER
          value: "{{ .Values.service.port }}"
        - name: MARIADB_MASTER_ROOT_USER
          value: "root"
        - name: MARIADB_MASTER_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
            {{- if .Values.existingSecret }}
              name: {{ .Values.existingSecret }}
            {{- else }}
              name: {{ template "mariadb.fullname" . }}
            {{- end }}
              key: mariadb-root-password
        - name: MARIADB_REPLICATION_USER
          value: "{{ .Values.replication.user }}"
        - name: MARIADB_REPLICATION_PASSWORD
          valueFrom:
            secretKeyRef:
            {{- if .Values.existingSecret }}
              name: {{ .Values.existingSecret }}
            {{- else }}
              name: {{ template "mariadb.fullname" . }}
            {{- end }}
              key: mariadb-replication-password
        ports:
        - name: mysql
          containerPort: 3306
        {{- if .Values.slave.livenessProbe.enabled }}
        livenessProbe:
          exec:
            command: ["sh", "-c", "exec mysqladmin status -uroot -p$MARIADB_MASTER_ROOT_PASSWORD"]
          initialDelaySeconds: {{ .Values.slave.livenessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.slave.livenessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.slave.livenessProbe.timeoutSeconds }}
          successThreshold: {{ .Values.slave.livenessProbe.successThreshold }}
          failureThreshold: {{ .Values.slave.livenessProbe.failureThreshold }}
        {{- end }}
        {{- if .Values.slave.readinessProbe.enabled }}
        readinessProbe:
          exec:
            command: ["sh", "-c", "exec mysqladmin status -uroot -p$MARIADB_MASTER_ROOT_PASSWORD"]
          initialDelaySeconds: {{ .Values.slave.readinessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.slave.readinessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.slave.readinessProbe.timeoutSeconds }}
          successThreshold: {{ .Values.slave.readinessProbe.successThreshold }}
          failureThreshold: {{ .Values.slave.readinessProbe.failureThreshold }}
        {{- end }}
        resources:
{{ toYaml .Values.slave.resources | indent 10 }}
        volumeMounts:
        - name: data
          mountPath: /bitnami/mariadb
{{- if .Values.slave.config }}
        - name: config
          mountPath: /opt/bitnami/mariadb/conf/my.cnf
          subPath: my.cnf
{{- end }}
{{- if .Values.metrics.enabled }}
      - name: metrics
        image: {{ template "metrics.image" . }}
        imagePullPolicy: {{ .Values.metrics.image.pullPolicy | quote }}
        env:
        - name: MARIADB_MASTER_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
            {{- if .Values.existingSecret }}
              name: {{ .Values.existingSecret }}
            {{- else }}
              name: {{ template "mariadb.fullname" . }}
            {{- end }}
              key: mariadb-root-password
        command: [ 'sh', '-c', 'DATA_SOURCE_NAME="root:$MARIADB_MASTER_ROOT_PASSWORD@(localhost:3306)/" /bin/mysqld_exporter' ]
        ports:
        - name: metrics
          containerPort: 9104
        livenessProbe:
          httpGet:
            path: /metrics
            port: metrics
          initialDelaySeconds: 15
          timeoutSeconds: 5
        readinessProbe:
          httpGet:
            path: /metrics
            port: metrics
          initialDelaySeconds: 5
          timeoutSeconds: 1
        resources:
{{ toYaml .Values.metrics.resources | indent 10 }}
{{- end }}
      volumes:
      {{- if .Values.slave.config }}
        - name: config
          configMap:
            name: {{ template "slave.fullname" . }}
      {{- end }}
{{- if .Values.slave.persistence.enabled }}
  volumeClaimTemplates:
    - metadata:
        name: data
        labels:
          app: "{{ template "mariadb.name" . }}"
          component: "slave"
          release: {{ .Release.Name | quote }}
          heritage: {{ .Release.Service | quote }}
      spec:
        accessModes:
        {{- range .Values.slave.persistence.accessModes }}
          - {{ . | quote }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.slave.persistence.size | quote }}
      {{- if .Values.slave.persistence.storageClass }}
      {{- if (eq "-" .Values.slave.persistence.storageClass) }}
        storageClassName: ""
      {{- else }}
        storageClassName: {{ .Values.slave.persistence.storageClass | quote }}
      {{- end }}
      {{- end }}
{{- else }}
        - name: "data"
          emptyDir: {}
{{- end }}
{{- end }}
