apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: {{ template "percona.fullname" . }}
  labels:
    app: {{ template "percona.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  template:
    metadata:
      labels:
        app: {{ template "percona.fullname" . }}
    spec:
      initContainers:
      - name: "remove-lost-found"
        image: "busybox:1.25.0"
        imagePullPolicy: {{ .Values.imagePullPolicy | quote }}
        command:
        - "rm"
        - "-fr"
        - "/var/lib/mysql/lost+found"
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
      containers:
      - name: {{ template "percona.fullname" . }}
        image: "{{ .Values.image }}:{{ .Values.imageTag }}"
        imagePullPolicy: {{ .Values.imagePullPolicy | quote }}
        resources:
{{ toYaml .Values.resources | indent 10 }}
        env:
        {{- if .Values.mysqlAllowEmptyPassword }}
        - name: MYSQL_ALLOW_EMPTY_PASSWORD
          value: "true"
        {{- else }}
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "percona.fullname" . }}
              key: mysql-root-password
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "percona.fullname" . }}
              key: mysql-password
        {{- end }}
        - name: MYSQL_USER
          value: {{ default "" .Values.mysqlUser | quote }}
        - name: MYSQL_DATABASE
          value: {{ default "" .Values.mysqlDatabase | quote }}
        ports:
        - name: mysql
          containerPort: 3306
        livenessProbe:
          exec:
            command:
            - mysqladmin
            - ping
          initialDelaySeconds: 30
          timeoutSeconds: 5
        readinessProbe:
          exec:
            command:
            - mysqladmin
            - ping
          initialDelaySeconds: 5
          timeoutSeconds: 1
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
      volumes:
      - name: data
      {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ template "percona.fullname" . }}
      {{- else }}
        emptyDir: {}
      {{- end -}}
      {{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
      {{- end -}}
      {{- if .Values.tolerations }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
      {{- end -}}
