Code Monkey home page Code Monkey logo

kube-ps1's Introduction

kube-ps1: Kubernetes prompt for bash and zsh

GitHub Release CI

A script that lets you add the current Kubernetes context and namespace configured on kubectl to your Bash/Zsh prompt strings (i.e. the $PS1).

Inspired by several tools used to simplify usage of kubectl.

prompt demo

Installing

Packages

MacOS Brew Ports

Homebrew package manager:

brew update
brew install kube-ps1

Arch Linux

AUR Package available at https://aur.archlinux.org/packages/kube-ps1/.

Oh My Zsh

https://github.com/ohmyzsh/ohmyzsh

kube-ps1 is included as a plugin in the oh-my-zsh project. To enable it, edit your ~/.zshrc and add the plugin:

plugins=(
  kube-ps1
)
PROMPT='$(kube_ps1)'$PROMPT # or RPROMPT='$(kube_ps1)'

Zsh zinit plugin

Using zinit

Update .zshrc with:

zinit light jonmosco/kube-ps1
PROMPT='$(kube_ps1)'$PROMPT # or RPROMPT='$(kube_ps1)'

Fig

Install kube-ps1 in zsh, bash, or fish with one click.

From Source (git clone)

  1. Clone this repository
  2. Source the kube-ps1.sh in your ~/.zshrc or your ~/.bashrc

Zsh

source /path/to/kube-ps1.sh
PROMPT='$(kube_ps1)'$PROMPT # or RPROMPT='$(kube_ps1)'

Bash

source /path/to/kube-ps1.sh
PS1='[\u@\h \W $(kube_ps1)]\$ '

Requirements

The default prompt assumes you have the kubectl command line utility installed. Official installation instructions and binaries are available:

Install and Set up kubectl

If using this with OpenShift, the oc tool needs installed. It can be obtained from brew ports:

brew install openshift-cli

or the source can be downloaded:

OC Client Tools

Set the binary to oc with the following variable:

KUBE_PS1_BINARY=oc

If neither binary is available, the prompt will print the following:

(<symbol>|BINARY-N/A:N/A)

Helper utilities

There are several great tools that make using kubectl very enjoyable:

Tmux port

I have begun porting kube-ps1 to tmux as a status line plugin. If you prefer tmux, and like the functionality provided by kube-ps1, checkout the kube-tmux project

Prompt Structure

The default prompt layout is:

(<symbol>|<context>:<namespace>)

If the current-context is not set, kube-ps1 will return the following:

(<symbol>|N/A:N/A)

Enabling/Disabling

If you want to stop showing Kubernetes status on your prompt string temporarily run kubeoff. To disable the prompt for all shell sessions, run kubeoff -g. You can enable it again in the current shell by running kubeon, and globally with kubeon -g.

kubeon     : turn on kube-ps1 status for this shell.  Takes precedence over
             global setting for current session
kubeon -g  : turn on kube-ps1 status globally
kubeoff    : turn off kube-ps1 status for this shell. Takes precedence over
             global setting for current session
kubeoff -g : turn off kube-ps1 status globally

Symbol

The default symbols are UTF8 and should work with most fonts. If you want to use the Kubernetes and OpenShift glyphs, you need to install a patched font that contains the glyph. Nerd Fonts provides both glyphs. Follow their installation instructions to install the patched font.

KUBE_PS1_SYMBOL_CUSTOM options

Options Symbol Description
default (empty string) Default symbol (Unicode \u2388)
img ☸️ Symbol often used to represent Kubernetes (Unicode \u2638)
oc openshift-glyph Symbol representing OpenShift (Unicode \ue7b7)
k8s k8s-glyph Symbol representing Kubernetes (Unicode \ue7b7)

To set the symbol to one of the custom glyphs, add the following to your ~/.bashrc or ~/.zshrc:

KUBE_PS1_SYMBOL_CUSTOM=img

To set the symbol to the default, set the KUBE_PS1_SYMBOL to an empty string.

Heres a demo of the symbols in action: kube-ps1-symbols

