kubernetes-periodic-controller
An example of a periodic Kubernetes controller, bootstrapped with kubebuilder
.
Description
This repo contains an example of a periodic Kubernetes controller, which
reconciles objects on regular basis.
The example controller will reconcile Pod
s, which are annotated every 30
seconds by default.
Check out the Implementing a periodic Kubernetes
controller post for
more details about how this all started.
Usage
In order to quickly test the periodic controller in a local Kubernetes cluster
follow these steps.
Create a local kind cluster.
kind create cluster
Start the controller. The command below will start the controller on your
system.
make run
Create a sample pod to test things out.
---
apiVersion: v1
kind: Pod
metadata:
name: busybox
namespace: default
spec:
containers:
- name: busybox
image: busybox
command:
- sleep
- infinity
imagePullPolicy: IfNotPresent
restartPolicy: Always
Create the pod.
kubectl apply -f /path/to/sample-pod.yaml
Check that we have the pod up and running.
kubectl get pods
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 0 98s
And now, let's annotate our pod properly, so that it is considered by our
controller.
kubectl annotate pod busybox dnaeon.github.io/reconcile-me=true
If you watch the controller's logs you should see similar log entries.
2024-03-23T21:28:06+02:00 INFO reconcile {"controller": "pod_controller", "namespace": "default", "name": "busybox", "reconcileID": "fa88e341-bb90-448e-a206-9f0cb8e52778"}
2024-03-23T21:28:36+02:00 INFO reconcile {"controller": "pod_controller", "namespace": "default", "name": "busybox", "reconcileID": "1676b1fd-3d0e-4f43-8542-109b0ac88fd2"}
2024-03-23T21:29:06+02:00 INFO reconcile {"controller": "pod_controller", "namespace": "default", "name": "busybox", "reconcileID": "a2588b69-416c-435d-bab3-06f88f26843e"}
Getting Started
Prerequisites
- Go version v1.21.0
- Docker version 17.03+
- kubectl version v1.11.3+
- Access to a Kubernetes v1.11.3+ cluster
To Deploy on the cluster
Build and push your image to the location specified by IMG
:
make docker-build docker-push IMG=<some-registry>/kubernetes-periodic-controller:tag
NOTE: This image ought to be published in the personal registry you
specified. And it is required to have access to pull the image from the working
environment. Make sure you have the proper permission to the registry if the
above commands don’t work.
Install the CRDs into the cluster:
make install
Deploy the Manager to the cluster with the image specified by IMG
:
make deploy IMG=<some-registry>/kubernetes-periodic-controller:tag
NOTE: If you encounter RBAC errors, you may need to grant yourself cluster-admin
privileges or be logged in as admin.
To Uninstall
Delete the APIs(CRDs) from the cluster:
make uninstall
Uninstall the controller from the cluster:
make undeploy
Project Distribution
Following are the steps to build the installer and distribute this project to users.
- Build the installer for the image built and published in the registry:
make build-installer IMG=<some-registry>/kubernetes-periodic-controller:tag
NOTE: The makefile target mentioned above generates an 'install.yaml' file in
the dist directory. This file contains all the resources built with Kustomize,
which are necessary to install this project without its dependencies.
- Using the installer
Users can just run kubectl apply -f to install the project, i.e.:
kubectl apply -f https://raw.githubusercontent.com/<org>/kubernetes-periodic-controller/<tag or branch>/dist/install.yaml
Contributing
Please contribute any suggestions and/or improvements to the Github repo.
NOTE: Run make help
for more information on all potential make
targets
Also, make sure to check the Kubebuilder
Documentation.
License
Copyright 2024.
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.