Skip to content

A simple command-line tool for managing Kubernetes contexts with a clean table interface

License

Notifications You must be signed in to change notification settings

hlebkanonik/k8s-context-switcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kubernetes Context Switcher (cs)

Presentation

A simple command-line tool for managing Kubernetes contexts with a clean table interface. Perfect for servers and bastion hosts where you need to manage many contexts - no need to remember complex context names, just use numbers to switch between them quickly.

Features

  • List all available Kubernetes contexts in a formatted table
  • Switch between contexts by number
  • Check cluster reachability status
  • Clean, intuitive interface with emojis
  • Perfect for servers and bastion hosts with many contexts
  • Number-based switching - no need to remember complex context names

Installation

Download from GitHub Releases (Recommended)

  1. Go to the Releases page
  2. Download the appropriate binary for your platform:
    • cs-linux-amd64 - Linux x86_64
    • cs-linux-arm64 - Linux ARM64
    • cs-darwin-amd64 - macOS Intel
    • cs-darwin-arm64 - macOS Apple Silicon
    • cs-windows-amd64.exe - Windows x86_64
    • cs-windows-arm64.exe - Windows ARM64
  3. Make the binary executable and move it to your PATH:
# For Linux/macOS
chmod +x cs-linux-amd64
sudo mv cs-linux-amd64 /usr/local/bin/cs

# For Windows
# Move cs-windows-amd64.exe to a directory in your PATH

Build from source

# Clone the repository
git clone https://github.com/hlebkanonik/k8s-context-switcher.git
cd k8s-context-switcher

# Build the binary
make build

# Install to /usr/local/bin
make install

Manual installation

# Build
go build -o cs .

# Install (requires sudo)
sudo cp cs /usr/local/bin/

Usage

List all contexts

cs get

This will display a table with the following columns:

  • #: Context number (1, 2, 3, etc.)
  • NAME: Context name (dynamically sized to fit the longest name)
  • CLUSTER: Cluster name (dynamically sized)
  • AUTHINFO: Authentication info name (dynamically sized)
  • STATUS: Reachability status (✅ for reachable, ❌ for unreachable)

The currently active context is highlighted with a green background.

Dynamic Sizing: The table automatically adjusts column widths based on:

  • The longest content in each column
  • Your terminal window width
  • Optimal space distribution for readability

Perfect for Servers & Bastions: When working on servers or bastion hosts with many Kubernetes contexts, you don't need to remember complex context names like arn:aws:eks:eu-central-1:123456789:cluster/production-cluster. Just use cs 1, cs 2, etc. to switch between them quickly!

Switch context

cs 1    # Switch to context #1
cs 2    # Switch to context #2
cs 3    # Switch to context #3

Show help

cs help

Example Output

┌────┬──────────────┬──────────────────────┬──────────────────────┬──────────┐
│ #  │ NAME         │ CLUSTER              │ AUTHINFO             │ STATUS   │
├────┼──────────────┼──────────────────────┼──────────────────────┼──────────┤
│ 1  │ minikube     │ minikube             │ minikube             │ ✅        │
│ 2  │ docker-desk  │ docker-desktop       │ docker-desktop       │ ❌        │
│ 3  │ production   │ prod-cluster         │ prod-user            │ ✅        │
└────┴──────────────┴──────────────────────┴──────────────────────┴──────────┘

Note: The currently active context is highlighted with a green background.

Requirements

  • Go 1.21 or later
  • Valid kubeconfig file (usually at ~/.kube/config)
  • Access to Kubernetes clusters for reachability testing

Configuration

The tool reads from the standard kubeconfig location:

  • ~/.kube/config (default)
  • Or the path specified in the KUBECONFIG environment variable

Building

# Build the binary
make build

# Run tests
make test

# Clean build artifacts
make clean

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the Apache License, Version 2.0 - see the LICENSE file for details.

About

A simple command-line tool for managing Kubernetes contexts with a clean table interface

Resources

License

Stars

Watchers

Forks

Packages

No packages published