controller-runtime-example
An example project that is multi-cluster aware and works with kcp
Description
In this example, we intentionally not using advanced kubebuilder patterns to keep the example simple and easy to understand.
In the future, we will add more advanced examples. Example covers 3 parts:
- KCP bootstrapping - creating APIExport & consumer workspaces
- Creating WorkspaceType for particular exports
- Running controller with APIExport aware configuration and reconciling multiple consumer workspaces
This example contains an example project that works with APIExports and multiple kcp workspaces. It demonstrates
two reconcilers:
-
ConfigMap
- Get a ConfigMap for the key from the queue, from the correct logical cluster
- If the ConfigMap has labels["name"], set labels["response"] = "hello-$name" and save the changes
- List all ConfigMaps in the logical cluster and log each one's namespace and name
- If the ConfigMap from step 1 has data["namespace"] set, create a namespace whose name is the data value.
- If the ConfigMap from step 1 has data["secretData"] set, create a secret in the same namespace as the ConfigMap,
with an owner reference to the ConfigMap, and data["dataFromCM"] set to the data value.
-
Widget
- Show how to list all Widget instances across all logical clusters
- Get a Widget for the key from the queue, from the correct logical cluster
- List all Widgets in the same logical cluster
- Count the number of Widgets (list length)
- Make sure
.status.total
matches the current count (via a patch
)
Getting Started
Running on kcp
- Run KCP with the following command:
make kcp-server
From this point onwards you can inspect kcp configuration using kubeconfig:
export KUBECONFIG=.test/kcp.kubeconfig
- Bootstrap the KCP server with the following command:
export KUBECONFIG=./.test/kcp.kubeconfig
make kcp-bootstrap
- Run controller:
export KUBECONFIG=./.test/kcp.kubeconfig
make run-local
- In separate shell you can run tests to exercise the controller:
export KUBECONFIG=./.test/kcp.kubeconfig
make test
Uninstall resources
To delete the resources from kcp:
make test-clean
How it works
This project aims to follow the Kubernetes Operator pattern
It uses Controllers
which provides a reconcile function responsible for synchronizing resources until the desired state is reached.