apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "mailproxy.fullname" . }}-mailauth
  labels:
    {{- include "mailproxy.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.mailauth.replicaCount }}
  selector:
    matchLabels:
      {{- include "mailproxy.mailauth.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "mailproxy.mailauth.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.mailauth.image.repository }}:{{ .Values.mailauth.image.tag }}"
          imagePullPolicy: {{ .Values.mailauth.image.pullPolicy }}
          ports:
          {{- range $i, $port := .Values.mailauth.service.ports }}
            - name: {{ $port.name }}
              containerPort: {{ $port.number }}
              protocol: {{ $port.protocol }}
          {{- end }}
          {{- if .Values.mailauth.relayHost }}
          env:
            - name: RELAY_HOST
              value: {{ .Values.mailauth.relayHost | quote }}
          {{- end }}
          livenessProbe:
            tcpSocket:
              port: {{ (index .Values.mailauth.service.ports 0).number }}
            initialDelaySeconds: 5
            periodSeconds: 10
          readinessProbe:
            tcpSocket:
              port: {{ (index .Values.mailauth.service.ports 0).number }}
            initialDelaySeconds: 5
            periodSeconds: 10
          resources:
            {{- toYaml .Values.mailauth.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 "mailproxy.name" . }}
                    - key: app.kubernetes.io/instance
                      operator: In
                      values:
                        - {{ .Release.Name }}
                    - key: app.kubernetes.io/component
                      operator: In
                      values:
                        - mailauth
                topologyKey: "kubernetes.io/hostname"
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}