Skip to main content

Writing a Kubernetes operator in Python

Let me start with a brief introduction to Kubernetes operators. Consider this scenario. You have an application deployment which is associated with a configmap. This configmap determines application startup and behaviour. In more concrete terms, think of an Nginx deployment with the nginx.conf file as a configmap. Every time the nginx.conf file changes, we have to re-roll the deployment. This action sounds trivial to warrant any automation, but it might become tedious at scale. Imagine a human operator watching configmaps and re-rolling the associated deployments whenever these configmaps change. Now imagine this “human operator” to be a long running pod in a Kubernetes cluster. This is termed as the “operator pattern” in Kubernetes. To quote a more formal definition,

Kubernetes’ operator pattern concept lets you extend the cluster’s behaviour without modifying the code of Kubernetes itself by linking controllers to one or more custom resources.

Here, we are extending the cluster’s behaviour by writing an operator which will re-roll the deployment when an associated configmap changes. The definition mentions “custom resources”, but that needn’t be the case always. Here, we’re building an operator which deals with deployments and configmaps. Both are first-class citizens of Kubernetes. If we are creating an operator to take a DB snapshot of a Postgres database on a periodic basis, then we might create a custom resource like DBSnapshot.

How to build operators #

Operator framework is a popular choice to build operators. It helps write operators in Golang, Ansible or Helm. Writing operators in Golang is the most preferred and popular approach. It has a rich ecosystem, most of the popular open source operators are written in Golang and it offers great programmability. Ansible offers a lower barrier to entry. I haven’t quite used or tried Helm to fit into an operator pattern to comment on it. But this is not all. You can write operators in your favourite programming language. I was shopping for a way to write operators in Python and I stumbled upon Kopf.