apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: {{ template "master.fullname" . }}
  labels:
    app: "{{ template "mariadb.name" . }}"
    chart: {{ template "mariadb.chart" . }}
    component: "master"
    release: {{ .Release.Name | quote }}
    heritage: {{ .Release.Service | quote }}
spec:
  serviceName: "{{ template "master.fullname" . }}"
  replicas: 1
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: "{{ template "mariadb.name" . }}"
        component: "master"
        release: "{{ .Release.Name }}"
        chart: {{ template "mariadb.chart" . }}
    spec:
      securityContext:
        runAsUser: 1001
        fsGroup: 1001
      {{- if eq .Values.master.antiAffinity "hard" }}
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - topologyKey: "kubernetes.io/hostname"
              labelSelector:
                matchLabels:
                  app: "{{ template "mariadb.name" . }}"
                  release: "{{ .Release.Name }}"
      {{- else if eq .Values.master.antiAffinity "soft" }}
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 1
            podAffinityTerm:
              topologyKey: kubernetes.io/hostname
              labelSelector:
                matchLabels:
                  app: "{{ template "mariadb.name" . }}"
                  release: "{{ .Release.Name }}"
      {{- end }}
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
      {{- range .Values.image.pullSecrets }}
        - name: {{ . }}
      {{- end}}
      {{- end }}
      containers:
      - name: "mariadb"
        image: {{ template "mariadb.image" . }}
        imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
        env:
        - name: MARIADB_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "mariadb.fullname" . }}
              key: mariadb-root-password
        {{- if .Values.db.user }}
        - name: MARIADB_USER
          value: "{{ .Values.db.user }}"
        - name: MARIADB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "mariadb.fullname" . }}
              key: mariadb-password
        {{- 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 }}"
        - name: MARIADB_REPLICATION_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "mariadb.fullname" . }}
              key: mariadb-replication-password
        {{- end }}
        ports:
        - name: mysql
          containerPort: 3306
        {{- if .Values.master.livenessProbe.enabled }}
        livenessProbe:
          exec:
            command: ["sh", "-c", "exec mysqladmin status -uroot -p$MARIADB_ROOT_PASSWORD"]
          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", "exec mysqladmin status -uroot -p$MARIADB_ROOT_PASSWORD"]
          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 }}
        resources:
{{ toYaml .Values.master.resources | indent 10 }}
        volumeMounts:
        - name: data
          mountPath: /bitnami/mariadb
        - name: custom-init-scripts
          mountPath: /docker-entrypoint-initdb.d
{{- if .Values.master.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_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "mariadb.fullname" . }}
              key: mariadb-root-password
        command: [ 'sh', '-c', 'DATA_SOURCE_NAME="root:$MARIADB_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.master.config }}
        - name: config
          configMap:
            name: {{ template "master.fullname" . }}
      {{- end }}
        - name: custom-init-scripts
          configMap:
            name: {{ template "master.fullname" . }}-init-scripts
{{- if .Values.master.persistence.enabled }}
  volumeClaimTemplates:
    - metadata:
        name: data
        labels:
          app: "{{ template "mariadb.name" . }}"
          chart: {{ template "mariadb.chart" . }}
          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 }}
      {{- if .Values.master.persistence.storageClass }}
      {{- if (eq "-" .Values.master.persistence.storageClass) }}
        storageClassName: ""
      {{- else }}
        storageClassName: {{ .Values.master.persistence.storageClass | quote }}
      {{- end }}
      {{- end }}
{{- else }}
        - name: "data"
          emptyDir: {}
{{- end }}
