apiVersion: apps/v1
kind: {{ .Values.postfix.kind }}
metadata:
  name: {{ include "postfix.fullname" . }}
  labels:
    {{- include "postfix.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  {{- if (eq .Values.postfix.kind "StatefulSet") }}
  serviceName: {{ include "postfix.fullname" . }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "postfix.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "postfix.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
            - name: DOMAIN
              value: {{ .Values.postfix.domain | quote }}
            - name: HOSTNAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            - name: MESSAGE_SIZE_LIMIT
              value: {{ .Values.postfix.messageSizeLimit | quote }}
            - name: RELAYHOST
              value: {{ .Values.postfix.relayHost | quote }}
            - name: RELAYNETS
              value: {{ .Values.postfix.relayNets | quote }}
            - name: SENDER_BCC
              value: {{ .Values.postfix.senderBcc | quote }}  
          ports:
            - name: smtp
              containerPort: 25
              protocol: TCP
            {{- if .Values.postfix.hostPort }}
              hostPort: {{ .Values.postfix.hostPort }}
            {{- end }}
          livenessProbe:
            tcpSocket:
               port: 25
            initialDelaySeconds: 10
            periodSeconds: 15
          readinessProbe:
            tcpSocket:
              port: 25
            initialDelaySeconds: 10
            periodSeconds: 15
          volumeMounts:
          {{- if .Values.postfix.configuration }}
            - name: config
              mountPath: /overrides/postfix.cf
              subPath: postfix.cf
            {{- end }} 
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 100
              podAffinityTerm:
                labelSelector:
                  matchExpressions:
                    - key: app.kubernetes.io/name
                      operator: In
                      values:
                        - {{ include "postfix.name" . }}
                    - key: app.kubernetes.io/instance
                      operator: In
                      values:
                        - {{ .Release.Name }}
                topologyKey: "kubernetes.io/hostname"
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      volumes:
        {{- if .Values.postfix.configuration }}
        - name: config
          configMap:
            name: {{ include "postfix.fullname" . }}
        {{- end }}