@@ -19,8 +19,10 @@ package main
19
19
import (
20
20
"context"
21
21
"errors"
22
+ "fmt"
22
23
"os"
23
24
25
+ "github.com/go-logr/logr"
24
26
flag "github.com/spf13/pflag"
25
27
"golang.org/x/sync/errgroup"
26
28
@@ -30,6 +32,7 @@ import (
30
32
"k8s.io/apimachinery/pkg/util/runtime"
31
33
"k8s.io/client-go/rest"
32
34
"k8s.io/klog/v2"
35
+ "k8s.io/utils/ptr"
33
36
34
37
ctrl "sigs.k8s.io/controller-runtime"
35
38
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -59,41 +62,45 @@ func main() {
59
62
kubeconfig := flag .String ("kubeconfig" , "" , "path to the kubeconfig file. If not given a test env is started." )
60
63
flag .Parse ()
61
64
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 {
62
72
var cfg * rest.Config
63
- if * kubeconfig == "" {
73
+ if kubeconfig == "" {
64
74
testEnv := & envtest.Environment {}
65
75
var err error
66
76
cfg , err = testEnv .Start ()
67
77
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 )
70
79
}
71
80
defer func () {
72
81
if testEnv == nil {
73
82
return
74
83
}
75
84
if err := testEnv .Stop (); err != nil {
76
- entryLog .Error (err , "failed to stop local environment" )
85
+ log .Error (err , "failed to stop local environment" )
77
86
os .Exit (1 )
78
87
}
79
88
}()
80
89
} else {
81
90
var err error
82
91
cfg , err = ctrl .GetConfig ()
83
92
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 )
86
94
}
87
95
}
88
96
89
97
// Test fixtures
90
98
cli , err := client .New (cfg , client.Options {})
91
99
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 )
94
101
}
95
102
96
- entryLog .Info ("Creating Namespace and ConfigMap objects" )
103
+ log .Info ("Creating Namespace and ConfigMap objects" )
97
104
runtime .Must (client .IgnoreAlreadyExists (cli .Create (ctx , & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "zoo" }})))
98
105
runtime .Must (client .IgnoreAlreadyExists (cli .Create (ctx , & corev1.ConfigMap {ObjectMeta : metav1.ObjectMeta {Namespace : "zoo" , Name : "elephant" }})))
99
106
runtime .Must (client .IgnoreAlreadyExists (cli .Create (ctx , & corev1.ConfigMap {ObjectMeta : metav1.ObjectMeta {Namespace : "zoo" , Name : "lion" }})))
@@ -104,20 +111,18 @@ func main() {
104
111
105
112
cl , err := cluster .New (cfg )
106
113
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 )
109
115
}
110
116
provider := namespace .New (cl )
111
117
112
118
// 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" )
114
120
mgr , err := mcmanager .New (cfg , provider , manager.Options {})
115
121
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 )
118
123
}
119
124
120
- mcbuilder .ControllerManagedBy (mgr ).
125
+ if err := mcbuilder .ControllerManagedBy (mgr ).
121
126
Named ("multicluster-configmaps" ).
122
127
For (& corev1.ConfigMap {}).
123
128
Complete (mcreconcile .Func (
@@ -142,7 +147,9 @@ func main() {
142
147
143
148
return ctrl.Result {}, nil
144
149
},
145
- ))
150
+ )); err != nil {
151
+ return fmt .Errorf ("unable to build controller: %w" , err )
152
+ }
146
153
147
154
// Starting everything.
148
155
g , ctx := errgroup .WithContext (ctx )
@@ -156,9 +163,10 @@ func main() {
156
163
return ignoreCanceled (mgr .Start (ctx ))
157
164
})
158
165
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 )
161
167
}
168
+
169
+ return nil
162
170
}
163
171
164
172
func ignoreCanceled (err error ) error {
0 commit comments