kubectl explore, a better kubectl explain

Keisuke Umegaki
3 min readJan 22, 2022

I created a kubectl plugin named kubectl-explore. I hope it can improve your productivity when searching the resource fields, such as “pod.spec”, “cronJob.spec.jobTemplate”, etc.

I am writing this blog post to let everyone know kubectl-explore, and share how to create it.

As you may already know, I introduce the article “Extend kubectl with plugins” which explains what kubectl plugin system is and how it works.

What

kubectl-explore is a kubectl plugin to fuzzy-find and explain the field supported API resource like “pod.spec”, “cronJob.spec.jobTemplate”, etc.

You can get the explanation of the field selected by fuzzy-finder as following commands. Or you can also get the usage from kubectl explore --help

I tried to keep compatibility with kubectl explain as possible.

$ kubectl explore
$ kubectl explore pod.spec
$ kubectl explore --context=onecluster

Motivation

  • kubectl explain needs knowing in advance the resources name/fields.
  • kubectl explain needs typing the accurate path to the resource name/field, which is a tedious and typo-prone.

Installation

You can install this plugin via krew.

$ kubectl krew install explore

For other users, please see the installation guide in the README. You can download the binary from GitHub Releases.

How I created

Learn how to create a plugin

Extend kubectl with plugins is an official guide to demonstrates how to install and write extensions for kubectl. This is the best resource to learn how to create a plugin.

Find good examples

kubernetes/kubectl would be a good example to learn practices when creating a kubectl plugin. A plugin I want to create in this time is an extended version of kubectl explain , so I dived deeply into pkg/cmd/explain/explain.go, kubectl/pkg/explain and pkg/cmd/cmd.go. Then, I followed the implementation details such as struct design, method name, etc. Especially, there are many references to how to implement unit tests for kubectl command, which was very helpful.

Moreover, I found d-kuro/kubectl-fuzzy interesting that it provides a kubectl commands with the fuzzy-finder; it uses ktr0731/go-fuzzyfinder. This library provides fzf like Go library. I decided to use it in my project.

Unfortunately, fzf doesn’t provide their features as a Go library. I hope it supports.

See following links.

Get feedback from Reddit

I published the Reddit post to get many attentions and feedbacks after releasing the first version.

I got many attentions!! They gave me a lot of suggestions, like supporting the installation via krew and kubectl global options, which you can get by kubectl options . I really appreciate their constructive feedbacks.

Introduction to plugin development is a helpful resource to support krew installation. Besides, I can know good other plugins through the process to support krew; please check krew-index!!

Conclusion

I appreciate every open source works😊 I enjoyed this process to write the plugin. If you have any suggestions or troubles about kubectl-explre, feel free to reach out to me!

--

--