README
¶
ksw
Kubeconfig SWitcher
Installation
brew install chickenzord/tap/ksw
Alternatively you can also install from the source by running go install github.com/chickenzord/ksw (requires Go build tools)
How it works
ksw context-name
First time (not in a ksw session):
- Loads kubeconfig from these locations (in order):
- Path set in
KSW_KUBECONFIG_ORIGINAL - Path set in
KUBECONFIG - Default location
$HOME/.kube/config
- Path set in
- Minifies the config to only include the cluster, user, and context for the specified context
- Writes the minified config to a temporary file
- Replaces the ksw process with your shell using
exec, settingKUBECONFIGto the temp file - Your shell now uses the isolated context - kubectl commands work immediately
When already in a ksw session:
- Running
ksw another-contextdetects you're already in a session - Updates the existing temp kubeconfig file with the new context
- Returns immediately - kubectl sees the new context right away
- No nested shells, no process spawning
Environment variables available in the shell:
KSW_KUBECONFIG_ORIGINAL: Path to your original kubeconfig fileKSW_KUBECONFIG: Path to the temp minified kubeconfigKUBECONFIG: Same as KSW_KUBECONFIG (standard kubectl environment variable)KSW_ACTIVE: Always set to "true" when in a ksw sessionKSW_SHELL: Path to your shell (e.g./bin/zsh)
Note: Starting from v0.5.0, ksw uses
syscall.Exec()to replace its process with your shell, eliminating the ksw process from the process tree. When switching contexts within a ksw session, the kubeconfig file is updated in-place without spawning new shells, avoiding any nesting issues.
Wait, why am I creating this?
I want a kubeconfig switcher that simple (as in Unix philosophy) and can integrate easily with my existing ZSH and Prezto setup without getting in the way. Must also be able to integrate with other kubernetes tools without much changes.
Other solutions I have tried:
- kubectx and kubens: They are good, but I switch and use multiple contexts concurrently a lot. Changing context in one terminal will change other terminals as well because they are sharing the same kubeconfig file.
- kubie: Took a lot of inspirations from this project. But somehow it's doing too much and messed with ZDOTDIR breaking my ZSH setup.
- kube_ps1: Still using this for showing current context, and it integrates well with ksw
Features
- Isolated contexts per terminal: Work with different Kubernetes contexts across multiple terminals simultaneously without conflicts
- No nested shells: Switching contexts in a ksw session updates the config in-place
- Process efficiency: Uses
execto replace ksw process with your shell (v0.5.0+) - Fuzzy finder: Built-in fuzzy finder (like fzf) when no context specified
- Minified configs: Each session uses a minified kubeconfig with only the necessary context
- Simple integration: Works with any kubectl-compatible tool without configuration
Limitations
- No automatic prompt indicator - use the environment variables (
KSW_ACTIVE,KSW_KUBECONFIG_ORIGINAL) in your prompt setup - Temp kubeconfig files rely on OS cleanup (typically automatic in
/tmp) - Primarily tested on ZSH on Darwin Arm64
Documentation
¶
There is no documentation for this package.