-
Notifications
You must be signed in to change notification settings - Fork 254
/
main.go
459 lines (404 loc) · 16 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"crypto/tls"
"flag"
"fmt"
"os"
"runtime"
"strconv"
"strings"
"time"
metal3api "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
metal3iocontroller "github.com/metal3-io/baremetal-operator/controllers/metal3.io"
"github.com/metal3-io/baremetal-operator/pkg/imageprovider"
"github.com/metal3-io/baremetal-operator/pkg/provisioner"
"github.com/metal3-io/baremetal-operator/pkg/provisioner/demo"
"github.com/metal3-io/baremetal-operator/pkg/provisioner/fixture"
"github.com/metal3-io/baremetal-operator/pkg/provisioner/ironic"
"github.com/metal3-io/baremetal-operator/pkg/secretutils"
"github.com/metal3-io/baremetal-operator/pkg/version"
"github.com/pkg/errors"
"go.uber.org/zap/zapcore"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
cliflag "k8s.io/component-base/cli/flag"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
// Constants for TLS versions.
const (
TLSVersion12 = "TLS12"
TLSVersion13 = "TLS13"
)
type TLSOptions struct {
TLSMaxVersion string
TLSMinVersion string
TLSCipherSuites string
}
var (
scheme = k8sruntime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
healthAddr string
tlsOptions = TLSOptions{}
tlsSupportedVersions = []string{TLSVersion12, TLSVersion13}
)
const leaderElectionID = "baremetal-operator"
func init() {
_ = clientgoscheme.AddToScheme(scheme)
_ = metal3api.AddToScheme(scheme)
}
func printVersion() {
setupLog.Info(fmt.Sprintf("Go Version: %s", runtime.Version()))
setupLog.Info(fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH))
setupLog.Info(fmt.Sprintf("Git commit: %s", version.Commit))
setupLog.Info(fmt.Sprintf("Build time: %s", version.BuildTime))
setupLog.Info(fmt.Sprintf("Component: %s", version.String))
}
func setupChecks(mgr ctrl.Manager) {
if err := mgr.AddReadyzCheck("ping", healthz.Ping); err != nil {
setupLog.Error(err, "unable to create ready check")
os.Exit(1)
}
if err := mgr.AddHealthzCheck("ping", healthz.Ping); err != nil {
setupLog.Error(err, "unable to create health check")
os.Exit(1)
}
}
func setupWebhooks(mgr ctrl.Manager) {
var bmh webhook.Validator = &metal3api.BareMetalHost{}
if err := ctrl.NewWebhookManagedBy(mgr).
For(bmh).
Complete(); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "BareMetalHost")
os.Exit(1)
}
var bmces webhook.Validator = &metal3api.BMCEventSubscription{}
if err := ctrl.NewWebhookManagedBy(mgr).
For(bmces).
Complete(); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "BMCEventSubscription")
os.Exit(1)
}
}
func main() {
var watchNamespace string
var metricsBindAddr string
var enableLeaderElection bool
var preprovImgEnable bool
var devLogging bool
var runInTestMode bool
var runInDemoMode bool
var webhookPort int
var restConfigQPS float64
var restConfigBurst int
var controllerConcurrency int
var leaseDurationSeconds string
var renewDeadlineSeconds string
var retryPeriodSeconds string
// From CAPI point of view, BMO should be able to watch all namespaces
// in case of a deployment that is not multi-tenant. If the deployment
// is for multi-tenancy, then the BMO should watch only the provided
// namespace.
flag.StringVar(&watchNamespace, "namespace", os.Getenv("WATCH_NAMESPACE"),
"Namespace that the controller watches to reconcile host resources.")
flag.StringVar(&metricsBindAddr, "metrics-addr", "127.0.0.1:8085",
"The address the metric endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.BoolVar(&preprovImgEnable, "build-preprov-image", false, "enable integration with the PreprovisioningImage API")
flag.BoolVar(&devLogging, "dev", false, "enable developer logging")
flag.BoolVar(&runInTestMode, "test-mode", false, "disable ironic communication")
flag.BoolVar(&runInDemoMode, "demo-mode", false,
"use the demo provisioner to set host states")
flag.StringVar(&healthAddr, "health-addr", ":9440",
"The address the health endpoint binds to.")
flag.IntVar(&webhookPort, "webhook-port", 9443,
"Webhook Server port (set to 0 to disable)")
flag.Float64Var(&restConfigQPS, "kube-api-qps", 20,
"Maximum queries per second from the controller client to the Kubernetes API server. Default 20")
flag.IntVar(&restConfigBurst, "kube-api-burst", 30,
"Maximum number of queries that should be allowed in one burst from the controller client to the Kubernetes API server. Default 30")
flag.StringVar(&tlsOptions.TLSMinVersion, "tls-min-version", TLSVersion12,
"The minimum TLS version in use by the webhook server.\n"+
fmt.Sprintf("Possible values are %s.", strings.Join(tlsSupportedVersions, ", ")),
)
flag.StringVar(&tlsOptions.TLSMaxVersion, "tls-max-version", TLSVersion13,
"The maximum TLS version in use by the webhook server.\n"+
fmt.Sprintf("Possible values are %s.", strings.Join(tlsSupportedVersions, ", ")),
)
tlsCipherPreferredValues := cliflag.PreferredTLSCipherNames()
tlsCipherInsecureValues := cliflag.InsecureTLSCipherNames()
flag.StringVar(&tlsOptions.TLSCipherSuites, "tls-cipher-suites", "",
"Comma-separated list of cipher suites for the webhook server. "+
"If omitted, the default Go cipher suites will be used. \n"+
"Preferred values: "+strings.Join(tlsCipherPreferredValues, ", ")+". \n"+
"Insecure values: "+strings.Join(tlsCipherInsecureValues, ", ")+".")
flag.IntVar(&controllerConcurrency, "controller-concurrency", 0,
"Number of CRs of each type to process simultaneously")
flag.StringVar(&leaseDurationSeconds, "lease-duration-seconds", os.Getenv("LEASE_DURATION_SECONDS"), "Leader election duration in seconds.")
flag.StringVar(&renewDeadlineSeconds, "renew-deadline-seconds", os.Getenv("RENEW_DEADLINE_SECONDS"), "Leader election renew deadline duration in seconds.")
flag.StringVar(&retryPeriodSeconds, "retry-period-seconds", os.Getenv("RETRY_PERIOD_SECONDS"), "Leader election retry period in seconds.")
flag.Parse()
logOpts := zap.Options{}
if devLogging {
logOpts.Development = true
logOpts.TimeEncoder = zapcore.ISO8601TimeEncoder
} else {
logOpts.TimeEncoder = zapcore.EpochTimeEncoder
}
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&logOpts)))
printVersion()
enableWebhook := webhookPort != 0
leaderElectionNamespace := os.Getenv("POD_NAMESPACE")
if leaderElectionNamespace == "" {
leaderElectionNamespace = watchNamespace
}
tlsOptionOverrides, err := GetTLSOptionOverrideFuncs(tlsOptions)
if err != nil {
setupLog.Error(err, "unable to add TLS settings to the webhook server")
os.Exit(1)
}
restConfig := ctrl.GetConfigOrDie()
restConfig.QPS = float32(restConfigQPS)
restConfig.Burst = restConfigBurst
var watchNamespaces map[string]cache.Config
if watchNamespace != "" {
watchNamespaces = map[string]cache.Config{
watchNamespace: {},
}
}
ctrlOpts := ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{BindAddress: metricsBindAddr},
WebhookServer: webhook.NewServer(webhook.Options{
Port: webhookPort,
TLSOpts: tlsOptionOverrides,
}),
LeaderElection: enableLeaderElection,
LeaderElectionID: leaderElectionID,
LeaderElectionNamespace: leaderElectionNamespace,
LeaderElectionReleaseOnCancel: true,
HealthProbeBindAddress: healthAddr,
Cache: cache.Options{
ByObject: secretutils.AddSecretSelector(nil),
DefaultNamespaces: watchNamespaces,
},
}
if leaseDurationSeconds != "" {
seconds, err := strconv.ParseInt(leaseDurationSeconds, 10, 16)
if err != nil {
setupLog.Error(err, "failed to parse duration")
os.Exit(1)
}
duration := time.Second * time.Duration(seconds)
ctrlOpts.LeaseDuration = &duration
}
if renewDeadlineSeconds != "" {
seconds, err := strconv.ParseInt(renewDeadlineSeconds, 10, 16)
if err != nil {
setupLog.Error(err, "failed to parse renew deadline")
os.Exit(1)
}
duration := time.Second * time.Duration(seconds)
ctrlOpts.RenewDeadline = &duration
}
if retryPeriodSeconds != "" {
seconds, err := strconv.ParseInt(retryPeriodSeconds, 10, 16)
if err != nil {
setupLog.Error(err, "failed to parse retry period")
os.Exit(1)
}
duration := time.Second * time.Duration(seconds)
ctrlOpts.RetryPeriod = &duration
}
mgr, err := ctrl.NewManager(restConfig, ctrlOpts)
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
var provisionerFactory provisioner.Factory
if runInTestMode {
ctrl.Log.Info("using test provisioner")
provisionerFactory = &fixture.Fixture{}
} else if runInDemoMode {
ctrl.Log.Info("using demo provisioner")
provisionerFactory = &demo.Demo{}
} else {
provLog := zap.New(zap.UseFlagOptions(&logOpts)).WithName("provisioner")
provisionerFactory = ironic.NewProvisionerFactory(provLog, preprovImgEnable)
}
maxConcurrency, err := getMaxConcurrentReconciles(controllerConcurrency)
if err != nil {
setupLog.Error(err, "unable to create controllers")
os.Exit(1)
}
if err = (&metal3iocontroller.BareMetalHostReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("BareMetalHost"),
ProvisionerFactory: provisionerFactory,
APIReader: mgr.GetAPIReader(),
}).SetupWithManager(mgr, preprovImgEnable, maxConcurrency); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "BareMetalHost")
os.Exit(1)
}
if preprovImgEnable {
imgReconciler := metal3iocontroller.PreprovisioningImageReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("PreprovisioningImage"),
APIReader: mgr.GetAPIReader(),
Scheme: mgr.GetScheme(),
ImageProvider: imageprovider.NewDefaultImageProvider(),
}
if imgReconciler.CanStart() {
if err = (&imgReconciler).SetupWithManager(mgr, maxConcurrency); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "PreprovisioningImage")
os.Exit(1)
}
}
}
// +kubebuilder:scaffold:builder
if err = (&metal3iocontroller.HostFirmwareSettingsReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("HostFirmwareSettings"),
ProvisionerFactory: provisionerFactory,
}).SetupWithManager(mgr, maxConcurrency); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "HostFirmwareSettings")
os.Exit(1)
}
if err = (&metal3iocontroller.BMCEventSubscriptionReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("BMCEventSubscription"),
ProvisionerFactory: provisionerFactory,
}).SetupWithManager(mgr, maxConcurrency); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "BMCEventSubscription")
os.Exit(1)
}
if err = (&metal3iocontroller.HostFirmwareComponentsReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("HostFirmwareComponents"),
ProvisionerFactory: provisionerFactory,
}).SetupWithManager(mgr, maxConcurrency); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "HostFirmwareComponents")
os.Exit(1)
}
if err = (&metal3iocontroller.DataImageReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("DataImage"),
ProvisionerFactory: provisionerFactory,
}).SetupWithManager(mgr, maxConcurrency); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "DataImage")
os.Exit(1)
}
setupChecks(mgr)
if enableWebhook {
setupWebhooks(mgr)
}
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}
// GetTLSOptionOverrideFuncs returns a list of TLS configuration overrides to be used
// by the webhook server.
func GetTLSOptionOverrideFuncs(options TLSOptions) ([]func(*tls.Config), error) {
var tlsOptions []func(config *tls.Config)
// To make a static analyzer happy, this block ensures there is no code
// path that sets a TLS version outside the acceptable values, even in
// case of unexpected user input.
var tlsMinVersion, tlsMaxVersion uint16
for version, option := range map[*uint16]string{&tlsMinVersion: options.TLSMinVersion, &tlsMaxVersion: options.TLSMaxVersion} {
switch option {
case TLSVersion12:
*version = tls.VersionTLS12
case TLSVersion13:
*version = tls.VersionTLS13
default:
return nil, fmt.Errorf("unexpected TLS version %q (must be one of: %s)", option, strings.Join(tlsSupportedVersions, ", "))
}
}
if tlsMaxVersion != 0 && tlsMinVersion > tlsMaxVersion {
return nil, fmt.Errorf("TLS version flag min version (%s) is greater than max version (%s)",
options.TLSMinVersion, options.TLSMaxVersion)
}
tlsOptions = append(tlsOptions, func(cfg *tls.Config) {
cfg.MinVersion = tlsMinVersion
})
tlsOptions = append(tlsOptions, func(cfg *tls.Config) {
cfg.MaxVersion = tlsMaxVersion
})
// Cipher suites should not be set if empty.
if tlsMinVersion >= tls.VersionTLS13 &&
options.TLSCipherSuites != "" {
setupLog.Info("warning: Cipher suites should not be set for TLS version 1.3. Ignoring ciphers")
options.TLSCipherSuites = ""
}
if options.TLSCipherSuites != "" {
tlsCipherSuites := strings.Split(options.TLSCipherSuites, ",")
suites, err := cliflag.TLSCipherSuites(tlsCipherSuites)
if err != nil {
return nil, err
}
insecureCipherValues := cliflag.InsecureTLSCipherNames()
for _, cipher := range tlsCipherSuites {
for _, insecureCipherName := range insecureCipherValues {
if insecureCipherName == cipher {
setupLog.Info(fmt.Sprintf("warning: use of insecure cipher '%s' detected.", cipher))
}
}
}
tlsOptions = append(tlsOptions, func(cfg *tls.Config) {
cfg.CipherSuites = suites
})
}
return tlsOptions, nil
}
func getMaxConcurrentReconciles(controllerConcurrency int) (int, error) {
if controllerConcurrency > 0 {
ctrl.Log.Info(fmt.Sprintf("controller concurrency will be set to %d according to command line flag", controllerConcurrency))
return controllerConcurrency, nil
} else if controllerConcurrency < 0 {
return 0, fmt.Errorf("controller concurrency value: %d is invalid", controllerConcurrency)
}
// controller-concurrency value is 0 i.e. no values passed via the flag
// maxConcurrentReconcile value would be set based on env var or number of CPUs.
maxConcurrentReconciles := runtime.NumCPU()
if maxConcurrentReconciles > 8 {
maxConcurrentReconciles = 8
}
if maxConcurrentReconciles < 2 {
maxConcurrentReconciles = 2
}
if mcrEnv, ok := os.LookupEnv("BMO_CONCURRENCY"); ok {
mcr, err := strconv.Atoi(mcrEnv)
if err != nil {
return 0, errors.Wrap(err, fmt.Sprintf("BMO_CONCURRENCY value: %s is invalid", mcrEnv))
}
if mcr > 0 {
ctrl.Log.Info(fmt.Sprintf("BMO_CONCURRENCY of %d is set via an environment variable", mcr))
maxConcurrentReconciles = mcr
} else {
ctrl.Log.Info(fmt.Sprintf("Invalid BMO_CONCURRENCY value. Operator Concurrency will be set to a default value of %d", maxConcurrentReconciles))
}
} else {
ctrl.Log.Info(fmt.Sprintf("Operator Concurrency will be set to a default value of %d", maxConcurrentReconciles))
}
return maxConcurrentReconciles, nil
}