Introduction

In this article we will see how to use kubebuilder and Kind to create a local test cluster and an operator, then deploy that operator in the cluster and test it, the repository with the files can be found here, also if you want to learn more about the idea and the project go: forward.

Basically what the code does is create an alpine/socat pod and you can specify the host, port and protocol and it will make a tunnel for you, so then you can use port-forward or a service or ingress or whatever to expose things that are in another private subnet, while this might not sound like a good idea it has some use cases, so check your security constraints before doing any of that in a normal scenario it should be safe, it can be useful for testing or for reaching a DB while doing some debugging or test, but well, that is for another discussion, the tools used here is what makes this so interesting, this is a cloud native application, since it native to kubernetes and that’s what we will explore here.

While Kind is not actually a requirement I used that for testing and really liked it, it’s faster and simpler than minikube.

Also if you are interested how I got the idea to make this operator check this github issue.

Prerequisites

Create the project

In this step we need to create the kubebuilder project, so in an empty folder we run:

Create the API

Next let’s create an API, something for us to have control of (our controller).

Right until here we only have some boilerplate and basic or empty project with defaults, if you test it now it will work, but it won’t do anything interesting, but it covers a lot of ground and we should be grateful that such a tool exists.

Add our code to the mix

First we will add it to api/v1beta1/map_types.go , which will add our fields to our type. Basically we just edited the MapSpec and the MapStatus struct.

Now we need to add the code to our controller in controllers/map_controller.go

In this controller we added two functions one to create a pod and modified basically the entire Reconcile function (this one takes care of checking the status and make the transitions in other words makes a controller work like a controller), also notice the kubebuilder annotations which will generate the rbac config for us, pretty handy! right?

Starting the cluster

Now we will use Kind to create a local cluster to test it could be that easy!?!?! yes, it is!

Running our operator locally

For testing you can run your operator locally like this:

Testing it

First we spin up a pod, and launch nc -l -p 8000

Then we edit our manifest and apply it, check that everything is in place, and do the port-forward and launch another nc localhost 8000 to test if everything went well. First the manifest

Then the port-forward and test

Making it publicly ready

Here we just build and push the docker image to dockerhub or our favorite public registry. Then you can install it with make deploy IMG=kainlite/forward:0.0.1 and uninstall it with make uninstall

Closing notes

Be sure to check the kubebuilder book if you want to learn more and the kind docs, I hope you enjoyed it and hope to see you on twitter or github!

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