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

{{- if .Values.roundcube.enabled }}

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

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "mailu.fullname" . }}-roundcube
spec:
  selector:
    matchLabels:
      app: {{ include "mailu.fullname" . }}
      component: roundcube
  replicas: 1
  template:
    metadata:
      labels:
        app: {{ include "mailu.fullname" . }}
        component: roundcube
    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: roundcube
        image: {{ template "imageRegistry" . -}} {{ .Values.roundcube.image.repository }}:{{ default .Values.mailuVersion .Values.roundcube.image.tag }}
        imagePullPolicy: Always
        volumeMounts:
          - mountPath: /data
            name: data
            subPath: roundcube
          {{- if .Values.timezone }}
          - name: zoneinfo
            mountPath: /usr/share/zoneinfo
            readOnly: true
          - name: zoneinfo
            subPath: {{ .Values.timezone }}
            mountPath: /etc/localtime
            readOnly: true
          {{- end }}
        env:
          - name: MESSAGE_SIZE_LIMIT
            value: "{{ mul .Values.mail.messageSizeLimitInMegabytes (mul 1024 1024) }}"
          - name: IMAP_ADDRESS
            value: {{ include "mailu.fullname" . }}-dovecot
          - name: FRONT_ADDRESS
            value: {{ include "mailu.fullname" . }}-front
          - name: SECRET_KEY
            valueFrom:
              secretKeyRef:
                name: mailu-session-encryption-key
                key: key
          - name: SUBNET
            value: {{ .Values.subnet }}
          {{- if eq .Values.database.roundcubeType "sqlite" }}
          - name: ROUNDCUBE_DB_FLAVOR
            value: sqlite
          {{- else if eq .Values.database.roundcubeType "mysql" }}
          - name: ROUNDCUBE_DB_FLAVOR
            value: mysql
          - name: ROUNDCUBE_DB_USER
            value: {{ required "database.mysql.roundcubeUser" .Values.database.mysql.roundcubeUser }}
          - name: ROUNDCUBE_DB_PW
            value: {{ required "database.mysql.roundcubePassword" .Values.database.mysql.roundcubePassword }}
          - name: ROUNDCUBE_DB_NAME
            value: {{ required "database.mysql.roundcubeDatabase" .Values.database.mysql.roundcubeDatabase }}
          - name: ROUNDCUBE_DB_HOST
            {{- if .Values.database.mysql.host }}
            value: {{ .Values.database.mysql.host }}
            {{- else }}
            value: {{ include "mailu.fullname" . }}-mysql
            {{- end }}
          {{- else if eq .Values.database.roundcubeType "postgresql" }}
          - name: ROUNDCUBE_DB_FLAVOR
            value: postgresql
          - name: ROUNDCUBE_DB_USER
            value: {{ required "database.postgresql.roundcubeUser" .Values.database.postgresql.roundcubeUser }}
          - name: ROUNDCUBE_DB_PW
            value: {{ required "database.postgresql.roundcubePassword" .Values.database.postgresql.roundcubePassword }}
          - name: ROUNDCUBE_DB_NAME
            value: {{ required "database.postgresql.roundcubeDatabase" .Values.database.postgresql.roundcubeDatabase }}
          - name: ROUNDCUBE_DB_HOST
            {{- if .Values.database.postgresql.host }}
            value: {{ .Values.database.postgresql.host }}
            {{- end }}
          {{- else }}
          value: {{ required "database.type must be one of sqlite/mysql/postgresql" .None }}
          {{- end }}
        ports:
          - name: http
            containerPort: 80
        {{- with .Values.roundcube.resources }}
        resources:
        {{- .|toYaml|nindent 10}}
        {{- end }}
        {{- if .Values.roundcube.startupProbe}}
        startupProbe:
          httpGet:
            path: /
            port: http
          periodSeconds:  {{ default 10 .Values.roundcube.startupProbe.periodSeconds }} 
          failureThreshold: {{ default 30 .Values.roundcube.startupProbe.failureThreshold }} 
          timeoutSeconds: {{ default 5 .Values.roundcube.startupProbe.timeoutSeconds }} 
        {{- end }}
        {{- if .Values.roundcube.livenessProbe}}
        livenessProbe:
          httpGet:
            path: /
            port: http
          periodSeconds:  {{ default 10 .Values.roundcube.livenessProbe.periodSeconds }} 
          failureThreshold: {{ default 3 .Values.roundcube.livenessProbe.failureThreshold }} 
          timeoutSeconds: {{ default 5 .Values.roundcube.livenessProbe.timeoutSeconds }} 
        {{- end }}
        {{- if .Values.roundcube.readinessProbe}}
        readinessProbe:
          httpGet:
            path: /
            port: http
          periodSeconds:  {{ default 10 .Values.roundcube.readinessProbe.periodSeconds }} 
          failureThreshold: {{ default 1 .Values.roundcube.readinessProbe.failureThreshold }} 
          timeoutSeconds: {{ default 5 .Values.roundcube.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.roundcube.persistence.annotations }}
  annotations:
{{ toYaml .Values.roundcube.persistence.annotations | indent 4 }}
{{- end }}
spec:
  accessModes:
    - {{ .Values.roundcube.persistence.accessMode }}
  resources:
    requests:
      storage: {{ .Values.roundcube.persistence.size }}
  {{- if .Values.roundcube.persistence.storageClass }}
  storageClassName: {{ .Values.roundcube.persistence.storageClass }}
  {{- end }}
{{- end }}

---

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

{{- end }}
