{{- if and (eq .Values.database.type "mysql") .Values.database.mysql.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "katib.fullname" . }}-mysql
  namespace: {{ include "katib.namespace" . }}
  labels:
    {{- include "katib.mysql.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.database.mysql.replicas }}
  selector:
    matchLabels:
      {{- include "katib.mysql.selectorLabels" . | nindent 6 }}
  strategy:
    type: {{ .Values.database.mysql.strategy.type }}
  template:
    metadata:
      labels:
        {{- include "katib.mysql.selectorLabels" . | nindent 8 }}
        sidecar.istio.io/inject: "false"
    spec:
      {{- with .Values.database.mysql.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: katib-mysql
          image: {{ .Values.database.mysql.image.repository }}:{{ .Values.database.mysql.image.tag }}
          {{- include "katib.imagePullPolicy" (dict "pullPolicy" .Values.database.mysql.image.pullPolicy "Values" .Values) | nindent 10 }}
          args:
            {{- toYaml .Values.database.mysql.args | nindent 12 }}
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "katib.database.secretName" . }}
                  key: MYSQL_ROOT_PASSWORD
            - name: MYSQL_ALLOW_EMPTY_PASSWORD
              value: "true"
            - name: MYSQL_DATABASE
              value: {{ .Values.database.mysql.auth.database | quote }}
          ports:
            - name: {{ .Values.database.mysql.port.name }}
              containerPort: {{ .Values.database.mysql.port.containerPort }}
          readinessProbe:
            exec:
              command:
                - "/bin/bash"
                - "-c"
                - "mysql -D ${MYSQL_DATABASE} -u root -p${MYSQL_ROOT_PASSWORD} -e 'SELECT 1'"
            initialDelaySeconds: {{ .Values.database.mysql.probes.readiness.initialDelaySeconds }}
            periodSeconds: {{ .Values.database.mysql.probes.readiness.periodSeconds }}
            failureThreshold: {{ .Values.database.mysql.probes.readiness.failureThreshold }}
          livenessProbe:
            exec:
              command:
                - "/bin/bash"
                - "-c"
                - "mysqladmin ping -u root -p${MYSQL_ROOT_PASSWORD}"
            initialDelaySeconds: {{ .Values.database.mysql.probes.liveness.initialDelaySeconds }}
            periodSeconds: {{ .Values.database.mysql.probes.liveness.periodSeconds }}
            failureThreshold: {{ .Values.database.mysql.probes.liveness.failureThreshold }}
          startupProbe:
            exec:
              command:
                - "/bin/bash"
                - "-c"
                - "mysqladmin ping -u root -p${MYSQL_ROOT_PASSWORD}"
            periodSeconds: {{ .Values.database.mysql.probes.startup.periodSeconds }}
            failureThreshold: {{ .Values.database.mysql.probes.startup.failureThreshold }}
          volumeMounts:
            - name: katib-mysql
              mountPath: /var/lib/mysql
          {{- with .Values.database.mysql.securityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.database.mysql.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
      volumes:
        - name: katib-mysql
          {{- if .Values.database.mysql.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ include "katib.fullname" . }}-mysql
          {{- else }}
          emptyDir: {}
          {{- end }}
{{- end }}