Ashley
Ashley

Reputation: 1629

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.

Upvotes: 7

Views: 5436

Answers (2)

scarlet_dragon
scarlet_dragon

Reputation: 43

I tried the following command:

kubectl patch sc test-storage-class -p '{"allowVolumeExpansion": true}'

It worked fine and did not affect any of the existing PVs, PVCs or pods. When it ran fine, I executed the following command for my actual use case:

kubectl patch sc gp2 -p '{"allowVolumeExpansion": true}'

Upvotes: 2

Rico
Rico

Reputation: 61521

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 

Upvotes: 12

Related Questions