forked from kyma-project/telemetry-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
506 lines (424 loc) · 18.8 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
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
498
499
500
501
502
503
504
505
506
/*
Copyright 2021.
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 (
"context"
"flag"
"fmt"
"os"
"time"
"github.com/go-logr/zapr"
"go.uber.org/zap/zapcore"
istiosecurityclientv1 "istio.io/client-go/pkg/apis/security/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/utils/ptr"
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/event"
"sigs.k8s.io/controller-runtime/pkg/manager"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
operatorv1alpha1 "github.com/kyma-project/telemetry-manager/apis/operator/v1alpha1"
telemetryv1alpha1 "github.com/kyma-project/telemetry-manager/apis/telemetry/v1alpha1"
telemetryv1beta1 "github.com/kyma-project/telemetry-manager/apis/telemetry/v1beta1"
"github.com/kyma-project/telemetry-manager/controllers/operator"
telemetrycontrollers "github.com/kyma-project/telemetry-manager/controllers/telemetry"
"github.com/kyma-project/telemetry-manager/internal/featureflags"
"github.com/kyma-project/telemetry-manager/internal/overrides"
"github.com/kyma-project/telemetry-manager/internal/reconciler/telemetry"
"github.com/kyma-project/telemetry-manager/internal/resources/selfmonitor"
selfmonitorwebhook "github.com/kyma-project/telemetry-manager/internal/selfmonitor/webhook"
loggerutils "github.com/kyma-project/telemetry-manager/internal/utils/logger"
"github.com/kyma-project/telemetry-manager/internal/webhookcert"
logparserwebhookv1alpha1 "github.com/kyma-project/telemetry-manager/webhook/logparser/v1alpha1"
logpipelinewebhookv1alpha1 "github.com/kyma-project/telemetry-manager/webhook/logpipeline/v1alpha1"
logpipelinewebhookv1beta1 "github.com/kyma-project/telemetry-manager/webhook/logpipeline/v1beta1"
metricpipelinewebhookv1alpha1 "github.com/kyma-project/telemetry-manager/webhook/metricpipeline/v1alpha1"
metricpipelinewebhookv1beta1 "github.com/kyma-project/telemetry-manager/webhook/metricpipeline/v1beta1"
tracepipelinewebhookv1alpha1 "github.com/kyma-project/telemetry-manager/webhook/tracepipeline/v1alpha1"
tracepipelinewebhookv1beta1 "github.com/kyma-project/telemetry-manager/webhook/tracepipeline/v1beta1"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
telemetryNamespace string
// TODO: replace with build version based on git revision
version = "main"
// Operator flags
certDir string
enableV1Beta1LogPipelines bool
enableLogPipelinesOTLP bool
highPriorityClassName string
normalPriorityClassName string
fluentBitExporterImage string
fluentBitImage string
otelCollectorImage string
selfMonitorImage string
)
const (
defaultFluentBitExporterImage = "europe-docker.pkg.dev/kyma-project/prod/directory-size-exporter:v20241024-8bc3f6a8"
defaultFluentBitImage = "europe-docker.pkg.dev/kyma-project/prod/external/fluent/fluent-bit:3.1.9"
defaultOTelCollectorImage = "europe-docker.pkg.dev/kyma-project/prod/kyma-otel-collector:0.114.0-main"
defaultSelfMonitorImage = "europe-docker.pkg.dev/kyma-project/prod/tpi/telemetry-self-monitor:3.0.1-857b814"
cacheSyncPeriod = 1 * time.Minute
telemetryNamespaceEnvVar = "MANAGER_NAMESPACE"
telemetryNamespaceDefault = "default"
metricOTLPServiceName = "telemetry-otlp-metrics"
selfMonitorName = "telemetry-self-monitor"
traceOTLPServiceName = "telemetry-otlp-traces"
logOTLPServiceName = "telemetry-otlp-logs"
webhookServiceName = "telemetry-manager-webhook"
healthProbePort = 8081
metricsPort = 8080
pprofPort = 6060
webhookPort = 9443
)
//nolint:gochecknoinits // Runtime's scheme addition is required.
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(apiextensionsv1.AddToScheme(scheme))
utilruntime.Must(telemetryv1alpha1.AddToScheme(scheme))
utilruntime.Must(operatorv1alpha1.AddToScheme(scheme))
utilruntime.Must(istiosecurityclientv1.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}
func main() {
if err := run(); err != nil {
setupLog.Error(err, "Manager exited with error")
os.Exit(1)
}
}
func run() error {
flag.BoolVar(&enableV1Beta1LogPipelines, "enable-v1beta1-log-pipelines", false, "Enable v1beta1 log pipelines CRD")
flag.BoolVar(&enableLogPipelinesOTLP, "enable-log-pipelines-otlp", false, "Enable otlp input and output for log pipelines")
flag.StringVar(&certDir, "cert-dir", ".", "Webhook TLS certificate directory")
flag.StringVar(&highPriorityClassName, "high-priority-class-name", "", "High priority class name used by managed DaemonSets")
flag.StringVar(&normalPriorityClassName, "normal-priority-class-name", "", "Normal priority class name used by managed Deployments")
flag.StringVar(&fluentBitExporterImage, "fluent-bit-exporter-image", defaultFluentBitExporterImage, "Image for exporting fluent bit filesystem usage")
flag.StringVar(&fluentBitImage, "fluent-bit-image", defaultFluentBitImage, "Image for fluent-bit")
flag.StringVar(&otelCollectorImage, "otel-collector-image", defaultOTelCollectorImage, "Image for OpenTelemetry Collector")
flag.StringVar(&selfMonitorImage, "self-monitor-image", defaultSelfMonitorImage, "Image for self-monitor")
flag.Parse()
featureflags.Set(featureflags.V1Beta1, enableV1Beta1LogPipelines)
featureflags.Set(featureflags.LogPipelineOTLP, enableLogPipelinesOTLP)
telemetryNamespace = os.Getenv(telemetryNamespaceEnvVar)
if telemetryNamespace == "" {
telemetryNamespace = telemetryNamespaceDefault
}
overrides.AtomicLevel().SetLevel(zapcore.InfoLevel)
zapLogger, err := loggerutils.New(overrides.AtomicLevel())
if err != nil {
return fmt.Errorf("failed to create logger: %w", err)
}
defer zapLogger.Sync() //nolint:errcheck // if flusing logs fails there is nothing else we can do
ctrl.SetLogger(zapr.NewLogger(zapLogger))
setupLog.Info("Starting Telemetry Manager", "version", version)
for _, flag := range featureflags.EnabledFlags() {
setupLog.Info("Enabled feature flag", "flag", flag)
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{BindAddress: fmt.Sprintf(":%d", metricsPort)},
HealthProbeBindAddress: fmt.Sprintf(":%d", healthProbePort),
PprofBindAddress: fmt.Sprintf(":%d", pprofPort),
LeaderElection: true,
LeaderElectionNamespace: telemetryNamespace,
LeaderElectionID: "cdd7ef0b.kyma-project.io",
WebhookServer: webhook.NewServer(webhook.Options{
Port: webhookPort,
CertDir: certDir,
}),
Cache: cache.Options{
SyncPeriod: ptr.To(cacheSyncPeriod),
// The operator handles various resource that are namespace-scoped, and additionally some resources that are cluster-scoped (clusterroles, clusterrolebindings, etc.).
// For namespace-scoped resources we want to restrict the operator permissions to only fetch resources from a given namespace.
ByObject: map[client.Object]cache.ByObject{
&appsv1.Deployment{}: {Field: setNamespaceFieldSelector()},
&appsv1.ReplicaSet{}: {Field: setNamespaceFieldSelector()},
&appsv1.DaemonSet{}: {Field: setNamespaceFieldSelector()},
&corev1.ConfigMap{}: {Field: setNamespaceFieldSelector()},
&corev1.ServiceAccount{}: {Field: setNamespaceFieldSelector()},
&corev1.Service{}: {Field: setNamespaceFieldSelector()},
&networkingv1.NetworkPolicy{}: {Field: setNamespaceFieldSelector()},
&corev1.Secret{}: {Field: setNamespaceFieldSelector()},
&operatorv1alpha1.Telemetry{}: {Field: setNamespaceFieldSelector()},
},
},
Client: client.Options{
Cache: &client.CacheOptions{
DisableFor: []client.Object{
&corev1.Secret{},
},
},
},
})
if err != nil {
return fmt.Errorf("failed to start manager: %w", err)
}
tracePipelineReconcileTriggerChan := make(chan event.GenericEvent)
if err := setupTracePipelineController(mgr, tracePipelineReconcileTriggerChan); err != nil {
return fmt.Errorf("failed to enable trace pipeline controller: %w", err)
}
metricPipelineReconcileTriggerChan := make(chan event.GenericEvent)
if err := setupMetricPipelineController(mgr, metricPipelineReconcileTriggerChan); err != nil {
return fmt.Errorf("failed to enable metric pipeline controller: %w", err)
}
logPipelineReconcileTriggerChan := make(chan event.GenericEvent)
if err := setupLogPipelineController(mgr, logPipelineReconcileTriggerChan); err != nil {
return fmt.Errorf("failed to enable log pipeline controller: %w", err)
}
webhookConfig := createWebhookConfig()
selfMonitorConfig := createSelfMonitoringConfig()
if err := enableTelemetryModuleController(mgr, webhookConfig, selfMonitorConfig); err != nil {
return fmt.Errorf("failed to enable telemetry module controller: %w", err)
}
// +kubebuilder:scaffold:builder
if err := mgr.AddHealthzCheck("healthz", mgr.GetWebhookServer().StartedChecker()); err != nil {
return fmt.Errorf("failed to add health check: %w", err)
}
if err := mgr.AddReadyzCheck("readyz", mgr.GetWebhookServer().StartedChecker()); err != nil {
return fmt.Errorf("failed to add ready check: %w", err)
}
if err := ensureWebhookCert(mgr, webhookConfig); err != nil {
return fmt.Errorf("failed to enable webhook server: %w", err)
}
if err := setupAdmissionsWebhooks(mgr); err != nil {
return fmt.Errorf("failed to setup admission webhooks: %w", err)
}
mgr.GetWebhookServer().Register("/api/v2/alerts", selfmonitorwebhook.NewHandler(
mgr.GetClient(),
selfmonitorwebhook.WithTracePipelineSubscriber(tracePipelineReconcileTriggerChan),
selfmonitorwebhook.WithMetricPipelineSubscriber(metricPipelineReconcileTriggerChan),
selfmonitorwebhook.WithLogPipelineSubscriber(logPipelineReconcileTriggerChan),
selfmonitorwebhook.WithLogger(ctrl.Log.WithName("self-monitor-webhook"))))
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
return fmt.Errorf("failed to start manager: %w", err)
}
return nil
}
func setupAdmissionsWebhooks(mgr manager.Manager) error {
if err := metricpipelinewebhookv1alpha1.SetupWithManager(mgr); err != nil {
return fmt.Errorf("failed to setup metric pipeline v1alpha1 webhook: %w", err)
}
if featureflags.IsEnabled(featureflags.V1Beta1) {
if err := metricpipelinewebhookv1beta1.SetupWithManager(mgr); err != nil {
return fmt.Errorf("failed to setup metric pipeline v1beta1 webhook: %w", err)
}
}
if err := tracepipelinewebhookv1alpha1.SetupWithManager(mgr); err != nil {
return fmt.Errorf("failed to setup trace pipeline v1alpha1 webhook: %w", err)
}
if featureflags.IsEnabled(featureflags.V1Beta1) {
if err := tracepipelinewebhookv1beta1.SetupWithManager(mgr); err != nil {
return fmt.Errorf("failed to setup trace pipeline v1beta1 webhook: %w", err)
}
}
if err := logpipelinewebhookv1alpha1.SetupWithManager(mgr); err != nil {
return fmt.Errorf("failed to setup log pipeline v1alpha1 webhook: %w", err)
}
if featureflags.IsEnabled(featureflags.V1Beta1) {
if err := logpipelinewebhookv1beta1.SetupWithManager(mgr); err != nil {
return fmt.Errorf("failed to setup log pipeline v1beta1 webhook: %w", err)
}
}
logparserwebhookv1alpha1.SetupWithManager(mgr)
return nil
}
func enableTelemetryModuleController(mgr manager.Manager, webhookConfig telemetry.WebhookConfig, selfMonitorConfig telemetry.SelfMonitorConfig) error {
setupLog.Info("Setting up telemetry controller")
telemetryController := operator.NewTelemetryController(
mgr.GetClient(),
mgr.GetScheme(),
operator.TelemetryControllerConfig{
Config: telemetry.Config{
Traces: telemetry.TracesConfig{
OTLPServiceName: traceOTLPServiceName,
Namespace: telemetryNamespace,
},
Metrics: telemetry.MetricsConfig{
OTLPServiceName: metricOTLPServiceName,
Namespace: telemetryNamespace,
},
Webhook: webhookConfig,
SelfMonitor: selfMonitorConfig,
},
SelfMonitorName: selfMonitorName,
TelemetryNamespace: telemetryNamespace,
},
)
if err := telemetryController.SetupWithManager(mgr); err != nil {
return fmt.Errorf("failed to setup telemetry controller: %w", err)
}
return nil
}
func setupLogPipelineController(mgr manager.Manager, reconcileTriggerChan <-chan event.GenericEvent) error {
if featureflags.IsEnabled(featureflags.V1Beta1) {
setupLog.Info("Registering conversion webhooks for LogPipelines")
utilruntime.Must(telemetryv1beta1.AddToScheme(scheme))
// Register conversion webhooks for LogPipelines
if err := ctrl.NewWebhookManagedBy(mgr).
For(&telemetryv1alpha1.LogPipeline{}).
Complete(); err != nil {
return fmt.Errorf("failed to create v1alpha1 conversion webhook: %w", err)
}
if err := ctrl.NewWebhookManagedBy(mgr).
For(&telemetryv1beta1.LogPipeline{}).
Complete(); err != nil {
return fmt.Errorf("failed to create v1beta1 conversion webhook: %w", err)
}
}
setupLog.Info("Setting up logpipeline controller")
logPipelineController, err := telemetrycontrollers.NewLogPipelineController(
mgr.GetClient(),
reconcileTriggerChan,
telemetrycontrollers.LogPipelineControllerConfig{
ExporterImage: fluentBitExporterImage,
FluentBitImage: fluentBitImage,
OTelCollectorImage: otelCollectorImage,
FluentBitPriorityClassName: highPriorityClassName,
LogGatewayPriorityClassName: normalPriorityClassName,
LogGatewayServiceName: logOTLPServiceName,
RestConfig: mgr.GetConfig(),
SelfMonitorName: selfMonitorName,
TelemetryNamespace: telemetryNamespace,
},
)
if err != nil {
return fmt.Errorf("failed to create logpipeline controller: %w", err)
}
if err := logPipelineController.SetupWithManager(mgr); err != nil {
return fmt.Errorf("failed to setup logpipeline controller: %w", err)
}
setupLog.Info("Setting up logparser controller")
logParserController := telemetrycontrollers.NewLogParserController(
mgr.GetClient(),
telemetrycontrollers.LogParserControllerConfig{
TelemetryNamespace: telemetryNamespace,
},
)
if err := logParserController.SetupWithManager(mgr); err != nil {
return fmt.Errorf("failed to setup logparser controller: %w", err)
}
return nil
}
func setupTracePipelineController(mgr manager.Manager, reconcileTriggerChan <-chan event.GenericEvent) error {
setupLog.Info("Setting up tracepipeline controller")
tracePipelineController, err := telemetrycontrollers.NewTracePipelineController(
mgr.GetClient(),
reconcileTriggerChan,
telemetrycontrollers.TracePipelineControllerConfig{
RestConfig: mgr.GetConfig(),
OTelCollectorImage: otelCollectorImage,
SelfMonitorName: selfMonitorName,
TelemetryNamespace: telemetryNamespace,
TraceGatewayPriorityClassName: normalPriorityClassName,
TraceGatewayServiceName: traceOTLPServiceName,
},
)
if err != nil {
return fmt.Errorf("failed to create tracepipeline controller: %w", err)
}
if err := tracePipelineController.SetupWithManager(mgr); err != nil {
return fmt.Errorf("failed to setup tracepipeline controller: %w", err)
}
return nil
}
func setupMetricPipelineController(mgr manager.Manager, reconcileTriggerChan <-chan event.GenericEvent) error {
setupLog.Info("Setting up metricpipeline controller")
metricPipelineController, err := telemetrycontrollers.NewMetricPipelineController(
mgr.GetClient(),
reconcileTriggerChan,
telemetrycontrollers.MetricPipelineControllerConfig{
MetricAgentPriorityClassName: highPriorityClassName,
MetricGatewayPriorityClassName: normalPriorityClassName,
MetricGatewayServiceName: metricOTLPServiceName,
ModuleVersion: version,
OTelCollectorImage: otelCollectorImage,
RestConfig: mgr.GetConfig(),
SelfMonitorName: selfMonitorName,
TelemetryNamespace: telemetryNamespace,
},
)
if err != nil {
return fmt.Errorf("failed to create metricpipeline controller: %w", err)
}
if err := metricPipelineController.SetupWithManager(mgr); err != nil {
return fmt.Errorf("failed to setup metricpipeline controller: %w", err)
}
return nil
}
func ensureWebhookCert(mgr manager.Manager, webhookConfig telemetry.WebhookConfig) error {
// Create own client since manager might not be started while using
clientOptions := client.Options{
Scheme: scheme,
}
k8sClient, err := client.New(mgr.GetConfig(), clientOptions)
if err != nil {
return fmt.Errorf("failed to create webhook client: %w", err)
}
if err = webhookcert.EnsureCertificate(context.Background(), k8sClient, webhookConfig.CertConfig); err != nil {
return fmt.Errorf("failed to ensure webhook cert: %w", err)
}
setupLog.Info("Ensured webhook cert")
return nil
}
func setNamespaceFieldSelector() fields.Selector {
return fields.SelectorFromSet(fields.Set{"metadata.namespace": telemetryNamespace})
}
func createSelfMonitoringConfig() telemetry.SelfMonitorConfig {
return telemetry.SelfMonitorConfig{
Config: selfmonitor.Config{
BaseName: selfMonitorName,
Namespace: telemetryNamespace,
Deployment: selfmonitor.DeploymentConfig{
Image: selfMonitorImage,
PriorityClassName: normalPriorityClassName,
},
},
WebhookScheme: "https",
WebhookURL: fmt.Sprintf("%s.%s.svc", webhookServiceName, telemetryNamespace),
}
}
func createWebhookConfig() telemetry.WebhookConfig {
return telemetry.WebhookConfig{
CertConfig: webhookcert.Config{
CertDir: certDir,
ServiceName: types.NamespacedName{
Name: webhookServiceName,
Namespace: telemetryNamespace,
},
CASecretName: types.NamespacedName{
Name: "telemetry-webhook-cert",
Namespace: telemetryNamespace,
},
ValidatingWebhookName: types.NamespacedName{
Name: "telemetry-validating-webhook.kyma-project.io",
},
MutatingWebhookName: types.NamespacedName{
Name: "telemetry-mutating-webhook.kyma-project.io",
},
},
}
}