bitscuit
bitscuit

Reputation: 1052

Label deployment pod templates

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.

Upvotes: 2

Views: 4643

Answers (2)

helmbert
helmbert

Reputation: 38064

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"}}}}}'

Upvotes: 7

Chris
Chris

Reputation: 81

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"}}}}}'

Upvotes: 2

Related Questions