can you use kubectl patch to add a value dynamically

8/4/2018

I am to patch an image onto init containers

my image is stored in a variable called $IMAGE_NAME

When I run

kubectl patch deployment production-art-backend -p {"spec":{"template":{"spec":{"initContainers":[{"name":"run-migrations","image":"$IMAGE_NAME"}]}}}}

it patches the image as 'IMAGE_NAME' and not the value inside the variable IMAGE_NAME, how can I patch the image dynamically?

-- cjmash
kubectl
kubernetes
kubernetes-helm
patch

2 Answers

4/2/2020

Please try below command :

kubectl patch deployment production-art-backend --patch="{\"spec\":{\"template\":{\"spec\":{\"initContainers\":[{\"name\":\"run-migrations\",\"image\":\"$IMAGE_NAME\"}]}}}}"

It worked for me.

-- taher
Source: StackOverflow

8/4/2018

This is a bash issue, not necessarily a kubectl issue!

Because the JSON string requires double quotes, you need to wrap the variable inside single quotes. Try this:

kubectl patch deployment production-art-backend -p {"spec":{"template":{"spec":{"initContainers":[{"name":"run-migrations","image":"'${IMAGE_NAME}'"}]}}}}
-- jaxxstorm
Source: StackOverflow