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 isa feature request opened for this.
Let's see how we can add color support. I'll be using zsh withoh my zsh.
Edit: this feature wasmerged in oh my zsh, so it is now standard.
Zsh plugin
Let's make this extension into a zsh plugin calledkubectl_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 infamousjq
:
kj(){ kubectl"$@"-o json | jq}compdefkj=kubectl
Thecompdef
line ensures thekj
function gets autocompleted just likekubectl
.
Edit: I've added another wrapper forfx
, which provides a dynamic way to parse JSON:
kjx(){ kubectl"$@"-o json | fx}compdefkjx=kubectl
YAML colorizing
Just like for JSON, we can useyh
to colorize YAML output:
ky(){ kubectl"$@"-o yaml | yh}compdefky=kubectl
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)
and withfx
:
Top comments(1)
For further actions, you may consider blocking this person and/orreporting abuse