The goal is to use the Operator SDK to write a small Kubernetes Custom Controller in Go and then deploy it on a Kubernetes cluster.
Example of Customer Resource:
apiVersion: interview.com/v1alpha1
kind: Dummy
metadata:
name: dummy1
namespace: default
spec:
message: "I'm just a dummy"
status:
specEcho: "I'm just a dummy"
podStatus: "Pending"
Where
- specEcho - copy the value of spec.message into status.specEcho by custom controller
- podStatus - track of the status of the Pod (Phase) associated to the Dummy by custom controller
At a high-level, this is the flow sequence of the operator's functionality:
- User creates a custom resource (CR) via kubectl command under a Kubernetes namespace.
- Operator is running on the cluster under the operator's namespace and it watches for these specific custom resources (CR) object.
- Operator takes action: create or delete Pods (not scaled).
You’ll need a Kubernetes cluster to run against. You can use KIND or minikube 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).
Install all the following:
- git client - Apple Xcode or any git command line
- Go (v1.19+) - https://golang.org/dl/
- Docker Desktop - https://www.docker.com/products/docker-desktop
- Kind (Kubernetes in Docker) - https://kind.sigs.K8s.io/docs/user/quick-start/
- Minikube - https://minikube.sigs.k8s.io/docs/start/
- Operator SDK - https://sdk.operatorframework.io/docs/installation/
- Kubebuilder - https://go.kubebuilder.io/quick-start.html
- Kustomize - https://kubectl.docs.kubernetes.io/installation/
- [Optional but recommended] - Code editor such as VScode or goland
- Getting image from hub.docker.com
docker pull savelievant/dummy-operator
or dowload sources from current repo github.com
git clone https://github.com/antonmisa/dummy-operator
- To start a minikube cluster on your local machine, run the following command, setting as an arbitrarily name for your cluster (this name will be used for kubectl context):
minikube start
Or using kind cluster on your local machine, run the following command, setting as an arbitrarily name for your cluster (this name will be used for kubectl context):
kind create cluster --name operator-dev
- Install Instances of Custom Resources:
kubectl apply -f config/samples/_v1alpha1_dummy2.yaml
- Build and push your image to the location specified by
IMG
:
make docker-build docker-push IMG=interview.com/dummy-operator:v0.0.1
- Deploy the controller to the cluster with the image specified by
IMG
:
make deploy IMG=interview.com/dummy-operator:v0.0.1
To delete the CRDs from the cluster:
make uninstall
UnDeploy the controller from the cluster:
make undeploy
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.
- Run unit tests
make test
- Run e2e tests:
make test-e2e
- Install the CRDs into the cluster:
make install
- Run your controller (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