Skip to content

pmaddev/kubeprompt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ kubeprompt - Kubernetes Context & Namespace Aware Shell Prompt

kubeprompt is a Bash script that dynamically updates your shell prompt to display the current Kubernetes cluster and namespace.
It enhances usability by providing quick commands to switch contexts, namespaces, and clusters efficiently.


πŸ“Œ Features

βœ… Dynamic Kubernetes-aware prompt displaying the active cluster & namespace
βœ… Quick context switching with kc and namespace switching with kn
βœ… Aliases for frequently used kubectl commands (k, kgp, kgd, etc.)
βœ… Retains last used namespace per cluster instead of resetting to default
βœ… **Autocompletion support for kubectl, kubectx, and kubens

πŸ“₯ Prerequisites

Note: As its context based which is not seamlessly sync across the terminals

To enable Bash completion for kubectl and aliases, run:

sudo yum install -y bash-completion # For redhat or centos
exec bash    # Restart shell for changes to take effect
type _get_comp_words_by_ref  # Check if Bash completion is working
echo 'source <(kubectl completion bash)' >> ~/.bashrc
source ~/.bashrc  # Reload Bash settings

Enable Shortcuts for kubectl

alias k='kubectl'  # Set 'k' as a shortcut for kubectl
complete -F __start_kubectl k  # Enable autocompletion for alias 'k'
echo 'complete -F __start_kubectl k' >> ~/.bashrc  # Persist autocompletion

Install kubectx and kubens for Context/Namespace Switching

sudo git clone https://github.com/ahmetb/kubectx /opt/kubectx
sudo ln -s /opt/kubectx/kubectx /usr/local/bin/kubectx
sudo ln -s /opt/kubectx/kubens /usr/local/bin/kubens
echo "source /opt/kubectx/completion/kubectx.bash" >> ~/.bashrc
echo "source /opt/kubectx/completion/kubens.bash" >> ~/.bashrc
source ~/.bashrc

πŸ“₯ Installation

1️⃣ Clone the Repository

git clone https://github.com/yourusername/kubeprompt.git
cd kubeprompt

πŸš€ kubeprompt

A shell prompt extension for Kubernetes users that displays the current Kubernetes cluster and namespace in your terminal prompt.

πŸ“Œ Installation

Add to Your .bashrc or .bash_profile

For Linux (.bashrc):

echo "source ~/kubeprompt/kubeprompt.sh" >> ~/.bashrc
source ~/.bashrc

For macOS (.bash_profile):

echo "source ~/kubeprompt/kubeprompt.sh" >> ~/.bash_profile
source ~/.bash_profile

πŸ–₯️ Example Prompt

After installing, your shell prompt will look like this:

user@machine [~/work] [k-1] | kube-system $
  • [k-1] β†’ Shows the current Kubernetes cluster (config1, config2, etc.).
  • kube-system β†’ Displays the active namespace.

⚑ Usage

1️⃣ Switch Clusters

Switch between Kubernetes clusters using:

k1    # Switch to cluster 1
k2    # Switch to cluster 2
k3    # Switch to cluster 3
k4    # Switch to cluster 4

The prompt updates immediately with the new cluster and retains the last used namespace.

Example Scenario:

user@machine [~/work] [k-1] | default $
$ k2
Switched to Kubernetes config: ~/.kube/config2 with namespace: default
user@machine [~/work] [k-2] | default $

2️⃣ Switch Kubernetes Context

kc my-cluster

Switches to the given Kubernetes context while preserving the current namespace.

Example Scenario:

user@machine [~/work] [k-1] | default $
$ kc my-new-cluster
Switched to Kubernetes context: my-new-cluster
user@machine [~/work] [k-3] | default $

3️⃣ Change Kubernetes Namespace

kn kube-system

Changes the current namespace to kube-system.

Example Scenario:

user@machine [~/work] [k-1] | default $
$ kn kube-system
Switched to namespace: kube-system
user@machine [~/work] [k-1] | kube-system $

4️⃣ List Available Kubernetes Configs

klist

Shows available kubeconfigs stored in ~/.kube/.

Example Output:

config1
config2
config3
config4

5️⃣ Check Active Kubeconfig

kcur

Displays the currently used Kubernetes configuration.

Example Output:

Current KUBECONFIG: ~/.kube/config2

6️⃣ Reset or Clear Kubeconfig

kclear

Unsets the current KUBECONFIG variable.

πŸ”§ Built-in Aliases

Command Description
k Alias for kubectl
kg Alias for kubectl get
kgp Get all pods (kubectl get pods)
kgd Get all deployments (kubectl get deployments)
kgn Get all nodes (kubectl get nodes)
kgs Get all services (kubectl get services)
kga Get all Kubernetes objects (kubectl get all)

🎯 How It Works

βœ… Automatically Updates Prompt

  • Displays current Kubernetes cluster and namespace.
  • Uses short names [k-1], [k-2] instead of long kubernetes-admin@cluster-name.

βœ… Preserves the Last Used Namespace

  • If you switch from k1 to k2, the namespace remains the same.

βœ… Seamless Integration with kubectx and kubens

  • Uses kubectx (kc) for context switching.
  • Uses kubens (kn) for namespace switching.

βœ… Full Autocompletion Support

  • Works with kubectl, kubectx, and kubens.

πŸ“Œ Requirements

Make sure the following tools are installed:

Install kubectl

sudo apt install -y kubectl  # Debian/Ubuntu
brew install kubectl         # macOS

Install kubectx & kubens (For context & namespace switching)

sudo apt install kubectx    # Debian/Ubuntu
brew install kubectx        # macOS

πŸ› Troubleshooting

πŸ”Ή Issue: Namespace not updating in the prompt

Try running:

source ~/.bashrc
kn kube-system

If the issue persists, restart your terminal.

πŸ’‘ Contributions

Feel free to fork and submit a Pull Request! πŸš€
If you have ideas for improvements, create an Issue in the repository.

πŸ“œ License

This project is licensed under the MIT License.

πŸš€ Start Using kubeprompt Today!

Now, you can manage Kubernetes clusters and namespaces efficiently with a single glance at your terminal!
πŸ”₯ Star the repository on GitHub and spread the word! πŸ”₯

πŸ“Œ GitHub Repository: https://github.com/pmaddev/kubeprompt

About

kubeprompt - Kubernetes Context & Namespace Aware Shell Prompt

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages