From eea858c1af1dc6f79217b5f9821c105bbe86e993 Mon Sep 17 00:00:00 2001 From: Teodor-Adrian Mihaescu Date: Wed, 27 Nov 2024 15:55:34 +0100 Subject: [PATCH] chore: Refactor otelcollector and fluentbit config to use default parameters --- .testcoverage.yml | 2 +- .../telemetry/logpipeline_controller.go | 42 +-------- .../telemetry/metricpipeline_controller.go | 24 ++--- .../telemetry/tracepipeline_controller.go | 28 +----- .../logpipeline/fluentbit/config.go | 13 +++ internal/resources/otelcollector/config.go | 92 +++++++++++++++++++ 6 files changed, 126 insertions(+), 75 deletions(-) diff --git a/.testcoverage.yml b/.testcoverage.yml index fed2bcfc9..9161bd107 100644 --- a/.testcoverage.yml +++ b/.testcoverage.yml @@ -35,7 +35,7 @@ override: path: ^internal/reconciler/telemetry$ - threshold: 74 path: ^internal/reconciler/tracepipeline$ - - threshold: 82 + - threshold: 76 path: ^internal/resources/otelcollector$ - threshold: 78 path: ^internal/resources/selfmonitor$ diff --git a/controllers/telemetry/logpipeline_controller.go b/controllers/telemetry/logpipeline_controller.go index d0ecc61de..ddf4ee204 100644 --- a/controllers/telemetry/logpipeline_controller.go +++ b/controllers/telemetry/logpipeline_controller.go @@ -22,7 +22,6 @@ import ( appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" - "k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/discovery" "k8s.io/client-go/rest" @@ -51,32 +50,11 @@ import ( ) const ( - // FluentBit fbBaseName = "telemetry-fluent-bit" - // OTel otelLogGatewayName = "telemetry-log-gateway" ) -var ( - // FluentBit - fbCPULimit = resource.MustParse("1") - fbMemoryLimit = resource.MustParse("1Gi") - fbCPURequest = resource.MustParse("100m") - fbMemoryRequest = resource.MustParse("50Mi") - - // OTel - // TODO: Check if these values need to be adjusted - logGatewayBaseCPULimit = resource.MustParse("700m") - logGatewayDynamicCPULimit = resource.MustParse("500m") - logGatewayBaseMemoryLimit = resource.MustParse("500Mi") - logGatewayDynamicMemoryLimit = resource.MustParse("1500Mi") - logGatewayBaseCPURequest = resource.MustParse("100m") - logGatewayDynamicCPURequest = resource.MustParse("100m") - logGatewayBaseMemoryRequest = resource.MustParse("32Mi") - logGatewayDynamicMemoryRequest = resource.MustParse("0") -) - // LogPipelineController reconciles a LogPipeline object type LogPipelineController struct { client.Client @@ -170,10 +148,6 @@ func configureFluentBitReconciler(client client.Client, config LogPipelineContro logpipelinefluentbit.WithFluentBitImage(config.FluentBitImage), logpipelinefluentbit.WithExporterImage(config.ExporterImage), logpipelinefluentbit.WithPriorityClassName(config.FluentBitPriorityClassName), - logpipelinefluentbit.WithCPULimit(fbCPULimit), - logpipelinefluentbit.WithMemoryLimit(fbMemoryLimit), - logpipelinefluentbit.WithCPURequest(fbCPURequest), - logpipelinefluentbit.WithMemoryRequest(fbMemoryRequest), ) pipelineValidator := &logpipelinefluentbit.Validator{ @@ -211,18 +185,10 @@ func configureOtelReconciler(client client.Client, config LogPipelineControllerC BaseName: otelLogGatewayName, Namespace: config.TelemetryNamespace, }, - Deployment: otelcollector.DeploymentConfig{ - Image: config.OTelCollectorImage, - PriorityClassName: config.LogGatewayPriorityClassName, - BaseCPULimit: logGatewayBaseCPULimit, - DynamicCPULimit: logGatewayDynamicCPULimit, - BaseMemoryLimit: logGatewayBaseMemoryLimit, - DynamicMemoryLimit: logGatewayDynamicMemoryLimit, - BaseCPURequest: logGatewayBaseCPURequest, - DynamicCPURequest: logGatewayDynamicCPURequest, - BaseMemoryRequest: logGatewayBaseMemoryRequest, - DynamicMemoryRequest: logGatewayDynamicMemoryRequest, - }, + Deployment: otelcollector.NewDeploymentConfig( + otelcollector.WithImage(config.OTelCollectorImage), + otelcollector.WithPriorityClassName(config.LogGatewayPriorityClassName), + ), OTLPServiceName: config.LogGatewayServiceName, } diff --git a/controllers/telemetry/metricpipeline_controller.go b/controllers/telemetry/metricpipeline_controller.go index 3ac303c2d..a45de62e2 100644 --- a/controllers/telemetry/metricpipeline_controller.go +++ b/controllers/telemetry/metricpipeline_controller.go @@ -199,18 +199,18 @@ func newMetricGatewayApplierDeleter(config MetricPipelineControllerConfig) *otel BaseName: metricGatewayBaseName, Namespace: config.TelemetryNamespace, }, - Deployment: otelcollector.DeploymentConfig{ - Image: config.OTelCollectorImage, - PriorityClassName: config.MetricGatewayPriorityClassName, - BaseCPULimit: metricGatewayBaseCPULimit, - DynamicCPULimit: metricGatewayDynamicCPULimit, - BaseMemoryLimit: metricGatewayBaseMemoryLimit, - DynamicMemoryLimit: metricGatewayDynamicMemoryLimit, - BaseCPURequest: metricGatewayBaseCPURequest, - DynamicCPURequest: metricGatewayDynamicCPURequest, - BaseMemoryRequest: metricGatewayBaseMemoryRequest, - DynamicMemoryRequest: metricGatewayDynamicMemoryRequest, - }, + Deployment: otelcollector.NewDeploymentConfig( + otelcollector.WithImage(config.OTelCollectorImage), + otelcollector.WithPriorityClassName(config.MetricGatewayPriorityClassName), + otelcollector.WithBaseCPULimit(metricGatewayBaseCPULimit), + otelcollector.WithDynamicCPULimit(metricGatewayDynamicCPULimit), + otelcollector.WithBaseMemoryLimit(metricGatewayBaseMemoryLimit), + otelcollector.WithDynamicMemoryLimit(metricGatewayDynamicMemoryLimit), + otelcollector.WithBaseCPURequest(metricGatewayBaseCPURequest), + otelcollector.WithDynamicCPURequest(metricGatewayDynamicCPURequest), + otelcollector.WithBaseMemoryRequest(metricGatewayBaseMemoryRequest), + otelcollector.WithDynamicMemoryRequest(metricGatewayDynamicMemoryRequest), + ), OTLPServiceName: config.MetricGatewayServiceName, } diff --git a/controllers/telemetry/tracepipeline_controller.go b/controllers/telemetry/tracepipeline_controller.go index 10a582b00..978ddba85 100644 --- a/controllers/telemetry/tracepipeline_controller.go +++ b/controllers/telemetry/tracepipeline_controller.go @@ -24,7 +24,6 @@ import ( corev1 "k8s.io/api/core/v1" networkingv1 "k8s.io/api/networking/v1" rbacv1 "k8s.io/api/rbac/v1" - "k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/discovery" "k8s.io/client-go/rest" @@ -59,17 +58,6 @@ const ( traceGatewayBaseName = "telemetry-trace-gateway" ) -var ( - traceGatewayBaseCPULimit = resource.MustParse("700m") - traceGatewayDynamicCPULimit = resource.MustParse("500m") - traceGatewayBaseMemoryLimit = resource.MustParse("500Mi") - traceGatewayDynamicMemoryLimit = resource.MustParse("1500Mi") - traceGatewayBaseCPURequest = resource.MustParse("100m") - traceGatewayDynamicCPURequest = resource.MustParse("100m") - traceGatewayBaseMemoryRequest = resource.MustParse("32Mi") - traceGatewayDynamicMemoryRequest = resource.MustParse("0") -) - // TracePipelineController reconciles a TracePipeline object type TracePipelineController struct { client.Client @@ -149,18 +137,10 @@ func newTraceGatewayApplierDeleter(config TracePipelineControllerConfig) *otelco BaseName: traceGatewayBaseName, Namespace: config.TelemetryNamespace, }, - Deployment: otelcollector.DeploymentConfig{ - Image: config.OTelCollectorImage, - PriorityClassName: config.TraceGatewayPriorityClassName, - BaseCPULimit: traceGatewayBaseCPULimit, - DynamicCPULimit: traceGatewayDynamicCPULimit, - BaseMemoryLimit: traceGatewayBaseMemoryLimit, - DynamicMemoryLimit: traceGatewayDynamicMemoryLimit, - BaseCPURequest: traceGatewayBaseCPURequest, - DynamicCPURequest: traceGatewayDynamicCPURequest, - BaseMemoryRequest: traceGatewayBaseMemoryRequest, - DynamicMemoryRequest: traceGatewayDynamicMemoryRequest, - }, + Deployment: otelcollector.NewDeploymentConfig( + otelcollector.WithImage(config.OTelCollectorImage), + otelcollector.WithPriorityClassName(config.TraceGatewayPriorityClassName), + ), OTLPServiceName: config.TraceGatewayServiceName, } diff --git a/internal/reconciler/logpipeline/fluentbit/config.go b/internal/reconciler/logpipeline/fluentbit/config.go index dec7f24a0..8a5533bbb 100644 --- a/internal/reconciler/logpipeline/fluentbit/config.go +++ b/internal/reconciler/logpipeline/fluentbit/config.go @@ -10,6 +10,13 @@ import ( "github.com/kyma-project/telemetry-manager/internal/resources/fluentbit" ) +var ( + defaultCPULimit = resource.MustParse("1") + defaultMemoryLimit = resource.MustParse("1Gi") + defaultCPURequest = resource.MustParse("100m") + defaultMemoryRequest = resource.MustParse("50Mi") +) + type Config struct { DaemonSet types.NamespacedName SectionsConfigMap types.NamespacedName @@ -77,6 +84,12 @@ func NewConfig(baseName string, namespace string, options ...ConfigOption) *Conf EnvConfigSecret: types.NamespacedName{Name: baseName + "-env", Namespace: namespace}, TLSFileConfigSecret: types.NamespacedName{Name: baseName + "-output-tls-config", Namespace: namespace}, DaemonSet: types.NamespacedName{Name: baseName, Namespace: namespace}, + DaemonSetConfig: fluentbit.DaemonSetConfig{ + CPULimit: defaultCPULimit, + MemoryLimit: defaultMemoryLimit, + CPURequest: defaultCPURequest, + MemoryRequest: defaultMemoryRequest, + }, } for _, opt := range options { diff --git a/internal/resources/otelcollector/config.go b/internal/resources/otelcollector/config.go index 292c48cff..a2bc0af51 100644 --- a/internal/resources/otelcollector/config.go +++ b/internal/resources/otelcollector/config.go @@ -4,6 +4,17 @@ import ( "k8s.io/apimachinery/pkg/api/resource" ) +var ( + defaultBaseCPULimit = resource.MustParse("700m") + defaultDynamicCPULimit = resource.MustParse("500m") + defaultBaseMemoryLimit = resource.MustParse("500Mi") + defaultDynamicMemoryLimit = resource.MustParse("1500Mi") + defaultBaseCPURequest = resource.MustParse("100m") + defaultDynamicCPURequest = resource.MustParse("100m") + defaultBaseMemoryRequest = resource.MustParse("32Mi") + defaultDynamicMemoryRequest = resource.MustParse("0") +) + type Config struct { BaseName string Namespace string @@ -29,6 +40,87 @@ type DeploymentConfig struct { DynamicMemoryRequest resource.Quantity } +type DeploymentConfigOption func(*DeploymentConfig) + +func WithImage(image string) DeploymentConfigOption { + return func(c *DeploymentConfig) { + c.Image = image + } +} + +func WithPriorityClassName(name string) DeploymentConfigOption { + return func(c *DeploymentConfig) { + c.PriorityClassName = name + } +} + +func WithBaseCPULimit(limit resource.Quantity) DeploymentConfigOption { + return func(c *DeploymentConfig) { + c.BaseCPULimit = limit + } +} + +func WithDynamicCPULimit(limit resource.Quantity) DeploymentConfigOption { + return func(c *DeploymentConfig) { + c.DynamicCPULimit = limit + } +} + +func WithBaseMemoryLimit(limit resource.Quantity) DeploymentConfigOption { + return func(c *DeploymentConfig) { + c.BaseMemoryLimit = limit + } +} + +func WithDynamicMemoryLimit(limit resource.Quantity) DeploymentConfigOption { + return func(c *DeploymentConfig) { + c.DynamicMemoryLimit = limit + } +} + +func WithBaseCPURequest(request resource.Quantity) DeploymentConfigOption { + return func(c *DeploymentConfig) { + c.BaseCPURequest = request + } +} + +func WithDynamicCPURequest(request resource.Quantity) DeploymentConfigOption { + return func(c *DeploymentConfig) { + c.DynamicCPURequest = request + } +} + +func WithBaseMemoryRequest(request resource.Quantity) DeploymentConfigOption { + return func(c *DeploymentConfig) { + c.BaseMemoryRequest = request + } +} + +func WithDynamicMemoryRequest(request resource.Quantity) DeploymentConfigOption { + return func(c *DeploymentConfig) { + c.DynamicMemoryRequest = request + } +} + +func NewDeploymentConfig(opts ...DeploymentConfigOption) DeploymentConfig { + config := DeploymentConfig{ + BaseCPULimit: defaultBaseCPULimit, + DynamicCPULimit: defaultDynamicCPULimit, + BaseMemoryLimit: defaultBaseMemoryLimit, + DynamicMemoryLimit: defaultDynamicMemoryLimit, + BaseCPURequest: defaultBaseCPURequest, + DynamicCPURequest: defaultDynamicCPURequest, + BaseMemoryRequest: defaultBaseMemoryRequest, + DynamicMemoryRequest: defaultDynamicMemoryRequest, + } + + for _, opt := range opts { + opt(&config) + } + + return config +} + type AgentConfig struct { Config