khop 0.1.0

Fast Kubernetes context and namespace switcher
Documentation

kubehop

CI Crates.io License

Fast Kubernetes context and namespace switcher, written in Rust. Built-in fuzzy finder, sub-50ms startup, no external dependencies at runtime.

Context switching

kubectx demo

Namespace switching

kubens demo

Features

  • Built-in fuzzy picker powered by nucleo (from the Helix editor) and ratatui -- no fzf required
  • Single kubeconfig parse per operation, held in memory for the entire flow
  • Selective parsing -- reads only current-context and context names; skips clusters, users, and certificates entirely
  • Multi-file KUBECONFIG support -- when names overlap, the first file wins
  • Sub-50ms startup on standard kubeconfig files
  • k9s plugin support via a pick subcommand
  • Shell completions for bash, zsh, and fish
  • Optional fzf fallback via --fzf flag

Install

From source

cargo install --git https://github.com/ikchifo/kubehop

This installs a binary called khop:

khop              # context switching mode (default)
khop -c           # show current context

Optional: kubectx/kubens symlinks

To use the kubectx/kubens command names, or for compatibility with tools that expect them, create symlinks:

ln -s ~/.cargo/bin/khop ~/.cargo/bin/kubectx
ln -s ~/.cargo/bin/khop ~/.cargo/bin/kubens

The tool detects its mode from the command name you use:

Invoked as Mode
khop, kubectx, kubectl-ctx Context switching
kubens, kubectl-ns Namespace switching

Usage

Context switching

khop                      List contexts (interactive if TTY)
khop <name>               Switch to context
khop -                    Switch to previous context
khop <new>=<old>          Rename context
khop -c, --current        Show current context name
khop -d, --delete <name>  Delete context
khop -u, --unset          Unset current context
khop --fzf                Use external fzf for selection
khop --completion <shell> Output shell completion (bash/zsh/fish)

Namespace switching

When invoked as kubens or kubectl-ns (via symlink):

kubens                    List namespaces (interactive if TTY)
kubens <name>             Switch namespace
kubens -                  Switch to previous namespace
kubens <name> -f          Switch without existence check
kubens -c, --current      Show current namespace
kubens -u, --unset        Reset namespace to "default"
kubens --fzf              Use external fzf for selection

Shell completions

# bash
khop --completion bash > ~/.local/share/bash-completion/completions/khop

# zsh
khop --completion zsh > ~/.zfunc/_khop

# fish
khop --completion fish > ~/.config/fish/completions/khop.fish

The completion/ directory has pre-generated scripts.

k9s plugin

kubehop works as a k9s plugin for context switching. Add this to your k9s plugins.yaml:

plugins:
  kubehop:
    shortCut: Ctrl-K
    description: Switch context
    scopes:
      - all
    command: khop
    args:
      - pick
      - --switch

Architecture

src/
  main.rs           Entry point (~10 lines)
  lib.rs            Crate root, module declarations
  cli.rs            Arg parsing and command dispatch
  dispatch.rs       Argv0-based mode detection
  completion.rs     Shell completion generation
  kubeconfig/       Selective serde parser, multi-file merge
  context/          List, switch, rename, delete, unset, state
  namespace/        List, switch, unset, per-context state
  picker/           Fuzzy scoring (nucleo) + inline TUI (ratatui)
  integration/      k9s plugin subcommand

Design decisions:

  • Selective serde for reads, full serde_yaml::Value round-trip for writes (so edits never drop unknown fields)
  • No async runtime -- all operations are local file I/O on small files
  • thiserror in library modules, anyhow in the CLI layer
  • State file compatibility -- stores previous-context state at the same path as kubectx, so khop - works for users migrating from kubectx

License

Apache-2.0