kubectx and kubens

repository·master·Indexed 12 days ago

https://github.com/ahmetb/kubectx

Power tools for kubectl that allow users to switch between Kubernetes clusters (contexts) and namespaces quickly and easily. Features include interactive selection via fzf, context renaming, deleting contexts, and launching isolated shells scoped to specific contexts.

Tokens
6.7K
Snippets
35
Records
38
Agent score
94%

What's inside kubectx

  1. Enable interactive mode with fzf

    master

    If you have fzf installed in your $PATH, kubectx and kubens will automatically present an interactive fuzzy-search menu for selecting contexts or namespaces.

    Configuration Options:

    • Disable interactive mode: Set the environment variable KUBECTX_IGNORE_FZF=1.
    • Bypass interactive mode for piping: Pipe the output to another command (e.g., kubectx | cat) to use the default non-interactive behavior.
    export KUBECTX_IGNORE_FZF=1
  2. Configure shell completion for kubectx and kubens

    master

    Both tools support tab completion for bash, zsh, and fish.

    zsh (with antibody)

    Add this to your ~/.zsh_plugins.txt:

    ahmetb/kubectx path:completion kind:fpath

    zsh (plain)

    Link the completion scripts to a directory in your $fpath. For oh-my-zsh users:

    mkdir -p ~/.oh-my-zsh/custom/completions
    chmod -R 755 ~/.oh-my-zsh/custom/completions
    ln -s /opt/kubectx/completion/_kubectx.zsh ~/.oh-my-zsh/custom/completions/_kubectx.zsh
    ln -s /opt/kubectx/completion/_kubens.zsh ~/.oh-my-zsh/custom/completions/_kubens.zsh
    echo "fpath=($ZSH/custom/completions $fpath)" >> ~/.zshrc

    bash

    git clone https://github.com/ahmetb/kubectx.git ~/.kubectx
    COMPDIR=$(pkg-config --variable=completionsdir bash-completion)
    ln -sf ~/.kubectx/completion/kubens.bash $COMPDIR/kubens
    ln -sf ~/.kubectx/completion/kubectx.bash $COMPDIR/kubectx
    cat << EOF >> ~/.bashrc
    #kubectx and kubens
    export PATH=~/.kubectx:\$PATH
    EOF

    fish

    mkdir -p ~/.config/fish/completions
    ln -s /opt/kubectx/completion/kubectx.fish ~/.config/fish/completions/
    ln -s /opt/kubectx/completion/kubens.fish ~/.config/fish/completions/
    ln -s /opt/kubectx/completion/kubectx.fish ~/.config/fish/completions/
  3. Install kubectx and kubens

    master

    You can install kubectx and kubens using various package managers depending on your operating system:

    • macOS: brew install kubectx or sudo port install kubectx (MacPorts)
    • Linux: sudo apt install kubectx (Debian/Ubuntu) or sudo pacman -S kubectx (Arch Linux)
    • Windows: choco install kubens kubectx (Chocolatey), scoop install main/kubens main/kubectx (Scoop), or winget install --id ahmetb.kubectx && winget install --id ahmetb.kubens (winget)
    • kubectl plugin: kubectl krew install ctx && kubectl krew install ns (Krew)

    Alternatively, download the binaries from the GitHub Releases page and add them to your PATH.

    brew install kubectx
  4. Use kubectx to manage kubectl contexts

    master

    The kubectx utility is a command-line tool designed to manage and switch between different Kubernetes contexts. It simplifies the process of changing the current context used by kubectl.

    When running the tool, it checks for deprecated environment variables and warns the user if any are found. If an error occurs during execution, the tool will exit with a non-zero status code. If the DEBUG environment variable is set, a detailed stack trace will be printed to stderr to assist with troubleshooting.

    # Note: This is the entrypoint for the CLI. Usage depends on the parsed arguments (e.g., switching contexts).
    # Example of enabling debug mode:
    export DEBUG=1
    kubectx my-context
  5. Where kubens stores namespace state files

    master

    The kubens tool persists the current namespace for each Kubernetes context in a state file. These files are stored in a directory determined by the system's cache directory, specifically under a kubens subdirectory.

    On Windows, context names containing colons (:) are automatically sanitized by replacing them with double underscores (__) to ensure they are valid filenames.

    Default directory: [CacheDir]/kubens
  6. Use interactive mode with fzf

    master

    If fzf is installed on your system, kubectx can use it to provide an interactive interface for selecting Kubernetes contexts. This allows you to search through your available contexts using fuzzy finding instead of typing the name manually.

    When using interactive mode:

    • It uses fzf --ansi --no-preview to render the list.
    • It forces color output to ensure the list is readable within the fzf interface.
    • You can use it to either switch to a context or delete a context.
    # No specific CLI flag is shown in this file, but the presence of fzf on the PATH enables interactive selection during standard kubectx operations.
  7. Configure output colors

    master

    You can customize the foreground and background colors used to indicate the current namespace or context using environment variables. Use tput color codes for values.

    • KUBECTX_CURRENT_FGCOLOR: Sets the foreground color.
    • KUBECTX_CURRENT_BGCOLOR: Sets the background color.
    • NO_COLOR: Set this environment variable to disable all colors in the output.
    export KUBECTX_CURRENT_FGCOLOR=$(tput setaf 6)
    export KUBECTX_CURRENT_BGCOLOR=$(tput setab 7)
  8. Launch an isolated shell for a specific context

    master

    The kubectx tool provides a mechanism to launch a sub-shell where the kubectl context is scoped only to a specific context. This prevents accidental changes to your global kubeconfig while working in a specific environment.

    When you enter an isolated shell, a badge [ISOLATED SHELL] is displayed. When you exit the shell (e.g., by typing exit), a badge [ISOLATED SHELL EXITED] is displayed, and your original context is restored in the parent shell.

    To use this feature, you must have kubectl installed and available in your PATH, or explicitly set the KUBECTL environment variable.

    # The tool uses an internal mechanism to spawn a shell with a minimized kubeconfig.
    # Users typically invoke this via the CLI (e.g., kubectx --shell or similar command structure).
    
    # Example behavior:
    [ISOLATED SHELL] kubectl context is my-cluster in this shell — type 'exit' to leave.
    $ kubectl get pods
    
    $ exit
    [ISOLATED SHELL EXITED] kubectl context is now original-context.
  9. Switch Kubernetes namespaces interactively using fzf

    master

    If you have fzf installed, kubens can use it to provide an interactive fuzzy-finding interface for selecting a namespace. This mode reads your current kubeconfig, lists available namespaces, and pipes them into fzf. Once you select a namespace from the list, kubens will switch your current context to that namespace.

    Requirements:

    • fzf must be installed and available in your PATH.
    • A valid kubeconfig file must be present.

    Behavior:

    • It uses fzf --ansi --no-preview to display the list.
    • If no kubeconfig is found, it displays a warning: kubeconfig file not found.
    • If no namespaces are found in the configuration, it returns an error: no namespaces found.
    • If you exit fzf without selecting an option, it returns: you did not choose any of the options.
    # No specific command is provided in this source file as it is an internal implementation of the kubens interactive mode, but it is triggered by running the standard `kubens` command when fzf is present.
  10. Configure the kubectl binary path

    master

    If kubectl is not in your system PATH, you can specify the location of the kubectl binary by setting the KUBECTL environment variable. The tool will use this path to generate the minimized kubeconfig required for the isolated shell.

    export KUBECTL=/path/to/your/kubectl
  11. Enable debug mode in kubens

    master

    You can enable verbose error reporting and stack traces by setting the KUBECTX_DEBUG environment variable. When this variable is present, kubens will print detailed debug information to stderr upon encountering an error.

    export KUBECTX_DEBUG=1
    kubens <namespace>
  12. Switch contexts interactively

    master

    When fzf is available, kubectx can trigger an interactive switch operation. The tool loads your kubeconfig, formats the list of available context names, and pipes them into fzf. Once you select a context from the list, kubectx performs the switch.

    Requirements:

    • fzf must be installed and available in your PATH.
    • A valid kubeconfig file must be present.
    # Assuming kubectx is configured to use fzf:
    $ kubectx
    # An interactive fzf menu appears. Select a context and press Enter.