Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

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 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
Enter fullscreen modeExit 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 infamousjq:

kj(){  kubectl"$@"-o json | jq}compdefkj=kubectl
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

YAML colorizing

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

ky(){  kubectl"$@"-o yaml | yh}compdefky=kubectl
Enter fullscreen modeExit 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 modeExit fullscreen mode

and withfx:

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
nitinbabariya profile image
IC1101
  • Joined

Its a good one. Thanks

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Cilium Alchemist @ Isovalent at Cisco | IaC ⚙ | DevOps ∞ | Family Historian 🌳 | Puppet Champion 🦊 | Terraform 🌍 | Docker 🐳 | Kubernetes ☸ | 🇫🇷 living in 🇨🇭
  • Location
    Lausanne, Switzerland
  • Work
    TME @ Isovalent at Cisco
  • Joined

More fromRaphaël Pinson

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp