Simple script to easily change kubeconfig and default kubernetes namespace with one command and short aliases. The script modifies environmental variables for current shell session.
It also provides a quick way to run scripts (multiple predefined examples, but you can add your own) with aliases. That makes work with deployments even faster and more convenient.
Unfortunately it is not possible (afaik) to pass kubeconfig file contents instead of file path. For slightly better security, kubeconfig files are stored encrypted at rest. The script decrypts the kubernetes config under entered alias and deletes plain text config right after ending current shell session.
🖥️ This script requires
kubectl,gpgand additional packages depending on the subcommands you intend to use.
⚠️ Read the code carefully. You are installing it at your own risk.
-
Clone repository to desired script location and enter repository directory.
-
Add alias to your shell file configuration - most likely
.bashrcor.zshrc(replace<skc_path>with correct path). If it is not the first time installing SKC, make sure to remove old aliases.# SKC alias skc="source <skc_path>/skc.sh"
You can use one of these commands that we'll do this step for you, but make sure to use '>>' instead of '>' - otherwise you risk removing your configuration.
$ printf "# SKC\nalias skc=\"source `pwd`/skc.sh\"\n" >> ~/.bashrc $ printf "# SKC\nalias skc=\"source `pwd`/skc.sh\"\n" >> ~/.zshrc
-
Set up your namespaces:
- Create a directory to store the config and copy the template configuration file.
$ mkdir config $ cp namespaces_template config/namespaces
- Edit the file to add desired namespaces. Each line is one namespace and has a following format: <alias>,<namespace-name>. Remember to use ',' character. You can add new namespaces any time you want.
-
Set up your kubeconfigs for different environments and clusters:
- Download kubeconfigs for every cluster you want.
- For every kubeconfig choose an alias for the config (e.g.
dev-1for kubeconfig for one of the dev clusters) and run following command (remember to replace placeholders). The command encrypts the configuration, moves it to the config directory created in the previous step and removes plain text configuration.$ gpg --output <repository directory>/config/<alias>.yaml -c --no-symkey-cache <path to downloaded file> && rm <path to downloaded file>
-
(Optional) Copy subcommands from examples and make them executable. Adjust the subcommand scripts to your environment. Replace all placeholders enclosed in angle brackets. Compatibility with other environments is not guaranteed - you can create your own subcommands according to your workflow.
$ for file in `find subcommands -type f -iname '*.example'`; do cp $file subcommands/`basename -s '.example' $file`; done $ for file in `find subcommands -type f -not -iname '*.example'`; do chmod +x $file; done
-
(Optional) Sometimes the terminal is closing so abruptly that currently used unencrypted kubeconfig is not deleted. To make sure that unecrypted kubeconfigs are deleted, set up an additional cleanup command for unused kubeconfig files.:
- If
crontabis available in your OS, execute following command (inswitch-kubernetes-contextrepository). This command adds a job that removes unencrypted kubeconfigs older than 3 hours and runs every 5 minutes.Alternatively, add following line to$ (crontab -l;echo "*/5 * * * * find \"`pwd`/config\" -type f -name 'current*.yaml' -mmin +180 -delete") | crontab -
crontabmanually (replace<skc_repo>with correct path).*/5 * * * * find <skc_repo>/config -type f -name 'current*.yaml' -mmin +180 -delete
- If crontab is not available (e.g. in Windows git-bash), add following line to your shell configuration file, e.g.
.bashrcor.zshrc(replace<skc_repo>with correct path). This will remove unused kubeconfig files at the startup of the shell/terminal.Alternatively, use one of the following commands (in# SKC cleanup find <skc_repo>/config -type f -name 'current*.yaml' -mmin +180 -delete;
switch-kubernetes-contextrepository) that we'll do this step for you.$ echo "find \"`pwd`/config\" -type f -name 'current*.yaml' -mmin +180 -delete" >> .bashrc $ echo "find \"`pwd`/config\" -type f -name 'current*.yaml' -mmin +180 -delete" >> .zshrc
- If
-
(Optional) Set up autocompletion:
- Create autocompletion script out of the template.
$ sed "s|<skc_dir>|`pwd`|g" completions/bash_skc_completion_template > completions/skc_completion.bash
- Add following line to the
.bashrcto execute the script at the start of the shell session.
source <skc_repo>/completions/skc_completion.bash
- (For zsh users) It's often necessary to add these lines. It depends on your configuration.
autoload -Uz compinit && compinit autoload -U +X bashcompinit && bashcompinit setopt completealiases