Skip to content

Commit 1929087

Browse files
committed
moved implementation to custom patcher
1 parent 4ae51b3 commit 1929087

File tree

5 files changed

+125
-67
lines changed

5 files changed

+125
-67
lines changed

internal/pkg/agent/application/application.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,11 @@ func New(
209209
log.Info("Parsed configuration and determined agent is in Fleet Server bootstrap mode")
210210

211211
compModifiers = append(compModifiers, FleetServerComponentModifier(cfg.Fleet.Server))
212-
configMgr = coordinator.NewConfigPatchManager(newFleetServerBootstrapManager(log), PatchAPMConfig(log, rawConfig))
212+
configMgr = coordinator.NewConfigPatchManager(
213+
newFleetServerBootstrapManager(log),
214+
PatchAPMConfig(log, rawConfig),
215+
PatchFleetConfig(log, rawConfig, caps, isManaged),
216+
)
213217
} else {
214218
log.Info("Parsed configuration and determined agent is managed by Fleet")
215219

@@ -259,7 +263,12 @@ func New(
259263
if err != nil {
260264
return nil, nil, nil, err
261265
}
262-
configMgr = coordinator.NewConfigPatchManager(managed, injectOutputOverrides(log, rawConfig), PatchAPMConfig(log, rawConfig))
266+
configMgr = coordinator.NewConfigPatchManager(
267+
managed,
268+
injectOutputOverrides(log, rawConfig),
269+
PatchAPMConfig(log, rawConfig),
270+
PatchFleetConfig(log, rawConfig, caps, isManaged),
271+
)
263272
}
264273
}
265274

internal/pkg/agent/application/coordinator/coordinator.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"context"
99
"errors"
1010
"fmt"
11-
"os"
1211
"reflect"
1312
"strings"
1413
"sync"
@@ -1610,14 +1609,6 @@ func (c *Coordinator) processConfigAgent(ctx context.Context, cfg *config.Config
16101609
c.logger.Errorf("failed to add secret markers: %v", err)
16111610
}
16121611

1613-
// override retrieved config from Fleet with persisted config from AgentConfig file
1614-
1615-
if c.caps != nil {
1616-
if err := applyPersistedConfig(cfg, paths.ConfigFile(), c.caps.AllowFleetOverride); err != nil {
1617-
return fmt.Errorf("could not apply persisted configuration: %w", err)
1618-
}
1619-
}
1620-
16211612
// perform and verify ast translation
16221613
m, err := cfg.ToMapStr()
16231614
if err != nil {
@@ -1705,32 +1696,6 @@ func (c *Coordinator) generateAST(cfg *config.Config, m map[string]interface{})
17051696
return nil
17061697
}
17071698

1708-
func applyPersistedConfig(cfg *config.Config, configFile string, checkFn func() bool) error {
1709-
if !checkFn() {
1710-
// Feature is disabled, nothing to do
1711-
return nil
1712-
}
1713-
1714-
f, err := os.OpenFile(configFile, os.O_RDONLY, 0)
1715-
if err != nil && os.IsNotExist(err) {
1716-
return nil
1717-
} else if err != nil {
1718-
return fmt.Errorf("opening config file: %w", err)
1719-
}
1720-
defer f.Close()
1721-
1722-
persisted, err := config.NewConfigFrom(f)
1723-
if err != nil {
1724-
return fmt.Errorf("parsing persisted config: %w", err)
1725-
}
1726-
1727-
err = cfg.Merge(persisted)
1728-
if err != nil {
1729-
return fmt.Errorf("merging persisted config: %w", err)
1730-
}
1731-
return nil
1732-
}
1733-
17341699
// observeASTVars identifies the variables that are referenced in the computed AST and passed to
17351700
// the varsMgr so it knows what providers are being referenced. If a providers is not being
17361701
// referenced then the provider does not need to be running.

internal/pkg/agent/application/coordinator/coordinator_test.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,36 +1011,6 @@ func TestCoordinator_UpgradeDetails(t *testing.T) {
10111011
require.Equal(t, expectedErr.Error(), coord.state.UpgradeDetails.Metadata.ErrorMsg)
10121012
}
10131013

