# This file is derived from https://github.com/Mailu/Mailu/blob/master/docs/kubernetes/mailu/imap.yaml

{{- if .Values.dovecot.enabled }}

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

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "mailu.fullname" . }}-dovecot
spec:
  selector:
    matchLabels:
      app: {{ include "mailu.fullname" . }}
      component: dovecot
  replicas: 1
  template:
    metadata:
      labels:
        app: {{ include "mailu.fullname" . }}
        component: dovecot
    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: admin
        image: {{ template "imageRegistry" . -}} {{ .Values.dovecot.image.repository }}:{{ default .Values.mailuVersion .Values.dovecot.image.tag }}
        imagePullPolicy: Always
        {{- with .Values.dovecot.containerSecurityContext }}
        securityContext:
        {{- . | toYaml | nindent 10 }}
        {{- end }}
        volumeMounts:
          - name: data
            subPath: dovecotdata
            mountPath: /data
          - name: data
            mountPath: /mail
            subPath: dovecotmail
          {{- if .Values.dovecot.overrides }}
          - name: overrides
            mountPath: /overrides
          {{- end }}
          {{- if .Values.timezone }}
          - name: zoneinfo
            mountPath: /usr/share/zoneinfo
            readOnly: true
          - name: zoneinfo
            subPath: {{ .Values.timezone }}
            mountPath: /etc/localtime
            readOnly: true
          {{- end }}
        env:
          - name: LOG_LEVEL
            value: {{ default .Values.logLevel .Values.dovecot.logLevel }}
          - name: FRONT_ADDRESS
            value: {{ include "mailu.fullname" . }}-front
          - name: ADMIN_ADDRESS
            value: {{ include "mailu.fullname" . }}-admin
          - name: ANTISPAM_WEBUI_ADDRESS
            value: {{ include "mailu.fullname" . }}-rspamd:11334
          - name: POSTMASTER
            value: postmaster
          - name: DOMAIN
            value: "{{ required "dnsWildcard" .Values.dnsWildcard }}"
          - name: HOSTNAMES
            value: "mail.{{ .Values.dnsWildcard }}"
          - name: RECIPIENT_DELIMITER
            value: +
          # TODO: COMPRESSION / COMPRESS_LEVEL -> documentation?

          # TODO: next entries should be removed when https://github.com/Mailu/Mailu/issues/1112 is fixed
          - name: REDIS_ADDRESS
            value: {{ include "mailu.fullname" . }}-redis
          - name: WEBMAIL
            value: none
          - name: SECRET_KEY
            valueFrom:
              secretKeyRef:
                name: mailu-session-encryption-key
                key: key
        ports:
          - name: auth
            containerPort: 2102
            protocol: TCP
          - name: lmtp
            containerPort: 2525
            protocol: TCP
          - name: imap
            containerPort: 143
            protocol: TCP
          - name: pop3
            containerPort: 110
            protocol: TCP
          - name: sieve
            containerPort: 4190
            protocol: TCP
        {{- with .Values.dovecot.resources }}
        resources:
        {{- .|toYaml|nindent 10}}
        {{- end }}
        {{- if .Values.dovecot.startupProbe}}
        startupProbe:
          exec:
            command:
              - sh
              - -c
              - 'echo QUIT|nc localhost 110|grep "Dovecot ready."'
          periodSeconds:  {{ default 10 .Values.dovecot.startupProbe.periodSeconds }} 
          failureThreshold: {{ default 30 .Values.dovecot.startupProbe.failureThreshold }} 
          timeoutSeconds: {{ default 5 .Values.dovecot.startupProbe.timeoutSeconds }} 
        {{- end }}
        {{- if .Values.dovecot.livenessProbe}}
        livenessProbe:
          exec:
            command:
              - sh
              - -c
              - 'echo QUIT|nc localhost 110|grep "Dovecot ready."'
          periodSeconds:  {{ default 10 .Values.dovecot.livenessProbe.periodSeconds }} 
          failureThreshold: {{ default 3 .Values.dovecot.livenessProbe.failureThreshold }} 
          timeoutSeconds: {{ default 5 .Values.dovecot.livenessProbe.timeoutSeconds }} 
        {{- end }}
        {{- if .Values.dovecot.readinessProbe}}
        readinessProbe:
          exec:
            command:
              - sh
              - -c
              - 'echo QUIT|nc localhost 110|grep "Dovecot ready."'
          periodSeconds:  {{ default 10 .Values.dovecot.readinessProbe.periodSeconds }} 
          failureThreshold: {{ default 1 .Values.dovecot.readinessProbe.failureThreshold }} 
          timeoutSeconds: {{ default 5 .Values.dovecot.readinessProbe.timeoutSeconds }} 
        {{- end }}
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: {{ $claimName }}
        {{- if .Values.dovecot.overrides }}
        - name: overrides
          configMap:
            name: {{ include "mailu.fullname" . }}-dovecot-override
        {{- end }}
        {{- if .Values.timezone }}
        - name: zoneinfo
          hostPath:
            path: /usr/share/zoneinfo
            type: Directory
        {{- end }}
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 0
      maxSurge: 1


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

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

---

apiVersion: v1
kind: Service
metadata:
  name: {{ include "mailu.fullname" . }}-dovecot
  labels:
    app: {{ include "mailu.fullname" . }}
    component: dovecot
spec:
  selector:
    app: {{ include "mailu.fullname" . }}
    component: dovecot
  ports:
  - name: imap-auth
    port: 2102
    protocol: TCP
  - name: imap-transport
    port: 2525
    protocol: TCP
  - name: imap-default
    port: 143
    protocol: TCP
  - name: pop3
    port: 110
    protocol: TCP
  - name: sieve
    port: 4190
    protocol: TCP

---

{{- if .Values.dovecot.overrides }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "mailu.fullname" . }}-dovecot-override
{{- with .Values.dovecot.overrides }}
data:
{{- .|toYaml|nindent 2 }}
{{ end }}
{{ end }}

{{ end }}
