DEV Community

Raphaël Pinson
Raphaël Pinson

Posted on • Edited on

Colored wrappers for kubectl

When using Kubernetes, kubectl is the command we use the most to visualize and debug objects.

However, it currently does not support colored output, though there is a feature request opened for this.

Let's see how we can add color support. I'll be using zsh with oh my zsh.

Edit: this feature was merged in oh my zsh, so it is now standard.

Zsh plugin

Let's make this extension into a zsh plugin called kubectl_color:

mkdir -p ~/.oh-my-zsh/custom/plugins/kubectl_color
❯ touch ~/.oh-my-zsh/custom/plugins/kubectl_color/kubectl_color.plugin.zsh
Enter fullscreen mode Exit fullscreen mode

Now we need to fill in this plugin.

JSON colorizing

Let's start with JSON, by adding an alias that colorizes JSON output using the infamous jq:

kj() {
  kubectl "$@" -o json | jq
}

compdef kj=kubectl
Enter fullscreen mode Exit fullscreen mode

The compdef line ensures the kj function gets autocompleted just like kubectl.

Edit: I've added another wrapper for fx, which provides a dynamic way to parse JSON:

kjx() {
  kubectl "$@" -o json | fx
}

compdef kjx=kubectl
Enter fullscreen mode Exit fullscreen mode

YAML colorizing

Just like for JSON, we can use yh to colorize YAML output:

ky() {
  kubectl "$@" -o yaml | yh
}

compdef ky=kubectl
Enter fullscreen mode Exit fullscreen mode

Energize!

Our plugin is now ready, we only need to activate it in ~/.zshrc by adding it to the list of plugins, e.g.:

plugins=(git ruby kubectl kubectl_color)
Enter fullscreen mode Exit fullscreen mode

and with fx:

Top comments (1)

Collapse
 
nitinbabariya profile image
IC1101

Its a good one. Thanks