1014-
func Test_ApplyPersistedConfig(t *testing.T) {
1015-
cfgFile := filepath.Join(".", "testdata", "overrides.yml")
1016-
1017-
testCases := []struct {
1018-
name string
1019-
featureEnable bool
1020-
expectedLogs bool
1021-
}{
1022-
{name: "enabled", featureEnable: true, expectedLogs: false},
1023-
{name: "disabled", featureEnable: false, expectedLogs: true},
1024-
}
1025-
1026-
for _, tc := range testCases {
1027-
t.Run(tc.name, func(t *testing.T) {
1028-
cfg, err := config.LoadFile(filepath.Join(".", "testdata", "config.yaml"))
1029-
require.NoError(t, err)
1030-
1031-
err = applyPersistedConfig(cfg, cfgFile, func() bool { return tc.featureEnable })
1032-
require.NoError(t, err)
1033-
1034-
c := &configuration.Configuration{}
1035-
require.NoError(t, cfg.Agent.Unpack(&c))
1036-
1037-
require.Equal(t, tc.expectedLogs, c.Settings.MonitoringConfig.MonitorLogs)
1038-
require.True(t, c.Settings.MonitoringConfig.MonitorMetrics)
1039-
require.True(t, c.Settings.MonitoringConfig.Enabled)
1040-
})
1041-
}
1042-
}
1043-
10441014
func BenchmarkCoordinator_generateComponentModel(b *testing.B) {
10451015
// load variables
10461016
varsMaps := []map[string]any{}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package application
2+
3+
import (
4+
"github.com/elastic/elastic-agent/internal/pkg/agent/application/coordinator"
5+
"github.com/elastic/elastic-agent/internal/pkg/capabilities"
6+
"github.com/elastic/elastic-agent/internal/pkg/config"
7+
"github.com/elastic/elastic-agent/pkg/core/logger"
8+
)
9+
10+
func PatchFleetConfig(log *logger.Logger,
11+
rawConfig *config.Config, // original config from fleet, this one won't reload
12+
caps capabilities.Capabilities,
13+
isManaged bool,
14+
) func(change coordinator.ConfigChange) coordinator.ConfigChange {
15+
if !isManaged || // no need to override fleet config when not managed
16+
caps == nil || !caps.AllowFleetOverride() {
17+
return noop
18+
}
19+
20+
return func(change coordinator.ConfigChange) coordinator.ConfigChange {
21+
newConfig := change.Config()
22+
if err := newConfig.Merge(rawConfig); err != nil {
23+
log.Errorf("error merging fleet config into config change: %v", err)
24+
}
25+
26+
return change
27+
}
28+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package application
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/elastic/elastic-agent/internal/pkg/agent/configuration"
9+
"github.com/elastic/elastic-agent/internal/pkg/config"
10+
"github.com/elastic/elastic-agent/pkg/core/logger/loggertest"
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/mock"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
func Test_FleetPatcher(t *testing.T) {
17+
configFile := filepath.Join(".", "coordinator", "testdata", "overrides.yml")
18+
19+
testCases := []struct {
20+
name string
21+
isManaged bool
22+
featureEnable bool
23+
expectedLogs bool
24+
}{
25+
{name: "managed - enabled", isManaged: true, featureEnable: true, expectedLogs: false},
26+
{name: "managed - disabled", isManaged: true, featureEnable: false, expectedLogs: true},
27+
{name: "not managed - enabled", isManaged: false, featureEnable: true, expectedLogs: true},
28+
{name: "not managed - disabled", isManaged: false, featureEnable: false, expectedLogs: true},
29+
}
30+
31+
overridesFile, err := os.OpenFile(configFile, os.O_RDONLY, 0)
32+
require.NoError(t, err)
33+
defer overridesFile.Close()
34+
35+
rawConfig, err := config.NewConfigFrom(overridesFile)
36+
require.NoError(t, err)
37+
38+
for _, tc := range testCases {
39+
t.Run(tc.name, func(t *testing.T) {
40+
caps := &mockCapabilities{}
41+
caps.On("AllowFleetOverride").Return(tc.featureEnable)
42+
43+
log, _ := loggertest.New(t.Name())
44+
45+
cfg, err := config.LoadFile(filepath.Join(".", "coordinator", "testdata", "config.yaml"))
46+
require.NoError(t, err)
47+
48+
configChange := &mockConfigChange{
49+
c: cfg,
50+
}
51+
52+
patcher := PatchFleetConfig(log, rawConfig, caps, tc.isManaged)
53+
patcher(configChange)
54+
55+
c := &configuration.Configuration{}
56+
require.NoError(t, cfg.Agent.Unpack(&c))
57+
assert.Equal(t, tc.expectedLogs, c.Settings.MonitoringConfig.MonitorLogs)
58+
require.True(t, c.Settings.MonitoringConfig.MonitorMetrics)
59+
require.True(t, c.Settings.MonitoringConfig.Enabled)
60+
})
61+
}
62+
}
63+
64+
type mockCapabilities struct {
65+
mock.Mock
66+
}
67+
68+
func (m *mockCapabilities) AllowUpgrade(version string, sourceURI string) bool {
69+
args := m.Called(version, sourceURI)
70+
return args.Bool(0)
71+
}
72+
73+
func (m *mockCapabilities) AllowInput(name string) bool {
74+
args := m.Called()
75+
return args.Bool(0)
76+
}
77+
78+
func (m *mockCapabilities) AllowOutput(name string) bool {
79+
args := m.Called()
80+
return args.Bool(0)
81+
}
82+
83+
func (m *mockCapabilities) AllowFleetOverride() bool {
84+
args := m.Called()
85+
return args.Bool(0)
86+
}

0 commit comments

Comments
 (0)