Max
Max

Reputation: 9879

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)

Upvotes: 9

Views: 14900

Answers (2)

Max
Max

Reputation: 9879

This works:

for f in *.yaml; do envsubst < $f | kubectl apply -f -; done

Upvotes: 10

Inian
Inian

Reputation: 85560

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 -

Upvotes: 7

Related Questions