News: This article has now been accepted in official Kubernetes documentation:

Power of Kubernetes is in its extendability. One of the ways in which you can extend Kubernetes is by writing your own custom controllers. You can write custom controllers that handle in-built Kubernetes objects, such as Deployment, Service, in new ways, or you can add new custom resources within Kubernetes and write a custom controller to handle these new resources.

Recently we have been developing a Custom Resource Definition (CRD) for Postgres. While developing this CRD we spent some time studying Kubernetes’s client-go library. This library contains various mechanisms that you can use when developing your custom controllers. These mechanisms are defined in the tools/cache folder of the library.

We have put together a pictorial representation showing how the various components in the client-go library work and its interaction points with the custom controller code that you will write.

Here is that representation.

Kubernetes client-go and Custom Controller

The picture is divided into two parts— client-go and Custom Controller.

Below we explain each component pointing to appropriate places in the client-go code and our Postgres custom controller that shows the main actions of each component.

client-go components

Reflector: A reflector watches the Kubernetes API for the specified resource type (kind). This could be an in-built resource or it could be a custom resource. When it receives notification about existence of new resource instance through the watch API, it gets the newly created object using the corresponding listing API. It then puts the object in a Delta Fifo queue. Informer: An informer pops objects from the Delta Fifo queue. Its job is to save object for later retrieval, and invoke the controller code passing it the object. Indexer: An indexer provides indexing functionality over objects. A typical indexing use-case is to create an index based on object labels. Indexer can maintain indexes based on several indexing functions. Indexer uses a thread-safe data store to store objects and their keys. There is a default function that generates an object’s key as<namespace>/<name> combination for that object.

Custom Controller components

You can try out our Postgres custom resource to see how these components fit together in real code. This custom resource has been developed following the sample-controller available in Kubernetes.

We hope that above explanation of various components and mechanisms involved in writing custom Kubernetes controllers will help you in writing your own custom controllers.

www.cloudark.io