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

{{- if .Values.persistence.single_pvc }}
{{- if .Values.persistence.hostPath }}
########################################
###
### Variant: persistence.hostPath is set
###
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: {{ include "mailu.fullname" . }}-storage
{{- if .Values.persistence.annotations }}
  annotations:
{{ toYaml .Values.persistence.annotations | indent 4 }}
{{- end }}
spec:
  accessModes:
    - ReadWriteMany
  volumeName: {{ include "mailu.fullname" . }}-storage
  resources:
    requests:
      storage: {{ .Values.persistence.size }}

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: {{ include "mailu.claimName" . }}
spec:
  capacity:
    storage: {{ .Values.persistence.size }}
  accessModes:
  - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  volumeMode: Filesystem
  hostPath:
    path: {{ required "persistence.hostPath" .Values.persistence.hostPath }}
    type: DirectoryOrCreate
  {{ if .Values.nodeSelector }}
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        {{- range $k,$v := .Values.nodeSelector }}
        - key: "{{ $k }}"
          operator: In
          values:
            - "{{ $v }}"
        {{- end }}
  {{- end }}
{{- else if not .Values.persistence.existingClaim }}
########################################
###
### Variant: normal volume claim
###

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: {{ include "mailu.claimName" . }}
{{- if .Values.persistence.annotations }}
  annotations:
{{ toYaml .Values.persistence.annotations | indent 4 }}
{{- end }}
spec:
  accessModes:
    - {{ .Values.persistence.accessMode }}
  resources:
    requests:
      storage: {{ .Values.persistence.size }}
  {{- if .Values.persistence.storageClass }}
  {{- if (eq "-" .Values.persistence.storageClass) }}
  storageClassName: ""
  {{- else }}
  storageClassName: {{ .Values.persistence.storageClass }}
  {{- end }}
  {{- end }}
{{- end }}
{{- end }}
