Kooper: extending Kubernetes made easy

Xabier Larrakoetxea
Spotahome Product
Published in
3 min readFeb 15, 2018

--

Say hello to Kooper

We have the pleasure to announce that Kooper has been open sourced.

Here at Spotahome, we are building our platform on top of Kubernetes through controllers and operators, and Kooper is the pillar for all this tooling.

Okay, Kooper is cool, and now what?, what is kooper?

Kooper is a library, toolkit or framework (pick the one that you want) to create Kubernetes operators and controllers in a faster and cleaner way, so our operator/controller code is mainly the one of our domain logic and delegate the bootstrapping and testing of the internals of kubernetes client-go library utilities to Kooper.

Kooper has been designed to be easy to use. It also creates some conventions on structure and concepts, making easy to understand every controller/operator that uses this library. When the code, structure and concepts are uniform, understanding and start contributing in a new project is faster and easier.

Check out Kooper in the repository: https://github.com/spotahome/kooper

Motivations

There are other libraries that try to solve the same problems that Kooper does. Before we started making it, we checked out these alternatives (specifically giantswarm/operatorkit and rook/operator-kit) but they didn’t meet the requirements we had:

  • Start developing an operator or a controller easily.
  • Simple to understand.
  • Controllers and operators as first class citizens and at the same level of importance.
  • Strong concepts of what an operator and a controller is (almost the same).
  • Tested.
  • Documented.
  • Well structured and a clear API.
  • Easy to mock and extend functionality (Go interfaces!)
  • Only support CRD, no TPR support (Kubernetes >=1.7)

Some were met by these projects, but not all of them, so… we made Kooper.

Operators and controllers?

For kooper the distinction is easy:

  • Controller acts and takes(or not) actions based on events of Kubernetes core resources (Pod, Service, Deployment, Ingress…).
  • Operator acts and takes(or not) actions based on events of our custom Kubernetes resources a.k.a CRD (MyCustomResource, Team, Workspace, CIEnvironment, ChaosEngineeringAttack, PutHereYourType…).

In a few words, an operator is a controller that initializes/uses CRDs. Simple.

controller + CRD = operator

Using Kooper to implement an operator or a controller has the same workflow, except the operator needs some more steps for the CRD. (Kooper gives you utils to manage this CRDs).

Where do I start?

Kooper documentation has 3 main parts, the basic concepts, the controller tutorial and the operator tutorial. If you start from the basic concepts you can jump to do your first Kubernetes controller in a few lines.

Give me an example!

For the first example we will do a simple log controller (for an operator example check operator tutorial).

Let’s start by creating a retriever, this is the how Kubernetes apiserver will notify our controller of the state of the resources we want to act on.

Our retriever is will listen to all pod events on all namespaces.

Now we create the handler, this has our domain logic and is the simplest thing we can do (log the events of our pods).

It receives Add events when:

  • A new pod is created.
  • A pod is updated.
  • A resync period is made (gives again all the pods every T time).

It receives Delete events when:

  • A pod is deleted.

Finally we create and run the controller using the previously created retriever and handler.

As you see it’s very simple but you can structure this in a better way. This is just a quick example, in the controller tutorial you can see a better way of structuring the components and make them testable. Don’t forget that this is a controller, but Kooper also has strong support for operators, check the operator tutorial for that.

Digging deeply

Maybe you are wondering what’s happening behind the scenes…

This library abstracts from all the client-go tooling, kubernetes informers, work queues, indexed stores… and automates and transforms them in concepts that are easy to understand if you are not familiar with Kubernetes internals.

Contribute

If you want to contribute, if you have found an error or if you want a feature that is not implemented yet go to the Kooper repository at GitHub and open a PR or an issue.

--

--