{{ if and (or (eq .Values.database.type "mysql") (eq .Values.database.roundcubeType "mysql")) (not .Values.database.mysql.host) }}

{{- /* Define persistantVolume claimName */}}
{{- $claimName := .Values.persistence.single_pvc | ternary (include "mailu.claimName" .) .Values.mysql.persistence.claimNameOverride | default (printf "%s-mysql" (include "mailu.fullname" .)) }}

{{ if (eq .Values.database.roundcubeType "mysql") }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "mailu.fullname" . }}-mysql-init
data:
  extra.sql: >
    CREATE DATABASE IF NOT EXISTS {{ required "database.mysql.roundcubeDatabase" .Values.database.mysql.roundcubeDatabase }};
    GRANT ALL ON {{ required "database.mysql.roundcubeDatabase" .Values.database.mysql.roundcubeDatabase }}.* TO
      '{{ required "database.mysql.roundcubeUser" .Values.database.mysql.roundcubeUser }}'@'%'
      IDENTIFIED BY '{{ required "database.mysql.roundcubePassword" .Values.database.mysql.roundcubePassword }}';
{{ end }}
---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "mailu.fullname" . }}-mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ include "mailu.fullname" . }}
      component: mysql
  template:
    metadata:
      labels:
        app: {{ include "mailu.fullname" . }}
        component: mysql
    spec:
      serviceAccount: {{ include "mailu.serviceAccountName" . }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
      - name: mysql
        image: {{ .Values.mysql.image.repository }}:{{ .Values.mysql.image.tag }}
        imagePullPolicy: Always
        args:
          - --character-set-server=utf8
          - --skip-character-set-client-handshake
          - --collation-server=utf8_unicode_ci
        volumeMounts:
          - name: data
            subPath: mysql
            mountPath: /var/lib/mysql
{{- if (eq .Values.database.roundcubeType "mysql") }}
          - name: initscripts
            mountPath: /docker-entrypoint-initdb.d/
{{- end }}
          {{- if .Values.timezone }}
          - name: zoneinfo
            mountPath: /usr/share/zoneinfo
            readOnly: true
          - name: zoneinfo
            subPath: {{ .Values.timezone }}
            mountPath: /etc/localtime
            readOnly: true
          {{- end }}
        env:
          {{ if .Values.database.mysql.rootPassword }}
          - name: MYSQL_ROOT_PASSWORD
            value: {{ .Values.database.mysql.rootPassword }}
          {{ else }}
          - name: MYSQL_RANDOM_ROOT_PASSWORD
            value: "yes"
          {{ end }}
{{- if (eq .Values.database.type "mysql") }}
          - name: MYSQL_DATABASE
            value: {{ required "database.mysql.database" .Values.database.mysql.database }}
          - name: MYSQL_USER
            value: {{ required "database.mysql.user" .Values.database.mysql.user }}
          - name: MYSQL_PASSWORD
            value: {{ required "database.mysql.password" .Values.database.mysql.password }}
{{- end }}
        ports:
          - name: mysql
            containerPort: 3306
            protocol: TCP
        {{- with .Values.mysql.resources }}
        resources:
        {{- .|toYaml|nindent 10}}
        {{- end }}
        {{- if .Values.mysql.startupProbe}}
        startupProbe:
          exec:
            command:
              - sh
              - -c
              - "mysqladmin ping -u root -p${MYSQL_ROOT_PASSWORD}"
          periodSeconds:  {{ default 10 .Values.mysql.startupProbe.periodSeconds }} 
          failureThreshold: {{ default 30 .Values.mysql.startupProbe.failureThreshold }} 
          timeoutSeconds: {{ default 5 .Values.mysql.startupProbe.timeoutSeconds }} 
        {{- end }}
        {{- if .Values.mysql.livenessProbe}}
        livenessProbe:
          exec:
            command:
              - sh
              - -c
              - "mysqladmin ping -u root -p${MYSQL_ROOT_PASSWORD}"
          periodSeconds:  {{ default 10 .Values.mysql.livenessProbe.periodSeconds }} 
          failureThreshold: {{ default 30 .Values.mysql.livenessProbe.failureThreshold }} 
          timeoutSeconds: {{ default 5 .Values.mysql.livenessProbe.timeoutSeconds }} 
        {{- end }}
        {{- if .Values.mysql.readinessProbe}}
        readinessProbe:
          exec:
            command:
              - sh
              - -c
              - "mysqladmin ping -u root -p${MYSQL_ROOT_PASSWORD}"
          periodSeconds:  {{ default 10 .Values.mysql.readinessProbe.periodSeconds }} 
          failureThreshold: {{ default 30 .Values.mysql.readinessProbe.failureThreshold }} 
          timeoutSeconds: {{ default 5 .Values.mysql.readinessProbe.timeoutSeconds }} 
        {{- end }}
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: {{ $claimName }}
        - name: initscripts
          configMap:
            name: {{ include "mailu.fullname" . }}-mysql-init
        {{- if .Values.timezone }}
        - name: zoneinfo
          hostPath:
            path: /usr/share/zoneinfo
            type: Directory
        {{- end }}
  strategy:
    # This is a single-node mysql instance, so we need to shut down the old instance before starting a new
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 0


{{- if not .Values.persistence.single_pvc }}
---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name:  {{ $claimName }}
{{- if .Values.mysql.persistence.annotations }}
  annotations:
{{ toYaml .Values.mysql.persistence.annotations | indent 4 }}
{{- end }}
spec:
  accessModes:
    - {{ .Values.mysql.persistence.accessMode }}
  resources:
    requests:
      storage: {{ .Values.mysql.persistence.size }}
  {{- if .Values.mysql.persistence.storageClass }}
  storageClassName: {{ .Values.mysql.persistence.storageClass }}
  {{- end }}
{{- end }}

---

apiVersion: v1
kind: Service
metadata:
  name: {{ include "mailu.fullname" . }}-mysql
  labels:
    app: {{ include "mailu.fullname" . }}
    component: mysql
spec:
  selector:
    app: {{ include "mailu.fullname" . }}
    component: mysql
  ports:
  - name: mysql
    port: 3306
    protocol: TCP

{{ end }}
