Skip to content

08 K8s TDD

Daniel Morinigo edited this page Jan 15, 2020 · 1 revision

How to follow a TDD flow while developing Kubernetes native components?

To simplify this topic, we will be focusing on controllers given its popularity

Pre-requisites

Options

One of the hardest problems that need to be tackled when dealing with unit tests is how to solve the problem generated by external dependencies such as database, apis, etc. In the Kubernetes community and official repos we can see very different approaches

Kubebuilder sample-controller
approach Integration Mock
code https://github.com/kubernetes-sigs/kubebuilder https://github.com/kubernetes/sample-controller

Client-go

Client-go and generated code already tackles the mock problem offering its code signature using interfaces, a implementation, and a mock/fake

We deal with this in detail here

Kubebuilder

Bundling all external dependencies together, kubebuilder uses the host operating system to start ephemeral instances of the kube-apiserver, etcd, etc. to simplify writing and maintaining unit tests. Check this page for a detailed exploration

Gomock

Source

Given that kubernetes clients are retrieved from cascading functions, it would take a lot of preparation in order to prepare on client call. Still, gomock comes handy when implementing mocks for internal interfaces and other external services.

Maybe someone could create a package to simplify bundling and preparing mocks for client-go interfaces?

Clone this wiki locally