Skip to content

eyalev/kubectl-context-prompt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

Currently kubectl doesn't easily show which GCP project and cluster you're working on. This solution adds relevant info to the Bash / Z-Shell prompt.

Based on: https://pracucci.com/display-the-current-kubelet-context-in-the-bash-prompt.html

Add to your .bashrc or .zshrc:

kube_prompt()
{
   kubectl_current_context=$(kubectl config current-context)
   kubectl_project=$(echo $kubectl_current_context | cut -d '_' -f 2)
   kubectl_cluster=$(echo $kubectl_current_context | cut -d '_' -f 4)
   kubectl_prompt="k8s:($kubectl_project|$kubectl_cluster)"
   echo $kubectl_prompt
}

Add kube_prompt to the prompt variable

Z-Shell config example

PROMPT='[%D{%F %T}] ${ret_status}%{$fg_bold[green]%}%p%{$fg[cyan]%}%2d %{$fg_bold[green]%}$(kube_prompt) %{$fg_bold[blue]%}$(git_prompt_info)$(hg_prompt_info)%{$fg_bold[blue]%} % %{$reset_color%}'

kubectl example

Or just:

PROMPT="${PROMPT} $(kube_prompt) "

Bash config example

PS1="($PS1) $(kube_prompt) "

About

Add kubectl context info to bash or zshell prompt

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published