Skip to content

Commit a6f123f

Browse files
committed
refactor: improve capi provider
1 parent 51b058b commit a6f123f

2 files changed

Lines changed: 209 additions & 148 deletions

File tree

examples/cluster-api/main.go

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ package main
1919
import (
2020
"context"
2121
"errors"
22+
"fmt"
2223
"os"
2324

24-
"golang.org/x/sync/errgroup"
2525
capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
2626

2727
corev1 "k8s.io/api/core/v1"
@@ -33,7 +33,6 @@ import (
3333
"sigs.k8s.io/controller-runtime/pkg/client"
3434
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
3535
"sigs.k8s.io/controller-runtime/pkg/log/zap"
36-
"sigs.k8s.io/controller-runtime/pkg/manager"
3736
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
3837
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3938
"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -53,42 +52,36 @@ func main() {
5352
entryLog := ctrllog.Log.WithName("entrypoint")
5453
ctx := signals.SetupSignalHandler()
5554

56-
// Start local manager to read the Cluster-API objects.
5755
cfg, err := ctrl.GetConfig()
5856
if err != nil {
5957
entryLog.Error(err, "unable to get kubeconfig")
6058
os.Exit(1)
6159
}
62-
localMgr, err := manager.New(cfg, manager.Options{
60+
61+
// Create the Cluster API provider.
62+
provider := capi.New(capi.Options{})
63+
64+
// Create a multi-cluster manager with the provider.
65+
entryLog.Info("Setting up multi-cluster manager")
66+
mcMgr, err := mcmanager.New(cfg, provider, mcmanager.Options{
6367
Client: client.Options{
6468
Cache: &client.CacheOptions{
6569
Unstructured: true,
6670
DisableFor: []client.Object{&corev1.Secret{}},
6771
},
6872
},
73+
Metrics: metricsserver.Options{
74+
BindAddress: "0",
75+
},
6976
})
7077
if err != nil {
71-
entryLog.Error(err, "unable to set up overall controller manager")
72-
os.Exit(1)
73-
}
74-
75-
// Create the provider against the local manager.
76-
provider, err := capi.New(localMgr, capi.Options{})
77-
if err != nil {
78-
entryLog.Error(err, "unable to create provider")
78+
entryLog.Error(err, "unable to set up multi-cluster manager")
7979
os.Exit(1)
8080
}
8181

82-
// Create a multi-cluster manager attached to the provider.
83-
entryLog.Info("Setting up local manager")
84-
mcMgr, err := mcmanager.New(cfg, provider, mcmanager.Options{
85-
LeaderElection: false, // TODO(sttts): how to sync that with the upper manager?
86-
Metrics: metricsserver.Options{
87-
BindAddress: "0", // only one can listen
88-
},
89-
})
90-
if err != nil {
91-
entryLog.Error(err, "unable to set up overall controller manager")
82+
// Set up the provider controller on the manager.
83+
if err := provider.SetupWithManager(mcMgr); err != nil {
84+
entryLog.Error(err, "unable to set up provider")
9285
os.Exit(1)
9386
}
9487

@@ -114,7 +107,7 @@ func main() {
114107
return reconcile.Result{}, err
115108
}
116109

117-
log.Info("ConfigMap %s/%s in cluster %q", cm.Namespace, cm.Name, req.ClusterName)
110+
log.Info(fmt.Sprintf("ConfigMap %s/%s in cluster %q", cm.Namespace, cm.Name, req.ClusterName))
118111

119112
return ctrl.Result{}, nil
120113
},
@@ -123,16 +116,9 @@ func main() {
123116
os.Exit(1)
124117
}
125118

126-
// Starting everything.
127-
g, ctx := errgroup.WithContext(ctx)
128-
g.Go(func() error {
129-
return ignoreCanceled(localMgr.Start(ctx))
130-
})
131-
g.Go(func() error {
132-
return ignoreCanceled(mcMgr.Start(ctx))
133-
})
134-
if err := g.Wait(); err != nil {
135-
entryLog.Error(err, "unable to start")
119+
// Start the manager.
120+
if err := ignoreCanceled(mcMgr.Start(ctx)); err != nil {
121+
entryLog.Error(err, "unable to start manager")
136122
os.Exit(1)
137123
}
138124
}

0 commit comments

Comments
 (0)