Skip to content

Commit 565b073

Browse files
committed
wip
1 parent 0d50584 commit 565b073

File tree

11 files changed

+125
-25
lines changed

11 files changed

+125
-25
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,6 +1939,7 @@ func (c *Coordinator) generateComponentModel() (err error) {
19391939

19401940
comps, err := c.specs.ToComponents(
19411941
cfg,
1942+
c.cfg.Settings.Internal.Runtime,
19421943
configInjector,
19431944
c.state.LogLevel,
19441945
c.agentInfo,

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,9 +1117,10 @@ func createCoordinator(t testing.TB, ctx context.Context, opts ...CoordinatorOpt
11171117
require.NoError(t, err)
11181118

11191119
monitoringMgr := newTestMonitoringMgr()
1120-
cfg := configuration.DefaultGRPCConfig()
1121-
cfg.Port = 0
1122-
rm, err := runtime.NewManager(l, l, ai, apmtest.DiscardTracer, monitoringMgr, cfg)
1120+
cfg := configuration.DefaultConfiguration()
1121+
grpcCfg := cfg.Settings.GRPC
1122+
grpcCfg.Port = 0
1123+
rm, err := runtime.NewManager(l, l, ai, apmtest.DiscardTracer, monitoringMgr, grpcCfg)
11231124
require.NoError(t, err)
11241125
otelMgr := &fakeOTelManager{}
11251126
caps, err := capabilities.LoadFile(paths.AgentCapabilitiesPath(), l)
@@ -1137,8 +1138,7 @@ func createCoordinator(t testing.TB, ctx context.Context, opts ...CoordinatorOpt
11371138
if acker == nil {
11381139
acker = &fakeActionAcker{}
11391140
}
1140-
1141-
coord := New(l, nil, logp.DebugLevel, ai, specs, &fakeReExecManager{}, upgradeManager, rm, cfgMgr, varsMgr, caps, monitoringMgr, o.managed, otelMgr, acker, nil)
1141+
coord := New(l, cfg, logp.DebugLevel, ai, specs, &fakeReExecManager{}, upgradeManager, rm, cfgMgr, varsMgr, caps, monitoringMgr, o.managed, otelMgr, acker, nil)
11421142
return coord, cfgMgr, varsMgr
11431143
}
11441144

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ func TestCoordinatorReportsInvalidPolicy(t *testing.T) {
496496
otelMgr: &fakeOTelManager{},
497497

498498
// Set valid but empty initial values for ast and vars
499+
cfg: configuration.DefaultConfiguration(),
499500
vars: emptyVars(t),
500501
ast: emptyAST(t),
501502
componentPIDTicker: time.NewTicker(time.Second * 30),
@@ -613,6 +614,7 @@ func TestCoordinatorReportsComponentModelError(t *testing.T) {
613614
otelMgr: &fakeOTelManager{},
614615

615616
// Set valid but empty initial values for ast and vars
617+
cfg: configuration.DefaultConfiguration(),
616618
vars: emptyVars(t),
617619
ast: emptyAST(t),
618620
componentPIDTicker: time.NewTicker(time.Second * 30),
@@ -717,6 +719,7 @@ func TestCoordinatorPolicyChangeUpdatesMonitorReloader(t *testing.T) {
717719
},
718720
runtimeMgr: runtimeManager,
719721
otelMgr: &fakeOTelManager{},
722+
cfg: configuration.DefaultConfiguration(),
720723
vars: emptyVars(t),
721724
componentPIDTicker: time.NewTicker(time.Second * 30),
722725
secretMarkerFunc: mockSecretMarkerFunc,
@@ -859,6 +862,7 @@ func TestCoordinatorPolicyChangeUpdatesRuntimeAndOTelManager(t *testing.T) {
859862
},
860863
runtimeMgr: runtimeManager,
861864
otelMgr: otelManager,
865+
cfg: configuration.DefaultConfiguration(),
862866
vars: emptyVars(t),
863867
componentPIDTicker: time.NewTicker(time.Second * 30),
864868
secretMarkerFunc: testSecretMarkerFunc,
@@ -1033,6 +1037,7 @@ func TestCoordinatorPolicyChangeUpdatesRuntimeAndOTelManagerWithOtelComponents(t
10331037
runtimeMgr: runtimeManager,
10341038
otelMgr: otelManager,
10351039
specs: specs,
1040+
cfg: configuration.DefaultConfiguration(),
10361041
vars: emptyVars(t),
10371042
componentPIDTicker: time.NewTicker(time.Second * 30),
10381043
secretMarkerFunc: testSecretMarkerFunc,
@@ -1212,6 +1217,7 @@ func TestCoordinatorManagesComponentWorkDirs(t *testing.T) {
12121217
runtimeMgr: runtimeManager,
12131218
otelMgr: otelManager,
12141219
specs: specs,
1220+
cfg: configuration.DefaultConfiguration(),
12151221
vars: emptyVars(t),
12161222
componentPIDTicker: time.NewTicker(time.Second * 30),
12171223
secretMarkerFunc: testSecretMarkerFunc,
@@ -1349,9 +1355,9 @@ func TestCoordinatorReportsRuntimeManagerUpdateFailure(t *testing.T) {
13491355
// manager, so it receives the update result.
13501356
runtimeManagerError: updateErrChan,
13511357
},
1352-
runtimeMgr: runtimeManager,
1353-
otelMgr: &fakeOTelManager{},
1354-
1358+
runtimeMgr: runtimeManager,
1359+
otelMgr: &fakeOTelManager{},
1360+
cfg: configuration.DefaultConfiguration(),
13551361
vars: emptyVars(t),
13561362
componentPIDTicker: time.NewTicker(time.Second * 30),
13571363
secretMarkerFunc: testSecretMarkerFunc,
@@ -1414,6 +1420,7 @@ func TestCoordinatorReportsOTelManagerUpdateFailure(t *testing.T) {
14141420
},
14151421
runtimeMgr: runtimeManager,
14161422
otelMgr: otelManager,
1423+
cfg: configuration.DefaultConfiguration(),
14171424
vars: emptyVars(t),
14181425
componentPIDTicker: time.NewTicker(time.Second * 30),
14191426
secretMarkerFunc: testSecretMarkerFunc,
@@ -1479,6 +1486,7 @@ func TestCoordinatorAppliesVarsToPolicy(t *testing.T) {
14791486
},
14801487
runtimeMgr: runtimeManager,
14811488
otelMgr: &fakeOTelManager{},
1489+
cfg: configuration.DefaultConfiguration(),
14821490
vars: emptyVars(t),
14831491
componentPIDTicker: time.NewTicker(time.Second * 30),
14841492
secretMarkerFunc: testSecretMarkerFunc,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ agent:
9595
download: null
9696
grpc: null
9797
id: ""
98+
internal: null
9899
path: ""
99100
process: null
100101
reload: null

internal/pkg/agent/cmd/inspect.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"github.com/spf13/cobra"
1515
"gopkg.in/yaml.v2"
1616

17+
"github.com/elastic/elastic-agent/internal/pkg/agent/configuration"
18+
1719
"github.com/elastic/elastic-agent-libs/logp"
1820
"github.com/elastic/elastic-agent-libs/service"
1921

@@ -137,11 +139,15 @@ func inspectConfig(ctx context.Context, cfgPath string, opts inspectConfigOpts,
137139
if err != nil {
138140
return fmt.Errorf("error checking for root/Administrator privileges: %w", err)
139141
}
142+
fullCfg, err := operations.LoadFullAgentConfig(ctx, l, cfgPath, true, !isAdmin)
143+
if err != nil {
144+
return fmt.Errorf("error loading agent config: %w", err)
145+
}
146+
agentCfg, err := configuration.NewFromConfig(fullCfg)
147+
if err != nil {
148+
return fmt.Errorf("error loading agent config: %w", err)
149+
}
140150
if !opts.variables && !opts.includeMonitoring {
141-
fullCfg, err := operations.LoadFullAgentConfig(ctx, l, cfgPath, true, !isAdmin)
142-
if err != nil {
143-
return fmt.Errorf("error loading agent config: %w", err)
144-
}
145151
// Ensure secret markers are injected based on secret_paths before redaction.
146152
if err := diagnostics.AddSecretMarkers(l, fullCfg); err != nil {
147153
fmt.Fprintf(streams.Err, "failed to add secret markers: %v\n", err)
@@ -180,7 +186,7 @@ func inspectConfig(ctx context.Context, cfgPath string, opts inspectConfigOpts,
180186
if err != nil {
181187
return fmt.Errorf("failed to get monitoring: %w", err)
182188
}
183-
components, err := specs.PolicyToComponents(cfg, lvl, agentInfo)
189+
components, err := specs.PolicyToComponents(cfg, agentCfg.Settings.Internal.Runtime, lvl, agentInfo)
184190
if err != nil {
185191
return fmt.Errorf("failed to get binary mappings: %w", err)
186192
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License 2.0;
3+
// you may not use this file except in compliance with the Elastic License 2.0.
4+
5+
package configuration
6+
7+
import "github.com/elastic/elastic-agent/pkg/component"
8+
9+
type InternalConfig struct {
10+
Runtime *component.RuntimeConfig `yaml:"runtime" config:"runtime" json:"runtime"`
11+
}
12+
13+
func DefaultInternalConfig() *InternalConfig {
14+
return &InternalConfig{
15+
Runtime: component.DefaultRuntimeConfig(),
16+
}
17+
}

internal/pkg/agent/configuration/settings.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type SettingsConfig struct {
2323
EventLoggingConfig *logger.Config `yaml:"logging.event_data,omitempty" config:"logging.event_data,omitempty" json:"logging.event_data,omitempty"`
2424
Upgrade *UpgradeConfig `yaml:"upgrade" config:"upgrade" json:"upgrade"`
2525
Collector *CollectorConfig `yaml:"collector" config:"collector" json:"collector"`
26+
Internal *InternalConfig `yaml:"internal" config:"internal" json:"internal"`
2627

2728
// standalone config
2829
Reload *ReloadConfig `config:"reload" yaml:"reload" json:"reload"`
@@ -41,6 +42,7 @@ func DefaultSettingsConfig() *SettingsConfig {
4142
GRPC: DefaultGRPCConfig(),
4243
Upgrade: DefaultUpgradeConfig(),
4344
Collector: DefaultCollectorConfig(),
45+
Internal: DefaultInternalConfig(),
4446
Reload: DefaultReloadConfig(),
4547
V1MonitoringEnabled: true,
4648
}

internal/pkg/agent/install/componentvalidation/validation.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ func GetComponentsFromPolicy(ctx context.Context, l *logger.Logger, cfgPath stri
9090
return nil, err
9191
}
9292

93+
rawCfg, err := operations.LoadFullAgentConfig(ctx, l, cfgPath, true, !isAdmin)
94+
if err != nil {
95+
return nil, err
96+
}
97+
cfg, err := configuration.NewFromConfig(rawCfg)
98+
if err != nil {
99+
return nil, err
100+
}
101+
93102
monitorFn, err := GetMonitoringFn(ctx, l, m, otel)
94103
if err != nil {
95104
return nil, fmt.Errorf("failed to get monitoring: %w", err)
@@ -101,7 +110,7 @@ func GetComponentsFromPolicy(ctx context.Context, l *logger.Logger, cfgPath stri
101110
}
102111

103112
// Compute the components from the computed configuration.
104-
comps, err := specs.ToComponents(m, monitorFn, lvl, agentInfo, map[string]uint64{})
113+
comps, err := specs.ToComponents(m, cfg.Settings.Internal.Runtime, monitorFn, lvl, agentInfo, map[string]uint64{})
105114
if err != nil {
106115
return nil, fmt.Errorf("failed to render components: %w", err)
107116
}

internal/pkg/agent/install/uninstall.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,17 @@ func uninstallServiceComponent(ctx context.Context, log *logp.Logger, comp compo
417417
return nil
418418
}
419419

420-
func serviceComponentsFromConfig(specs component.RuntimeSpecs, cfg *config.Config) ([]component.Component, error) {
421-
mm, err := cfg.ToMapStr()
420+
func serviceComponentsFromConfig(specs component.RuntimeSpecs, rawCfg *config.Config) ([]component.Component, error) {
421+
mm, err := rawCfg.ToMapStr()
422422
if err != nil {
423423
return nil, aerrors.New("failed to create a map from config", err)
424424
}
425-
allComps, err := specs.ToComponents(mm, nil, logp.InfoLevel, nil, map[string]uint64{})
425+
cfg, err := configuration.NewFromConfig(rawCfg)
426+
if err != nil {
427+
return nil, aerrors.New("failed to unpack config", err)
428+
}
429+
allComps, err := specs.ToComponents(
430+
mm, cfg.Settings.Internal.Runtime, nil, logp.InfoLevel, nil, map[string]uint64{})
426431
if err != nil {
427432
return nil, fmt.Errorf("failed to render components: %w", err)
428433
}

pkg/component/component.go

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,48 @@ type HeadersProvider interface {
3737

3838
type RuntimeManager string
3939

40+
type RuntimeConfig struct {
41+
Default string `yaml:"default" config:"default" json:"default"`
42+
InputType map[string]string `yaml:",inline,omitempty" config:",inline,omitempty" json:",inline,omitempty"`
43+
}
44+
45+
func DefaultRuntimeConfig() *RuntimeConfig {
46+
return &RuntimeConfig{
47+
Default: string(DefaultRuntimeManager),
48+
}
49+
}
50+
51+
func (r *RuntimeConfig) Validate() error {
52+
validateRuntime := func(val string) error {
53+
switch RuntimeManager(val) {
54+
case OtelRuntimeManager, ProcessRuntimeManager:
55+
return nil
56+
default:
57+
return fmt.Errorf("invalid runtime manager: %s, must be either %s or %s",
58+
val, OtelRuntimeManager, ProcessRuntimeManager)
59+
}
60+
}
61+
if err := validateRuntime(r.Default); err != nil {
62+
return err
63+
}
64+
for _, val := range r.InputType {
65+
if err := validateRuntime(val); err != nil {
66+
return err
67+
}
68+
}
69+
return nil
70+
}
71+
72+
func (r *RuntimeConfig) RuntimeManagerForInputType(inputType string) RuntimeManager {
73+
if manager, ok := r.InputType[inputType]; ok {
74+
return RuntimeManager(manager)
75+
}
76+
if r.Default != "" {
77+
return RuntimeManager(r.Default)
78+
}
79+
return DefaultRuntimeManager
80+
}
81+
4082
const (
4183
// defaultUnitLogLevel is the default log level that a unit will get if one is not defined.
4284
defaultUnitLogLevel = client.UnitLogLevelInfo
@@ -339,12 +381,13 @@ type Model struct {
339381
// the current runtime specification.
340382
func (r *RuntimeSpecs) ToComponents(
341383
policy map[string]interface{},
384+
runtimeCfg *RuntimeConfig,
342385
monitoringInjector GenerateMonitoringCfgFn,
343386
ll logp.Level,
344387
headers HeadersProvider,
345388
currentServiceCompInts map[string]uint64,
346389
) ([]Component, error) {
347-
components, err := r.PolicyToComponents(policy, ll, headers)
390+
components, err := r.PolicyToComponents(policy, runtimeCfg, ll, headers)
348391
if err != nil {
349392
return nil, err
350393
}
@@ -357,7 +400,7 @@ func (r *RuntimeSpecs) ToComponents(
357400

358401
if monitoringCfg != nil {
359402
// monitoring is enabled
360-
monitoringComps, err := r.PolicyToComponents(monitoringCfg, ll, headers)
403+
monitoringComps, err := r.PolicyToComponents(monitoringCfg, runtimeCfg, ll, headers)
361404
if err != nil {
362405
return nil, fmt.Errorf("failed to generate monitoring components: %w", err)
363406
}
@@ -499,6 +542,7 @@ func (r *RuntimeSpecs) componentsForOutput(output outputI, featureFlags *feature
499542
// PolicyToComponents takes the policy and generates a component model.
500543
func (r *RuntimeSpecs) PolicyToComponents(
501544
policy map[string]interface{},
545+
runtimeCfg *RuntimeConfig,
502546
ll logp.Level,
503547
headers HeadersProvider,
504548
) ([]Component, error) {
@@ -508,7 +552,7 @@ func (r *RuntimeSpecs) PolicyToComponents(
508552
return nil, fmt.Errorf("could not parse feature flags from policy: %w", err)
509553
}
510554

511-
outputsMap, err := toIntermediate(policy, r.aliasMapping, ll, headers)
555+
outputsMap, err := toIntermediate(policy, runtimeCfg, r.aliasMapping, ll, headers)
512556
if err != nil {
513557
return nil, err
514558
}
@@ -576,7 +620,13 @@ func injectInputPolicyID(fleetPolicy map[string]interface{}, inputConfig map[str
576620

577621
// toIntermediate takes the policy and returns it into an intermediate representation that is easier to map into a set
578622
// of components.
579-
func toIntermediate(policy map[string]interface{}, aliasMapping map[string]string, ll logp.Level, headers HeadersProvider) (map[string]outputI, error) {
623+
func toIntermediate(
624+
policy map[string]interface{},
625+
runtimeCfg *RuntimeConfig,
626+
aliasMapping map[string]string,
627+
ll logp.Level,
628+
headers HeadersProvider,
629+
) (map[string]outputI, error) {
580630
const (
581631
outputsKey = "outputs"
582632
enabledKey = "enabled"
@@ -728,7 +778,7 @@ func toIntermediate(policy map[string]interface{}, aliasMapping map[string]strin
728778
return nil, fmt.Errorf("invalid 'inputs.%d.log_level', %w", idx, err)
729779
}
730780

731-
runtimeManager := DefaultRuntimeManager
781+
runtimeManager := runtimeCfg.RuntimeManagerForInputType(t)
732782
// determine the runtime manager for the input
733783
if runtimeManagerRaw, ok := input[runtimeManagerKey]; ok {
734784
runtimeManagerStr, ok := runtimeManagerRaw.(string)

0 commit comments

Comments
 (0)