Managing Multiple Kube Config Files

This is a simple script that takes multiple kube config files and deeply merges them into one.

Simply place your kube config files in ~/.kube/configs/ and run the script to replace ~/.kube/config with the merged result of the config folder.

# Merge multiple kube config files into one for use with kubectl

set -e


# Merge all configs in this folder into a config file for use by kubectl
yq eval-all '. as $item ireduce ({}; . *+ $item )' $CONFIGS_DIR/*.yaml > $MERGE_FILE

# Backup current config
printf -v datestamp '%(%Y-%m-%d-%H-%M-%S)T'
cp $KUBE_CONFIG "$KUBE_CONFIG.$datestamp"

# Overwrite the old config

See also