Skip to content

Commit a5da96b

Browse files
committed
examples: simplify and unify
Signed-off-by: Dr. Stefan Schimanski <stefan.schimanski@gmail.com>
1 parent 4056545 commit a5da96b

File tree

4 files changed

+58
-84
lines changed

4 files changed

+58
-84
lines changed

examples/cluster-api/main.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ import (
2727
"k8s.io/apimachinery/pkg/util/runtime"
2828
"k8s.io/client-go/kubernetes/scheme"
2929
capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
30+
3031
ctrl "sigs.k8s.io/controller-runtime"
3132
"sigs.k8s.io/controller-runtime/pkg/client"
32-
"sigs.k8s.io/controller-runtime/pkg/log"
33+
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
3334
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3435
"sigs.k8s.io/controller-runtime/pkg/manager"
3536
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
@@ -47,10 +48,9 @@ func init() {
4748
}
4849

4950
func main() {
50-
log.SetLogger(zap.New(zap.UseDevMode(true)))
51-
51+
ctrllog.SetLogger(zap.New(zap.UseDevMode(true)))
52+
entryLog := ctrllog.Log.WithName("entrypoint")
5253
ctx := signals.SetupSignalHandler()
53-
entryLog := log.Log.WithName("entrypoint")
5454

5555
// Start local manager to read the Cluster-API objects.
5656
cfg, err := ctrl.GetConfig()
@@ -97,7 +97,7 @@ func main() {
9797
For(&corev1.ConfigMap{}).
9898
Complete(mcreconcile.Func(
9999
func(ctx context.Context, req mcreconcile.Request) (ctrl.Result, error) {
100-
log := log.FromContext(ctx).WithValues("cluster", req.ClusterName)
100+
log := ctrllog.FromContext(ctx).WithValues("cluster", req.ClusterName)
101101
log.Info("Reconciling ConfigMap")
102102

103103
cl, err := mcMgr.GetCluster(ctx, req.ClusterName)
@@ -113,7 +113,7 @@ func main() {
113113
return reconcile.Result{}, err
114114
}
115115

116-
log.Info("Found ConfigMap", "uid", cm.UID)
116+
log.Info("ConfigMap %s/%s in cluster %q", cm.Namespace, cm.Name, req.ClusterName)
117117

118118
return ctrl.Result{}, nil
119119
},
@@ -134,7 +134,7 @@ func main() {
134134
return ignoreCanceled(mcMgr.Start(ctx))
135135
})
136136
if err := g.Wait(); err != nil {
137-
entryLog.Error(err, "unable to run manager")
137+
entryLog.Error(err, "unable to start")
138138
os.Exit(1)
139139
}
140140
}

examples/kind/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ replace github.com/multicluster-runtime/multicluster-runtime/providers/kind => .
99
require (
1010
github.com/multicluster-runtime/multicluster-runtime v0.20.0-alpha.3
1111
github.com/multicluster-runtime/multicluster-runtime/providers/kind v0.20.0-alpha.3
12+
golang.org/x/sync v0.8.0
1213
k8s.io/api v0.32.1
1314
k8s.io/apimachinery v0.32.1
1415
sigs.k8s.io/controller-runtime v0.20.1
@@ -58,7 +59,6 @@ require (
5859
go.uber.org/zap v1.27.0 // indirect
5960
golang.org/x/net v0.30.0 // indirect
6061
golang.org/x/oauth2 v0.23.0 // indirect
61-
golang.org/x/sync v0.8.0 // indirect
6262
golang.org/x/sys v0.26.0 // indirect
6363
golang.org/x/term v0.25.0 // indirect
6464
golang.org/x/text v0.19.0 // indirect

examples/kind/main.go

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ package main
1818

1919
import (
2020
"context"
21+
"errors"
2122
"os"
2223

24+
"golang.org/x/sync/errgroup"
2325
corev1 "k8s.io/api/core/v1"
2426
apierrors "k8s.io/apimachinery/pkg/api/errors"
25-
ctrl "sigs.k8s.io/controller-runtime"
26-
"sigs.k8s.io/controller-runtime/pkg/envtest"
27-
"sigs.k8s.io/controller-runtime/pkg/log"
2827
"sigs.k8s.io/controller-runtime/pkg/log/zap"
28+
29+
ctrl "sigs.k8s.io/controller-runtime"
30+
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
2931
"sigs.k8s.io/controller-runtime/pkg/manager"
3032
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
3133
"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -37,42 +39,23 @@ import (
3739
)
3840

3941
func main() {
40-
log.SetLogger(zap.New(zap.UseDevMode(true)))
41-
42+
ctrllog.SetLogger(zap.New(zap.UseDevMode(true)))
43+
entryLog := ctrllog.Log.WithName("entrypoint")
4244
ctx := signals.SetupSignalHandler()
43-
entryLog := log.Log.WithName("entrypoint")
4445

45-
testEnv := &envtest.Environment{}
46-
cfg, err := testEnv.Start()
47-
if err != nil {
48-
entryLog.Error(err, "failed to start local environment")
49-
os.Exit(1)
50-
}
51-
defer func() {
52-
if testEnv == nil {
53-
return
54-
}
55-
if err := testEnv.Stop(); err != nil {
56-
entryLog.Error(err, "failed to stop local environment")
57-
os.Exit(1)
58-
}
59-
}()
60-
61-
// Setup a Manager, note that this not yet engages clusters, only makes them available.
62-
entryLog.Info("Setting up manager")
6346
provider := kind.New()
64-
mgr, err := mcmanager.New(cfg, provider, manager.Options{})
47+
mgr, err := mcmanager.New(ctrl.GetConfigOrDie(), provider, manager.Options{})
6548
if err != nil {
66-
entryLog.Error(err, "unable to set up overall controller manager")
67-
os.Exit(1) //nolint:gocritic // We want to return an error RC.
49+
entryLog.Error(err, "unable to create manager")
50+
os.Exit(1)
6851
}
6952

70-
if err := mcbuilder.ControllerManagedBy(mgr).
53+
err = mcbuilder.ControllerManagedBy(mgr).
7154
Named("multicluster-configmaps").
7255
For(&corev1.ConfigMap{}).
7356
Complete(mcreconcile.Func(
7457
func(ctx context.Context, req mcreconcile.Request) (ctrl.Result, error) {
75-
log := log.FromContext(ctx).WithValues("cluster", req.ClusterName)
58+
log := ctrllog.FromContext(ctx).WithValues("cluster", req.ClusterName)
7659
log.Info("Reconciling ConfigMap")
7760

7861
cl, err := mgr.GetCluster(ctx, req.ClusterName)
@@ -88,26 +71,33 @@ func main() {
8871
return reconcile.Result{}, err
8972
}
9073

91-
log.Info("Found ConfigMap", "uid", cm.UID)
74+
log.Info("ConfigMap %s/%s in cluster %q", cm.Namespace, cm.Name, req.ClusterName)
9275

9376
return ctrl.Result{}, nil
9477
},
95-
)); err != nil {
96-
entryLog.Error(err, "failed to build controller")
78+
))
79+
if err != nil {
80+
entryLog.Error(err, "unable to create controller")
9781
os.Exit(1)
9882
}
9983

100-
entryLog.Info("Starting provider")
101-
go func() {
102-
if err := provider.Run(ctx, mgr); err != nil {
103-
entryLog.Error(err, "unable to run provider")
104-
os.Exit(1)
105-
}
106-
}()
107-
108-
entryLog.Info("Starting manager")
109-
if err := mgr.Start(ctx); err != nil {
110-
entryLog.Error(err, "unable to run manager")
84+
// Starting everything.
85+
g, ctx := errgroup.WithContext(ctx)
86+
g.Go(func() error {
87+
return ignoreCanceled(provider.Run(ctx, mgr))
88+
})
89+
g.Go(func() error {
90+
return ignoreCanceled(mgr.Start(ctx))
91+
})
92+
if err := g.Wait(); err != nil {
93+
entryLog.Error(err, "unable to start")
11194
os.Exit(1)
11295
}
11396
}
97+
98+
func ignoreCanceled(err error) error {
99+
if errors.Is(err, context.Canceled) {
100+
return nil
101+
}
102+
return err
103+
}

examples/namespace/main.go

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package main
1919
import (
2020
"context"
2121
"errors"
22-
"fmt"
2322
"os"
2423

2524
flag "github.com/spf13/pflag"
@@ -37,7 +36,7 @@ import (
3736
"sigs.k8s.io/controller-runtime/pkg/client"
3837
"sigs.k8s.io/controller-runtime/pkg/cluster"
3938
"sigs.k8s.io/controller-runtime/pkg/envtest"
40-
"sigs.k8s.io/controller-runtime/pkg/log"
39+
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
4140
"sigs.k8s.io/controller-runtime/pkg/log/zap"
4241
"sigs.k8s.io/controller-runtime/pkg/manager"
4342
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
@@ -54,8 +53,8 @@ func init() {
5453
}
5554

5655
func main() {
57-
log.SetLogger(zap.New(zap.UseDevMode(true)))
58-
entryLog := log.Log.WithName("entrypoint")
56+
ctrllog.SetLogger(zap.New(zap.UseDevMode(true)))
57+
entryLog := ctrllog.Log.WithName("entrypoint")
5958
ctx := signals.SetupSignalHandler()
6059

6160
kubeconfig := flag.String("kubeconfig", "", "path to the kubeconfig file. If not given a test env is started.")
@@ -104,12 +103,7 @@ func main() {
104103
runtime.Must(client.IgnoreAlreadyExists(cli.Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "island"}})))
105104
runtime.Must(client.IgnoreAlreadyExists(cli.Create(ctx, &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Namespace: "island", Name: "bird"}})))
106105

107-
entryLog.Info("Setting up provider")
108106
cl, err := cluster.New(cfg, namespace.WithClusterNameIndex())
109-
if err != nil {
110-
entryLog.Error(err, "unable to set up provider")
111-
os.Exit(1)
112-
}
113107
provider := namespace.New(cl)
114108

115109
// Setup a cluster-aware Manager, with the provider to lookup clusters.
@@ -125,12 +119,12 @@ func main() {
125119
os.Exit(1)
126120
}
127121

128-
if err := mcbuilder.ControllerManagedBy(mgr).
122+
mcbuilder.ControllerManagedBy(mgr).
129123
Named("multicluster-configmaps").
130124
For(&corev1.ConfigMap{}).
131125
Complete(mcreconcile.Func(
132126
func(ctx context.Context, req mcreconcile.Request) (ctrl.Result, error) {
133-
log := log.FromContext(ctx).WithValues("cluster", req.ClusterName)
127+
log := ctrllog.FromContext(ctx).WithValues("cluster", req.ClusterName)
134128
log.Info("Reconciling ConfigMap")
135129

136130
cl, err := mgr.GetCluster(ctx, req.ClusterName)
@@ -146,35 +140,25 @@ func main() {
146140
return reconcile.Result{}, err
147141
}
148142

149-
log.Info("Found ConfigMap", "uid", cm.UID)
143+
log.Info("ConfigMap %s/%s in cluster %q", cm.Namespace, cm.Name, req.ClusterName)
150144

151145
return ctrl.Result{}, nil
152146
},
153-
)); err != nil {
154-
entryLog.Error(err, "unable to set up controller")
155-
os.Exit(1)
156-
}
157-
158-
entryLog.Info("Starting provider")
159-
go func() {
160-
if err := ignoreCanceled(provider.Run(ctx, mgr)); err != nil {
161-
entryLog.Error(err, "failed to start provider")
162-
os.Exit(1)
163-
}
164-
}()
147+
))
165148

166-
entryLog.Info("Starting cluster")
149+
// Starting everything.
167150
g, ctx := errgroup.WithContext(ctx)
168151
g.Go(func() error {
169-
if err := ignoreCanceled(cl.Start(ctx)); err != nil {
170-
return fmt.Errorf("failed to start cluster backing provider: %w", err)
171-
}
172-
return nil
152+
return ignoreCanceled(provider.Run(ctx, mgr))
173153
})
174-
175-
entryLog.Info("Starting cluster-aware manager")
176-
if err := ignoreCanceled(mgr.Start(ctx)); err != nil {
177-
entryLog.Error(err, "unable to run manager")
154+
g.Go(func() error {
155+
return ignoreCanceled(cl.Start(ctx))
156+
})
157+
g.Go(func() error {
158+
return ignoreCanceled(mgr.Start(ctx))
159+
})
160+
if err := g.Wait(); err != nil {
161+
entryLog.Error(err, "unable to start")
178162
os.Exit(1)
179163
}
180164
}

0 commit comments

Comments
 (0)