From 6c70aa48ec7865d11c5ccbcf58476ff933c59778 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20=C5=9Awi=C4=85tek?= Date: Mon, 15 Apr 2024 18:04:59 +0200 Subject: [PATCH] wip: change default otel image --- apis/v1alpha1/convert.go | 62 ++++++++++++- ...emetry-operator.clusterserviceversion.yaml | 2 +- internal/distros/coredistro.go | 59 +++++++++++++ internal/distros/image.go | 88 +++++++++++++++++++ internal/distros/k8sdistro.go | 81 +++++++++++++++++ main.go | 2 +- 6 files changed, 290 insertions(+), 4 deletions(-) create mode 100644 internal/distros/coredistro.go create mode 100644 internal/distros/image.go create mode 100644 internal/distros/k8sdistro.go diff --git a/apis/v1alpha1/convert.go b/apis/v1alpha1/convert.go index 1f51d5d97c..3ab8f0e161 100644 --- a/apis/v1alpha1/convert.go +++ b/apis/v1alpha1/convert.go @@ -25,6 +25,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/conversion" "github.com/open-telemetry/opentelemetry-operator/apis/v1beta1" + "github.com/open-telemetry/opentelemetry-operator/internal/distros" + "github.com/open-telemetry/opentelemetry-operator/internal/version" ) var _ conversion.Convertible = &OpenTelemetryCollector{} @@ -94,7 +96,7 @@ func tov1beta1(in OpenTelemetryCollector) (v1beta1.OpenTelemetryCollector, error PodSecurityContext: copy.Spec.PodSecurityContext, PodAnnotations: copy.Spec.PodAnnotations, ServiceAccount: copy.Spec.ServiceAccount, - Image: copy.Spec.Image, + Image: tov1beta1Image(copy.Spec.Image, *cfg), ImagePullPolicy: copy.Spec.ImagePullPolicy, VolumeMounts: copy.Spec.VolumeMounts, Ports: tov1beta1Ports(copy.Spec.Ports), @@ -270,6 +272,34 @@ func tov1beta1ConfigMaps(in []ConfigMapsSpec) []v1beta1.ConfigMapsSpec { return mapsSpecs } +func tov1beta1Image(image string, otelcolConfig v1beta1.Config) string { + if image != "" { + return image + } + + config := distros.OtelcolConfig{ + Receivers: otelcolConfig.Receivers.Object, + Exporters: otelcolConfig.Exporters.Object, + } + if otelcolConfig.Connectors != nil { + config.Connectors = otelcolConfig.Connectors.Object + } + if otelcolConfig.Processors != nil { + config.Connectors = otelcolConfig.Processors.Object + } + if otelcolConfig.Extensions != nil { + config.Connectors = otelcolConfig.Extensions.Object + } + + if distros.IsValidConfigK8sDistro(config) { + return "" + } + + return fmt.Sprintf( + "ghcr.io/open-telemetry/opentelemetry-collector-releases/opentelemetry-collector:%s", + version.OpenTelemetryCollector()) +} + func tov1alpha1Ports(in []v1beta1.PortsSpec) []PortsSpec { var ports []PortsSpec @@ -323,7 +353,7 @@ func tov1alpha1(in v1beta1.OpenTelemetryCollector) (*OpenTelemetryCollector, err TargetAllocator: tov1alpha1TA(copy.Spec.TargetAllocator), Mode: Mode(copy.Spec.Mode), ServiceAccount: copy.Spec.ServiceAccount, - Image: copy.Spec.Image, + Image: tov1alpha1Image(copy.Spec.Image, copy.Spec.Config), UpgradeStrategy: UpgradeStrategy(copy.Spec.UpgradeStrategy), ImagePullPolicy: copy.Spec.ImagePullPolicy, Config: configYaml, @@ -466,3 +496,31 @@ func tov1alpha1TA(in v1beta1.TargetAllocatorEmbedded) OpenTelemetryTargetAllocat PodDisruptionBudget: tov1alpha1PodDisruptionBudget(in.PodDisruptionBudget), } } + +func tov1alpha1Image(image string, otelcolConfig v1beta1.Config) string { + if image != "" { + return image + } + + config := distros.OtelcolConfig{ + Receivers: otelcolConfig.Receivers.Object, + Exporters: otelcolConfig.Exporters.Object, + } + if otelcolConfig.Connectors != nil { + config.Connectors = otelcolConfig.Connectors.Object + } + if otelcolConfig.Processors != nil { + config.Connectors = otelcolConfig.Processors.Object + } + if otelcolConfig.Extensions != nil { + config.Connectors = otelcolConfig.Extensions.Object + } + + if distros.IsValidConfigCoreDistro(config) { + return "" + } + + return fmt.Sprintf( + "ghcr.io/open-telemetry/opentelemetry-collector-releases/opentelemetry-collector-k8s:%s", + version.OpenTelemetryCollector()) +} diff --git a/bundle/manifests/opentelemetry-operator.clusterserviceversion.yaml b/bundle/manifests/opentelemetry-operator.clusterserviceversion.yaml index c8116daa19..e6c6a34ec2 100644 --- a/bundle/manifests/opentelemetry-operator.clusterserviceversion.yaml +++ b/bundle/manifests/opentelemetry-operator.clusterserviceversion.yaml @@ -99,7 +99,7 @@ metadata: categories: Logging & Tracing,Monitoring certified: "false" containerImage: ghcr.io/open-telemetry/opentelemetry-operator/opentelemetry-operator - createdAt: "2024-04-11T16:00:15Z" + createdAt: "2024-04-25T11:55:48Z" description: Provides the OpenTelemetry components, including the Collector operators.operatorframework.io/builder: operator-sdk-v1.29.0 operators.operatorframework.io/project_layout: go.kubebuilder.io/v3 diff --git a/internal/distros/coredistro.go b/internal/distros/coredistro.go new file mode 100644 index 0000000000..071e964173 --- /dev/null +++ b/internal/distros/coredistro.go @@ -0,0 +1,59 @@ +// Copyright The OpenTelemetry Authors +// +// 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 distros + +var CoreReceivers = []string{ + "hostmetrics", + "jaeger", + "kafka", + "nop", + "opencensus", + "otlp", + "prometheus", + "zipkin", +} +var CoreProcessors = []string{ + "attributes", + "batch", + "filter", + "memory_limiter", + "probabilistic_sampler", + "resource", + "span", +} +var CoreExporters = []string{ + "debug", + "file", + "kafka", + "logging", + "nop", + "opencensus", + "otlp", + "otlphttp", + "prometheus", + "prometheusremotewrite", + "zipkin", +} + +var CoreExtensions = []string{ + "health_check", + "memory_ballast", + "pprof", + "zpages", +} + +var CoreConnectors = []string{ + "forward", +} diff --git a/internal/distros/image.go b/internal/distros/image.go new file mode 100644 index 0000000000..649a9ae576 --- /dev/null +++ b/internal/distros/image.go @@ -0,0 +1,88 @@ +// Copyright The OpenTelemetry Authors +// +// 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 distros + +import "k8s.io/utils/strings/slices" + +type OtelcolConfig struct { + // +kubebuilder:pruning:PreserveUnknownFields + Receivers map[string]any `json:"receivers" yaml:"receivers"` + // +kubebuilder:pruning:PreserveUnknownFields + Exporters map[string]any `json:"exporters" yaml:"exporters"` + // +kubebuilder:pruning:PreserveUnknownFields + Processors map[string]any `json:"processors,omitempty" yaml:"processors,omitempty"` + // +kubebuilder:pruning:PreserveUnknownFields + Connectors map[string]any `json:"connectors,omitempty" yaml:"connectors,omitempty"` + // +kubebuilder:pruning:PreserveUnknownFields + Extensions map[string]any `json:"extensions,omitempty" yaml:"extensions,omitempty"` +} + +func IsValidConfigK8sDistro(config OtelcolConfig) bool { + for receiver := range config.Receivers { + if !slices.Contains(K8sReceivers, receiver) { + return false + } + } + for exporter := range config.Exporters { + if !slices.Contains(K8sExporters, exporter) { + return false + } + } + for processor := range config.Processors { + if !slices.Contains(K8sProcessors, processor) { + return false + } + } + for connector := range config.Connectors { + if !slices.Contains(K8sConnectors, connector) { + return false + } + } + for extension := range config.Extensions { + if !slices.Contains(K8sConnectors, extension) { + return false + } + } + return true +} + +func IsValidConfigCoreDistro(config OtelcolConfig) bool { + for receiver := range config.Receivers { + if !slices.Contains(CoreReceivers, receiver) { + return false + } + } + for exporter := range config.Exporters { + if !slices.Contains(CoreExporters, exporter) { + return false + } + } + for processor := range config.Processors { + if !slices.Contains(CoreProcessors, processor) { + return false + } + } + for connector := range config.Connectors { + if !slices.Contains(CoreConnectors, connector) { + return false + } + } + for extension := range config.Extensions { + if !slices.Contains(CoreConnectors, extension) { + return false + } + } + return true +} diff --git a/internal/distros/k8sdistro.go b/internal/distros/k8sdistro.go new file mode 100644 index 0000000000..3965f4be36 --- /dev/null +++ b/internal/distros/k8sdistro.go @@ -0,0 +1,81 @@ +// Copyright The OpenTelemetry Authors +// +// 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 distros + +var K8sReceivers = []string{ + "filelog", + "fluentforward", + "hostmetrics", + "httpcheck", + "jaeger", + "journald", + "k8s_cluster", + "k8s_events", + "k8sobjects", + "kubeletstats", + "opencensus", + "otlp", + "prometheus", + "receiver_creator", + "zipkin", +} +var K8sProcessors = []string{ + "attributes", + "batch", + "cumulativetodelta", + "deltatorate", + "filter", + "groupbyattrs", + "groupbytrace", + "k8sattributes", + "memory_limiter", + "metricstransform", + "probabilistic_sampler", + "redaction", + "remotetap", + "resource", + "resourcedetection", + "tail_sampling", + "transform", +} +var K8sExporters = []string{ + "debug", + "file", + "loadbalancing", + "otlp", + "otlphttp", +} + +var K8sExtensions = []string{ + "basicauth", + "bearertokenauth", + "file_storage", + "headers_setter", + "health_check", + "host_observer", + "k8s_observer", + "oauth2client", + "oidc", + "pprof", + "zpages", +} + +var K8sConnectors = []string{ + "count", + "forward", + "routing", + "servicegraph", + "spanmetrics", +} diff --git a/main.go b/main.go index 0d89d09b22..a87b30d344 100644 --- a/main.go +++ b/main.go @@ -144,7 +144,7 @@ func main() { pflag.BoolVar(&enablePythonInstrumentation, constants.FlagPython, true, "Controls whether the operator supports python auto-instrumentation") pflag.BoolVar(&enableNginxInstrumentation, constants.FlagNginx, false, "Controls whether the operator supports nginx auto-instrumentation") pflag.BoolVar(&enableJavaInstrumentation, constants.FlagJava, true, "Controls whether the operator supports java auto-instrumentation") - stringFlagOrEnv(&collectorImage, "collector-image", "RELATED_IMAGE_COLLECTOR", fmt.Sprintf("ghcr.io/open-telemetry/opentelemetry-collector-releases/opentelemetry-collector:%s", v.OpenTelemetryCollector), "The default OpenTelemetry collector image. This image is used when no image is specified in the CustomResource.") + stringFlagOrEnv(&collectorImage, "collector-image", "RELATED_IMAGE_COLLECTOR", fmt.Sprintf("ghcr.io/open-telemetry/opentelemetry-collector-releases/opentelemetry-collector-k8s:%s", v.OpenTelemetryCollector), "The default OpenTelemetry collector image. This image is used when no image is specified in the CustomResource.") stringFlagOrEnv(&targetAllocatorImage, "target-allocator-image", "RELATED_IMAGE_TARGET_ALLOCATOR", fmt.Sprintf("ghcr.io/open-telemetry/opentelemetry-operator/target-allocator:%s", v.TargetAllocator), "The default OpenTelemetry target allocator image. This image is used when no image is specified in the CustomResource.") stringFlagOrEnv(&operatorOpAMPBridgeImage, "operator-opamp-bridge-image", "RELATED_IMAGE_OPERATOR_OPAMP_BRIDGE", fmt.Sprintf("ghcr.io/open-telemetry/opentelemetry-operator/operator-opamp-bridge:%s", v.OperatorOpAMPBridge), "The default OpenTelemetry Operator OpAMP Bridge image. This image is used when no image is specified in the CustomResource.") stringFlagOrEnv(&autoInstrumentationJava, "auto-instrumentation-java-image", "RELATED_IMAGE_AUTO_INSTRUMENTATION_JAVA", fmt.Sprintf("ghcr.io/open-telemetry/opentelemetry-operator/autoinstrumentation-java:%s", v.AutoInstrumentationJava), "The default OpenTelemetry Java instrumentation image. This image is used when no image is specified in the CustomResource.")