DEV Community


Colored wrappers for kubectl

raphink profile image Raphaël Pinson Updated on ・1 min read

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

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

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

YAML colorizing

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

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

compdef ky=kubectl


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)

and with fx:

Discussion (0)

Editor guide