From 7edcbd2680c5c4393da20b7b90323ae2082a80a5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Miko=C5=82aj=20=C5=9Awi=C4=85tek?=
 <mail+sumo@mikolajswiatek.com>
Date: Sun, 14 Jan 2024 14:06:43 +0100
Subject: [PATCH] [target allocator] Set the default filter strategy in the CRD

---
 .chloggen/fix_filterstrategy-default.yaml        | 16 ++++++++++++++++
 apis/v1alpha1/opentelemetrycollector_types.go    |  3 ++-
 ...opentelemetry.io_opentelemetrycollectors.yaml |  1 +
 ...opentelemetry.io_opentelemetrycollectors.yaml |  1 +
 controllers/builder_test.go                      | 10 +++++++---
 controllers/reconcile_test.go                    |  1 +
 internal/manifests/targetallocator/configmap.go  |  4 +---
 .../manifests/targetallocator/configmap_test.go  |  3 +++
 .../manifests/targetallocator/deployment_test.go |  3 ++-
 .../e2e/targetallocator-features/00-install.yaml |  1 +
 10 files changed, 35 insertions(+), 8 deletions(-)
 create mode 100755 .chloggen/fix_filterstrategy-default.yaml

diff --git a/.chloggen/fix_filterstrategy-default.yaml b/.chloggen/fix_filterstrategy-default.yaml
new file mode 100755
index 0000000000..465545618e
--- /dev/null
+++ b/.chloggen/fix_filterstrategy-default.yaml
@@ -0,0 +1,16 @@
+# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
+change_type: enhancement
+
+# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, github action)
+component: target allocator
+
+# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
+note: Set the default filter strategy in the CRD
+
+# One or more tracking issues related to the change
+issues: [2477]
+
+# (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:
diff --git a/apis/v1alpha1/opentelemetrycollector_types.go b/apis/v1alpha1/opentelemetrycollector_types.go
index db894716e6..0c81b381a3 100644
--- a/apis/v1alpha1/opentelemetrycollector_types.go
+++ b/apis/v1alpha1/opentelemetrycollector_types.go
@@ -303,8 +303,9 @@ type OpenTelemetryTargetAllocator struct {
 	AllocationStrategy OpenTelemetryTargetAllocatorAllocationStrategy `json:"allocationStrategy,omitempty"`
 	// FilterStrategy determines how to filter targets before allocating them among the collectors.
 	// The only current option is relabel-config (drops targets based on prom relabel_config).
-	// Filtering is disabled by default.
+	// The default is relabel-config.
 	// +optional
+	// +kubebuilder:default:=relabel-config
 	FilterStrategy string `json:"filterStrategy,omitempty"`
 	// ServiceAccount indicates the name of an existing service account to use with this instance. When set,
 	// the operator will not automatically create a ServiceAccount for the TargetAllocator.
diff --git a/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml b/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml
index 0e5983e97f..e6819a4578 100644
--- a/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml
+++ b/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml
@@ -5147,6 +5147,7 @@ spec:
                       type: object
                     type: array
                   filterStrategy:
+                    default: relabel-config
                     description: FilterStrategy determines how to filter targets before
                       allocating them among the collectors. The only current option
                       is relabel-config (drops targets based on prom relabel_config).
diff --git a/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml b/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml
index cbb808876a..8a26581209 100644
--- a/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml
+++ b/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml
@@ -5144,6 +5144,7 @@ spec:
                       type: object
                     type: array
                   filterStrategy:
+                    default: relabel-config
                     description: FilterStrategy determines how to filter targets before
                       allocating them among the collectors. The only current option
                       is relabel-config (drops targets based on prom relabel_config).
diff --git a/controllers/builder_test.go b/controllers/builder_test.go
index e0f8ae461d..03549b25b0 100644
--- a/controllers/builder_test.go
+++ b/controllers/builder_test.go
@@ -1129,7 +1129,8 @@ service:
 						Image:    "test",
 						Config:   goodConfig,
 						TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{
-							Enabled: true,
+							Enabled:        true,
+							FilterStrategy: "relabel-config",
 							PrometheusCR: v1alpha1.OpenTelemetryTargetAllocatorPrometheusCR{
 								Enabled: true,
 							},
@@ -1340,6 +1341,7 @@ config:
       source_labels:
       - __meta_service_name
       target_label: instance
+filter_strategy: relabel-config
 label_selector:
   app.kubernetes.io/component: opentelemetry-collector
   app.kubernetes.io/instance: test.test
@@ -1377,7 +1379,7 @@ label_selector:
 									"app.kubernetes.io/version":    "latest",
 								},
 								Annotations: map[string]string{
-									"opentelemetry-targetallocator-config/hash": "9126d0bb3bde858f655580a9ae6d4557c69ee430b4ab9f72e08c66334efe7989",
+									"opentelemetry-targetallocator-config/hash": "bf084cbbdcb09d03a40ad2352e0869ccf75d01f5dec977938b94d5a3239ea491",
 								},
 							},
 							Spec: corev1.PodSpec{
@@ -1517,6 +1519,7 @@ label_selector:
 							PrometheusCR: v1alpha1.OpenTelemetryTargetAllocatorPrometheusCR{
 								Enabled: true,
 							},
+							FilterStrategy: "relabel-config",
 							Observability: v1alpha1.ObservabilitySpec{
 								Metrics: v1alpha1.MetricsConfigSpec{
 									EnableMetrics: true,
@@ -1729,6 +1732,7 @@ config:
       source_labels:
       - __meta_service_name
       target_label: instance
+filter_strategy: relabel-config
 label_selector:
   app.kubernetes.io/component: opentelemetry-collector
   app.kubernetes.io/instance: test.test
@@ -1766,7 +1770,7 @@ label_selector:
 									"app.kubernetes.io/version":    "latest",
 								},
 								Annotations: map[string]string{
-									"opentelemetry-targetallocator-config/hash": "9126d0bb3bde858f655580a9ae6d4557c69ee430b4ab9f72e08c66334efe7989",
+									"opentelemetry-targetallocator-config/hash": "bf084cbbdcb09d03a40ad2352e0869ccf75d01f5dec977938b94d5a3239ea491",
 								},
 							},
 							Spec: corev1.PodSpec{
diff --git a/controllers/reconcile_test.go b/controllers/reconcile_test.go
index 982a48e32f..26ecd8464c 100644
--- a/controllers/reconcile_test.go
+++ b/controllers/reconcile_test.go
@@ -459,6 +459,7 @@ func TestOpenTelemetryCollectorReconciler_Reconcile(t *testing.T) {
 							}
 							taConfig["config"] = promConfig["config"]
 							taConfig["allocation_strategy"] = "consistent-hashing"
+							taConfig["filter_strategy"] = "relabel-config"
 							taConfig["prometheus_cr"] = map[string]string{
 								"scrape_interval": "30s",
 							}
diff --git a/internal/manifests/targetallocator/configmap.go b/internal/manifests/targetallocator/configmap.go
index def642da0c..bae0b62503 100644
--- a/internal/manifests/targetallocator/configmap.go
+++ b/internal/manifests/targetallocator/configmap.go
@@ -62,9 +62,7 @@ func ConfigMap(params manifests.Params) (*corev1.ConfigMap, error) {
 		taConfig["allocation_strategy"] = v1alpha1.OpenTelemetryTargetAllocatorAllocationStrategyConsistentHashing
 	}
 
-	if len(params.OtelCol.Spec.TargetAllocator.FilterStrategy) > 0 {
-		taConfig["filter_strategy"] = params.OtelCol.Spec.TargetAllocator.FilterStrategy
-	}
+	taConfig["filter_strategy"] = params.OtelCol.Spec.TargetAllocator.FilterStrategy
 
 	if params.OtelCol.Spec.TargetAllocator.PrometheusCR.ScrapeInterval.Size() > 0 {
 		prometheusCRConfig["scrape_interval"] = params.OtelCol.Spec.TargetAllocator.PrometheusCR.ScrapeInterval.Duration
diff --git a/internal/manifests/targetallocator/configmap_test.go b/internal/manifests/targetallocator/configmap_test.go
index 3550c74b1e..10bf2e5875 100644
--- a/internal/manifests/targetallocator/configmap_test.go
+++ b/internal/manifests/targetallocator/configmap_test.go
@@ -54,6 +54,7 @@ config:
     - targets:
       - 0.0.0.0:8888
       - 0.0.0.0:9999
+filter_strategy: relabel-config
 label_selector:
   app.kubernetes.io/component: opentelemetry-collector
   app.kubernetes.io/instance: default.my-instance
@@ -96,6 +97,7 @@ config:
     - targets:
       - 0.0.0.0:8888
       - 0.0.0.0:9999
+filter_strategy: relabel-config
 label_selector:
   app.kubernetes.io/component: opentelemetry-collector
   app.kubernetes.io/instance: default.my-instance
@@ -148,6 +150,7 @@ config:
     - targets:
       - 0.0.0.0:8888
       - 0.0.0.0:9999
+filter_strategy: relabel-config
 label_selector:
   app.kubernetes.io/component: opentelemetry-collector
   app.kubernetes.io/instance: default.my-instance
diff --git a/internal/manifests/targetallocator/deployment_test.go b/internal/manifests/targetallocator/deployment_test.go
index 3bd1f81b82..007ad0c3c6 100644
--- a/internal/manifests/targetallocator/deployment_test.go
+++ b/internal/manifests/targetallocator/deployment_test.go
@@ -188,7 +188,8 @@ func collectorInstance() v1alpha1.OpenTelemetryCollector {
 			Image:  "ghcr.io/open-telemetry/opentelemetry-operator/opentelemetry-operator:0.47.0",
 			Config: string(configYAML),
 			TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{
-				Image: "ghcr.io/open-telemetry/opentelemetry-operator/opentelemetry-targetallocator:0.47.0",
+				Image:          "ghcr.io/open-telemetry/opentelemetry-operator/opentelemetry-targetallocator:0.47.0",
+				FilterStrategy: "relabel-config",
 			},
 		},
 	}
diff --git a/tests/e2e/targetallocator-features/00-install.yaml b/tests/e2e/targetallocator-features/00-install.yaml
index b859af8fac..ec078f929a 100644
--- a/tests/e2e/targetallocator-features/00-install.yaml
+++ b/tests/e2e/targetallocator-features/00-install.yaml
@@ -67,6 +67,7 @@ spec:
                     - "true"
     prometheusCR:
       enabled: true
+      filterStrategy: ""
     env:
       - name: TEST_ENV
         value: "test"