From 755e1b7af2c3f4a07d488760c65ac45a8174e1dd Mon Sep 17 00:00:00 2001 From: Erik Baranowski <39704712+erikbaranowski@users.noreply.github.com> Date: Thu, 1 Feb 2024 10:36:50 -0500 Subject: [PATCH] fix for integrations v2 masrhaling multiplexes (#6284) * fix for integrations v2 marshaling multiplexes Signed-off-by: erikbaranowski <39704712+erikbaranowski@users.noreply.github.com> * Update pkg/integrations/v2/register.go Co-authored-by: Robert Fratto --------- Signed-off-by: erikbaranowski <39704712+erikbaranowski@users.noreply.github.com> Co-authored-by: Robert Fratto --- CHANGELOG.md | 3 +++ pkg/integrations/v2/register.go | 2 +- pkg/integrations/v2/register_test.go | 33 ++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 95b159217118..7a7599bbc329 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,9 @@ Main (unreleased) - Fix an issue where blocks having the same type and the same label across modules could result in missed updates. (@thampiotr) +- Fix an issue with static integrations-next marshaling where non singletons + would cause `/-/config` to fail to marshal. (@erikbaranowski) + ### Other changes - Removed support for Windows 2012 in line with Microsoft end of life. (@mattdurham) diff --git a/pkg/integrations/v2/register.go b/pkg/integrations/v2/register.go index 0deb356c6cb9..52b26b7794c4 100644 --- a/pkg/integrations/v2/register.go +++ b/pkg/integrations/v2/register.go @@ -228,7 +228,7 @@ func MarshalYAML(v interface{}) (interface{}, error) { panic(fmt.Sprintf("config not registered: %T", data)) } - if _, exists := uniqueSingletons[fieldName]; exists { + if _, exists := uniqueSingletons[fieldName]; exists && integrationType == TypeSingleton { return nil, fmt.Errorf("integration %q may not be defined more than once", fieldName) } uniqueSingletons[fieldName] = struct{}{} diff --git a/pkg/integrations/v2/register_test.go b/pkg/integrations/v2/register_test.go index 2e883131144e..df99cf50b481 100644 --- a/pkg/integrations/v2/register_test.go +++ b/pkg/integrations/v2/register_test.go @@ -186,6 +186,39 @@ func TestIntegrationRegistration_Marshal_MultipleSingleton(t *testing.T) { require.EqualError(t, err, `integration "test" may not be defined more than once`) } +func TestIntegrationRegistration_Marshal_Multiplex(t *testing.T) { + setRegistered(t, map[Config]Type{ + &testIntegrationA{}: TypeMultiplex, + &testIntegrationB{}: TypeMultiplex, + }) + + // Generate an invalid config, which has two instances of a Singleton + // integration. + input := testFullConfig{ + Name: "John Doe", + Duration: 500 * time.Millisecond, + Default: 12345, + Configs: []Config{ + &testIntegrationA{Text: "Hello, world!", Truth: true}, + &testIntegrationA{Text: "Hello again!", Truth: true}, + }, + } + + expectedCfg := `name: John Doe +duration: 500ms +default: 12345 +test_configs: +- text: Hello, world! + truth: true +- text: Hello again! + truth: true +` + + cfg, err := yaml.Marshal(&input) + require.NoError(t, err) + require.Equal(t, expectedCfg, string(cfg)) +} + type legacyConfig struct { Text string `yaml:"text"` }