Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with dynamic persistent volume in Helm

I'm creating a helm chart with some PersistentVolumeClaims. I don't create any PersistentVolume resources so that the volumes will be dynamically created by the provisioner the user specifies. The PVs are successfully created by the provisioner, but subsequent helm upgrade fails due to immutable volumeName on the PVC after a PV is created and assigned.

To do so I create a PVC template without the spec.volumeName field. This works as expected and in this case my CSI provisioner creates a PersistentVolume and adds the "volumeName" to the PVC resource

PVC helm template:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: test-pvc
  namespace: {{ .Release.Namespace }}  
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 4Gi
  storageClassName: {{ .Values.storageClassName }}

The PV gets created by the provisioner, which adds volumeName to the PVC resource in the cluster:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: test-pvc
  namespace: default
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 4Gi
  storageClassName: csi-sc
  volumeName: pvc-9ace9d3f-84f8-4504-a419-2c7a327122da

But now when I run helm upgrade, helm always gets an error from the k8s api because the volumeName does not match, even though I didn't explicitly set this field in the pvc.yaml:

cannot patch "test-pvc" with kind PersistentVolumeClaim: PersistentVolumeClaim "test-pvc" is invalid: spec: Forbidden: spec is immutable after creation except resources.requests for bound claims
  core.PersistentVolumeClaimSpec{
        AccessModes:      {"ReadWriteOnce"},
        Selector:         nil,
        Resources:        {Requests: {s"storage": {i: {...}, Format: "BinarySI"}}},
-       VolumeName:       "pvc-9ace9d3f-84f8-4504-a419-2c7a327122da",
+       VolumeName:       "",
        StorageClassName: &"csi-sc",
        VolumeMode:       &"Filesystem",
        ... // 2 identical fields
  }

Is there a recommended way to deal with dynamic volume claims in helm charts? Or is it unavoidable to create PersistentVolume resources manually?

like image 338
jongarner Avatar asked Sep 01 '25 01:09

jongarner


1 Answers

With helm v3, you can retrieve the volumeName with lookup function.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: test-pvc
  namespace: default
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 4Gi
  storageClassName: csi-sc
  {{- if (lookup "v1" "PersistentVolumeClaim" "default" "test-pvc") }}
  volumeName: {{ (lookup "v1" "PersistentVolumeClaim" "default" "test-pvc").spec.volumeName }}
  {{- end }}
like image 186
cyril Avatar answered Sep 03 '25 00:09

cyril