If the font is not properly installed, and the glyph is not available, it will display an empty set of brackets or similar:

 echo -n "\ue7b7"

Customization

The default settings can be overridden in ~/.bashrc or ~/.zshrc by setting the following variables:

Variable Default Meaning
KUBE_PS1_BINARY kubectl Default Kubernetes binary
KUBE_PS1_NS_ENABLE true Display the namespace. If set to false, this will also disable KUBE_PS1_DIVIDER
KUBE_PS1_PREFIX ( Prompt opening character
KUBE_PS1_SYMBOL_ENABLE true Display the prompt Symbol. If set to false, this will also disable KUBE_PS1_SEPARATOR
KUBE_PS1_SYMBOL_PADDING false Adds a space (padding) after the symbol to prevent clobbering prompt characters
KUBE_PS1_SYMBOL_CUSTOM Change the Default prompt symbol. Unicode \u2388. Options are k8s, img, oc
KUBE_PS1_SYMBOL_COLOR blue Change the Default symbol color.
KUBE_PS1_SEPARATOR | Separator between symbol and context name
KUBE_PS1_DIVIDER : Separator between context and namespace
KUBE_PS1_SUFFIX ) Prompt closing character
KUBE_PS1_CLUSTER_FUNCTION No default, must be user supplied Function to customize how cluster is displayed
KUBE_PS1_NAMESPACE_FUNCTION No default, must be user supplied Function to customize how namespace is displayed

To disable a feature, set it to an empty string:

KUBE_PS1_SEPARATOR=''

Colors

The default colors are set with the following variables:

Variable Default Meaning
KUBE_PS1_PREFIX_COLOR null Set default color of the prompt prefix
KUBE_PS1_SYMBOL_COLOR blue Set default color of the Kubernetes symbol
KUBE_PS1_CTX_COLOR red Set default color of the context
KUBE_PS1_SUFFIX_COLOR null Set default color of the prompt suffix
KUBE_PS1_NS_COLOR cyan Set default color of the namespace
KUBE_PS1_BG_COLOR null Set default color of the prompt background

Blue was used for the default symbol to match the Kubernetes color as closely as possible. Red was chosen as the context name to stand out, and cyan for the namespace.

Set the variable to an empty string if you do not want color for each prompt section:

KUBE_PS1_CTX_COLOR=''

Names are usable for the following colors:

black, red, green, yellow, blue, magenta, cyan

256 colors are available by specifying the numerical value as the variable argument.

Customize display of cluster name and namespace

You can change how the cluster name and namespace are displayed using the KUBE_PS1_CLUSTER_FUNCTION and KUBE_PS1_NAMESPACE_FUNCTION variables respectively.

For the following examples let's assume the following:

cluster name: sandbox.k8s.example.com namespace: alpha

If you're using domain style cluster names, your prompt will get quite long very quickly. Let's say you only want to display the first portion of the cluster name (sandbox), you could do that by adding the following:

function get_cluster_short() {
  echo "$1" | cut -d . -f1
}

KUBE_PS1_CLUSTER_FUNCTION=get_cluster_short

The same pattern can be followed to customize the display of the namespace. Let's say you would prefer the namespace to be displayed in all uppercase (ALPHA), here's one way you could do that:

function get_namespace_upper() {
    echo "$1" | tr '[:lower:]' '[:upper:]'
}

export KUBE_PS1_NAMESPACE_FUNCTION=get_namespace_upper

In both cases, the variable is set to the name of the function, and you must have defined the function in your shell configuration before kube_ps1 is called. The function must accept a single parameter and echo out the final value.

Bug Reports and shell configuration

Due to the vast ways of customizing the shell, please try the prompt with a minimal configuration before submitting a bug report.

This can be done as follows for each shell before loading kube-ps1:

Bash:

bash --norc

Zsh:

zsh -f
or
zsh --no-rcs

For the prompt symbol, a patched font that contains the glyphs must be installed. Nerd Fonts Downloads provides patched fonts containing the glyphs. Please consult their documentation for this, support is out of scope for this project.

Contributors

Thank you to everyone in the community for their contributions to kube-ps1!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.