This document is intended for developers wanting to create new Controllers following the approach of the Knative Project A Sample Controller Project template can be found here: https://github.com/knative-sandbox/sample-controller you can use this template to start up your new controller project.
This section covers the main directories in a controller project. There is no main.go
file, as the project can be considered a library and the executables are commands (cmd
) that bootstrap the controllers as we will see in later sections.
cmd/
config/
hack/
pkg/
test/
<third_party>/
<vendor>/
go.mod
Here is a quick summary of the contents of these directories:
cmd
: contains directories with differentmain.go
files that boostrap code defined usually insidepkg/reconciler
confif
: contains yaml files required to install the components into a Kubernetes cluster. These resources usesko://
to point to controllers defined inpkg/reconciler
. Usually to start these components for dev mode you will useko -R -f config/
. Insideconfig/core
you have the CRDs which your controllers will use.hack
: for me this is where the magic lives. This directory contains a set of scripts that deals with code generation. Look for the code generation section od this document for more insights about these scripts and the things that are being code generatedpkg
: This is where the code go. A simple controller will have the following subdirectories:apis
,client
andreconciler
:apis
: a very self-descriptive name for the directory which will contain the types (go structures) that will represent your CRDs and other lifecycle functionality for your resource types.client
: auto generated code such asclientset
,informers
,injection
,listers
.reconciler
: this is where you controllers code will go. In other words, where you will need to write your reconcilation logic.
test
: another very self-descriptive directory. Knative components are quite complex, so I will add more details later on about testing and how the tests are organized forserving
andeventing
Creating your new CRDs and controllers is a simple process but involves creating different resources and knowing exactly how the code generation will work. This section will be showing all the files that are involved in creating new CRDs and their respective controllers code.
In contrast with other frameworks, Knative choose to not use code generation from the CRDs definitions as these resources doesn't change often and usually are defined just once.
This are the files that you need to create for your new CRD types: