Skip to content

Commit 04d2bf9

Browse files
committed
[cmd/opampsupervisor] add config to validate the config
Adds the ability for users to configure the opamp supervisor to call the collector's `validate` command to report any errors via the supervisor's logs Signed-off-by: alex boten <[email protected]>
1 parent 20d3551 commit 04d2bf9

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

cmd/opampsupervisor/supervisor/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ type Agent struct {
190190
ConfigFiles []string `mapstructure:"config_files"`
191191
Arguments []string `mapstructure:"args"`
192192
Env map[string]string `mapstructure:"env"`
193+
ValidateConfig bool `mapstructure:"validate"`
193194
}
194195

195196
func (a Agent) Validate() error {

cmd/opampsupervisor/supervisor/supervisor.go

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,13 @@ func (s *Supervisor) Start() error {
328328
return fmt.Errorf("failed loading initial config: %w", err)
329329
}
330330

331+
if s.config.Agent.ValidateConfig {
332+
err = s.validateConfig()
333+
if err != nil {
334+
return err
335+
}
336+
}
337+
331338
s.commander, err = commander.NewCommander(
332339
s.telemetrySettings.Logger,
333340
s.config.Storage.Directory,
@@ -354,17 +361,7 @@ func (s *Supervisor) Start() error {
354361
}
355362

356363
func (s *Supervisor) getFeatureGates() error {
357-
cmd, err := commander.NewCommander(
358-
s.telemetrySettings.Logger,
359-
s.config.Storage.Directory,
360-
s.config.Agent,
361-
"featuregate",
362-
)
363-
if err != nil {
364-
return err
365-
}
366-
367-
stdout, _, err := cmd.StartOneShot()
364+
stdout, _, err := s.runCommand("featuregate")
368365
if err != nil {
369366
return err
370367
}
@@ -388,6 +385,28 @@ func (s *Supervisor) getFeatureGates() error {
388385
return nil
389386
}
390387

388+
func (s *Supervisor) validateConfig() error {
389+
_, stderr, err := s.runCommand("validate", "--config", s.agentConfigFilePath())
390+
if err != nil {
391+
return fmt.Errorf("error running validate command:%s", stderr)
392+
}
393+
return nil
394+
}
395+
396+
func (s *Supervisor) runCommand(args ...string) ([]byte, []byte, error) {
397+
cmd, err := commander.NewCommander(
398+
s.telemetrySettings.Logger,
399+
s.config.Storage.Directory,
400+
s.config.Agent,
401+
args...,
402+
)
403+
if err != nil {
404+
return nil, nil, err
405+
}
406+
407+
return cmd.StartOneShot()
408+
}
409+
391410
func (s *Supervisor) createTemplates() error {
392411
var err error
393412

@@ -1568,6 +1587,13 @@ func (s *Supervisor) onMessage(ctx context.Context, msg *types.MessageData) {
15681587
s.telemetrySettings.Logger.Error("The OpAMP client failed to update the effective config", zap.Error(err))
15691588
}
15701589

1590+
if s.config.Agent.ValidateConfig {
1591+
err = s.validateConfig()
1592+
if err != nil {
1593+
s.telemetrySettings.Logger.Error("The OpAMP client failed to validate the effective config", zap.Error(err))
1594+
}
1595+
}
1596+
15711597
s.telemetrySettings.Logger.Debug("Config is changed. Signal to restart the agent")
15721598
// Signal that there is a new config.
15731599
select {

0 commit comments

Comments
 (0)