Colorize kubectl output by kubecolor

Abstract

kubectl command is a CLI client of Kubernetes. If you are an administrator of your company’s Kubernetes cluster or a developer who makes an application which runs on Kubernetes, you will be using kubectl almost every day. And I am also one of them.

While kubectl is already good, I have had a pain about its behavior. kubectl’s output is sometimes not easy to read because of its lack of color. Because kubectl sometimes outputs something very long, it is difficult to find what you are looking for.

kubecolor is my new open source CLI tool which colorizes the kubectl output automatically. In this article, I will explain what I wanted to improve about current kubectl. Then, I will describe what kubecolor can change the situation.

How I wanted to improve kubectl

First, let’s see what kubectl outputs.

kubectl never sets any colors on its output. Yes, because this “kubectl get pods” output is quite short, it’s not so a big problem. However, some commands which shows long output, such as “kubectl describe”, “kubectl get -o json”, will show you this:

Do they look harder to read? If it’s way easy for you, then you don’t need kubecolor. For me, it was difficult to find what I am looking for and that’s why I developed kubecolor.

So now, let’s talk about how kubecolor can contribute to resolve this problem.

How kubecolor changes the kubectl output

kubecolor is an OSS mainly developed by me and it’s available in this GitHub repo:

It’s licensed MIT.

So now, let’s see what kubecolor changes the game.

Additionally, if the command resulted error, kubecolor shows the output in error color.

No more boring kubectl output! If they look useful easier to read, then keep reading this article to find the installation and usage.

How to use kubecolor

Disclaimer: this chapter’s content can be stale in the future. The latest information should be available in README.

Overview

kubecolor accepts the command line options which should be passed to kubectl, then execute kubectl command internally, then colorizes the output. It does nothing else, just colorize.

kubecolor is intended to colorize the output of only READ command (get, describe etc). When you want to run WRITE command (apply, edit, exec), it just runs kubectl but doesn’t colorize them.

Installation

For now, kubecolor requires go command to be installed. Run following command:

go get github.com/dty1er/kubecolor/cmd/kubecolor

To update it, run:

go get -u  github.com/dty1er/kubecolor/cmd/kubecolor

In the future, we will support binary release and package manager integrations.

Usage

What you need to do is just passing the arguments to be passed to kubectl to kubecolor. For example…

kubecolor --context my-context get pods
kubecolor edit deployment
kubecolor exec -it pod-a bash

kubecolor is intended to be a complete alternative of kubectl command. It means you can set an alias like this:

alias kubectl=kubecolor

This should completely work. If you want to use kubectl completion feature with kubecolor, you will need some configuration. It will follow.

Available command line options

kubecolor accepts some options. They are just used to switch kubecolor’s behavior and of course not passed to kubectl.

  • Don’t colorize by --plain

When--plain is specified, kubecolor don’t colorize the output. This is useful when you want to save the output into a file.

  • Configure color preset by --light-background

kubecolor uses the color preset for “dark backgrounded” environment by default. However, it might look too bright if you use “light backgrounded” environment (e.g. white colored terminal). If you pass --light-background to kubecolor, it uses the color preset for “light background” . This is the output of kubecolor describe without --light-background

With --light-background

For now, configuring each color is not supported.

Use kubecolor with kubectl autocompletion feature

kubectl has a autocompletion feature. When you want to enable it with kubecolor, you need this setting in your shell configuration:

complete -o default -F __start_kubectl kubecolor

When you want to set another alias like k=kubecolor , then you need to change kubecolor to k:

complete -o default -F __start_kubectl k

Please also refer to the official kubectl doc.

Specify which “kubectl” to use

Sometimes we want to use specific “kubectl” command. For example, when you installed your kubectl via gcloud, you might be able to specify another version of kubectl like kubectl.1.18 . You can tell kubecolor that like:

KUBECTL_COMMAND="kubectl.1.18" kubecolor get pods

When KUBECTL_COMMAND is empty, kubecolor uses kubectl by default.

Summing up

I introduced my new OSS kubecolor. With kubecolor, your kubectl will be more useful and more fun! Please check it out: https://github.com/dty1er/kubecolor

If you find it useful, please give it a star on GitHub! Opening an issue or submitting a PR should also be greatful.

Thank you for reading!

Hidetatsu loves infrastructures, database, concurrent programming, transactions, distributed systems… https://github.com/dty1er https://dtyler.io

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store