This example shows how to build a syncher which syncs objects from kcp workspaces into a separate Kubernetes cluster and afterwards syncs any status updates from the downstream object back into kcp. It includes a demo based on the MongoDB Community Operator.
It is a barebone example to show that you can build a custom syncher if api-syncagent is not sufficient for your use-cases. It should be noted that this example does not handle any object collisions or related resources. It is far from production ready!
Here is a high-level workflow for creation:
sequenceDiagram
participant M as k8s cluster with MongoDB Operator
participant O as This Operator
actor Y as You
participant CW as kcp Consumer Workspace
O ->> CW: watches
Y ->> CW: creates new MongoDB Object
CW ->> O: Syncs MongoDB object
O ->> M: Syncs MongoDB object
M ->> M: Creates Database and <br/> updates status of MongoDB object
M ->> O: Syncs back status
O ->> CW: Syncs back status
Y ->> CW: Retrieve new status
Here is a high-level workflow for deletion:
sequenceDiagram
participant M as k8s cluster with MongoDB Operator
participant O as This Operator
actor Y as You
participant CW as kcp Consumer Workspace
O ->> CW: watches
Y ->> CW: deletes their MongoDB
CW ->> O: Retrieves Info that object got deleted
O ->> M: Deletes Object
M ->> M: Deletes database
-
Create a kind cluster and install the MongoDB Operator
export KUBECONFIG=cluster.kubeconfig kind create cluster --name mongodb helm repo add mongodb https://mongodb.github.io/helm-charts helm repo update helm upgrade mongodb mongodb/community-operator --version 0.13.0 --install --namespace mongodb --create-namespace kubectl apply -f sample/mongo-secret.yaml -
Start kcp locally
kcp start --bind-address=127.0.0.1
-
Create the consumer and mongodb workspace
export KUBECONFIG=".kcp/admin.kubeconfig" kubectl create workspace consumer kubectl create workspace mongodb
-
Create the ResourceSchema, APIExport, APIBinding & MongoDB namespace
kubectl ws :root:mongodb kubectl apply -f sample/mongo-api.yaml kubectl ws :root:consumer kubectl apply -f sample/apibinding.yaml kubectl create namespace mongodb
-
Create the kcp kubeconfig for our controller
kubectl ws :root:mongodb kubectl config view --minify --flatten > kcp.kubeconfig # set the server to the VirtualWorkspace url kubectl --kubeconfig=kcp.kubeconfig config set-cluster "workspace.kcp.io/current" --server $(kubectl get apiexportendpointslices.apis.kcp.io mongodb -o jsonpath='{.status.endpoints[0].url}')
-
Start the controller
go run main.go --kcp-kubeconfig=kcp.kubeconfig --target-kubeconfig=cluster.kubeconfig
-
Create a MongoDB in the consumer workspace
export KUBECONFIG=".kcp/admin.kubeconfig" kubectl ws :root:consumer kubectl apply -f sample/mongodb.yaml
The syncer will sync the mongodb from kcp into the kubernetes cluster. After around a minute you should see in your Kubernetes cluster that the PHASE and VERSION field of your cluster are filled. The syncer will then sync this status into kcp. In the end, you should see the following in your kcp consumer workspace:
$ export KUBECONFIG=.kcp/admin.kubeconfig $ k ws :root:consumer $ k -n mongodb get mongodbcommunity.mongodbcommunity.mongodb.com example-mongodb NAME PHASE VERSION example-mongodb Running 6.0.5 -
Delete the object again
export KUBECONFIG=".kcp/admin.kubeconfig" kubectl ws :root:consumer kubectl delete -f sample/mongodb.yaml
You should now see that the object disappears both in kcp as well as in the downstream cluster.
This project is open to feature requests/suggestions, bug reports etc. via GitHub issues. Contribution and feedback are encouraged and always welcome. For more information about how to contribute, the project structure, as well as additional contribution information, see our Contribution Guidelines.
If you find any bug that may be a security problem, please follow our instructions at in our security policy on how to report it. Please do not create GitHub issues for security-related doubts or problems.
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone. By participating in this project, you agree to abide by its Code of Conduct at all times.
Copyright (20xx-)20xx SAP SE or an SAP affiliate company and example-mongodb-multiclusterruntime contributors. Please see our LICENSE for copyright and license information. Detailed information including third-party components and their licensing/copyright information is available via the REUSE tool.