(My) Educational example of how to create a Kubernetes operator/controller in
Rust using the Kube client.
This repo creates a controller for a CRD (Custom Resource Definition) called
Echo that will:
- create a
deploymentwith thehashicorp/http-echoimage, using the number ofreplicasandtext(as--text={text}) specified; - and create a
NodePort servicetoexposethisdeploymentexternally to the cluster. - adds a
finalizersto control theCRDdeletion.
Echo example:
apiVersion: test.com/v1
kind: Echo
metadata:
name: test-echo
namespace: default
spec:
replicas: 2
text: Hello worldThis example was deeply inspired by the projects:
- https://github.com/Pscheidl/rust-kubernetes-operator-example
- https://github.com/kube-rs/controller-rs/blob/main/src/controller.rs
and in the examples found in the kube repo.
Ps: this repository is educational only and does not necessarily reflect standards that should be used in production.
In this repository I am using the terms Kubernetes operator and Kubernetes
controller interchangeably - which is not conceptually correct - but simplifies
our discussion.
Technically, there is no difference between a typical controller and an operator. Often the difference referred to is the operational knowledge that is included in the operator. As a result, a controller which spins up a pod when a custom resource is created and the pod gets destroyed afterwards can be described as a simple controller. If the controller has additional operational knowledge like how to upgrade or remediate from errors, it is an operator. – https://github.com/cncf/tag-app-delivery/blob/eece8f7307f2970f46f100f51932db106db46968/operator-wg/whitepaper/Operator-WhitePaper_v1-0.md
- Install Rust
- Install kubectl
- Install some local (lightweight) Kubernetes distribution and create a local cluster. I’m using Kind.
- Run
kubectl apply -f echoes.test.com.yamlto create theEchocdrdefinition (). - Run
cargo runto start theEchocontroller. - Run
kubectl apply -f echo-test.yamlto create atest-echoEcho crdexample. - Run
kubectl get allto see thepods,deployment,servicesbeing created. - Run
curl <node-ip>:/<NodePort>to get thetextspecified insideecho-test.yaml. - Change the
test-echodefinition, like the number ofreplicas, then:- Run
kubectl apply -f echo-test.yaml,kubectl get allto watch the changes happening, andcurlagain.
- Run
- [ ] Add tests.
- [ ] Review/Add OwnerReference chain.