From 01f2689eca64d2e844f26eb82da23db7ed4fafe5 Mon Sep 17 00:00:00 2001
From: Andri Joos <andri@joos.io>
Date: Fri, 1 Nov 2024 21:11:17 +0100
Subject: [PATCH] add support for multiple volumes

---
 nfs-static-provisioning/Chart.yaml            |  4 +--
 .../templates/_helpers.tpl                    | 11 --------
 .../templates/persistentVolume.yaml           | 23 +++++++++--------
 .../templates/persistentVolumeClaim.yaml      | 17 +++++++------
 nfs-static-provisioning/values.yaml           | 25 ++++++++-----------
 5 files changed, 35 insertions(+), 45 deletions(-)

diff --git a/nfs-static-provisioning/Chart.yaml b/nfs-static-provisioning/Chart.yaml
index fc2b76b..b80576b 100644
--- a/nfs-static-provisioning/Chart.yaml
+++ b/nfs-static-provisioning/Chart.yaml
@@ -15,10 +15,10 @@ type: application
 # This is the chart version. This version number should be incremented each time you make changes
 # to the chart and its templates, including the app version.
 # Versions are expected to follow Semantic Versioning (https://semver.org/)
-version: 1.0.0
+version: 1.1.0
 
 # This is the version number of the application being deployed. This version number should be
 # incremented each time you make changes to the application. Versions are not expected to
 # follow Semantic Versioning. They should reflect the version the application is using.
 # It is recommended to use it with quotes.
-appVersion: "1.0.0"
+appVersion: "1.1.0"
diff --git a/nfs-static-provisioning/templates/_helpers.tpl b/nfs-static-provisioning/templates/_helpers.tpl
index 303fc0f..19e331e 100644
--- a/nfs-static-provisioning/templates/_helpers.tpl
+++ b/nfs-static-provisioning/templates/_helpers.tpl
@@ -60,14 +60,3 @@ Create the name of the service account to use
 {{- default "default" .Values.serviceAccount.name }}
 {{- end }}
 {{- end }}
-
-{{/*
-PersistentVolume
-*/}}
-{{- define "nfs-static-provisioning.persistentvolume.name" -}}
-{{- if .Values.pvName }}
-{{- printf "pvc-static-%s" .Values.pvName }}
-{{- else}}
-{{- printf "pvc-static-%s" uuidv4 }}
-{{- end }}
-{{- end }}
diff --git a/nfs-static-provisioning/templates/persistentVolume.yaml b/nfs-static-provisioning/templates/persistentVolume.yaml
index fa75cf8..abe9c16 100644
--- a/nfs-static-provisioning/templates/persistentVolume.yaml
+++ b/nfs-static-provisioning/templates/persistentVolume.yaml
@@ -1,25 +1,28 @@
+{{- range .Values.volumes }}
+---
 apiVersion: v1
 kind: PersistentVolume
 metadata:
-  name: {{ include "nfs-static-provisioning.persistentvolume.name" . }}
-  {{- with .Values.annotations }}
+  name: {{ .pvName }}
+  {{- with $.Values.annotations }}
   annotations:
     {{- toYaml . | nindent 4 }}
   {{- end }}
   labels:
-    {{- include "nfs-static-provisioning.labels" . | nindent 4 }}
+    {{- include "nfs-static-provisioning.labels" $ | nindent 4 }}
 spec:
   capacity:
-    storage: {{ .Values.size }}
-  {{- with .Values.accessModes }}
+    storage: {{ .size }}
+  {{- with .accessModes }}
   accessModes:
     {{- toYaml . | nindent 4 }}
   {{- end }}
-  persistentVolumeReclaimPolicy: {{ .Values.reclaimPolicy }}
-  storageClassName: {{ .Values.storageClassName }}
+  persistentVolumeReclaimPolicy: {{ .reclaimPolicy }}
+  storageClassName: {{ .storageClassName }}
   csi:
     driver: nfs.csi.k8s.io
-    volumeHandle: {{ include "nfs-static-provisioning.persistentvolume.name" . }} # Unique identifier for the NFS volume
+    volumeHandle: {{ .pvName }} # Unique identifier for the NFS volume
     volumeAttributes:
-      server: {{ .Values.server }}
-      share: {{ .Values.path }}
+      server: {{ .server }}
+      share: {{ .path }}
+{{- end }}
diff --git a/nfs-static-provisioning/templates/persistentVolumeClaim.yaml b/nfs-static-provisioning/templates/persistentVolumeClaim.yaml
index e8252e7..d50475c 100644
--- a/nfs-static-provisioning/templates/persistentVolumeClaim.yaml
+++ b/nfs-static-provisioning/templates/persistentVolumeClaim.yaml
@@ -1,20 +1,23 @@
+{{- range .Values.volumes }}
+---
 apiVersion: v1
 kind: PersistentVolumeClaim
 metadata:
-  name: {{ .Release.Name }}
-  {{- with .Values.annotations }}
+  name: {{ .name }}
+  {{- with $.Values.annotations }}
   annotations:
     {{- toYaml . | nindent 4 }}
   {{- end }}
   labels:
-    {{- include "nfs-static-provisioning.labels" . | nindent 4 }}
+    {{- include "nfs-static-provisioning.labels" $ | nindent 4 }}
 spec:
-  {{- with .Values.accessModes }}
+  {{- with .accessModes }}
   accessModes:
     {{- toYaml . | nindent 4 }}
   {{- end }}
-  storageClassName: {{ .Values.storageClassName }}
-  volumeName: {{ include "nfs-static-provisioning.persistentvolume.name" . }}
+  storageClassName: {{ .storageClassName }}
+  volumeName: {{ .pvName }}
   resources:
     requests:
-      storage: {{ .Values.size }}
+      storage: {{ .size }}
+{{- end}}
diff --git a/nfs-static-provisioning/values.yaml b/nfs-static-provisioning/values.yaml
index 1505acf..4ba3564 100644
--- a/nfs-static-provisioning/values.yaml
+++ b/nfs-static-provisioning/values.yaml
@@ -1,17 +1,12 @@
-accessModes:
-- ReadWriteMany
-
-storageClassName: nfs-csi
-
-size: 1Mi
+volumes: []
+# - name: pvc
+#   pvName: pv
+#   size: 1Mi
+#   reclaimPolicy: Retain
+#   storageClassName: nfs-csi
+#   server: nfs-server.nfs-server.svc.cluster.local
+#   path: /
+#   accessModes:
+#   - ReadWriteMany
 
 annotations: {}
-
-reclaimPolicy: Retain
-
-server: nfs-server.nfs-server.svc.cluster.local
-path: /
-
-# It is highly recommended to set pvName, otherwise it is generated using uuid4.
-# Therefore, the pv changes everytime, which leads to errors while applying because the volumeName in the pvc is not allowed to change
-pvName:
-- 
GitLab