Thursday, October 29, 2020

Tutorial: Custom Resource Definition, Controller and Webhook from Scratch with Kubebuilder

Here is a tutorial that I wrote on how to create from scratch using kubebuilder a simple CRD with a controller with webhooks.

Gotta say that I had to jump multiple hoops but I documented the entire step-by-step.

This will be possibly a series of tutorials because I need to create multiple versions of the same API Group to support the work I am contributing to Project Velero.

Very well, in a nutshell, the tutorial creates the following:

RockBand CRD:
$ kubectl get crd rockbands.music.example.io NAME CREATED AT rockbands.music.example.io 2020-10-28T19:51:25Z 

Spec of the CRD:
type RockBandSpec struct {
	// +kubebuilder:validation:Optional
	Genre string `json:"genre"`
	// +kubebuilder:validation:Optional
	NumberComponents int32 `json:"numberComponents"`
	// +kubebuilder:validation:Optional
	LeadSinger string `json:"leadSinger"`
}
Webhook Mutator Logic:

- if LeadSinger is not specified, set it as "TBD". 
- if RockBand CR name is "beatles" and Lead Singer is "John", set it to "John Lennon" Webhook 

Validator Logic:

- We can't create RockBands CRs on "kube-system" namespace 
- We can't update Lead Singer as "John" if RockBand CR name is "beatles" 
- We can't update Lead Singer as "Ringo" if RockBand CR name is "beatles". 

Again, the link to the tutorial and code:

Enjoy!! And comments are appreciated!!

No comments:

kuard on OpenShift

A quick way to onboard the handy "Kubernetes Up and Running"  kuard application on OpenShift: curl https://raw.githubusercontent....