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

{{- $clusterDomain := default "cluster.local" .Values.clusterDomain}}
apiVersion: apps/v1
kind: {{ required "front.controller.kind" .Values.front.controller.kind }}
metadata:
  name: {{ include "mailu.fullname" . }}-front
spec:
  selector:
    matchLabels:
      app: {{ include "mailu.fullname" . }}
      component: front
  {{- if eq .Values.front.controller.kind "Deployment" }}
  replicas: 1
  strategy:
  {{- else if eq .Values.front.controller.kind "DaemonSet" }}
  updateStrategy:
  {{- end }}
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      {{- if eq .Values.front.controller.kind "Deployment" }}
      maxSurge: 0
      {{- end }}
  template:
    metadata:
      labels:
        app: {{ include "mailu.fullname" . }}
        component: front
    spec:
      serviceAccount: {{ include "mailu.serviceAccountName" . }}
      securityContext:
        runAsUser: 0
      {{- with (.Values.front.nodeSelector | default .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: front
        image: {{ template "imageRegistry" . -}} {{ .Values.front.image.repository }}:{{ default .Values.mailuVersion .Values.front.image.tag }}
        imagePullPolicy: Always
        # volumeMounts:
          # - name: certs
          #   mountPath: /certs
          # {{- 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.front.logLevel }}
          - name: KUBERNETES_INGRESS
            value: "{{ .Values.ingress.externalIngress }}"
          - name: TLS_FLAVOR
            value: {{ default "cert" .Values.ingress.tlsFlavor }}
          - name: HOSTNAMES
            value: "mail.{{ .Values.dnsWildcard }}"
          - name: ADMIN_ADDRESS
            value: {{ include "mailu.fullname" . }}-admin.{{ .Release.Namespace }}.svc.{{ $clusterDomain }}
          - name: ANTISPAM_WEBUI_ADDRESS
            value: {{ include "mailu.fullname" . }}-rspamd.{{ .Release.Namespace }}.svc.{{ $clusterDomain }}:11334
          {{- if .Values.roundcube.enabled }}
          - name: WEBMAIL
            value: roundcube
          - name: WEBMAIL_ADDRESS
            value: {{ include "mailu.fullname" . }}-roundcube.{{ .Release.Namespace }}.svc.{{ $clusterDomain }}
          {{- else }}
          - name: WEBMAIL
            value: none
          - name: WEBMAIL_ADDRESS
            value: localhost
          {{- end }}
          - name: MESSAGE_SIZE_LIMIT
            value: "{{ mul .Values.mail.messageSizeLimitInMegabytes (mul 1024 1024) }}"
          - name: WEB_WEBMAIL
            value: "/"
          - name: WEBDAV
            value: none
          - name: WEBDAV_ADDRESS
            value: localhost
          - name: ADMIN
            value: "true"
          - name: WEB_ADMIN
            value: "/admin"
          {{- if .Values.webdav.enabled }}
          - name: WEBDAV
            value: radicale
          - name: WEBDAV_ADDRESS
            value: {{ include "mailu.fullname" . }}-webdav.{{ .Release.Namespace }}.svc.{{ $clusterDomain }}:5232
          {{- end }}
          - name: SUBNET
            value: {{ .Values.subnet }}
        ports:
          - name: pop3
            protocol: TCP
            containerPort: 110
            {{- if .Values.front.hostPort.enabled }}
            hostPort: 110
            {{- end}}
          - name: pop3s
            protocol: TCP
            containerPort: 995
            {{- if .Values.front.hostPort.enabled }}
            hostPort: 995
            {{- end}}
          - name: imap
            protocol: TCP
            containerPort: 143
            {{- if .Values.front.hostPort.enabled }}
            hostPort: 143
            {{- end}}
          - name: imaps
            protocol: TCP
            containerPort: 993
            {{- if .Values.front.hostPort.enabled }}
            hostPort: 993
            {{- end}}
          - name: smtp
            protocol: TCP
            containerPort: 25
            {{- if .Values.front.hostPort.enabled }}
            hostPort: 25
            {{- end}}
          - name: smtps
            protocol: TCP
            containerPort: 465
            {{- if .Values.front.hostPort.enabled }}
            hostPort: 465
            {{- end}}
          - name: smtp-auth
            protocol: TCP
            containerPort: 10025
          - name: imap-auth
            protocol: TCP
            containerPort: 10143
          - name: smtpd
            protocol: TCP
            containerPort: 587
            {{- if .Values.front.hostPort.enabled }}
            hostPort: 587
            {{- end}}
          - name: auth
            containerPort: 8000
            protocol: TCP
          - name: http
            containerPort: 80
            protocol: TCP
          {{ if not .Values.ingress.externalIngress }}
          - name: https
            containerPort: 443
            protocol: TCP
          {{end}}
        {{- with .Values.front.resources }}
        resources:
        {{- .|toYaml|nindent 10}}
        {{- end }}
        {{- if .Values.front.startupProbe}}
        startupProbe:
          httpGet:
            path: /health
            port: http
          periodSeconds:  {{ default 10 .Values.front.startupProbe.periodSeconds }}
          failureThreshold: {{ default 30 .Values.front.startupProbe.failureThreshold }}
          timeoutSeconds: {{ default 5 .Values.front.startupProbe.timeoutSeconds }}
        {{- end }}
        {{- if .Values.front.livenessProbe}}
        livenessProbe:
          httpGet:
            path: /health
            port: http
          periodSeconds:  {{ default 10 .Values.front.livenessProbe.periodSeconds }}
          failureThreshold: {{ default 3 .Values.front.livenessProbe.failureThreshold }}
          timeoutSeconds: {{ default 5 .Values.front.livenessProbe.timeoutSeconds }}
        {{- end }}
        {{- if .Values.front.readinessProbe}}
        readinessProbe:
          httpGet:
            path: /health
            port: http
          periodSeconds:  {{ default 10 .Values.front.readinessProbe.periodSeconds }}
          failureThreshold: {{ default 1 .Values.front.readinessProbe.failureThreshold }}
          timeoutSeconds: {{ default 5 .Values.front.readinessProbe.timeoutSeconds }}
        {{- end }}
      # volumes:
        # - name: certs
        #   secret:
        #     items:
        #       - key: tls.crt
        #         path: cert.pem
        #       - key: tls.key
        #         path: key.pem
        #     secretName: {{ include "mailu.fullname" . }}-certificates
        # {{- if .Values.timezone }}
        # - name: zoneinfo
        #   hostPath:
        #     path: /usr/share/zoneinfo
        #     type: Directory
        # {{- end }}
      restartPolicy: Always
      terminationGracePeriodSeconds: 60

---

apiVersion: v1
kind: Service
metadata:
  name: {{ include "mailu.fullname" . }}-front
  labels:
    app: {{ include "mailu.fullname" . }}
    component: front
spec:
  selector:
    app: {{ include "mailu.fullname" . }}
    component: front
  ports:
  - name: pop3
    port: 110
    protocol: TCP
  - name: pop3s
    port: 995
    protocol: TCP
  - name: imap
    port: 143
    protocol: TCP
  - name: imaps
    port: 993
    protocol: TCP
  - name: smtp
    port: 25
    protocol: TCP
  - name: smtps
    port: 465
    protocol: TCP
  - name: smtpd
    port: 587
    protocol: TCP
  - name: smtp-auth
    port: 10025
    protocol: TCP
  - name: imap-auth
    port: 10143
    protocol: TCP
  - name: http
    port: 80
    protocol: TCP
{{ if not .Values.ingress.externalIngress }}
  - name: https
    port: 443
    protocol: TCP
{{end}}

---
{{- with .Values.front.externalService }}
{{- if .enabled }}

apiVersion: v1
kind: Service
metadata:
  name: {{ include "mailu.fullname" $ }}-front-ext
  labels:
    app: {{ include "mailu.fullname" $ }}
    component: front
  {{- with .annotations }}
  annotations:
{{ . | toYaml | indent 4 }}
  {{- end }}
spec:
  type: {{ .type | default "ClusterIP" }}
  externalTrafficPolicy: {{ .externalTrafficPolicy | default "Local" }}
  {{- if .loadBalancerIP }}
  loadBalancerIP: {{ .loadBalancerIP }}
  {{- end }}
  selector:
    app: {{ include "mailu.fullname" $ }}
    component: front
  ports:
{{- with .pop3 }}
  {{- if .pop3 }}
  - name: pop3
    port: 110
    protocol: TCP
  {{- end }}
  {{- if .pop3s }}
  - name: pop3s
    port: 995
    protocol: TCP
  {{- end }}
{{- end }}

{{- with .imap }}
  {{- if .imap }}
  - name: imap
    port: 143
    protocol: TCP
  {{- end }}
  {{- if .imaps }}
  - name: imaps
    port: 993
    protocol: TCP
  {{- end }}
{{- end }}

{{- with .smtp }}
  {{- if .smtp }}
  - name: smtp
    port: 25
    protocol: TCP
  {{- end }}
  {{- if .smtps }}
  - name: smtps
    port: 465
    protocol: TCP
  {{- end }}
  {{- if .submission }}
  - name: smtpd
    port: 587
    protocol: TCP
  {{- end }}
{{- end }}

{{- end }}
{{- end }}
