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:
- Cloud native applications with kubebuilder and kind aka kubernetes operators.
- Testing the Operator SDK and making a prefetch mechanism for Kubernetes.
You will notice that both are very similar and it is because the operator-sdk uses kubebuilder.
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.
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.
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
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
For more details and to see how everything fits together I encourage you to clone the repo, test it, and modify it yourself.
To clean up the operator from the cluster you can do this
The source for this article is here
DISCLAIMER: I’m not using OpenShift, but all examples are easily translatables to a vanilla cluster.
If you spot any error or have any suggestion, please send me a message so it gets fixed.