Observe your Kubernetes Admission Webhook Configurations easily
By: Batuhan Apaydın
Tags: Platform, Kubectl Plugin, Kubectl, Krew, Kubernetes

In this guide, we are gonna talk about the journey of writing a kubectl plugin for Kubernetes Admission Webhooks. Let’s divide this article into three parts, first, we are gonna explain how we decided to write a plugin for Kubernetes Admission Webhooks, then which tools we used for writing a plugin, how we accomplished to distribute the plugin via Krew.

📰 Ingredients

Please note that, the purpose of this article is not about how to write a kubectl plugin from scratch, which means we will not dive into the technical details. There is an official documentation exist about this, I strongly recommend you to take a look this documentation:

https://kubernetes.io/docs/tasks/extend-kubectl/kubectl-plugins The only purpose of this article is sharing our experience about writing kubectl-plugin, what we are expecting from this article is guiding to the users who wants to write a kubectl-plugin.

How we decided to write a plugin?

In the beginning, we did a little search about the available plugins in the Kubernetes community. We quickly noticed that there is no plugin available related to Kubernetes Admission Webhook. So, we started to do some research about how we could design our plugin, what information we had about Kubernetes Admission Webhook, and how we could make use of these pieces of information effectively.

Let’s first lookout for the full information that we have.

$ kubectl get mutatingwebhookconfigurations config-sidecar-injector -oyaml | silicon -l yaml — output out.png

In this picture, you will see nearly all the available fields for the MutatingWebhookConfiguration resource. You notice that there is a lot of information available on this resource that can we use, but the YAML output of this resource is not useful from the human perspective. To be able to make this output more human-readable and well-organized, we decided to extract the utmost important values to a table format in the terminal. By doing this, we are giving a clear see to the user what’s configurations that they have.

This is the final output of our plugin that shows fields that we decided to be able to understand the details of the plugin you can take a look at this from the Github repository.

What tools we used for it?

We followed the guide¹ that shows us how we can write a kubectl plugin from the Kubernetes official documentation. Writing a kubectl plugin is nothing than writing a simple CLI tool. You can write a plugin in any programming language or script that allows you to write command-line commands.We thought that Go is the best choice for this type of work, so we moved on with it. You have a lot of options to be able to write a CLI app with Go, for example, Cobra, Urfave/cli, or Go built-in flag package. We chose the Cobra among these tools because of its wonderful features that are provided. The next thing we need to do is we must give the users the same experience(flags, outputs, etc.) which kubectl does, to be able to do that we need to use some libraries. Here are the suggested libraries and samples that we can use when we write a plugin with Go.

If you are planning to write a plugin with Go, check these out ²:

• client-go: a Go SDK to work with Kubernetes API and kubeconfig files

• cli-runtime: Provides packages to share code with kubectl for printing output or sharing command-line options

• sample-cli-plugin: An example plugin implementation in Go There are also a lot of plugins available, if you interested in.

Last but not least, we need a library that we can use to gain beautiful Terminal UIs. Pterm and the tablewriter come into the picture in this part. Pterm is for the colorful and well-organized output like Tree or Bullet List, and tablewriter is for the show the output within the table. You can build great Terminal UIs by using them.

By default, plugins that use client-go cannot authenticate to Kubernetes clusters on many cloud providers. To overcome this, add the following import to somewhere in your binary ²:

import _ "k8s.io/client-go/plugin/pkg/client/auth"

How we accomplished distribute the plugin via Krew?

Krew is the plugin manager for kubectl command-line tool.

Krew helps you:

discover kubectl plugins,

install them on your machine,

and keep the installed plugins up-to-date.

Once you write this plugin, you can distribute it via krew ³, in order to do that, you should simply write a manifest file, then send it to the krew-index. Once the manifest added to the krew-index, the other krew users can download your plugin via krew with the simple command:

$ kubectl krew install view-webhook

Here is the our manifest file that we send it to the krew-index:

Conclusion

As you can see, writing a kubectl plugins is not as hard as you think.You can create your own wonderful kubectl plugins with just couple of code and the help of utility libraries.

Here is our plugin, you can find it on this repository. Please do not forget to drop a ⭐️ if you liked. 😅

Acknowledgements

Thanks to Furkan Türkal for all the support

Thanks to Ahmet Alp Balkan for adding the plugin to krew-index and all the suggestions

References

If you want to discover more: #joinus #cometotrendyol