kubernetes

kubectl command to patch a specific attribute in kubernetes storage class


I have the below Kubernetes storage class

Name:            encrypted-gp2
IsDefaultClass:  No
Annotations:     kubectl.kubernetes.io/last-applied-configuration={"allowVolumeExpansion":true,"apiVersion":"storage.k8s.io/v1","kind":"StorageClass","metadata":{"annotations":{},"name":"encrypted-gp2"},"parameters":{"encrypted":"true","fsType":"ext4","type":"gp2"},"provisioner":"kubernetes.io/aws-ebs","volumeBindingMode":"WaitForFirstConsumer"}

Provisioner:           kubernetes.io/aws-ebs
Parameters:            encrypted=true,fsType=ext4,type=gp2
AllowVolumeExpansion:  True
MountOptions:          <none>
ReclaimPolicy:         Delete
VolumeBindingMode:     WaitForFirstConsumer
Events:                <none>

Now I want to patch this storage class for only ReclaimPolicy attribute so that ReclaimPolicy is switched to Retain

I am looking for a specific patch command in this case.


Solution

  • It would be short and simple 🙂:

    kubectl patch sc encrypted-gp2 -p '{"ReclaimPolicy":"Retain"}'
    

    But StorageClass is immutable so you will have to create a new one:

    $ kubectl get sc encrypted-gp2 -o=yaml > sc.yaml
    

    Edit the sc.yaml and change the reclaim policy 🖥️. Then:

    $ kubectl replace -f sc.yaml --force