In Kubernetes, we are talking a lot about StatefulSets. But what about SteakfulSets? Sounds delicious to you? Read on...
This repository implements a small and simple Kubernetes controller that reconciles on the custom resource SteakfulSet and creates Steaks - another custom resource - out of it. It is intended to give a you an idea about what it takes to implement your own Kubernetes operator - something you might come across doing at some point in time.
This repository is heavily based on Kubebuilder and the Kubebuilder book. In fact, Kubebuilder has been used to create the skeleton of this repository, to provide all the generators and to design the CRD layout.
The custom resources SteakfulSet and Steak are defined in Go source code within the files inside the api/v1alpha folder. Inside the Go source files, we use some Kubebuilder decorators to describe some spefic of the custom resources. Then, the following command will invoke Kubebuilder's generator to create the custom resource definition files:
make manifestsThe resulting CRDs can be found in the config/crd directory.
The controller consists of only two files: the controller is set up and initialized in main.go. A quick overview about what happens in here is given in chapter 1.7.1 of the Kubebuilder book - in fact, main.go only contains generated code.
The real fun happens in controllers/steakfulset_controller.go. In here, the reconcilation logic for SteakfulSets is implemented. Have a look at the comments of the Reconcile() method to get an idea what happens.
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).
To install the SteakfulSet controller into your cluster with the help of Helm, simply look into the charts directory. Install it with:
kubectl create ns steakfulset-controller
helm install -n steakfulset-controller bbq charts/steakfulset-controllerTo fry your first Steaks, just kubectl apply -f config/samples/food_v1alpha1_steakfulset.yaml.
-
Install the CRDs into the cluster:
make install
-
Build and push your image to the location specified by
IMG:make docker-build docker-push IMG=<some-registry>/steakfulset-controller:tag
Note: If you are building this on arm64 (read 🍎 Apple Silicon) and your Kubernetes cluster is running with amd64 machines, you need to set the environment variable
BUILDPLATFORMtolinux/amd64before running this command. -
Deploy the controller to the cluster with the image specified by
IMG:make deploy IMG=<some-registry>/steakfulset-controller:tag
To delete the CRDs from the cluster:
make uninstallUnDeploy the controller from the cluster:
make undeployMore information can be found via the Kubebuilder Documentation