Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substitute environment variable in all files when kubectl apply-ing

Let's say we have a number of Kubernetes configuration files in a folder kubernetes and we want to apply them all:

kubectl apply -f kubernetes -n MyNamespace

Some of these files contain environment variables which need to be substituted first (no templating in Kubernetes). For instance, several of the deployment yamls contain something like:

image: myregistry.com/myrepo:$TAG

For a single yaml file, this can be done e.g. by using envsubst like this:

envsubst < deploy.yml | kubectl apply -f -

What's the best way to do these substitutions for all the yaml files?

(Looping over the files in the folder and calling envsubst as above is one option, but I suspect that it would be preferrable to pass the entire folder to kubectl and not individual files)

like image 562
Max Avatar asked Jan 24 '23 13:01

Max


1 Answers

You can let envsubst read the content of multiple files from standard input as if it were reading one file < deploy.yaml either using process substitution <(..) feature of bash or using plain ol' cat

envsubst < <(cat *.yaml) | kubectl apply -f -

or

cat *.yaml | envsubst - | kubectl apply -f -
like image 118
Inian Avatar answered Jan 29 '23 10:01

Inian