Skip to content

Commit 3014822

Browse files
committed
Rework example to not skip defer run on os.Exit(1) in main func
On-behalf-of: SAP <[email protected]> Signed-off-by: Marvin Beckers <[email protected]>
1 parent 7fa6bef commit 3014822

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

examples/namespace/main.go

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

25+
"github.com/go-logr/logr"
2426
flag "github.com/spf13/pflag"
2527
"golang.org/x/sync/errgroup"
2628

@@ -30,6 +32,7 @@ import (
3032
"k8s.io/apimachinery/pkg/util/runtime"
3133
"k8s.io/client-go/rest"
3234
"k8s.io/klog/v2"
35+
"k8s.io/utils/ptr"
3336

3437
ctrl "sigs.k8s.io/controller-runtime"
3538
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -59,41 +62,45 @@ func main() {
5962
kubeconfig := flag.String("kubeconfig", "", "path to the kubeconfig file. If not given a test env is started.")
6063
flag.Parse()
6164

65+
if err := run(ctx, entryLog, ptr.Deref(kubeconfig, "")); err != nil {
66+
entryLog.Error(err, "failed to run controller")
67+
os.Exit(1)
68+
}
69+
}
70+
71+
func run(ctx context.Context, log logr.Logger, kubeconfig string) error {
6272
var cfg *rest.Config
63-
if *kubeconfig == "" {
73+
if kubeconfig == "" {
6474
testEnv := &envtest.Environment{}
6575
var err error
6676
cfg, err = testEnv.Start()
6777
if err != nil {
68-
entryLog.Error(err, "failed to start local environment")
69-
os.Exit(1)
78+
return fmt.Errorf("failed to start local environment: %w", err)
7079
}
7180
defer func() {
7281
if testEnv == nil {
7382
return
7483
}
7584
if err := testEnv.Stop(); err != nil {
76-
entryLog.Error(err, "failed to stop local environment")
85+
log.Error(err, "failed to stop local environment")
7786
os.Exit(1)
7887
}
7988
}()
8089
} else {
8190
var err error
8291
cfg, err = ctrl.GetConfig()
8392
if err != nil {
84-
entryLog.Error(err, "failed to get kubeconfig")
85-
os.Exit(1)
93+
return fmt.Errorf("failed to get kubeconfig: %w", err)
8694
}
8795
}
8896

8997
// Test fixtures
9098
cli, err := client.New(cfg, client.Options{})
9199
if err != nil {
92-
entryLog.Error(err, "failed to create client")
93-
os.Exit(1)
100+
return fmt.Errorf("failed to create client: %w", err)
94101
}
95102

96-
entryLog.Info("Creating Namespace and ConfigMap objects")
103+
log.Info("Creating Namespace and ConfigMap objects")
97104
runtime.Must(client.IgnoreAlreadyExists(cli.Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "zoo"}})))
98105
runtime.Must(client.IgnoreAlreadyExists(cli.Create(ctx, &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Namespace: "zoo", Name: "elephant"}})))
99106
runtime.Must(client.IgnoreAlreadyExists(cli.Create(ctx, &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Namespace: "zoo", Name: "lion"}})))
@@ -104,20 +111,18 @@ func main() {
104111

105112
cl, err := cluster.New(cfg)
106113
if err != nil {
107-
entryLog.Error(err, "failed to create cluster")
108-
os.Exit(1)
114+
return fmt.Errorf("failed to create cluster: %w", err)
109115
}
110116
provider := namespace.New(cl)
111117

112118
// Setup a cluster-aware Manager, with the provider to lookup clusters.
113-
entryLog.Info("Setting up cluster-aware manager")
119+
log.Info("Setting up cluster-aware manager")
114120
mgr, err := mcmanager.New(cfg, provider, manager.Options{})
115121
if err != nil {
116-
entryLog.Error(err, "unable to set up overall controller manager")
117-
os.Exit(1)
122+
return fmt.Errorf("unable to set up overall controller manager: %w", err)
118123
}
119124

120-
mcbuilder.ControllerManagedBy(mgr).
125+
if err := mcbuilder.ControllerManagedBy(mgr).
121126
Named("multicluster-configmaps").
122127
For(&corev1.ConfigMap{}).
123128
Complete(mcreconcile.Func(
@@ -142,7 +147,9 @@ func main() {
142147

143148
return ctrl.Result{}, nil
144149
},
145-
))
150+
)); err != nil {
151+
return fmt.Errorf("unable to build controller: %w", err)
152+
}
146153

147154
// Starting everything.
148155
g, ctx := errgroup.WithContext(ctx)
@@ -156,9 +163,10 @@ func main() {
156163
return ignoreCanceled(mgr.Start(ctx))
157164
})
158165
if err := g.Wait(); err != nil {
159-
entryLog.Error(err, "unable to start")
160-
os.Exit(1)
166+
return fmt.Errorf("unable to start: %w", err)
161167
}
168+
169+
return nil
162170
}
163171

164172
func ignoreCanceled(err error) error {

0 commit comments

Comments
 (0)