Skip to content

A simple tool to help manage multiple kubeconfig files

License

Notifications You must be signed in to change notification settings

mirceanton/kubectl-switch

Repository files navigation

kubectl-switch

kubectl-switch is a command-line tool for managing and switching between multiple Kubernetes configuration files located in the same directory. It simplifies the process of selecting a Kubernetes context from multiple kubeconfig files and updating the active configuration or namespace.

Just dump all your kubeconfigs into a single dir and let kubectl-switch manage them for you!

Features

  • Multiple kubeconfig files: Manage multiple kubeconfig files with one or more contexts each, all present in a single directory. You don't have to generate a global kubeconfig to hold all of your contexts
  • Context Switching: Easily switch between different Kubernetes contexts from multiple config files.
  • Namespace Switching: Quickly change the current namespace in the active context.
  • Previous Config: Switch back to the previous configuration with kubectl switch -.
  • Non-Interactive Mode: Specify the desired context or namespace directly as an argument.
  • Interactive Mode: Run the program with no arguments to get an interactive list of available namespaces or contexts to choose from.
  • Persistent Configuration: Changes persist across different shell sessions.
  • Shell Completions: Provides tab completion for available contexts and namespaces.
  • Non-destructive: kubectl-switch will never edit those. It will only work with copies of them.

Why kubectl-switch?

kubectl-switch is an alternative to tools like kubectx, kubens and kubie. It has been created because I feel all of those fall short in certain regards:

  • kubectx and kubens assume all your contexts are defined in a single config file. Yes, there is some hackery you can do to your KUBECONFIG environment variable to make it work with multiple files, but it is (in my opinion) a sub-par workflow and I never really liked it
  • kubie spawns a new shell when you use it to change contexts, making it practically impossible to integrate into scripts or taskfile automation. Also I consider it to be too complicated of a solution for what is fundamentally a really simple problem

What I wanted was something very simple conceptually: I just want to dump all my kubeconfig files in a single directory and then have my tool parse them and "physically" move over the config file to .kube/config (or whatever is configured in my KUBECONFIG env var) such that it is also persistent between different shells. Here is where kubectl-switch comes in!

Installation

Download Precompiled Binaries

Precompiled binaries are available for various platforms. You can download the latest release from the GitHub Releases page.

  1. Download the appropriate binary for your system and extract the archive.

  2. Make the extracted binary executable:

    chmod +x kubectl-switch
  3. Move the binary to a directory in your PATH:

    mv kubectl-switch /usr/local/bin/kubectl-switch

Running via Docker

kubectl-switch is also available as a Docker container:

docker pull ghcr.io/mirceanton/kubectl-switch

Install via homebrew

  1. Add the tap

    brew tap mirceanton/taps
  2. Install kubectl-switch

    brew install kubectl-switch

Build from Source

  1. Clone the repository:

    git clone https://github.com/mirceanton/kubectl-switch
    cd kubectl-switch
  2. Build the tool:

    mise run build

Usage

Context Command

The context (or ctx) subcommand is used to switch between Kubernetes contexts (think of kubectx):

# Interactive mode - select context from a list
kubectl-switch context

# Switch to a specific context
kubectl-switch ctx my-context

Namespace Command

The namespace (or ns) subcommand is used to switch the current namespace (think of kubens):

# Interactive mode - select namespace from a list
kubectl-switch namespace

# Switch to a specific namespace
kubectl-switch ns kube-system

Quickly Switch to Previous Configuration

Switch back to the previous configuration:

kubectl-switch -

Usage with kubectl plugin

When installed as a kubectl plugin, you can use it directly with the kubectl command:

# These commands are equivalent
kubectl-switch ctx
kubectl switch ctx

Configuration

You can configure kubectl-switch using environment variables or CLI flags:

Environment Variable CLI Flag Description Default Value
KUBECONFIG_DIR --kubeconfig-dir Directory containing your kubeconfig files. None
KUBECONFIG N/A Path where the active kubeconfig will be stored. ~/.kube/config

Shell Completion

The completion subcommand generates shell completion scripts:

# Generate completions for bash
kubectl-switch completion bash > /etc/bash_completion.d/kubectl-switch

# Generate completions for zsh
kubectl-switch completion zsh > ~/.zsh/completion/_kubectl-switch

# Generate completions for fish
kubectl-switch completion fish > ~/.config/fish/completions/kubectl-switch.fish

# Generate completions for powershell
kubectl-switch completion powershell > ~/kubectl-switch.ps1

Contributing

Contributions are welcome! Please fork the repository, make your changes, and submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.