apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "postfix.fullname" . }}
  labels:
    {{- include "postfix.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "postfix.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "postfix.selectorLabels" . | nindent 8 }}
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
    spec:
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      serviceAccountName: {{ include "postfix.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      initContainers:
        - name: init-postfix
          image: busybox:latest
          command: ['sh', '-c', 'chown -R 1000:1000 /home/vmail']
          volumeMounts:
            - name: data
              mountPath: /home/vmail
      containers:
        - name: postfix
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.postfix.image.repository }}:{{ .Values.postfix.image.tag }}"
          imagePullPolicy: {{ .Values.postfix.image.pullPolicy }}
          ports:
            - name: smtp
              containerPort: 2525
              protocol: TCP
            - name: smtps
              containerPort: 10465
              protocol: TCP
            - name: msa
              containerPort: 10587
              protocol: TCP
          volumeMounts:
            - name: data
              mountPath: /home/vmail
            - mountPath: /tls
              name: tls
            {{- range $key, $val := .Values.postfix.configmaps }}
            - name: postfix-{{ $key }}
              mountPath: "/etc/postfix/{{ $key }}.cf"
              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.postfix.configmaps }}
      - name: postfix-{{ $key }}
        configMap:
          name: postfix-{{ $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 }}