---
apiVersion: v1
kind: Template
metadata:
  name: {{ kaas_project }}-gfs-volumes
  annotations:
    descriptions: "{{ kaas_project }} glusterfs volumes"
objects:
{% for name, vol in kaas_project_volumes.items() %}
{% set voltypes = kaas_storage_domains | json_query("[*].volumes." + vol.volume + ".type") %}
{% set voltype = voltypes[0] | default('host') %}
{% set mntpaths = kaas_storage_domains | json_query("[*].volumes." + vol.volume + ".mount") %}
{% set mntpath = mntpaths[0] | default('') %}
{% set oc_name = vol.name | default(name) | regex_replace('_','-') %}
{% set cfgpath = vol.path | default("") %}
{% set path = cfgpath if cfgpath[:1] == "/" else "/" + kaas_project + "/" + cfgpath %}
{% if oc_name | regex_search("^" + kaas_project) %}
{%   set pvname = oc_name %}
{% else %}
{%   set pvname = (kaas_project + "-" + oc_name) | regex_replace('_','-') %}
{% endif %}
  - apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: {{ pvname }}
    spec:
      persistentVolumeReclaimPolicy: Retain 
{% if voltype == 'host' %}
      hostPath:
        path: "{{ mntpath }}{{ path }}"
{% else %}
      glusterfs: 
        endpoints: {{ kaas_glusterfs_endpoints }}
        path: "{{ vol.volume }}{{ path }}"
{% endif %}
        readOnly: {{ not (vol.write | default(false)) }}
      accessModes:
        - {{ vol.access | default(vol.write | default(false) | ternary('ReadWriteMany', 'ReadOnlyMany')) }}
      capacity:
        storage: {{ vol.capacity | default(kaas_default_volume_capacity) }}
      claimRef:
        name: {{ oc_name }}
        namespace: {{ kaas_project }}
  - apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: {{ oc_name }}
    spec:
      volumeName: {{ pvname }}
      accessModes:
        - {{ vol.access | default('ReadWriteMany') }}
      resources:
        requests:
          storage: {{ vol.capacity | default(kaas_default_volume_capacity) }}
{% endfor %}
