Kubeconfig SWitcher
ksw is a fast and lightweight Go CLI tool to switch Kubernetes contexts by starting a new shell with an isolated kubeconfig file. This allows different terminals to use different contexts concurrently without interfering with each other.
I wanted a kubeconfig switcher that is simple (as in Unix philosophy) and integrates easily with my existing ZSH and Prezto setup without getting in the way. It also needs to work with other Kubernetes tools without many changes.
Other solutions I tried:
- kubectx / kubens: Changing the context in one terminal changes it everywhere.
- kubie: It does too much and broke my ZSH setup.
- kube_ps1: I still use it to show the current context alongside
ksw.
- Isolated contexts per terminal: Work with different Kubernetes contexts across multiple terminals simultaneously.
- No nested shells: Switching contexts in an active session updates the config file in-place instead of spawning new shells.
- Fuzzy finder: Shows a fuzzy finder to select a context if no exact match is specified.
- Optional minification: Can strip unused clusters, contexts, and users from the temporary kubeconfig.
brew install chickenzord/tap/kswAlternatively, install from source:
go install github.com/chickenzord/kswksw loads configuration from ~/.config/ksw/config.yaml or ~/.ksw.yaml (fallback).
kubeconfig:
# When true, extracts only the cluster, user, and context needed for the active context.
# Defaults to false, which preserves other contexts but updates current-context.
minify: false
# Directory to store generated session kubeconfigs.
# Defaults to ~/.ksw/sessions.
sessions_dir: ~/.ksw/sessionsksw [context-name]- 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
- Evaluates configuration options. If
minifyis enabled, extracts only the cluster, user, and context for the specified context. Otherwise, copies the config and updates thecurrent-context. - Writes the isolated config to a session file under
~/.ksw/sessions(named<context>.<pid>.yaml). - Replaces the
kswprocess with your shell usingsyscall.Exec(), settingKUBECONFIGto the session file. - Your shell now uses the isolated context.
- Running
ksw [another-context]detects you are already in a session. - Updates the existing session kubeconfig file in-place.
- Returns immediately. Kubectl sees the new context right away without spawning new shells.
KSW_KUBECONFIG_ORIGINAL: Path to your original kubeconfig fileKSW_KUBECONFIG: Path to the isolated session kubeconfigKUBECONFIG: Same as KSW_KUBECONFIGKSW_ACTIVE: Always set to "true" when in a ksw sessionKSW_SHELL: Path to your shell (e.g./bin/zsh)
- No automatic prompt indicator. Use the environment variables (
KSW_ACTIVE,KSW_KUBECONFIG_ORIGINAL) in your prompt setup. - Primarily tested on ZSH on Darwin Arm64.