Custom Kubernetes Operator With TypeScript (JavaScript).

Gabriel Garrido
ITNEXT
Published in
3 min readJul 22, 2021

--

Introduction

In this article we will explore how to create a sample operator using typescript and to deploy it to our cluster, the operator will be pretty dummy in the sense that it will only deploy some resources based in a CRD, but you can customize it to do whatever you might need or want, the idea is to get an idea of all that it takes to do an operator outside of the magic land of Go and kubebuilder.

If you want to check past articles that explore other alternative frameworks and languages go to:

You will notice that both are very similar and it is because the operator-sdk uses kubebuilder.

The source for this article is here TypeScript Operator and the docker image is here, also this article is based in this example from Nodeshift’s Operator in JavaScript.

Prerequisites

Let’s jump to the example

Creating the cluster

We will need a cluster to run and test our operator, so kind is pretty straight forward and lightweight enough to run anywhere.

Creating our operator

Creating all necessary resources for our operator to work

Deploying our operator

Creating our custom resource to see the operator in action

Logs from the operator

Example logs based in the creation, update and deletion of our custom resource

Brief comparison operator-sdk vs custom operator?

There are some main differences to have in mind, in Go you:

  • Have code generation from the framework for RBAC, controllers, etc.
  • Out of the box tooling to build, deploy and manage your operator.

In TypeScript or JavaScript you have to handle more things which can be easily done from a CI system, In this example I used github actions to build the image and the example already had everything else configured to make typescript usable with kubernetes as an example, but you mostly you use the API and set everything yourself, there are more differences but these are the main ones that I noticed or called my attention.

Building and pushing (docker image)

In this case we don’t have to do that it will be managed by actions using the free container registry that they provide, it will build and push the image matching the branch name, notice that it is fully transparent, you don’t need to configure anything on the repo, you can see the result here.

Local development

Bonus: if you want to run the operator locally when developing or debugging you can do so easily with ts-node, like this: The reason I used it like this was mostly to assume zero configuration, and it is possible because ts-node is listed as a development dependency, also the docker image could have been used with a bit of configuration.

Note that I did not add all the code from the resources folder or the setup for the typescript project, I recommend you to check that directly in the repo to understand all the missing pieces.

Now let’s see the code

Enough words, let’s see code, I have added comments and changed the original code a bit

The deployment.json file

This file basically is what gets deployed when we create our custom resource

And finally our custom resource

This is how we tell our operator that we need our operator to create some resources for a given task

Extra

For more details and to see how everything fits together I encourage you to clone the repo, test it, and modify it yourself.

Cleaning up

To clean up the operator from the cluster you can do this

Closing notes

Be sure to check the links if you want to learn more about the examples from Nodeshift and I hope you enjoyed it, see you on twitter or github!

The source for this article is here

DISCLAIMER: I’m not using OpenShift, but all examples are easily translatables to a vanilla cluster.

Errata

If you spot any error or have any suggestion, please send me a message so it gets fixed.

Also, you can check the source code and changes in the generated code and the sources here

Originally published at https://techsquad.rocks on July 22, 2021.

--

--