apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "dovecot.fullname" . }}
  labels:
    {{- include "dovecot.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "dovecot.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "dovecot.selectorLabels" . | nindent 8 }}
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
    spec:
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      serviceAccountName: {{ include "dovecot.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: dovecot
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.dovecot.image.repository }}:{{ .Values.dovecot.image.tag }}"
          imagePullPolicy: {{ .Values.dovecot.image.pullPolicy }}
          ports:
            - name: lmtp
              containerPort: 24
              protocol: TCP
            - name: pop3
              containerPort: 1110
              protocol: TCP
            - name: imap
              containerPort: 1109
              protocol: TCP
            - name: sieve
              containerPort: 4190
              protocol: TCP
            - name: imaps
              containerPort: 10993
              protocol: TCP
            - name: pop3s
              containerPort: 10995
              protocol: TCP
            - name: auth
              containerPort: 12345
              protocol: TCP
          volumeMounts:
            - name: data
              mountPath: /home/vmail
            - mountPath: /tls
              name: tls
            {{- range $key, $val := .Values.dovecot.configmaps.dovecot }}
            - name: dovecot-{{ $key }}
              mountPath: "/etc/dovecot/{{ $key }}.conf"
              subPath: {{ $key }}
            {{- end }}
            {{- range $key, $val := .Values.dovecot.configmaps.confd }}
            - name: dovecot-confd-{{ $key }}
              mountPath: "/etc/dovecot/conf.d/{{ $key }}.conf"
              subPath: {{ $key }}
            {{- end }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      volumes:
      - name: data
        {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ if .Values.persistence.existingClaim }}{{ .Values.persistence.existingClaim }}{{- end }}
        {{- else }}
        emptyDir: {}
        {{- end }}
      {{- if .Values.tls.enabled }}
      - name: tls
        secret:
          defaultMode: 420
          secretName: {{ if .Values.tls.existingSecret }}{{ .Values.tls.existingSecret }}{{- end }}
      {{- end }}
      {{- range $key, $val := .Values.dovecot.configmaps.dovecot }}
      - name: dovecot-{{ $key }}
        configMap:
          name: dovecot-{{ $key }}
      {{- end }}
      {{- range $key, $val := .Values.dovecot.configmaps.confd }}
      - name: dovecot-confd-{{ $key }}
        configMap:
          name: dovecot-confd-{{ $key }}
      {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}