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

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

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "mailu.fullname" . }}-postfix
spec:
  selector:
    matchLabels:
      app: {{ include "mailu.fullname" . }}
      component: postfix
  replicas: 1
  template:
    metadata:
      labels:
        app: {{ include "mailu.fullname" . }}
        component: postfix
    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: postfix
        image: {{ template "imageRegistry" . -}} {{ .Values.postfix.image.repository }}:{{ default .Values.mailuVersion .Values.postfix.image.tag }}
        imagePullPolicy: Always
        {{- with .Values.postfix.containerSecurityContext }}
        securityContext:
        {{- . | toYaml | nindent 10 }}
        {{- end }}
        volumeMounts:
          - mountPath: /queue
            name: data
            subPath: mailqueue
          {{- 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.postfix.logLevel }}
          - name: REJECT_UNLISTED_RECIPIENT
            value: "yes"
          - name: DOMAIN
            value: "{{ required "dnsWildcard" .Values.dnsWildcard }}"
          - name: HOSTNAMES
            value: "mail.{{ .Values.dnsWildcard }}"
          - name: MESSAGE_SIZE_LIMIT
            value: "{{ mul .Values.mail.messageSizeLimitInMegabytes (mul 1024 1024) }}"
          - name: SUBNET
            value: "{{ .Values.subnet }}"
          - name: RECIPIENT_DELIMITER
            value: "+"
          - name: LMTP_ADDRESS
            value: {{ include "mailu.fullname" . }}-dovecot:2525
          - name: ANTISPAM_MILTER_ADDRESS
            value: {{ include "mailu.fullname" . }}-rspamd:11332
          - name: ADMIN_ADDRESS
            value: {{ include "mailu.fullname" . }}-admin
          - name: FRONT_ADDRESS
            value: {{ include "mailu.fullname" . }}-front
          {{ if hasKey .Values.external_relay "host" }}
          - name: RELAYHOST
            value: "{{ .Values.external_relay.host }}"
          {{ if hasKey .Values.external_relay "username" }}
          - name: RELAYUSER
            value: "{{ .Values.external_relay.username }}"
          - name: RELAYPASSWORD
            value: "{{ .Values.external_relay.password }}"
          {{- end}}
          {{- end}}
        ports:
          - name: smtp
            containerPort: 25
            protocol: TCP
          - name: smtp-ssl
            containerPort: 465
            protocol: TCP
          - name: smtp-starttls
            containerPort: 587
            protocol: TCP
          - name: smtp-auth
            containerPort: 10025
            protocol: TCP
        {{- with .Values.postfix.resources }}
        resources:
        {{- .|toYaml|nindent 10}}
        {{- end }}
        {{- if .Values.postfix.startupProbe}}
        startupProbe:
          exec:
            command:
              - sh
              - -c
              - 'echo QUIT|nc localhost 25|grep "220 .* ESMTP Postfix"'
          periodSeconds:  {{ default 10 .Values.postfix.startupProbe.periodSeconds }} 
          failureThreshold: {{ default 30 .Values.postfix.startupProbe.failureThreshold }} 
          timeoutSeconds: {{ default 5 .Values.postfix.startupProbe.timeoutSeconds }} 
        {{- end }}
        {{- if .Values.postfix.livenessProbe}}
        livenessProbe:
          exec:
            command:
              - sh
              - -c
              - 'echo QUIT|nc localhost 25|grep "220 .* ESMTP Postfix"'
          periodSeconds:  {{ default 10 .Values.postfix.livenessProbe.periodSeconds }} 
          failureThreshold: {{ default 3 .Values.postfix.livenessProbe.failureThreshold }} 
          timeoutSeconds: {{ default 5 .Values.postfix.livenessProbe.timeoutSeconds }} 
        {{- end }}
        {{- if .Values.postfix.readinessProbe}}
        readinessProbe:
          exec:
            command:
              - sh
              - -c
              - 'echo QUIT|nc localhost 25|grep "220 .* ESMTP Postfix"'
          periodSeconds:  {{ default 10 .Values.postfix.readinessProbe.periodSeconds }} 
          failureThreshold: {{ default 1 .Values.postfix.readinessProbe.failureThreshold }} 
          timeoutSeconds: {{ default 5 .Values.postfix.readinessProbe.timeoutSeconds }} 
        {{- end }}
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: {{ $claimName }}
        {{- if .Values.timezone }}
        - name: zoneinfo
          hostPath:
            path: /usr/share/zoneinfo
            type: Directory
        {{- end }}
  strategy:
    type: Recreate


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

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

---

apiVersion: v1
kind: Service
metadata:
  name: {{ include "mailu.fullname" . }}-postfix
  labels:
    app: {{ include "mailu.fullname" . }}
    component: postfix
spec:
  selector:
    app: {{ include "mailu.fullname" . }}
    component: postfix
  ports:
  - name: smtp
    port: 25
    protocol: TCP
  - name: smtp-ssl
    port: 465
    protocol: TCP
  - name: smtp-starttls
    port: 587
    protocol: TCP
  - name: smtp-auth
    port: 10025
    protocol: TCP
