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

{{- if .Values.fetchmail.enabled }}

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

{{ if .Capabilities.APIVersions.Has "apps/v1/Deployment" }}
apiVersion: apps/v1
{{ else }}
apiVersion: extensions/v1beta1
{{ end }}
kind: Deployment
metadata:
  name: {{ include "mailu.fullname" . }}-fetchmail
spec:
  selector:
    matchLabels:
      app: {{ include "mailu.fullname" . }}
      component: fetchmail
  replicas: 1
  template:
    metadata:
      labels:
        app: {{ include "mailu.fullname" . }}
        component: fetchmail
    spec:
      serviceAccount: {{ include "mailu.serviceAccountName" . }}
      securityContext:
        runAsUser: 0
      {{- 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: admin
        image: {{ .Values.fetchmail.image.repository }}:{{ default .Values.mailuVersion .Values.fetchmail.image.tag }}
        imagePullPolicy: Always
        volumeMounts:
          - name: data
            subPath: dovecotdata
            mountPath: /data
          {{- if .Values.timezone }}
          - name: zoneinfo
            mountPath: /usr/share/zoneinfo
            readOnly: true
          - name: zoneinfo
            subPath: {{ .Values.timezone }}
            mountPath: /etc/localtime
            readOnly: true
          {{- end }}
        env:
          # LOG_LEVEL is called DEBUG in https://github.com/Mailu/Mailu/blob/master/optional/fetchmail/fetchmail.py#L98
          # and will only give debug output if value is True
          - name: DEBUG
          {{- if or (eq .Values.logLevel "DEBUG") (eq .Values.fetchmail.logLevel "DEBUG") }}
            value: "True"
          {{- else }}
            value: "False"
          {{- end }}
          - name: HOST_SMTP
            value: {{ include "mailu.fullname" . }}-front
          - name: HOST_ADMIN
            value: {{ include "mailu.fullname" . }}-admin
          - name: FETCHMAIL_DELAY
            value: "{{ .Values.fetchmail.delay }}"
        ports:
          - containerPort: 5232
          - containerPort: 80
        {{- with .Values.fetchmail.resources }}
        resources:
        {{- .|toYaml|nindent 10}}
        {{- end }}
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: {{ $claimName }}
        {{- if .Values.timezone }}
        - name: zoneinfo
          hostPath:
            path: /usr/share/zoneinfo
            type: Directory
        {{- end }}
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 0
      maxSurge: 1


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

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

{{- end }}
