Skip to content

platform-mesh/example-mongodb-multiclusterruntime

Multiclusterruntime Example for MongoDB

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
Loading

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
Loading

How to use this example

  1. 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
  2. Start kcp locally

    kcp start --bind-address=127.0.0.1
  3. Create the consumer and mongodb workspace

    export KUBECONFIG=".kcp/admin.kubeconfig"
    kubectl create workspace consumer
    kubectl create workspace mongodb
  4. 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
  5. 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}')
  6. Start the controller

    go run main.go --kcp-kubeconfig=kcp.kubeconfig --target-kubeconfig=cluster.kubeconfig
  7. 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
  8. 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.

Support, Feedback, Contributing

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.

Security / Disclosure

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.

Code of Conduct

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.

Licensing

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.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors 2

  •  
  •