Docker & Kubernetes
Operator SDK
Learn development with the Operator SDK.
Operator SDK
Operator SDK and Kubebuilder, which it uses underneath, offer a skeleton, code generation and tools for writing operators in Go. Creating the project, adding an API (CRD + type) and a controller take a few commands:
operator-sdk init --domain ornek.com --repo github.com/ali/yedek-operator
operator-sdk create api --group yedek --version v1 --kind Yedek --resource --controller
make manifests # generates the CRD YAML from Go types
make install # installs the CRD into the cluster
make run # runs the controller locally
There are also Ansible- and Helm-based operator types; Go is the most flexible.
Type definitions and markers
The comment markers on Go structs turn into the CRD's validation rules and printer columns:
type YedekSpec struct {
// +kubebuilder:validation:MinLength=1
Veritabani string `json:"veritabani"`
Takvim string `json:"takvim"`
// +kubebuilder:validation:Minimum=1
// +kubebuilder:default=7
SaklamaGun int32 `json:"saklamaGun,omitempty"`
}
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Takvim",type=string,JSONPath=`.spec.takvim`
type Yedek struct { ... }
Exercise An operator project
Write four commands: initialize a project with --domain ornek.com --repo github.com/ali/yedek-operator; create an API with --group yedek --version v1 --kind Yedek --resource --controller; make manifests; make install.
Show example solution
operator-sdk init --domain ornek.com --repo github.com/ali/yedek-operator
operator-sdk create api --group yedek --version v1 --kind Yedek --resource --controller
make manifests
make install
Test yourself
-
What does make manifests produce?
- A Helm chart
- A test report
- CRD and RBAC YAMLs from the markers in the Go type definitions
- A Docker image
Show answer
CRD and RBAC YAMLs from the markers in the Go type definitions — Markers like // +kubebuilder:validation:Minimum=1 turn into validation rules.
-
Add a new API and controller to the project.
operator-sdk create ____ --group yedek --version v1 --kind Yedek --resource --controller
Show answer
operator-sdk create api --group yedek --version v1 --kind Yedek --resource --controller
-
Match.
- operator-sdk init
- Project skeleton
- create api
- Type and controller
- make manifests
- Generate CRD YAML
- make run
- Run locally
Next lesson: Custom resource management