CodeBlanks Logo Icon CodeBlanks

Challenge - Operator

Write a structure explaining operator reconcile logic.

Challenge: a Yedek operator

A CRD on its own only stores data; an operator that does the work is needed. The operator will create a CronJob for each Yedek object and write the status of the last backup into status:

Yedek magaza-db  ──reconcile──►  CronJob magaza-db-yedek (0 2 * * *)
                ◄──status─────   sonYedek: 2026-11-03T02:00:00Z, durum: Basarili

You'll set up the skeleton with kubebuilder, then deploy the operator to the cluster and test the result.

Practice Set up the skeleton

Write three commands: kubebuilder init --domain sirket.com --repo github.com/sirket/yedek-operator; create an API for the Yedek kind in group platform, version v1 (--resource --controller); generate the CRD and RBAC files (make manifests).

Starter code

terminal
# buraya yaz

Sample solution

terminal
kubebuilder init --domain sirket.com --repo github.com/sirket/yedek-operator
kubebuilder create api --group platform --version v1 --kind Yedek --resource --controller
make manifests

Expected output

INFO Writing kustomize manifests for you to edit...
INFO Writing scaffold for you to edit...
INFO api/v1/yedek_types.go
INFO internal/controller/yedek_controller.go
/home/ali/yedek-operator/bin/controller-gen rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases

Practice Deploy and test

Write four commands: install the CRD into the cluster (make install); deploy the operator with the ghcr.io/sirket/yedek-operator:0.1.0 image (make deploy IMG=...); apply yedek.yaml; print the {.status.durum} field of the magaza-db object in magaza with jsonpath.

Starter code

terminal
# buraya yaz

Sample solution

terminal
make install
make deploy IMG=ghcr.io/sirket/yedek-operator:0.1.0
kubectl apply -f yedek.yaml
kubectl get yedek magaza-db -n magaza -o jsonpath='{.status.durum}'

Expected output

customresourcedefinition.apiextensions.k8s.io/yedekler.platform.sirket.com created
deployment.apps/yedek-operator-controller-manager created
yedek.platform.sirket.com/magaza-db created
Basarili

Questions and answers

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

1 What is the benefit of the operator adding an ownerReference to the CronJob it creates?

  • The CronJob runs faster
  • No RBAC is needed
  • Status fills automatically
  • When the Yedek object is deleted, the CronJob is removed by the garbage collector too Correct answer

Explanation: The ownership relation leaves cleanup to Kubernetes.

2 Add a new API type and its controller.

Question
kubebuilder create ____ --group platform --version v1 --kind Yedek
Answer
kubebuilder create api --group platform --version v1 --kind Yedek

Explanation: create api generates the type and controller skeleton.

3 Match.

Term Meaning
reconcile Bring to desired state
ownerReference Ownership
status Observed state
make manifests Generate CRD and RBAC

All lessons in Expert