how to Update and append value in Labels for kubernetes deployment

2/28/2019

I have requirement, where I would like to update and append labels value on deployment, using some form of automation

For example on initial Deployment I have set label on kubernetes deployment as

labels:
    customer: customerA

Now I would like update label as,

labels:
    customer: customerA,CustomerB

One of the way I figured out to achieve this is via using kubectl patch command

 kubectl patch deployment application --patch '{"spec": {"template": {"metadata": {"labels": {"customer": "customerB"}}}}}'

However above command shows the obvious behaviour and overwrites the previous label for customer and set the new value as

labels
    customer: CustomerB

however I would like to append the value in lables customer, one of of the way is to use command as below

kubectl patch deployment application --patch '{"spec": {"template": {"metadata": {"labels": {"customer": "customerA,customerB"}}}}}'

however with above approach, I would need to maintain list of customer label value and pass it.

is there a way to append values of labels in existing deployment, so that I dont need to maintain the list.

-- Ruchir Bharadwaj
kubernetes

0 Answers