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

{{- if .Values.webdav.enabled }}

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

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "mailu.fullname" . }}-webdav
spec:
  selector:
    matchLabels:
      app: {{ include "mailu.fullname" . }}
      component: webdav
  replicas: 1
  template:
    metadata:
      labels:
        app: {{ include "mailu.fullname" . }}
        component: webdav
    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: webdav
        image: {{ .Values.webdav.image.repository }}:{{ default .Values.mailuVersion .Values.webdav.image.tag }}
        imagePullPolicy: Always
        volumeMounts:
          - mountPath: /data
            name: data
            subPath: webdav
          {{- if .Values.timezone }}
          - name: zoneinfo
            mountPath: /usr/share/zoneinfo
            readOnly: true
          - name: zoneinfo
            subPath: {{ .Values.timezone }}
            mountPath: /etc/localtime
            readOnly: true
          {{- end }}
        ports:
          - name: http
            containerPort: 5232
        {{- with .Values.webdav.resources }}
        resources:
        {{- .|toYaml|nindent 10}}
        {{- end }}
        {{- if .Values.webdav.startupProbe}}
        startupProbe:
          httpGet:
            path: /
            port: http
          periodSeconds:  {{ default 10 .Values.webdav.startupProbe.periodSeconds }} 
          failureThreshold: {{ default 30 .Values.webdav.startupProbe.failureThreshold }} 
          timeoutSeconds: {{ default 5 .Values.webdav.startupProbe.timeoutSeconds }} 
        {{- end }}
        {{- if .Values.webdav.livenessProbe}}
        livenessProbe:
          httpGet:
            path: /
            port: http
          periodSeconds:  {{ default 10 .Values.webdav.livenessProbe.periodSeconds }} 
          failureThreshold: {{ default 3 .Values.webdav.livenessProbe.failureThreshold }} 
          timeoutSeconds: {{ default 5 .Values.webdav.livenessProbe.timeoutSeconds }} 
        {{- end }}
        {{- if .Values.webdav.readinessProbe}}
        readinessProbe:
          httpGet:
            path: /
            port: http
          periodSeconds:  {{ default 10 .Values.webdav.readinessProbe.periodSeconds }} 
          failureThreshold: {{ default 1 .Values.webdav.readinessProbe.failureThreshold }} 
          timeoutSeconds: {{ default 5 .Values.webdav.readinessProbe.timeoutSeconds }} 
        {{- 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.webdav.persistence.annotations }}
  annotations:
{{ toYaml .Values.webdav.persistence.annotations | indent 4 }}
{{- end }}
spec:
  accessModes:
    - {{ .Values.webdav.persistence.accessMode }}
  resources:
    requests:
      storage: {{ .Values.webdav.persistence.size }}
  {{- if .Values.webdav.persistence.storageClass }}
  storageClassName: {{ .Values.webdav.persistence.storageClass }}
  {{- end }}
{{- end }}
---

apiVersion: v1
kind: Service
metadata:
  name: {{ include "mailu.fullname" . }}-webdav
  labels:
    app: {{ include "mailu.fullname" . }}
    component: webdav
spec:
  selector:
    app: {{ include "mailu.fullname" . }}
    component: webdav
  ports:
  - name: http
    port: 5232
    protocol: TCP

{{- end }}