Skip to content

michizubi-SRF/kns

Repository files navigation

kns - Kubernetes Namespace Switcher

A simple and fast CLI tool for switching between Kubernetes namespaces with interactive filtering.

Features

  • 🚀 Interactive namespace selection with fuzzy search
  • ⌨️ Direct namespace switching via command-line argument
  • 🔍 Real-time filtering as you type
  • ✅ Shows current namespace
  • 📝 Updates your kubeconfig automatically
  • ⚡ Tab completion for namespaces (bash, zsh, fish)

Installation

Build from source:

git clone git@github.com:michizubi-SRF/kns.git
cd kns
go build -o kns
sudo mv kns /usr/local/bin/

Tab Completion Setup

Enable tab completion for namespace names by sourcing the appropriate completion script for your shell.

Zsh

Quick test in current session:

# Ensure completion system is loaded (usually already done in .zshrc)
autoload -Uz compinit && compinit

# Load kns completion
source <(./kns completion zsh)

# Now try: ./kns [TAB]

Permanent setup - Add to your ~/.zshrc:

# Add this line to enable kns completion
source <(kns completion zsh)

Or place the completion file in your $fpath:

kns completion zsh > /usr/local/share/zsh/site-functions/_kns
# Then restart your shell or run: compinit

Bash

Add to your ~/.bashrc:

source <(kns completion bash)

Or save the completion script to a file:

kns completion bash > /usr/local/etc/bash_completion.d/kns

Fish

Save the completion file to Fish's completion directory:

kns completion fish > ~/.config/fish/completions/kns.fish

Testing Tab Completion

After setup, you can use tab completion:

kns <TAB>         # Shows all available namespaces
kns prod<TAB>     # Completes to 'production' if it exists

Usage

Interactive Mode (with filtering)

Simply run kns without arguments to get an interactive list:

kns
  • Use arrow keys to navigate
  • Start typing to filter namespaces
  • Press Enter to switch to the selected namespace
  • Press Ctrl+C to cancel

Direct Mode

Provide the namespace name as an argument:

kns production
kns dev
kns kube-system

Examples

# Interactive selection
$ kns
Use the arrow keys to navigate: ↓ ↑ → ← 
? Select Namespace (current: default):
  ▸ default
    kube-system
    kube-public
    production
    staging
    dev

# Direct switching
$ kns production
Switched to namespace 'production'

# Trying to switch to current namespace
$ kns production
Already in namespace 'production'

Requirements

  • Go 1.21 or later
  • Valid kubeconfig file
  • Access to a Kubernetes cluster

How It Works

kns reads your kubeconfig file, lists all available namespaces from your cluster, and updates the namespace for your current context. The change persists across kubectl commands.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors