diff --git a/.chloggen/feat_targetallocator-selector.yaml b/.chloggen/feat_targetallocator-selector.yaml index 54136547da..13df1836c4 100755 --- a/.chloggen/feat_targetallocator-selector.yaml +++ b/.chloggen/feat_targetallocator-selector.yaml @@ -13,4 +13,6 @@ issues: [2422] # (Optional) One or more lines of additional information to render under the primary note. # These lines will be padded with 2 spaces and then inserted directly into the document. # Use pipe (|) for multiline entries. -subtext: This is a breaking change only for users of standalone target allocator. Operator users are unaffected. +subtext: | + This is a breaking change only for users of standalone target allocator. Operator users are unaffected. + The operator is still compatible with previous target allocator versions, and will be for the next 3 releases. diff --git a/cmd/otel-allocator/config/config.go b/cmd/otel-allocator/config/config.go index edece24c98..d7aea48d35 100644 --- a/cmd/otel-allocator/config/config.go +++ b/cmd/otel-allocator/config/config.go @@ -40,17 +40,17 @@ const DefaultConfigFilePath string = "/conf/targetallocator.yaml" const DefaultCRScrapeInterval model.Duration = model.Duration(time.Second * 30) type Config struct { - ListenAddr string `yaml:"listen_addr,omitempty"` - KubeConfigFilePath string `yaml:"kube_config_file_path,omitempty"` - ClusterConfig *rest.Config `yaml:"-"` - RootLogger logr.Logger `yaml:"-"` - CollectorSelector metav1.LabelSelector `yaml:"collector_selector,omitempty"` - PromConfig *promconfig.Config `yaml:"config"` - AllocationStrategy *string `yaml:"allocation_strategy,omitempty"` - FilterStrategy *string `yaml:"filter_strategy,omitempty"` - PrometheusCR PrometheusCRConfig `yaml:"prometheus_cr,omitempty"` - PodMonitorSelector map[string]string `yaml:"pod_monitor_selector,omitempty"` - ServiceMonitorSelector map[string]string `yaml:"service_monitor_selector,omitempty"` + ListenAddr string `yaml:"listen_addr,omitempty"` + KubeConfigFilePath string `yaml:"kube_config_file_path,omitempty"` + ClusterConfig *rest.Config `yaml:"-"` + RootLogger logr.Logger `yaml:"-"` + CollectorSelector *metav1.LabelSelector `yaml:"collector_selector,omitempty"` + PromConfig *promconfig.Config `yaml:"config"` + AllocationStrategy *string `yaml:"allocation_strategy,omitempty"` + FilterStrategy *string `yaml:"filter_strategy,omitempty"` + PrometheusCR PrometheusCRConfig `yaml:"prometheus_cr,omitempty"` + PodMonitorSelector map[string]string `yaml:"pod_monitor_selector,omitempty"` + ServiceMonitorSelector map[string]string `yaml:"service_monitor_selector,omitempty"` } type PrometheusCRConfig struct { diff --git a/cmd/otel-allocator/config/config_test.go b/cmd/otel-allocator/config/config_test.go index d82df4f52c..b7ffcaf954 100644 --- a/cmd/otel-allocator/config/config_test.go +++ b/cmd/otel-allocator/config/config_test.go @@ -45,7 +45,7 @@ func TestLoad(t *testing.T) { file: "./testdata/config_test.yaml", }, want: Config{ - CollectorSelector: metav1.LabelSelector{ + CollectorSelector: &metav1.LabelSelector{ MatchLabels: map[string]string{ "app.kubernetes.io/instance": "default.test", "app.kubernetes.io/managed-by": "opentelemetry-operator", @@ -111,7 +111,7 @@ func TestLoad(t *testing.T) { file: "./testdata/pod_service_selector_test.yaml", }, want: Config{ - CollectorSelector: metav1.LabelSelector{ + CollectorSelector: &metav1.LabelSelector{ MatchLabels: map[string]string{ "app.kubernetes.io/instance": "default.test", "app.kubernetes.io/managed-by": "opentelemetry-operator", diff --git a/cmd/otel-allocator/main.go b/cmd/otel-allocator/main.go index 7707bfc64e..5bda64003f 100644 --- a/cmd/otel-allocator/main.go +++ b/cmd/otel-allocator/main.go @@ -149,7 +149,7 @@ func main() { }) runGroup.Add( func() error { - err := collectorWatcher.Watch(ctx, &cfg.CollectorSelector, allocator.SetCollectors) + err := collectorWatcher.Watch(ctx, cfg.CollectorSelector, allocator.SetCollectors) setupLog.Info("Collector watcher exited") return err }, diff --git a/controllers/builder_test.go b/controllers/builder_test.go index 06a3ad0125..807d0f9d05 100644 --- a/controllers/builder_test.go +++ b/controllers/builder_test.go @@ -1340,6 +1340,11 @@ config: source_labels: - __meta_service_name target_label: instance +label_selector: + app.kubernetes.io/component: opentelemetry-collector + app.kubernetes.io/instance: test.test + app.kubernetes.io/managed-by: opentelemetry-operator + app.kubernetes.io/part-of: opentelemetry `, }, }, @@ -1372,7 +1377,7 @@ config: "app.kubernetes.io/version": "latest", }, Annotations: map[string]string{ - "opentelemetry-targetallocator-config/hash": "40afbbdb738923bf9cb4a117648cd86030cc5b748601d19120226eb1ee74c91a", + "opentelemetry-targetallocator-config/hash": "20c09760c240d08287ff05bd2375985220b577d938e82efd85467e17174690e0", }, }, Spec: corev1.PodSpec{ @@ -1724,6 +1729,11 @@ config: source_labels: - __meta_service_name target_label: instance +label_selector: + app.kubernetes.io/component: opentelemetry-collector + app.kubernetes.io/instance: test.test + app.kubernetes.io/managed-by: opentelemetry-operator + app.kubernetes.io/part-of: opentelemetry `, }, }, @@ -1756,7 +1766,7 @@ config: "app.kubernetes.io/version": "latest", }, Annotations: map[string]string{ - "opentelemetry-targetallocator-config/hash": "40afbbdb738923bf9cb4a117648cd86030cc5b748601d19120226eb1ee74c91a", + "opentelemetry-targetallocator-config/hash": "20c09760c240d08287ff05bd2375985220b577d938e82efd85467e17174690e0", }, }, Spec: corev1.PodSpec{ diff --git a/controllers/reconcile_test.go b/controllers/reconcile_test.go index d229e4b1c7..817da2eab1 100644 --- a/controllers/reconcile_test.go +++ b/controllers/reconcile_test.go @@ -451,6 +451,12 @@ func TestOpenTelemetryCollectorReconciler_Reconcile(t *testing.T) { "app.kubernetes.io/part-of": "opentelemetry", }, } + taConfig["label_selector"] = map[string]string{ + "app.kubernetes.io/instance": "default.test", + "app.kubernetes.io/managed-by": "opentelemetry-operator", + "app.kubernetes.io/component": "opentelemetry-collector", + "app.kubernetes.io/part-of": "opentelemetry", + } taConfig["config"] = promConfig["config"] taConfig["allocation_strategy"] = "least-weighted" taConfig["prometheus_cr"] = map[string]string{ diff --git a/internal/manifests/targetallocator/configmap.go b/internal/manifests/targetallocator/configmap.go index ada968f27c..8f5fc76617 100644 --- a/internal/manifests/targetallocator/configmap.go +++ b/internal/manifests/targetallocator/configmap.go @@ -44,9 +44,13 @@ func ConfigMap(params manifests.Params) (*corev1.ConfigMap, error) { taConfig := make(map[interface{}]interface{}) prometheusCRConfig := make(map[interface{}]interface{}) + collectorSelectorLabels := manifestutils.SelectorLabels(params.OtelCol.ObjectMeta, collector.ComponentOpenTelemetryCollector) taConfig["collector_selector"] = map[string]any{ - "matchlabels": manifestutils.SelectorLabels(params.OtelCol.ObjectMeta, collector.ComponentOpenTelemetryCollector), + "matchlabels": collectorSelectorLabels, } + // The below instruction is here for compatibility with the previous target allocator version + // TODO: Drop it after 3 more versions + taConfig["label_selector"] = collectorSelectorLabels // We only take the "config" from the returned object, if it's present if prometheusConfig, ok := prometheusReceiverConfig["config"]; ok { taConfig["config"] = prometheusConfig diff --git a/internal/manifests/targetallocator/configmap_test.go b/internal/manifests/targetallocator/configmap_test.go index aaf90f672d..a73f9c17d9 100644 --- a/internal/manifests/targetallocator/configmap_test.go +++ b/internal/manifests/targetallocator/configmap_test.go @@ -54,6 +54,11 @@ config: - targets: - 0.0.0.0:8888 - 0.0.0.0:9999 +label_selector: + app.kubernetes.io/component: opentelemetry-collector + app.kubernetes.io/instance: default.my-instance + app.kubernetes.io/managed-by: opentelemetry-operator + app.kubernetes.io/part-of: opentelemetry `, } instance := collectorInstance() @@ -91,6 +96,11 @@ config: - targets: - 0.0.0.0:8888 - 0.0.0.0:9999 +label_selector: + app.kubernetes.io/component: opentelemetry-collector + app.kubernetes.io/instance: default.my-instance + app.kubernetes.io/managed-by: opentelemetry-operator + app.kubernetes.io/part-of: opentelemetry pod_monitor_selector: release: my-instance service_monitor_selector: @@ -138,6 +148,11 @@ config: - targets: - 0.0.0.0:8888 - 0.0.0.0:9999 +label_selector: + app.kubernetes.io/component: opentelemetry-collector + app.kubernetes.io/instance: default.my-instance + app.kubernetes.io/managed-by: opentelemetry-operator + app.kubernetes.io/part-of: opentelemetry prometheus_cr: scrape_interval: 30s `,