Build Your Kubernetes Operator With the Right Tool


This is a guest post by Rafał Leszko, Integration Team Lead at Hazelcast. It contains the summary of the OperatorCon talk and the related blog post published at Hazelcast Blog.

You have a lot of tools to choose from when you want to build a Kubernetes Operator for your software. Which is the right one for your use case? Operator SDK with Helm, Ansible, or Go? Or maybe something less common, like Python or Java? This blog post discusses different approaches to make your decision simpler.

Introduction

Kubernetes Operator is an application that watches a custom Kubernetes resource and performs some operations upon its changes.

When a user modifies anything in the custom resource, the operator application notices the change and performs some actions based on this change. These actions are usually some type of calls to Kubernetes API, but technically they can be anything.

Since an operator is simply an application, technically you can write it in any programming language you want and you can deploy it anywhere you want. However, being a pragmatic programmer, you should choose a language with a good Kubernetes client and a language that is simple to dockerize and run in the Kubernetes cluster.

Operator Tools Overview

In short, you can choose from the following options: Operator SDK (Helm, Ansible, Go), Operator Framework (that is, KOPF, Java Operator SDK), Bare Programming Language (such as Java, Kotlin, C#).

Operator SDK: Helm

Helm is a package manager for Kubernetes. It lets you create templated Kubernetes configuration files and render them automatically using Helm CLI. Then, Operator SDK can automatically generate an operator application from a given Helm chart. The benefits include:

  • Implementation is declarative and simple
  • Operator functionality is limited to Helm features
  • Operator manifest bundle files (CRD, RBAC, Operator Deployment) are automatically generated

Operator SDK: Ansible

Ansible is a powerful tool for IT automation. Its nature is declarative and, thanks to the "community.kubernetes.k8s" plugin, it can interact with Kubernetes API. Operator SDK provides a way to scaffold an Ansible-based operator and then writing an operator logic means writing an Ansible role, with embedded Kubernetes configuration. Advantages include:

  • Implementation is declarative and human-readable
  • Ansible can express almost any operator functionality
  • Operator manifest bundle files (CRD, RBAC, Operator Deployment) are automatically generated

Operator SDK: Go

Go is a general-purpose programming language, so you can write any operator logic you want. Kubernetes itself is written in Go, so this language interacts smoothly with Kubernetes API. Operator SDK provides a way to scaffold a Go-based operator and then implementing an operator means writing an imperative Go code. With Go:

  • Implementation is imperative and more complex
  • There is no limit on the functionality you want to implement
  • Operator manifest bundle files (CRD, RBAC, Operator Deployment) are generated from the Go source code

Operator Framework

Apart from Operator SDK, there are other frameworks to help build an operator, such as KOPF (Kubernetes Operator Pythonic Framework) and Java Operator SDK. By using a  framework:

  • Logic is used  in a general-purpose programming language
  • Frameworks are more experimental and less developed than Operator SDK
  • Kubernetes clients for Python, Java, or other languages are slightly worse than Go
  • There is no limit on the functionality desired

Bare Programming Language

The last choice is to start from scratch and write all the operator implementation yourself. One popular approach is to use Java with Quarkus and build a native Docker image using GraalVM. In such a case, you need to write a lot of code, but it can be a good approach if your goal is a single programming language inside your organization. With this approach:

  • Implementation is complex and requires writing a lot of code
  • There is no limit on the functionality you want to implement
  • There is no scaffolding or manifest files generation

Summary

Knowing the options is only half of the story. The other half is looking at what the community does and why. Programming is a collaborative effort, and you'd better stick to the solutions used by others for best results.

Here are the top used tools according to  data from OperatorHub.io:

  • Go: 71%
  • Helm: 13%
  • Java: 3%

Go-based operators are by far the most popular. That is why Go is probably the first option to consider. The other good choice is Helm, especially if you already have a Helm chart for your software or you want to build your operator quickly and you don't need any complex capability levels. I'd leave Operator Frameworks or Bare Programming Language implementations only for the cases when keeping a single programming language in your organization is a priority.