How to write Kubernetes custom controllers in Go

Anartz Nuin
Mar 28, 2019 · 3 min read
Image for post
Image for post
Photo by Patryk Grądys on Unsplash

client-go

The client-go library provides access to Kubernetes RESTful API interface served by the Kubernetes API server. Well-known tools like kubectl use it intensively.

Choosing the right version

It is important to note that the recommended version for client-go and some of its dependent packages k8s.io/api and k8s.io/apimachinery, depend on the Kubernetes version being used. Fortunately, client-go comes with a handy compatibility matrix which tells us exactly which version combinations are fully compatible (see figure below).

Image for post
Image for post
client-go compatibility matrix (source: https://github.com/kubernetes/client-go)

Controller structure

In the official Kubernetes GitHub repository, we can find a nice controller example, sample-controller. This repository provides a diagram which helps to understand the underlying components of a typical Kubernetes controller.

Image for post
Image for post
Kubernetes controller diagram(source: https://github.com/kubernetes/sample-controller/blob/master/docs/images/client-go-controller-interaction.jpeg)
  1. Consume items from the workqueue and process them.

Example

In order to illustrate this better let’s take a look at the sample-controller implementation. The sample-controller ensures that for every Foo resource there’s a corresponding Deployment with the number of replicas specified by the Foo resource.

func (c *Controller) runWorker() { 
for c.processNextWorkItem() {
}
}

Conclusion

client-go is a very powerful library for developing Kubernetes custom controllers. It comes with all the components needed for implementing the correct workflow in an efficient manner. However, it requires developers to learn low-level details about how Kubernetes libraries are implemented and write some boilerplate code. With this being said, there are some helpful initiatives trying to provide a better development experience, such as Kubebuilder, which may be worth exploring.

Speechmatics

Learn about all things machine learning, artificial…

Anartz Nuin

Written by

Anartz Nuin is a Software Development Engineer at Speechmatics. Speechmatics is a machine learning company which specialises in automatic speech recognition.

Speechmatics

Learn about all things machine learning, artificial intelligence, speech recognition, data and much more.

Anartz Nuin

Written by

Anartz Nuin is a Software Development Engineer at Speechmatics. Speechmatics is a machine learning company which specialises in automatic speech recognition.

Speechmatics

Learn about all things machine learning, artificial intelligence, speech recognition, data and much more.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

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