2 min read | by Jordi Prats
We can use the kubectl patch command with the -p option to update an existing kubernetes object:
$ kubectl patch sc gp2 -p '{"allowVolumeExpansion": true}' storageclass.storage.k8s.io/gp2 patched
When the patch is small is a very convenient way of patching it but as the patch grows it becomes less convenient
We can use the --patch-file option to specify a file containing what we want to add to the kubernetes object, for example:
metadata: labels: patch: demo
This is going to add the specified label to the existing ones. For example, the following object has the following labels:
$ kubectl describe pod testvault-vaultcli Name: testvault-vaultcli Namespace: testvault Priority: 0 Service Account: default Node: minikube/192.168.49.2 Start Time: Fri, 16 Sep 2022 21:08:38 +0200 Labels: app.kubernetes.io/instance=testvault app.kubernetes.io/managed-by=Helm app.kubernetes.io/name=testvault app.kubernetes.io/version=1.16.0 component=cli helm.sh/chart=testvault-1.0.0 Annotations: meta.helm.sh/release-name: testvault meta.helm.sh/release-namespace: testvault (...)
If we apply the patch file like follows:
$ kubectl patch pod testvault-vaultcli --patch-file patch_demo.yaml pod/testvault-vaultcli patched
We can see both objects gets merged:
$ kubectl describe pod testvault-vaultcli Name: testvault-vaultcli Namespace: testvault Priority: 0 Service Account: default Node: minikube/192.168.49.2 Start Time: Fri, 16 Sep 2022 21:08:38 +0200 Labels: app.kubernetes.io/instance=testvault app.kubernetes.io/managed-by=Helm app.kubernetes.io/name=testvault app.kubernetes.io/version=1.16.0 component=cli helm.sh/chart=testvault-1.0.0 patch=demo Annotations: meta.helm.sh/release-name: testvault meta.helm.sh/release-namespace: testvault Status: Running (...)
Posted on 19/09/2022