Skip to content

Commit 4056545

Browse files
authored
Merge pull request kubernetes-sigs#11 from multicluster-runtime/sttts-readme-example
📖 README.md: add example
2 parents 1189489 + ef23c72 commit 4056545

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,72 @@ Run reconcilers that listen to some cluster(s) and operate other clusters.
4141
5. multicluster-runtime could be a testbed for native controller-runtime functionality, eventually becoming superfluous.
4242
6. multicluster-runtime is provider agnostic, but may contain providers with its own go.mod files and dedicated OWNERS files.
4343

44+
## How it looks?
45+
46+
```golang
47+
package main
48+
49+
import (
50+
"context"
51+
"log"
52+
53+
corev1 "k8s.io/api/core/v1"
54+
apierrors "k8s.io/apimachinery/pkg/api/errors"
55+
56+
ctrl "sigs.k8s.io/controller-runtime"
57+
"sigs.k8s.io/controller-runtime/pkg/manager"
58+
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
59+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
60+
61+
mcbuilder "github.com/multicluster-runtime/multicluster-runtime/pkg/builder"
62+
mcmanager "github.com/multicluster-runtime/multicluster-runtime/pkg/manager"
63+
mcreconcile "github.com/multicluster-runtime/multicluster-runtime/pkg/reconcile"
64+
"github.com/multicluster-runtime/multicluster-runtime/providers/kind"
65+
)
66+
67+
func main() {
68+
ctx := signals.SetupSignalHandler()
69+
70+
provider := kind.New()
71+
mgr, err := mcmanager.New(ctrl.GetConfigOrDie(), provider, manager.Options{})
72+
if err != nil {
73+
log.Fatal(err, "unable to create manager")
74+
}
75+
76+
err = mcbuilder.ControllerManagedBy(mgr).
77+
Named("multicluster-configmaps").
78+
For(&corev1.ConfigMap{}).
79+
Complete(mcreconcile.Func(
80+
func(ctx context.Context, req mcreconcile.Request) (ctrl.Result, error) {
81+
cl, err := mgr.GetCluster(ctx, req.ClusterName)
82+
if err != nil {
83+
return reconcile.Result{}, err
84+
}
85+
86+
cm := &corev1.ConfigMap{}
87+
if err := cl.GetClient().Get(ctx, req.Request.NamespacedName, cm); err != nil {
88+
if apierrors.IsNotFound(err) {
89+
return reconcile.Result{}, nil
90+
}
91+
return reconcile.Result{}, err
92+
}
93+
94+
log.Printf("ConfigMap %s/%s in cluster %q", cm.Namespace, cm.Name, req.ClusterName)
95+
96+
return ctrl.Result{}, nil
97+
},
98+
))
99+
if err != nil {
100+
log.Fatal(err, "unable to create controller")
101+
}
102+
103+
go provider.Run(ctx, mgr)
104+
if err := mgr.Start(ctx); err != nil {
105+
log.Fatal(err, "unable to run manager")
106+
}
107+
}
108+
```
109+
44110
## FAQ
45111

46112
### How is it different from https://github.com/admiraltyio/multicluster-controller ?

0 commit comments

Comments
 (0)