Is there any way to add labels in .spec.template after a deployment has been created? So, I know this can be done
kubectl label deployment myDeployment myLabelKey=myLabelValue
But this would only add the label to .metadata.labels
. I would like to add a label to .spec.template.metadata.labels
.
the solution by @helmbert is correct but it's missing double quotes after myLabelValue.
$ kubectl patch deployment myDeployment --patch '{"spec": {"template": {"metadata": {"labels": {"myLabelKey": "myLabelValue"}}}}}'
This should be possible using the kubectl patch
command. The following patch file would add a new label to the spec.template.metadata.labels
property:
spec:
template:
metadata:
labels:
myLabelKey: myLabelValue
Then apply with:
$ kubectl patch deployment myDeployment --patch "$(cat patchfile.yaml)"
Alternatively, with inline JSON instead of a separate file:
$ kubectl patch deployment myDeployment --patch '{"spec": {"template": {"metadata": {"labels": {"myLabelKey": "myLabelValue}}}}}'