A script to manage multiple isolated kubeconfig files by automatically merging them into your shell environment and normalizing their internal identifiers.
Single Context Limitation: This tool is designed to work with kubeconfig files that contain exactly one context. If a file contains multiple contexts, the script will skip it to prevent data corruption or ambiguity.
If you work with multiple Kubernetes clusters, managing separate kubeconfig files can become tedious. This tool scans a specified directory (and its subdirectories) for kubeconfig files, renames the internal cluster, user, and context names to match the parent folder and filename, and securely merges them into your KUBECONFIG environment variable.
- Auto-discovery: Recursively finds config files in
~/.kube/configs/. - Smart Renaming: Normalizes contexts to
<folder-name>-<file-name>to avoid context name collisions. - Dynamic Merging: Automatically constructs and exports the
KUBECONFIGpath list.
bashorzsh- yq (Go implementation by Mike Farah)
# Install on macOS via Homebrew brew install yq
- Place the
kube-configer.shscript in a safe location, for example,~/.kube/kube-configer.sh. - Make the script executable:
chmod +x ~/.kube/kube-configer.sh - Add the following alias to your
~/.zshrcor~/.bashrcfile.Note: You must use
sourceso the script can export the environment variable directly to your current shell session.alias refresh-kubeconfig="source ~/.kube/kube-configer.sh"
- Reload your shell configuration:
source ~/.zshrc
- Run the alias anytime you add a new configuration file to your
~/.kube/configsdirectory:refresh-kubeconfig
The script expects your configuration files to be organized in the following hierarchy:
~/.kube/configs/
├── <environment>/ # Optional: Grouping folder (e.g., prod, staging, dev)
│ ├── <cluster-name>.yaml # Individual kubeconfig file
│ └── <cluster-name>.yml
└── <stand-alone-config>.yaml%