From 81d98b2722c20ef9e048cc9b6a093eaad6453242 Mon Sep 17 00:00:00 2001
From: Pavol Loffay
Date: Mon, 3 Feb 2025 15:59:07 +0100
Subject: [PATCH] Fix defaulting a component with empty value
Signed-off-by: Pavol Loffay
---
.chloggen/3452.yaml | 22 ++++++++++++++++++++++
apis/v1beta1/config.go | 3 +++
apis/v1beta1/config_test.go | 16 ++++++++++++++++
apis/v1beta1/testdata/issue-3452.yaml | 6 ++++++
4 files changed, 47 insertions(+)
create mode 100755 .chloggen/3452.yaml
create mode 100644 apis/v1beta1/testdata/issue-3452.yaml
diff --git a/.chloggen/3452.yaml b/.chloggen/3452.yaml
new file mode 100755
index 0000000000..64a8a26fc7
--- /dev/null
+++ b/.chloggen/3452.yaml
@@ -0,0 +1,22 @@
+# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
+change_type: bug_fix
+
+# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
+component: collector
+
+# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
+note: Fix config handling with null values.
+
+# One or more tracking issues related to the change
+issues: [3452]
+
+# (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: |
+ Fix the defaulting webhook error `src and dst must not be nil` caused by defaulting a known component with a null value:
+ e.g.
+ ```yaml
+ receivers:
+ zipkin:
+ ```
\ No newline at end of file
diff --git a/apis/v1beta1/config.go b/apis/v1beta1/config.go
index a7fd65835d..9307dd3113 100644
--- a/apis/v1beta1/config.go
+++ b/apis/v1beta1/config.go
@@ -306,6 +306,9 @@ func (c *Config) applyDefaultForComponentKinds(logger logr.Logger, componentKind
continue
}
+ if componentConf == nil {
+ componentConf = map[string]interface{}{}
+ }
if err := mergo.Merge(&mappedCfg, componentConf); err != nil {
return err
}
diff --git a/apis/v1beta1/config_test.go b/apis/v1beta1/config_test.go
index cb631889ee..a809afa2cd 100644
--- a/apis/v1beta1/config_test.go
+++ b/apis/v1beta1/config_test.go
@@ -77,6 +77,22 @@ func TestNullObjects(t *testing.T) {
assert.Equal(t, []string{"connectors.spanmetrics:", "exporters.otlp.endpoint:", "extensions.health_check:", "processors.batch:", "receivers.otlp.protocols.grpc:", "receivers.otlp.protocols.http:"}, nullObjects)
}
+func TestNullObjects_issue_3445(t *testing.T) {
+ collectorYaml, err := os.ReadFile("./testdata/issue-3452.yaml")
+ require.NoError(t, err)
+
+ collectorJson, err := yaml.YAMLToJSON(collectorYaml)
+ require.NoError(t, err)
+
+ cfg := &Config{}
+ err = json.Unmarshal(collectorJson, cfg)
+ require.NoError(t, err)
+
+ err = cfg.ApplyDefaults(logr.Discard())
+ require.NoError(t, err)
+ assert.Empty(t, cfg.nullObjects())
+}
+
func TestConfigFiles_go_yaml(t *testing.T) {
files, err := os.ReadDir("./testdata")
require.NoError(t, err)
diff --git a/apis/v1beta1/testdata/issue-3452.yaml b/apis/v1beta1/testdata/issue-3452.yaml
new file mode 100644
index 0000000000..39a580f184
--- /dev/null
+++ b/apis/v1beta1/testdata/issue-3452.yaml
@@ -0,0 +1,6 @@
+receivers:
+ zipkin:
+service:
+ pipelines:
+ traces:
+ receivers: [zipkin]
\ No newline at end of file