-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathoperator.go
More file actions
497 lines (431 loc) · 15.4 KB
/
operator.go
File metadata and controls
497 lines (431 loc) · 15.4 KB
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package operator
import (
"context"
"crypto/tls"
"fmt"
"os"
"path/filepath"
"slices"
"time"
configv1 "github.com/openshift/api/config/v1"
operatorv1 "github.com/openshift/api/operator/v1"
openshifttls "github.com/openshift/controller-runtime-common/pkg/tls"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apiserver/pkg/server/dynamiccertificates"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/manager"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
stack "github.com/rhobs/observability-operator/pkg/apis/monitoring/v1alpha1"
obsv1alpha1 "github.com/rhobs/observability-operator/pkg/apis/observability/v1alpha1"
uiv1alpha1 "github.com/rhobs/observability-operator/pkg/apis/uiplugin/v1alpha1"
stackctrl "github.com/rhobs/observability-operator/pkg/controllers/monitoring/monitoring-stack"
tqctrl "github.com/rhobs/observability-operator/pkg/controllers/monitoring/thanos-querier"
obsctrl "github.com/rhobs/observability-operator/pkg/controllers/observability"
opctrl "github.com/rhobs/observability-operator/pkg/controllers/operator"
uictrl "github.com/rhobs/observability-operator/pkg/controllers/uiplugin"
ctrlutil "github.com/rhobs/observability-operator/pkg/controllers/util"
)
const (
// The mount path for the serving certificate seret is hardcoded in the
// static assets.
tlsMountPath = "/etc/tls/private"
)
// Operator embeds a manager and a serving certificate controller (for
// OpenShift installations).
type Operator struct {
manager manager.Manager
restConfig *rest.Config
servingCertController *dynamiccertificates.DynamicServingCertificateController
clientCAController *dynamiccertificates.ConfigMapCAController
}
type OpenShiftFeatureGates struct {
Enabled bool `json:"enabled,omitempty"`
}
type FeatureGates struct {
OpenShift OpenShiftFeatureGates `json:"openshift,omitempty"`
}
type OperatorConfiguration struct {
Namespace string
MetricsAddr string
HealthProbeAddr string
Prometheus stackctrl.PrometheusConfiguration
Alertmanager stackctrl.AlertmanagerConfiguration
ThanosSidecar stackctrl.ThanosConfiguration
ThanosQuerier tqctrl.ThanosConfiguration
UIPlugins uictrl.UIPluginsConfiguration
FeatureGates FeatureGates
ObservabilityInstaller ObservabilityInstallerConfiguration
TLSProfile configv1.TLSProfileSpec
// CancelFunc is called to trigger graceful shutdown (e.g., on TLS profile change).
CancelFunc context.CancelFunc
}
type ObservabilityInstallerConfiguration struct {
COONamespace string
OpenTelemetryCSV string
TempoCSV string
}
func WithNamespace(ns string) func(*OperatorConfiguration) {
return func(oc *OperatorConfiguration) {
oc.Namespace = ns
oc.UIPlugins.ResourcesNamespace = ns
}
}
func WithPrometheusImage(image string) func(*OperatorConfiguration) {
return func(oc *OperatorConfiguration) {
oc.Prometheus.Image = image
}
}
func WithAlertmanagerImage(image string) func(*OperatorConfiguration) {
return func(oc *OperatorConfiguration) {
oc.Alertmanager.Image = image
}
}
func WithThanosSidecarImage(image string) func(*OperatorConfiguration) {
return func(oc *OperatorConfiguration) {
oc.ThanosSidecar.Image = image
}
}
func WithThanosQuerierImage(image string) func(*OperatorConfiguration) {
return func(oc *OperatorConfiguration) {
oc.ThanosQuerier.Image = image
}
}
func WithMetricsAddr(addr string) func(*OperatorConfiguration) {
return func(oc *OperatorConfiguration) {
oc.MetricsAddr = addr
}
}
func WithHealthProbeAddr(addr string) func(*OperatorConfiguration) {
return func(oc *OperatorConfiguration) {
oc.HealthProbeAddr = addr
}
}
func WithUIPluginImages(images map[string]string) func(*OperatorConfiguration) {
return func(oc *OperatorConfiguration) {
oc.UIPlugins.Images = images
}
}
func WithFeatureGates(featureGates FeatureGates) func(*OperatorConfiguration) {
return func(oc *OperatorConfiguration) {
oc.FeatureGates = featureGates
}
}
func NewOperatorConfiguration(opts ...func(*OperatorConfiguration)) *OperatorConfiguration {
cfg := &OperatorConfiguration{}
for _, o := range opts {
o(cfg)
}
return cfg
}
func WithObservabilityInstaller(configuration ObservabilityInstallerConfiguration) func(*OperatorConfiguration) {
return func(oc *OperatorConfiguration) {
oc.ObservabilityInstaller = configuration
}
}
func WithCancelFunc(cancel context.CancelFunc) func(*OperatorConfiguration) {
return func(oc *OperatorConfiguration) {
oc.CancelFunc = cancel
}
}
func WithTLSProfile(tlsProfile configv1.TLSProfileSpec) func(*OperatorConfiguration) {
return func(oc *OperatorConfiguration) {
oc.TLSProfile = tlsProfile
oc.UIPlugins.TLSProfile = tlsProfile
}
}
func New(ctx context.Context, cfg *OperatorConfiguration) (*Operator, error) {
restConfig := ctrl.GetConfigOrDie()
scheme := NewScheme(cfg)
setupLog := ctrl.Log.WithName("setup")
metricsOpts := metricsserver.Options{
BindAddress: cfg.MetricsAddr,
}
var (
clientCAController *dynamiccertificates.ConfigMapCAController
servingCertController *dynamiccertificates.DynamicServingCertificateController
)
if cfg.FeatureGates.OpenShift.Enabled {
// When running in OpenShift, the server uses HTTPS thanks to the
// service CA operator.
certFile := filepath.Join(tlsMountPath, "tls.crt")
keyFile := filepath.Join(tlsMountPath, "tls.key")
// Wait for the files to be mounted into the container.
var pollErr error
err := wait.PollUntilContextTimeout(ctx, time.Second, 30*time.Second, true, func(ctx context.Context) (bool, error) {
for _, f := range []string{certFile, keyFile} {
if _, err := os.Stat(f); err != nil {
pollErr = err
return false, nil
}
}
return true, nil
})
if err != nil {
return nil, fmt.Errorf("%w: %w", err, pollErr)
}
// DynamicCertKeyPairContent automatically reloads the certificate and key from disk.
certKeyProvider, err := dynamiccertificates.NewDynamicServingContentFromFiles("serving-cert", certFile, keyFile)
if err != nil {
return nil, err
}
if err := certKeyProvider.RunOnce(ctx); err != nil {
return nil, fmt.Errorf("failed to initialize cert/key content: %w", err)
}
kubeClient, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return nil, err
}
clientCAController, err = dynamiccertificates.NewDynamicCAFromConfigMapController(
"client-ca",
metav1.NamespaceSystem,
"extension-apiserver-authentication",
"client-ca-file",
kubeClient,
)
if err != nil {
return nil, fmt.Errorf("failed to initialize client CA controller: %w", err)
}
// Only log the events emitted by the certificate controller for now
// because the controller generates invalid events rejected by the
// Kubernetes API when used with DynamicServingContentFromFiles.
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(func(format string, args ...interface{}) {
ctrl.Log.WithName("events").Info(fmt.Sprintf(format, args...))
})
var tlsConfig tls.Config
tlsConfigFn, unsupportedCiphers := openshifttls.NewTLSConfigFromProfile(cfg.TLSProfile)
if len(unsupportedCiphers) > 0 {
setupLog.Info("Some ciphers from TLS profile are not supported", "ciphers", unsupportedCiphers)
}
tlsConfigFn(&tlsConfig)
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
servingCertController = dynamiccertificates.NewDynamicServingCertificateController(
&tlsConfig,
clientCAController,
certKeyProvider,
nil,
record.NewEventRecorderAdapter(
eventBroadcaster.NewRecorder(scheme, v1.EventSource{Component: "observability-operator"}),
),
)
if err := servingCertController.RunOnce(); err != nil {
return nil, fmt.Errorf("failed to initialize serving certificate controller: %w", err)
}
clientCAController.AddListener(servingCertController)
certKeyProvider.AddListener(servingCertController)
metricsOpts.SecureServing = true
metricsOpts.TLSOpts = []func(*tls.Config){
func(c *tls.Config) {
c.GetConfigForClient = servingCertController.GetConfigForClient
},
}
}
cacheOptions := cache.Options{
// All controller created resources carry the label
// defined below. This is added in the reconcilers.
DefaultLabelSelector: labels.SelectorFromSet(map[string]string{ctrlutil.ResourceLabel: ctrlutil.OpName}),
ByObject: map[client.Object]cache.ByObject{
// We define exceptions for the cache and
// thus from the default label selector.
// Secrets are watched by some controllers
// that accept TLS artifacts in a secret.
&v1.Secret{}: cache.ByObject{
Label: labels.Everything(),
},
// The user-facing CRDs need to be
// cached in absence of any labels.
&stack.MonitoringStack{}: cache.ByObject{
Label: labels.Everything(),
},
&stack.ThanosQuerier{}: cache.ByObject{
Label: labels.Everything(),
},
&uiv1alpha1.UIPlugin{}: cache.ByObject{
Label: labels.Everything(),
},
&obsv1alpha1.ObservabilityInstaller{}: cache.ByObject{
Label: labels.Everything(),
},
// The operator controller watches the
// service created by the olm bundle, so
// it can create a ServiceMonitor that
// can be scraped by the OCP in-cluster
// stack
&v1.Service{}: cache.ByObject{
Label: labels.SelectorFromSet(map[string]string{"app.kubernetes.io/name": ctrlutil.OpName}),
},
},
}
if cfg.FeatureGates.OpenShift.Enabled {
// APIServer CR is watched for TLS profile changes.
cacheOptions.ByObject[&configv1.APIServer{}] = cache.ByObject{
Label: labels.Everything(),
}
}
mgr, err := ctrl.NewManager(
restConfig,
ctrl.Options{
Scheme: scheme,
Metrics: metricsOpts,
HealthProbeBindAddress: cfg.HealthProbeAddr,
PprofBindAddress: "127.0.0.1:8083",
Cache: cacheOptions,
})
if err != nil {
return nil, fmt.Errorf("unable to create manager: %w", err)
}
if err := stackctrl.RegisterWithManager(mgr, stackctrl.Options{
Prometheus: cfg.Prometheus,
Alertmanager: cfg.Alertmanager,
Thanos: cfg.ThanosSidecar,
}); err != nil {
return nil, fmt.Errorf("unable to register monitoring stack controller: %w", err)
}
if err := tqctrl.RegisterWithManager(mgr, tqctrl.Options{Thanos: cfg.ThanosQuerier}); err != nil {
return nil, fmt.Errorf("unable to register the thanos querier controller with the manager: %w", err)
}
if cfg.FeatureGates.OpenShift.Enabled {
watcher := &openshifttls.SecurityProfileWatcher{
Client: mgr.GetClient(),
InitialTLSProfileSpec: cfg.TLSProfile,
OnProfileChange: func(_ context.Context, _, _ configv1.TLSProfileSpec) {
setupLog.Info("TLS security profile changed, triggering graceful restart")
if cfg.CancelFunc != nil {
cfg.CancelFunc()
}
},
}
if err = watcher.SetupWithManager(mgr); err != nil {
return nil, fmt.Errorf("unable to setup TLS profile watcher: %w", err)
}
if err := uictrl.RegisterWithManager(mgr, uictrl.Options{PluginsConf: cfg.UIPlugins}); err != nil {
return nil, fmt.Errorf("unable to register observability-ui-plugin controller: %w", err)
}
} else {
setupLog.Info("OpenShift feature gate is disabled, UIPlugins are not enabled")
}
if cfg.FeatureGates.OpenShift.Enabled {
if err := opctrl.RegisterWithManager(mgr, cfg.Namespace); err != nil {
return nil, fmt.Errorf("unable to register operator controller: %w", err)
}
} else {
setupLog.Info("OpenShift feature gate is disabled, Operator controller is not enabled")
}
if cfg.FeatureGates.OpenShift.Enabled {
if err := obsctrl.RegisterWithManager(mgr, obsctrl.Options{
COONamespace: cfg.ObservabilityInstaller.COONamespace,
OpenTelemetryOperator: obsctrl.OperatorInstallConfig{
Namespace: cfg.ObservabilityInstaller.COONamespace,
PackageName: "opentelemetry-product",
StartingCSV: cfg.ObservabilityInstaller.OpenTelemetryCSV,
Channel: "stable",
},
TempoOperator: obsctrl.OperatorInstallConfig{
Namespace: cfg.ObservabilityInstaller.COONamespace,
PackageName: "tempo-product",
StartingCSV: cfg.ObservabilityInstaller.TempoCSV,
Channel: "stable",
},
}); err != nil {
return nil, fmt.Errorf("unable to register cluster observability controller: %w", err)
}
} else {
setupLog.Info("OpenShift feature gate is disabled, cluster observability controller is not enabled")
}
if err := mgr.AddHealthzCheck("health probe", healthz.Ping); err != nil {
return nil, fmt.Errorf("unable to add health probe: %w", err)
}
op := &Operator{
manager: mgr,
restConfig: restConfig,
servingCertController: servingCertController,
clientCAController: clientCAController,
}
if cfg.FeatureGates.OpenShift.Enabled {
if err := mgr.Add(op.newShutdownCleanupRunnable()); err != nil {
return nil, fmt.Errorf("unable to add shutdown cleanup runnable: %w", err)
}
}
return op, nil
}
func (o *Operator) Start(ctx context.Context) error {
if o.clientCAController != nil {
go o.clientCAController.Run(ctx, 1)
}
if o.servingCertController != nil {
go o.servingCertController.Run(1, ctx.Done())
}
if err := o.manager.Start(ctx); err != nil {
return fmt.Errorf("unable to start manager: %w", err)
}
return nil
}
func (o *Operator) newShutdownCleanupRunnable() manager.Runnable {
return manager.RunnableFunc(func(ctx context.Context) error {
// Block until the manager's context is cancelled (shutdown signal).
<-ctx.Done()
o.cleanupUIPluginsFromConsole()
return nil
})
}
func (o *Operator) cleanupUIPluginsFromConsole() {
logger := ctrl.Log.WithName("shutdown-cleanup")
logger.Info("attempting best-effort UIPlugin console deregistration")
cleanupCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
directClient, err := client.New(o.restConfig, client.Options{
Scheme: o.manager.GetScheme(),
})
if err != nil {
logger.Error(err, "failed to create client for shutdown cleanup")
return
}
pluginList := &uiv1alpha1.UIPluginList{}
if err := directClient.List(cleanupCtx, pluginList); err != nil {
logger.Error(err, "failed to list UIPlugins during shutdown cleanup")
return
}
if len(pluginList.Items) == 0 {
return
}
toRemove := make(map[string]struct{}, len(pluginList.Items))
for _, plugin := range pluginList.Items {
if name := uictrl.ConsoleNameForType(plugin.Spec.Type); name != "" {
toRemove[name] = struct{}{}
}
}
if len(toRemove) == 0 {
return
}
cluster := &operatorv1.Console{}
if err := directClient.Get(cleanupCtx, client.ObjectKey{Name: "cluster"}, cluster); err != nil {
logger.Error(err, "failed to get Console CR during shutdown cleanup")
return
}
original := cluster.DeepCopy()
cluster.Spec.Plugins = slices.DeleteFunc(cluster.Spec.Plugins, func(name string) bool {
_, ok := toRemove[name]
return ok
})
if slices.Equal(cluster.Spec.Plugins, original.Spec.Plugins) {
return
}
patch := client.MergeFrom(original)
if err := directClient.Patch(cleanupCtx, cluster, patch); err != nil {
logger.Error(err, "failed to patch Console CR during shutdown cleanup")
return
}
logger.Info("successfully cleaned up Console CR during shutdown")
}
func (o *Operator) GetClient() client.Client {
return o.manager.GetClient()
}