Example Memcached operator based on Go operator tutorial https://sdk.operatorframework.io/docs/building-operators/golang/tutorial/
This project aims to follow the Kubernetes Operator pattern.
It uses Controllers, which provide a reconcile function responsible for synchronizing resources until the desired state is reached on the cluster.
You’ll need a Kubernetes cluster to run against. You can use KIND to get a local cluster for testing, or run against a remote cluster.
Note: Your controller will automatically use the current context in your kubeconfig file (i.e. whatever cluster kubectl cluster-info
shows).
kubectl create namespace operator
# using kubens
kubens operator
# using kubectl
kubectl config set-context --current --namespace=operator
NOTE: Make sure that your active is set to operator
# using kubens
$ kubens -c
operator
# using kubectl
$ kubectl config view --minify | grep namespace
namespace: operator
make install
Verify if CRD is created:
$ kubectl get crd
NAME CREATED AT
memcacheds.cache.github.com 2023-08-18T11:28:05Z
This will run in the foreground, so switch to a new terminal if you want to leave it running:
make run
NOTE: You can also run this in one step by running: make install run
If you are editing the API definitions, generate the manifests such as CRs or CRDs using:
make manifests
NOTE: Run make --help
for more information on all potential make
targets
More information can be found via the Kubebuilder Documentation
kubectl apply -f config/samples/cache_v1alpha1_memcached.yaml
Check operator logs
2023-08-18T14:52:34+02:00 INFO Creating a new Deployment {"controller": "memcached", "controllerGroup": "cache.github.com", "controllerKind": "Memcached", "Memcached": {"name":"memcached-sample","namespace":"operator"}, "namespace": "operator", "name": "memcached-sample", "reconcileID": "20046466-8ac8-4488-a0ca-1b4e2f99f7a8", "Deployment.Namespace": "operator", "Deployment.Name": "memcached-sample"}
- Delete memcached object:
$ kubectl delete -f config/samples/cache_v1alpha1_memcached.yaml
memcached.cache.github.com "memcached-sample" deleted
-
Stop the controller application
ctrl+C
-
Delete crd
kubectl delete crd memcacheds.cache.github.com
- Install l Instances of Custom Resources:
kubectl apply -f config/samples/
- Build and push your image to the location specified by
IMG
:
make docker-build docker-push IMG=<some-registry>/go-operator-tutorial:tag
- Deploy the controller to the cluster with the image specified by
IMG
:
make deploy IMG=<some-registry>/go-operator-tutorial:tag
To delete the CRDs from the cluster:
make uninstall
UnDeploy the controller from the cluster:
make undeploy
make run
...
2023-08-18T14:41:50+02:00 ERROR Reconciler error {"controller": "memcached", "controllerGroup": "cache.github.com", "controllerKind": "Memcached", "Memcached": {"name":"memcached-sample","namespace":"operator"}, "namespace": "operator", "name": "memcached-sample", "reconcileID": "c1fa0f69-4b37-42ae-a887-6e17e045c873", "error": "Unable to find MEMCACHED_IMAGE environment variable with the image"}
You have to provide image for deployment using MEMCACHED_IMAGE variable.
F.ex.
make MEMCACHED_IMAGE="memcached:1.4.36-alpine" run
When the following command got stuck:
$ kubectl delete -f config/samples/cache_v1alpha1_memcached.yaml
Edit CR:
$ kubectl edit memcached memcached-sample
Remove sections:
- deletionTimestamp:
- finalizers:
Run delete command again:
$ kubectl delete -f config/samples/cache_v1alpha1_memcached.yaml