Kubebuilder
What is Kubebuilder?
Kubebuilder is an SDK for quickly and easily building and publishing Kubernetes APIs in Golang. It is built on top of the Kubernetes existing canonical technique to provide simple abstractions to reduce the need for boilerplate.
Why Kubebuilder and Kubernetes APIs?
Applications and kubernetes cluster resources typically require some work operators, such as for replacing failed replicas with new ones, or scaling counts while resharding data. Running the MySQL application may require scheduling backups, reconfiguring replicas after scaling, setting up failure detection and remediation, etc.
With the Kubernetes API model, the management logic is embedded directly into application-specific Kubernetes APIs, for example, the “MySQL” API. Users then manage the application through YAML configuration using tools such as kubectl, just like they do for Kubernetes objects.
Under the hood of Kubebuilder

Kubebuilder component
Generic controller
Act as a wrapper of the custom controller, which is based on sample-controller.
It defines the queue in which Objects are delivered by Informers using event handlers (not shown). The queue itself is not exposed to our custom controller.
Defines a variable that will hold reference to the Reconcile function that our custom controller will implement.
Controller manager
The ControllerManager manages custom controllers. It maintains two data structures: list of controller references and a map of Objects and their Informers. It provides following functions to populate the two data structures: AddInformerProvider, AddController, RunInformersAndControllers.
Injector
An Injector defines two data structures:
a list for storing CRDs that need to be registered
a list for storing functions that should be invoked for starting informers and controllers.
The CRD list will be populated by the registration of CRDs whose Objects our custom controller will be monitoring. The registration of the CRDs is done in the setup phase. The function list is also populated during the setup phase.
Custome controller component
xxx_controller.go
The implementation of our controller.
main.go
The entrypoint of running a controller.
other bunch of generated files
These files have something like DeepCopy(), and the code which used by Kubebuilder components.
Installation of Kubebuilder
Check out the official kubebuilder book here. Once you have kubebuilder installed, you could simply run kubebuilder version
to check if the correct version is installed.
Known issues
Case
Solution
Remark
kubebuilder create api --group "storage.k8s.io" --version "v1" --kind "StorageClass" Create Resource under pkg/apis [y/n]? n Create Controller under pkg/controller [y/n]? y
......
2019/08/28 14:15:08 pkg/controller/storageclass/storageclass_controller.go:30:9: expected 'STRING', found '.' (and 10 more errors)
Change --group storage.k8s.io
to --group storage
This might not be an official solution.
Last updated
Was this helpful?