@@ -3,6 +3,7 @@ package main
3
3
import (
4
4
"errors"
5
5
"fmt"
6
+ "net/url"
6
7
"os"
7
8
"runtime"
8
9
"strconv"
@@ -19,7 +20,6 @@ import (
19
20
dvo_prom "github.com/app-sre/deployment-validation-operator/pkg/prometheus"
20
21
"github.com/app-sre/deployment-validation-operator/pkg/validations"
21
22
"github.com/app-sre/deployment-validation-operator/version"
22
- "github.com/prometheus/client_golang/prometheus"
23
23
24
24
"github.com/go-logr/logr"
25
25
osappsv1 "github.com/openshift/api/apps/v1"
@@ -43,13 +43,18 @@ func main() {
43
43
os .Setenv (operatorNameEnvVar , dvconfig .OperatorName )
44
44
45
45
opts := options.Options {
46
- MetricsPort : 8383 ,
47
- MetricsPath : "metrics" ,
48
- ProbeAddr : ":8081" ,
49
- ConfigFile : "config/deployment-validation-operator-config.yaml" ,
46
+ MetricsBindAddr : ":8383" ,
47
+ MetricsPath : "metrics" ,
48
+ MetricsServiceName : "deployment-validation-operator-metrics" ,
49
+ ProbeAddr : ":8081" ,
50
+ ConfigFile : "config/deployment-validation-operator-config.yaml" ,
50
51
}
51
52
52
- opts .Process ()
53
+ if err := opts .Process (); err != nil {
54
+ fmt .Fprintf (os .Stdout , "processing options: %v\n " , err )
55
+
56
+ os .Exit (1 )
57
+ }
53
58
54
59
// Use a zap logr.Logger implementation. If none of the zap
55
60
// flags are configured (or if the zap flag set is not being
@@ -106,12 +111,8 @@ func setupManager(log logr.Logger, opts options.Options) (manager.Manager, error
106
111
return nil , fmt .Errorf ("initializing manager: %w" , err )
107
112
}
108
113
109
- if err := mgr .AddHealthzCheck ("health" , healthz .Ping ); err != nil {
110
- return nil , fmt .Errorf ("adding healthz check: %w" , err )
111
- }
112
-
113
- if err := mgr .AddReadyzCheck ("check" , healthz .Ping ); err != nil {
114
- return nil , fmt .Errorf ("adding readyz check: %w" , err )
114
+ if err := setupProbes (mgr , opts ); err != nil {
115
+ return nil , fmt .Errorf ("setting up probes: %w" , err )
115
116
}
116
117
117
118
log .Info ("Registering Components" )
@@ -126,29 +127,12 @@ func setupManager(log logr.Logger, opts options.Options) (manager.Manager, error
126
127
return nil , fmt .Errorf ("initializing generic reconciler: %w" , err )
127
128
}
128
129
129
- if err = gr .AddToManager (mgr ); err != nil {
130
+ if err : = gr .AddToManager (mgr ); err != nil {
130
131
return nil , fmt .Errorf ("adding generic reconciler to manager: %w" , err )
131
132
}
132
133
133
- log .Info ("Initializing Prometheus Registry" )
134
-
135
- reg := prometheus .NewRegistry ()
136
-
137
- log .Info (fmt .Sprintf ("Initializing Prometheus metrics endpoint on %q" , opts .MetricsEndpoint ()))
138
-
139
- srv , err := dvo_prom .NewServer (reg , opts .MetricsPath , fmt .Sprintf (":%d" , opts .MetricsPort ))
140
- if err != nil {
141
- return nil , fmt .Errorf ("initializing metrics server: %w" , err )
142
- }
143
-
144
- if err := mgr .Add (srv ); err != nil {
145
- return nil , fmt .Errorf ("adding metrics server to manager: %w" , err )
146
- }
147
-
148
- log .Info ("Initializing Validation Engine" )
149
-
150
- if err := validations .InitializeValidationEngine (opts .ConfigFile , reg ); err != nil {
151
- return nil , fmt .Errorf ("initializing validation engine: %w" , err )
134
+ if err := setupComponents (log , mgr , opts ); err != nil {
135
+ return nil , fmt .Errorf ("setting up components: %w" , err )
152
136
}
153
137
154
138
return mgr , nil
@@ -193,9 +177,13 @@ func getManagerOptions(scheme *k8sruntime.Scheme, opts options.Options) (manager
193
177
}
194
178
195
179
mgrOpts := manager.Options {
196
- Namespace : ns ,
197
- HealthProbeBindAddress : opts .ProbeAddr ,
198
- MetricsBindAddress : "0" , // disable controller-runtime managed prometheus endpoint
180
+ LeaderElection : opts .EnableLeaderElection ,
181
+ LeaderElectionID : "23h85e23.deployment-validation-operator-lock" ,
182
+ LeaderElectionNamespace : opts .LeaderElectionNamespace ,
183
+ LeaderElectionResourceLock : "leases" ,
184
+ Namespace : ns ,
185
+ HealthProbeBindAddress : opts .ProbeAddr ,
186
+ MetricsBindAddress : "0" , // disable controller-runtime managed prometheus endpoint
199
187
// disable caching of everything
200
188
NewClient : newClient ,
201
189
Scheme : scheme ,
@@ -237,3 +225,62 @@ func kubeClientQPS() (float32, error) {
237
225
qps = float32 (val )
238
226
return qps , err
239
227
}
228
+
229
+ func setupProbes (mgr manager.Manager , opts options.Options ) error {
230
+ if err := mgr .AddHealthzCheck ("health" , healthz .Ping ); err != nil {
231
+ return fmt .Errorf ("adding healthz check: %w" , err )
232
+ }
233
+
234
+ if err := mgr .AddReadyzCheck ("check" , healthz .Ping ); err != nil {
235
+ return fmt .Errorf ("adding readyz check: %w" , err )
236
+ }
237
+
238
+ return nil
239
+ }
240
+
241
+ func setupComponents (log logr.Logger , mgr manager.Manager , opts options.Options ) error {
242
+ log .Info ("Initializing Prometheus Registry" )
243
+
244
+ reg , err := dvo_prom .NewRegistry ()
245
+ if err != nil {
246
+ return fmt .Errorf ("initializing prometheus registry: %w" , err )
247
+ }
248
+
249
+ log .Info (fmt .Sprintf ("Initializing Prometheus metrics endpoint on %q" , opts .MetricsEndpoint ()))
250
+
251
+ svcURL := & url.URL {
252
+ Scheme : "http" ,
253
+ Host : opts .MetricsServiceName ,
254
+ }
255
+ if parts := strings .Split (opts .MetricsBindAddr , ":" ); len (parts ) > 0 {
256
+ if len (parts ) > 1 {
257
+ svcURL .Host += parts [len (parts )- 1 ]
258
+ }
259
+ }
260
+
261
+ srv , err := dvo_prom .NewServer (reg ,
262
+ dvo_prom .WithMetricsAddr (opts .MetricsBindAddr ),
263
+ dvo_prom .WithMetricsPath (opts .MetricsPath ),
264
+ dvo_prom .WithServiceURL (svcURL .String ()),
265
+ )
266
+ if err != nil {
267
+ return fmt .Errorf ("initializing metrics server: %w" , err )
268
+ }
269
+
270
+ go func () {
271
+ <- mgr .Elected ()
272
+ srv .Ready ()
273
+ }()
274
+
275
+ if err := mgr .Add (srv ); err != nil {
276
+ return fmt .Errorf ("adding metrics server to manager: %w" , err )
277
+ }
278
+
279
+ log .Info ("Initializing Validation Engine" )
280
+
281
+ if err := validations .InitializeValidationEngine (opts .ConfigFile , reg ); err != nil {
282
+ return fmt .Errorf ("initializing validation engine: %w" , err )
283
+ }
284
+
285
+ return nil
286
+ }
0 commit comments