Example operator
- Docker & Kubernetes course
- Expert
- Lesson 17/47
- 4 min
Learn a simple operator scenario.
An example operator
The complete flow of the backup operator: the user writes a Yedek; the operator produces a CronJob from it (with an owner reference); it writes the CronJob's last successful run into the status; when the Yedek is deleted, the CronJob goes too through the garbage collector. The operator itself runs as a Deployment with its own ServiceAccount and RBAC permissions:
make docker-build docker-push IMG=ghcr.io/ali/yedek-operator:0.1.0
make deploy IMG=ghcr.io/ali/yedek-operator:0.1.0
kubectl get deploy -n yedek-operator-system
kubectl logs -n yedek-operator-system deploy/yedek-operator-controller-manager -fRBAC markers
The markers in the controller file produce the operator's ClusterRole; the principle of least privilege applies here too:
// +kubebuilder:rbac:groups=yedek.ornek.com,resources=yedekler,verbs=get;list;watch;update;patch
// +kubebuilder:rbac:groups=yedek.ornek.com,resources=yedekler/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=batch,resources=cronjobs,verbs=get;list;watch;create;update;patch;deletemake manifests writes these into config/rbac/role.yaml.
Practice Release the operator
Write four commands: make docker-build docker-push with IMG=ghcr.io/ali/yedek-operator:0.1.0; make deploy with the same IMG; list the deployments in the yedek-operator-system namespace; follow the logs of deploy/yedek-operator-controller-manager in that namespace live.
Starter code
# buraya yazSample solution
make docker-build docker-push IMG=ghcr.io/ali/yedek-operator:0.1.0
make deploy IMG=ghcr.io/ali/yedek-operator:0.1.0
kubectl get deploy -n yedek-operator-system
kubectl logs -n yedek-operator-system deploy/yedek-operator-controller-manager -fExpected output
=> naming to ghcr.io/ali/yedek-operator:0.1.0 done
deployment.apps/yedek-operator-controller-manager created
NAME READY UP-TO-DATE AVAILABLE
yedek-operator-controller-manager 1/1 1 1
INFO Starting workers {"controller": "yedek", "worker count": 1}
INFO CronJob oluşturuldu {"Yedek": "magaza/magaza-gece"}
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does the operator need to be able to create CronJobs?
- RBAC granting its ServiceAccount create/update on cronjobs in the batch group Correct answer
- Always cluster-admin
- No permissions
- Node access
Explanation: In Kubebuilder, // +kubebuilder:rbac markers produce these permissions.
2 Deploy the operator to the cluster.
make ____ IMG=ghcr.io/ali/yedek-operator:0.1.0
make deploy IMG=ghcr.io/ali/yedek-operator:0.1.0
Explanation: install installs the CRD, deploy installs the operator itself.
3 Match.
| Term | Meaning |
|---|---|
make docker-build |
Operator image |
make deploy |
Install in the cluster |
+kubebuilder:rbac |
Permission markers |
controller-manager |
Operator pod |