Skip to content

Customizable PS1 utility for Kubernetes current context

License

Notifications You must be signed in to change notification settings

NeodymiumFerBore/kube-prompt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

kube-prompt

Simple and customizable PS1 utility to show Kubernetes current context information.

Requires: kubectl command available in PATH

Installation

curl -LO https://raw.githubusercontent.com/NeodymiumFerBore/kube-prompt/main/kube-prompt.sh

Usage

Before your PS1 definition, source kube-prompt.sh, then use __kube_ps1. Optionally, provide a format string to customize the output.

source /path/to/kube-prompt.sh

# Default output: (⎈|namespace)
export PS1='\u@\h \W $(__kube_ps1) \$ '

# With custom output: user@cluster:namespace:
export PS1='\u@\h \W $(__kube_ps1 "%u@%c:%n") \$ '

# Show only current context name
export PS1='\u@\h \W $(__kube_ps1 "%C") \$ '

# user@cluster:namespace with fancy colors
export PS1='\u@\h \W $(__kube_ps1 "\e[0;32m%u@%c\e[0m:\e[0;34m%n\e[0m") \$ '

Testing before using

You can use the testing/testing.sh script to show examples of __kube_ps1 usage.

$ cd testing/
$ export KUBECONFIG="$(pwd)"/kubeconfig

$ bash testing.sh
+ __kube_ps1
(⎈|frontend)

$ bash testing.sh 1
+ __kube_ps1 '[%C]'
[dev-frontend]

$ bash testing.sh 2
+ __kube_ps1 '%u@%c:%n'
developer@development:frontend

$ bash testing.sh 3 # Will render in bold blue
+ __kube_ps1 '\e[1;34m⎈|%n\e[0m'|frontend

To help building your new PS1, you can also provide custom format string:

bash testing.sh '\e[0;32m%u@%c\e[0m: \e[0;34m%n\e[0m'

my-user@my-cluster: my-namespace

Format

%u - current context user name
%n - current context namespace
%c - current context cluster name
%C - current context name

If %n is used and current context namespace is not set, "default" will be printed.

About

Customizable PS1 utility for Kubernetes current context

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages