CodeBlanks Logo Icon CodeBlanks

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 { ... }

Practice 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.

Starter code

terminal
# buraya yaz

Sample solution

terminal
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

Expected output

INFO[0000] Writing kustomize manifests for you to edit...
INFO[0002] Writing scaffold for you to edit...
/bin/controller-gen rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
customresourcedefinition.apiextensions.k8s.io/yedekler.yedek.ornek.com created

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does make manifests produce?

  • A Helm chart
  • A test report
  • CRD and RBAC YAMLs from the markers in the Go type definitions Correct answer
  • A Docker image

Explanation: Markers like // +kubebuilder:validation:Minimum=1 turn into validation rules.

2 Add a new API and controller to the project.

Question
operator-sdk create ____ --group yedek --version v1 --kind Yedek --resource --controller
Answer
operator-sdk create api --group yedek --version v1 --kind Yedek --resource --controller

Explanation: create api generates the type and controller skeleton.

3 Match.

Term Meaning
operator-sdk init Project skeleton
create api Type and controller
make manifests Generate CRD YAML
make run Run locally

All lessons in Expert