2

I have a PV/PVC in my kubernetes cluster.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0003
spec:
  capacity:
    storage: 5Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: slow
  nfs:
    path: /tmp
    server: 172.17.0.2

I want to externally add mountOptions to all PVs like below.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0003
spec:
  capacity:
    storage: 5Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: slow
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    path: /tmp
    server: 172.17.0.2

Is there any way I can achieve this using kubectl cli like we add annotations to ingress rules and pods?

Stefan Lasiewski
  • 17,380
  • 5
  • 28
  • 35
Ujjawal Khare
  • 756
  • 2
  • 7
  • 20
  • You want to add `mountOptions` to every PV in the cluster? What about PVs that already has some `mountOptions` provided - do you want to replace them or add to existing? – Mikolaj S. Nov 10 '21 at 16:03
  • @Mikolaj S No pvs in existing name space has mount options so it will be okay to add mountoptions to all of them – Ujjawal Khare Nov 11 '21 at 16:24

1 Answers1

1

You can use kubectl patch command to add mountOptions to existing PV in the cluster:

kubectl patch pv pv0003 --patch '{"spec": {"mountOptions": ["hard","nfsvers=4.1"]}}'

If you want to add mountOptions to every PV in the cluster you can use simple bash for loop and kubectl patch command:

for pv in $(kubectl get pv --no-headers -o custom-columns=":metadata.name"); do kubectl patch pv $pv --patch '{"spec": {"mountOptions": ["hard","nfsvers=4.1"]}}'; done
Mikolaj S.
  • 2,850
  • 1
  • 5
  • 17