OPERATOR BLUEPRINT

This repo contains an example of a custom kubernetes operator, made with kubebuilder.
References
NOTE: Before delving into the programming of kubernetes operators, a thorough knowledge of the functioning of k8s is recommended.
Useful references:
Prerequisites
make
kubectl
docker
go
kubebuilder
Getting Started
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).
The Makefile already contains all the functions that you need.
to see all make targets run:
make help
Running on the cluster
- Install 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>/operator-blueprint:tag
- Deploy the controller to the cluster with the image specified by
IMG
:
make deploy IMG=<some-registry>/operator-blueprint:tag
Uninstall CRDs
To delete the CRDs from the cluster:
make uninstall
Undeploy controller
UnDeploy the controller from the cluster:
make undeploy
How it works
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.
This is a very simple and vanilla operator, basically it does nothing more than delete all the pods in the namespace specified in the CRD.
Let's take a look at the example manifest inside config/samples:
apiVersion: examples.stackzoo.io/v1alpha1
kind: PodBuster
metadata:
labels:
app.kubernetes.io/name: podbuster
app.kubernetes.io/instance: podbuster-sample
app.kubernetes.io/part-of: operator-blueprint
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/created-by: operator-blueprint
name: podbuster-sample
spec:
namespace: test
When you apply this manifest to the cluster (after deploying the operator) the custom controller will delete every pods inside the test namespace.
Test It Out
- Start a local kind cluster:
make kind-up
- Install the CRDs into the cluster:
make install
- Prepare resources on the cluster:
make prepare-resources
- Run your controller (this will run in the foreground, so switch to a new terminal if you want to leave it running):
make run
- Apply the CRD manifest:
kubectl apply -f config/samples/examples_v1alpha1_podbuster.yaml
- Check the operator logs in the first terminal:
2023-03-20T14:04:09+01:00 INFO Operator Blueprint {"controller": "podbuster", "controllerGroup": "examples.stackzoo.io", "controllerKind": "PodBuster", "PodBuster": {"name":"podbuster-sample","namespace":"default"}, "namespace": "default", "name": "podbuster-sample", "reconcileID": "3e93bdbf-1eed-47e1-92db-5ad9786f90a2", "Deleting pod": "busybox"}
2023-03-20T14:04:09+01:00 INFO Operator Blueprint {"controller": "podbuster", "controllerGroup": "examples.stackzoo.io", "controllerKind": "PodBuster", "PodBuster": {"name":"podbuster-sample","namespace":"default"}, "namespace": "default", "name": "podbuster-sample", "reconcileID": "3e93bdbf-1eed-47e1-92db-5ad9786f90a2", "Deleting pod": "nginx"}
- When you are done, stop the local kind cluster run:
make kind-down
Modifying the API definitions
If you are editing the API definitions, generate the manifests such as CRs or CRDs using:
make manifests
License
Copyright 2023 stackzoo.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.