apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: {{ template "mariadb.fullname" . }}
  labels:
    app: {{ template "mariadb.name" . }}
    chart: {{ template "mariadb.chart" . }}
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  template:
    metadata:
      labels:
        app: {{ template "mariadb.name" . }}
        release: "{{ .Release.Name }}"
    spec:
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
      {{- range .Values.image.pullSecrets }}
        - name: {{ . }}
      {{- end}}
      {{- end }}
      {{- if .Values.securitySettings.enabled }}
      securityContext:
        runAsUser: {{ .Values.securitySettings.runAsUser }}
        fsGroup: {{ .Values.securitySettings.fsGroup }}
      {{- end}}
      initContainers:
      - name: "copy-custom-config"
        image: "busybox"
        imagePullPolicy: "{{ .Values.imagePullPolicy }}"
        command: ["sh", "-c", "mkdir -p /bitnami/mariadb/conf && cp /bitnami/mariadb_config/my.cnf /bitnami/mariadb/conf/my_custom.cnf"]
        volumeMounts:
        - name: config
          mountPath: /bitnami/mariadb_config
        - name: data
          mountPath: /bitnami/mariadb
      containers:
      - name: mariadb
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
        env:
        {{- if .Values.usePassword }}
        - name: MARIADB_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "mariadb.fullname" . }}
              key: mariadb-root-password
        {{- if .Values.mariadbUser }}
        - name: MARIADB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "mariadb.fullname" . }}
              key: mariadb-password
        {{- end }}
        {{- else }}
        - name: ALLOW_EMPTY_PASSWORD
          value: "yes"
        {{- end }}
        - name: MARIADB_USER
          value: {{ default "" .Values.mariadbUser | quote }}
        - name: MARIADB_DATABASE
          value: {{ default "" .Values.mariadbDatabase | quote }}
        ports:
        - name: mysql
          containerPort: 3306
        livenessProbe:
          exec:
            command:
            - bash
            - -c
            - mysqladmin ping{{ if .Values.usePassword }} -uroot -p$MARIADB_ROOT_PASSWORD{{ end }}
          initialDelaySeconds: 30
          timeoutSeconds: 5
        readinessProbe:
          exec:
            command:
            - bash
            - -c
            - mysqladmin ping{{ if .Values.usePassword }} -uroot -p$MARIADB_ROOT_PASSWORD{{ end }}
          initialDelaySeconds: 5
          timeoutSeconds: 1
        resources:
{{ toYaml .Values.resources | indent 10 }}
        volumeMounts:
        - name: data
          mountPath: /bitnami/mariadb
{{- if .Values.metrics.enabled }}
      - name: metrics
        image: "{{ .Values.metrics.image }}:{{ .Values.metrics.imageTag }}"
        imagePullPolicy: {{ .Values.metrics.imagePullPolicy | quote }}
        env:
        {{- if .Values.usePassword }}
        - name: MARIADB_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "mariadb.fullname" . }}
              key: mariadb-root-password
        {{- end }}
        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:
      - name: config
        configMap:
          name: {{ template "mariadb.fullname" . }}
      - name: data
      {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ .Values.persistence.existingClaim | default (include "mariadb.fullname" .) }}
      {{- else }}
        emptyDir: {}
      {{- end -}}
