From 2d4a1bc5349080f0a16ef16d381986c1a88ec6fa Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 1 May 2024 16:03:16 +0000 Subject: [PATCH] [Fleet] Fix merge config with APM change (#3508) (#3509) (cherry picked from commit 3fbba143f3a466f20cc4990bbd49efd72047b29f) Co-authored-by: Nicolas Chaulet --- internal/pkg/config/config.go | 7 +++++++ internal/pkg/server/agent.go | 2 +- internal/pkg/server/agent_test.go | 16 +++++++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 6a9ec05df..7e511106a 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -26,6 +26,13 @@ var DefaultOptions = []ucfg.Option{ ucfg.FieldReplaceValues("inputs"), } +var MergeOptions = []ucfg.Option{ + ucfg.PathSep("."), + ucfg.ResolveEnv, + ucfg.VarExp, + ucfg.FieldMergeValues("inputs"), +} + const kRedacted = "[redacted]" // Config is the global configuration. diff --git a/internal/pkg/server/agent.go b/internal/pkg/server/agent.go index 0d39a1c92..1e1a4a372 100644 --- a/internal/pkg/server/agent.go +++ b/internal/pkg/server/agent.go @@ -433,7 +433,7 @@ func (a *Agent) configFromUnits(ctx context.Context) (*config.Config, error) { }, }, }} - err = cfgData.Merge(obj, config.DefaultOptions...) + err = cfgData.Merge(obj, config.MergeOptions...) if err != nil { zerolog.Ctx(ctx).Warn().Err(err).Msg("Failed to merge APM config into cfgData") } diff --git a/internal/pkg/server/agent_test.go b/internal/pkg/server/agent_test.go index a92d9ba91..13852ed77 100644 --- a/internal/pkg/server/agent_test.go +++ b/internal/pkg/server/agent_test.go @@ -253,6 +253,9 @@ func Test_Agent_configFromUnits(t *testing.T) { "server": map[string]interface{}{ "host": "0.0.0.0", }, + "policy": map[string]interface{}{ + "id": "test-policy", + }, }) require.NoError(t, err) mockInClient := &mockClientUnit{} @@ -276,8 +279,18 @@ func Test_Agent_configFromUnits(t *testing.T) { }, }) + cliCfg, err := ucfg.NewFrom(map[string]interface{}{ + "inputs": []interface{}{ + map[string]interface{}{ + "policy": map[string]interface{}{ + "id": "test-policy", + }, + }, + }, + }) + require.NoError(t, err) a := &Agent{ - cliCfg: ucfg.New(), + cliCfg: cliCfg, agent: mockAgent, inputUnit: mockInClient, outputUnit: mockOutClient, @@ -287,6 +300,7 @@ func Test_Agent_configFromUnits(t *testing.T) { require.NoError(t, err) require.Len(t, cfg.Inputs, 1) assert.Equal(t, "fleet-server", cfg.Inputs[0].Type) + assert.Equal(t, "test-policy", cfg.Inputs[0].Policy.ID) assert.Equal(t, "0.0.0.0", cfg.Inputs[0].Server.Host) assert.True(t, cfg.Inputs[0].Server.Instrumentation.Enabled) assert.False(t, cfg.Inputs[0].Server.Instrumentation.TLS.SkipVerify)