A simple CLI tool for managing Kubernetes contexts with ease.
Kontext helps you manage your Kubernetes contexts efficiently with a simple command-line interface. It allows you to:
- List all available Kubernetes contexts
- Show your current active context
- Switch between contexts with tab completion
- Interactively select contexts from a menu
- View and change namespaces within contexts
go install github.com/user-cube/kontext@latestkontext listThis will display all contexts from your kubeconfig with the current context highlighted.
kontext currentkontext switch <context-name>With tab completion for context names!
Simply run:
kontext switchWithout any arguments to get an interactive selection menu of all available contexts.
You can also run just:
kontextTo access the same interactive context selection.
View or change the current namespace:
kontext namespaceor use the shorter alias:
kontext nsShow the current namespace without the selector:
kontext ns --show
# or with short flag
kontext ns -sSwitch to a specific namespace without using the interactive selector:
kontext ns my-namespaceSwitch context and then set namespace in one command:
# Interactive context selection, then namespace selection
kontext switch -n
# or with the root command
kontext -n
# Select specific context, then namespace selection
kontext switch my-context -n
# or with the root command
kontext my-context -n
# Select specific context and directly set namespace
kontext switch my-context -n my-namespace
# or with the root command
kontext my-context -n my-namespace- Smart Context Sorting: Current context is prioritized in selection lists
- Smart Namespace Sorting: Current namespace is prioritized in selection lists
- Non-Existent Namespace Handling: Warns when non-existent namespaces are specified
- Detailed Information: Clear success/error messages with color-coded output
- Tab Completion: Supports bash/zsh completions for context and namespace names
- Intuitive UI: Interactive selectors with highlighted current selections
- Offline Mode Support: Fallback behavior when clusters are unavailable
Here are some common workflows:
# List all contexts
kontext list
# Show current context
kontext current
# Show current namespace
kontext ns -s# Switch to a context interactively
kontext switch
# or simply
kontext
# Switch to specific context
kontext switch my-dev-cluster
# Switch to production context (with tab completion)
kontext switch prod<TAB># View current namespace
kontext ns -s
# Change namespace interactively
kontext ns
# Change directly to a specific namespace
kontext ns kube-system
# Multi-step workflow
kontext switch my-dev-cluster # Switch context first
kontext ns my-app # Then switch namespace
# Combined workflow
kontext switch my-prod-cluster -n # Switch context and then select namespaceFor the most common case (switching contexts):
# Just type the main command for interactive selection
kontext
# Use the -n flag to also select namespace after switching context
kontext -n
# Directly switch to a context
kontext my-context
# Directly switch to a context and then select namespace
kontext my-context -nTo enable shell completion:
echo 'source <(kontext completion bash)' >> ~/.bashrcecho 'source <(kontext completion zsh)' >> ~/.zshrckontext completion fish > ~/.config/fish/completions/kontext.fishThe project is organized into the following packages:
-
cmd/ - Command implementations using Cobra
current.go- Show current contextlist.go- List available contextsnamespace.go- Namespace managementroot.go- Root command setupswitch.go- Context switchingversion.go- Version info
-
pkg/ - Reusable packages
- kubeconfig/ - Kubernetes configuration handling
kubeconfig.go- Functions for working with kubeconfig files
- ui/ - User interface components
ui.go- Shared UI formatting and interactive components
- kubeconfig/ - Kubernetes configuration handling
This clean separation ensures:
- UI code is centralized in the
uipackage - Kubernetes logic is contained in the
kubeconfigpackage - Command files only handle CLI argument parsing and orchestration
Contributions are welcome! Please feel free to submit a Pull Request.
