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)
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 -
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With