From 16c93f9def8d74b214027dc6670f530203426ccc Mon Sep 17 00:00:00 2001 From: Aaron Rhodes Date: Wed, 16 Oct 2024 10:18:25 -0400 Subject: [PATCH 1/6] [NDM] Fix for ignored_ip_addresses in autodiscovery --- pkg/config/autodiscovery/autodiscovery.go | 7 +++--- .../autodiscovery/autodiscovery_test.go | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/pkg/config/autodiscovery/autodiscovery.go b/pkg/config/autodiscovery/autodiscovery.go index 1f1455c39761e..c1fab80232a55 100644 --- a/pkg/config/autodiscovery/autodiscovery.go +++ b/pkg/config/autodiscovery/autodiscovery.go @@ -75,10 +75,11 @@ func DiscoverComponentsFromConfig() ([]pkgconfigsetup.ConfigurationProviders, [] } // Auto-activate autodiscovery without listeners: - snmp - configs := []snmplistener.Config{} - err := pkgconfigsetup.Datadog().UnmarshalKey("network_devices.autodiscovery.configs", &configs) + snmpConfig, err := snmplistener.NewListenerConfig() - if err == nil && len(configs) > 0 { + if err != nil { + log.Errorf("Error unmarshalling snmp listener config. Error: %v", err) + } else if len(snmpConfig.Configs) > 0 { detectedListeners = append(detectedListeners, pkgconfigsetup.Listeners{Name: "snmp"}) log.Info("Configs for autodiscovery detected: Adding the snmp listener") } diff --git a/pkg/config/autodiscovery/autodiscovery_test.go b/pkg/config/autodiscovery/autodiscovery_test.go index 09a7f28ae1a04..1696c1b1e79bc 100644 --- a/pkg/config/autodiscovery/autodiscovery_test.go +++ b/pkg/config/autodiscovery/autodiscovery_test.go @@ -12,6 +12,7 @@ import ( "github.com/stretchr/testify/assert" pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup" + snmplistener "github.com/DataDog/datadog-agent/pkg/snmp" ) func TestDiscoverComponentsFromConfigForSnmp(t *testing.T) { @@ -46,3 +47,25 @@ snmp_listener: _, configListeners = DiscoverComponentsFromConfig() assert.Empty(t, len(configListeners)) } + +func TestNewListenerConfigForSnmp(t *testing.T) { + pkgconfigsetup.Datadog().SetConfigType("yaml") + + err := pkgconfigsetup.Datadog().ReadConfig(strings.NewReader(` +network_devices: + autodiscovery: + configs: + - network_address: 127.0.0.1/30 + ignored_ip_addresses: + - 127.0.0.2 +`)) + assert.NoError(t, err) + + snmpConfig, err := snmplistener.NewListenerConfig() + assert.NoError(t, err) + + assert.Len(t, snmpConfig.Configs, 1) + assert.Equal(t, "127.0.0.1/30", snmpConfig.Configs[0].Network) + assert.Equal(t, 1, len(snmpConfig.Configs[0].IgnoredIPAddresses)) + assert.Contains(t, snmpConfig.Configs[0].IgnoredIPAddresses, "127.0.0.2") +} From b9324aeca0ec0488def0e5692913028a975ad593 Mon Sep 17 00:00:00 2001 From: Aaron Rhodes Date: Wed, 16 Oct 2024 12:48:49 -0400 Subject: [PATCH 2/6] Update to unit test --- .../autodiscovery/autodiscovery_test.go | 21 ++++++------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/pkg/config/autodiscovery/autodiscovery_test.go b/pkg/config/autodiscovery/autodiscovery_test.go index 1696c1b1e79bc..adff4da2c4c8f 100644 --- a/pkg/config/autodiscovery/autodiscovery_test.go +++ b/pkg/config/autodiscovery/autodiscovery_test.go @@ -12,7 +12,6 @@ import ( "github.com/stretchr/testify/assert" pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup" - snmplistener "github.com/DataDog/datadog-agent/pkg/snmp" ) func TestDiscoverComponentsFromConfigForSnmp(t *testing.T) { @@ -45,13 +44,10 @@ snmp_listener: `)) assert.NoError(t, err) _, configListeners = DiscoverComponentsFromConfig() - assert.Empty(t, len(configListeners)) -} - -func TestNewListenerConfigForSnmp(t *testing.T) { - pkgconfigsetup.Datadog().SetConfigType("yaml") + assert.Len(t, configListeners, 1) + assert.Equal(t, "snmp", configListeners[0].Name) - err := pkgconfigsetup.Datadog().ReadConfig(strings.NewReader(` + err = pkgconfigsetup.Datadog().ReadConfig(strings.NewReader(` network_devices: autodiscovery: configs: @@ -60,12 +56,7 @@ network_devices: - 127.0.0.2 `)) assert.NoError(t, err) - - snmpConfig, err := snmplistener.NewListenerConfig() - assert.NoError(t, err) - - assert.Len(t, snmpConfig.Configs, 1) - assert.Equal(t, "127.0.0.1/30", snmpConfig.Configs[0].Network) - assert.Equal(t, 1, len(snmpConfig.Configs[0].IgnoredIPAddresses)) - assert.Contains(t, snmpConfig.Configs[0].IgnoredIPAddresses, "127.0.0.2") + _, configListeners = DiscoverComponentsFromConfig() + assert.Len(t, configListeners, 1) + assert.Equal(t, "snmp", configListeners[0].Name) } From 7c788b9b5ac019c74b16547007a70d22fcad4e77 Mon Sep 17 00:00:00 2001 From: Aaron Rhodes Date: Wed, 16 Oct 2024 14:29:49 -0400 Subject: [PATCH 3/6] Minor change to trigger a rerun of integration tests --- pkg/config/autodiscovery/autodiscovery_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/config/autodiscovery/autodiscovery_test.go b/pkg/config/autodiscovery/autodiscovery_test.go index adff4da2c4c8f..1289f46e6c309 100644 --- a/pkg/config/autodiscovery/autodiscovery_test.go +++ b/pkg/config/autodiscovery/autodiscovery_test.go @@ -53,7 +53,7 @@ network_devices: configs: - network_address: 127.0.0.1/30 ignored_ip_addresses: - - 127.0.0.2 + - 127.0.0.3 `)) assert.NoError(t, err) _, configListeners = DiscoverComponentsFromConfig() From 061210fd33211409f3e9c426a6d3bcb6400bc33e Mon Sep 17 00:00:00 2001 From: Aaron Rhodes Date: Wed, 23 Oct 2024 09:44:33 -0400 Subject: [PATCH 4/6] Update pkg/config/autodiscovery/autodiscovery_test.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Lénaïc Huard --- pkg/config/autodiscovery/autodiscovery_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/config/autodiscovery/autodiscovery_test.go b/pkg/config/autodiscovery/autodiscovery_test.go index 1289f46e6c309..e05a4a7a65cd6 100644 --- a/pkg/config/autodiscovery/autodiscovery_test.go +++ b/pkg/config/autodiscovery/autodiscovery_test.go @@ -44,7 +44,7 @@ snmp_listener: `)) assert.NoError(t, err) _, configListeners = DiscoverComponentsFromConfig() - assert.Len(t, configListeners, 1) + require.Len(t, configListeners, 1) assert.Equal(t, "snmp", configListeners[0].Name) err = pkgconfigsetup.Datadog().ReadConfig(strings.NewReader(` From 9874fd23e8e0d3fd945c46b75cd3971d155941d1 Mon Sep 17 00:00:00 2001 From: Aaron Rhodes Date: Wed, 23 Oct 2024 09:43:44 -0400 Subject: [PATCH 5/6] Add release note for bug fix --- .../fix-config-autodiscovery-49d61b77e8f76f58.yaml | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 releasenotes/notes/fix-config-autodiscovery-49d61b77e8f76f58.yaml diff --git a/releasenotes/notes/fix-config-autodiscovery-49d61b77e8f76f58.yaml b/releasenotes/notes/fix-config-autodiscovery-49d61b77e8f76f58.yaml new file mode 100644 index 0000000000000..245f2938b6efa --- /dev/null +++ b/releasenotes/notes/fix-config-autodiscovery-49d61b77e8f76f58.yaml @@ -0,0 +1,11 @@ +# Each section from every release note are combined when the +# CHANGELOG.rst is rendered. So the text needs to be worded so that +# it does not depend on any information only available in another +# section. This may mean repeating some details, but each section +# must be readable independently of the other. +# +# Each section note must be formatted as reStructuredText. +--- +fixes: + - | + Fix a bug in the config parser that broke ignored_ip_addresses from working in NDM Autodiscovery. From a5addb82df84675a099762abbfbb3dad892b8be5 Mon Sep 17 00:00:00 2001 From: Aaron Rhodes Date: Wed, 23 Oct 2024 09:49:02 -0400 Subject: [PATCH 6/6] Finishing updating unit test to use require --- pkg/config/autodiscovery/autodiscovery_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/config/autodiscovery/autodiscovery_test.go b/pkg/config/autodiscovery/autodiscovery_test.go index e05a4a7a65cd6..811a72de9edf5 100644 --- a/pkg/config/autodiscovery/autodiscovery_test.go +++ b/pkg/config/autodiscovery/autodiscovery_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup" ) @@ -25,7 +26,7 @@ network_devices: `)) assert.NoError(t, err) _, configListeners := DiscoverComponentsFromConfig() - assert.Len(t, configListeners, 1) + require.Len(t, configListeners, 1) assert.Equal(t, "snmp", configListeners[0].Name) err = pkgconfigsetup.Datadog().ReadConfig(strings.NewReader(` @@ -57,6 +58,6 @@ network_devices: `)) assert.NoError(t, err) _, configListeners = DiscoverComponentsFromConfig() - assert.Len(t, configListeners, 1) + require.Len(t, configListeners, 1) assert.Equal(t, "snmp", configListeners[0].Name) }