Skip to content

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ktx

Fast Kubernetes context & namespace switcher — a delightful TUI plus scriptable subcommands, in a single Go binary.

CI Go Reference Go Report Card Release License

ktx makes hopping between clusters and namespaces instant. Launch it with no arguments for a fuzzy-filterable picker, or use the subcommands in scripts and prompts. It only edits your kubeconfig file — it never dials a cluster, so it is fast and safe to run anywhere.


Demo

Running ktx opens the interactive picker. Type to fuzzy-filter, use the arrow keys to move, and press Enter to switch. Your current context is marked with a green dot.

  ktx · switch kubecontext

▸ dev ●
  cluster: dev-cluster · ns: default · user: dev-user

  prod
  cluster: prod-cluster · ns: payments · user: prod-user

  staging
  cluster: dev-cluster · ns: web · user: dev-user

  enter: switch · /: filter · q: quit

Prefer one-liners? Everything the TUI does is available non-interactively:

$ ktx list
CURRENT  NAME     CLUSTER       NAMESPACE  USER
*        dev      dev-cluster   default    dev-user
         prod     prod-cluster  payments   prod-user
         staging  dev-cluster   web        dev-user

$ ktx use prod
Switched to context "prod"

$ ktx ns billing
Set namespace "billing" on context "prod"

$ ktx current
prod (namespace: billing)

Install

Homebrew (via tap)

brew install HariBoddapati01/tap/ktx

Go

go install github.com/HariBoddapati01/ktx@latest

Prebuilt binaries

Grab an archive for your OS/arch from the releases page, extract it, and put ktx on your PATH.

From source

git clone https://github.com/HariBoddapati01/ktx.git
cd ktx
make install

Usage

ktx                       Launch the interactive context picker
ktx list [--quiet]        List all contexts (--quiet prints names only)
ktx use <context>         Switch the current context
ktx ns <namespace>        Set the namespace on the current context
    ktx ns <ns> -c <ctx>  ...or on a specific context
ktx current [--namespace] Show the current context (and namespace)
ktx --version             Print the version

Kubeconfig resolution

ktx follows the standard client-go rules:

  1. the $KUBECONFIG environment variable (a :-separated list is honoured), then
  2. ~/.kube/config.

Writes go back to the file that owns the setting via clientcmd.ModifyConfig, so merged multi-file kubeconfigs are respected and untouched files are left alone.

Handy shell aliases

alias kx='ktx use'
alias kn='ktx ns'
# Show the active context/namespace in your prompt:
kctx() { ktx current 2>/dev/null; }

Why ktx?

  • No cluster calls. Pure kubeconfig manipulation means it works offline and can't hang on an unreachable API server.
  • Scriptable + interactive. The same logic backs both the TUI and the CLI.
  • Small and dependency-light at runtime. A single static binary.
  • Tested where it counts. The kubeconfig logic lives in internal/kube with pure functions and thorough unit tests against in-memory configs.

Development

make            # tidy, fmt, vet, lint, test, build
make test       # go test ./... -race -coverprofile=cover.out
make lint       # golangci-lint
make build      # build ./bin/ktx with version stamped in
make snapshot   # local goreleaser snapshot (no publish)

Project layout

.
├── main.go              # entrypoint; version injected via -ldflags
└── internal
    ├── cli              # cobra command tree
    ├── kube             # pure kubeconfig logic + loader (unit tested)
    └── tui              # Bubble Tea context picker

The interesting, well-tested code is in internal/kube: ListContexts, SwitchContext, and SetNamespace operate on an in-memory *clientcmdapi.Config, which keeps them trivial to test without touching the filesystem or a cluster.


Built with

License

Apache-2.0 © Hari Boddapati

About

Fast Kubernetes context & namespace switcher — a delightful TUI plus scriptable subcommands, in a single Go binary.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages