Skip to content

Commit

Permalink
[v2] Fix inspect command (#805)
Browse files Browse the repository at this point in the history
* Write the inspect command for v2.

* Fix lint.

* Fix code review. Load inputs from inputs.d for inspect.

* Fix lint.

* Refactor to use errgroup.

* Remove unused struct.
  • Loading branch information
blakerouse authored Aug 2, 2022
1 parent 9b68ea4 commit 2cc2338
Show file tree
Hide file tree
Showing 15 changed files with 437 additions and 366 deletions.
36 changes: 2 additions & 34 deletions internal/pkg/agent/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package application

import (
"fmt"
"path/filepath"

"go.elastic.co/apm"

Expand All @@ -20,17 +19,11 @@ import (
"github.com/elastic/elastic-agent/internal/pkg/capabilities"
"github.com/elastic/elastic-agent/internal/pkg/composable"
"github.com/elastic/elastic-agent/internal/pkg/config"
"github.com/elastic/elastic-agent/internal/pkg/dir"
"github.com/elastic/elastic-agent/pkg/component"
"github.com/elastic/elastic-agent/pkg/component/runtime"
"github.com/elastic/elastic-agent/pkg/core/logger"
)

type discoverFunc func() ([]string, error)

// ErrNoConfiguration is returned when no configuration are found.
var ErrNoConfiguration = errors.New("no configuration found", errors.TypeConfig)

// New creates a new Agent and bootstrap the required subsystem.
func New(
log *logger.Logger,
Expand Down Expand Up @@ -83,8 +76,8 @@ func New(
if configuration.IsStandalone(cfg.Fleet) {
log.Info("Parsed configuration and determined agent is managed locally")

loader := config.NewLoader(log, externalConfigsGlob())
discover := discoverer(pathConfigFile, cfg.Settings.Path, externalConfigsGlob())
loader := config.NewLoader(log, paths.ExternalInputs())
discover := config.Discoverer(pathConfigFile, cfg.Settings.Path, paths.ExternalInputs())
if !cfg.Settings.Reload.Enabled {
log.Debug("Reloading of configuration is off")
configMgr = newOnce(log, discover, loader)
Expand Down Expand Up @@ -173,28 +166,3 @@ func mergeFleetConfig(rawConfig *config.Config) (storage.Store, *configuration.C

return store, cfg, nil
}

func externalConfigsGlob() string {
return filepath.Join(paths.Config(), configuration.ExternalInputsPattern)
}

func discoverer(patterns ...string) discoverFunc {
p := make([]string, 0, len(patterns))
for _, newP := range patterns {
if len(newP) == 0 {
continue
}

p = append(p, newP)
}

if len(p) == 0 {
return func() ([]string, error) {
return []string{}, ErrNoConfiguration
}
}

return func() ([]string, error) {
return dir.DiscoverFiles(p...)
}
}
6 changes: 3 additions & 3 deletions internal/pkg/agent/application/once.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import (

type once struct {
log *logger.Logger
discover discoverFunc
discover config.DiscoverFunc
loader *config.Loader
ch chan coordinator.ConfigChange
errCh chan error
}

func newOnce(log *logger.Logger, discover discoverFunc, loader *config.Loader) *once {
func newOnce(log *logger.Logger, discover config.DiscoverFunc, loader *config.Loader) *once {
return &once{log: log, discover: discover, loader: loader, ch: make(chan coordinator.ConfigChange), errCh: make(chan error)}
}

Expand All @@ -34,7 +34,7 @@ func (o *once) Run(ctx context.Context) error {
}

if len(files) == 0 {
return ErrNoConfiguration
return config.ErrNoConfiguration
}

cfg, err := readfiles(files, o.loader)
Expand Down
10 changes: 9 additions & 1 deletion internal/pkg/agent/application/paths/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const (
tempSubdir = "tmp"
)

// ExternalInputsPattern is a glob that matches the paths of external configuration files.
var ExternalInputsPattern = filepath.Join("inputs.d", "*.yml")

var (
topPath string
configPath string
Expand Down Expand Up @@ -69,7 +72,7 @@ func TempDir() string {
tmpDir := filepath.Join(Data(), tempSubdir)
tmpCreator.Do(func() {
// create tempdir as it probably don't exists
os.MkdirAll(tmpDir, 0750)
_ = os.MkdirAll(tmpDir, 0750)
})
return tmpDir
}
Expand Down Expand Up @@ -119,6 +122,11 @@ func ConfigFile() string {
return filepath.Join(Config(), configFilePath)
}

// ExternalInputs returns the path to load external inputs from.
func ExternalInputs() string {
return filepath.Join(Config(), ExternalInputsPattern)
}

// Data returns the data directory for Agent
func Data() string {
if unversionedHome {
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/agent/application/periodic.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type periodic struct {
period time.Duration
watcher *filewatcher.Watch
loader *config.Loader
discover discoverFunc
discover config.DiscoverFunc
ch chan coordinator.ConfigChange
errCh chan error
}
Expand Down Expand Up @@ -62,7 +62,7 @@ func (p *periodic) work() error {
}

if len(files) == 0 {
return ErrNoConfiguration
return config.ErrNoConfiguration
}

// Reset the state of the watched files
Expand Down Expand Up @@ -115,7 +115,7 @@ func (p *periodic) work() error {
func newPeriodic(
log *logger.Logger,
period time.Duration,
discover discoverFunc,
discover config.DiscoverFunc,
loader *config.Loader,
) *periodic {
w, err := filewatcher.New(log, filewatcher.DefaultComparer)
Expand Down
12 changes: 6 additions & 6 deletions internal/pkg/agent/cmd/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,14 +398,19 @@ func outputDiagnostics(w io.Writer, d DiagnosticsInfo) error {
}

func gatherConfig() (AgentConfig, error) {
log, err := newErrorLogger()
if err != nil {
return AgentConfig{}, err
}

cfg := AgentConfig{}
localCFG, err := loadConfig(nil)
if err != nil {
return cfg, err
}
cfg.ConfigLocal = localCFG

renderedCFG, err := operations.LoadFullAgentConfig(paths.ConfigFile(), true)
renderedCFG, err := operations.LoadFullAgentConfig(log, paths.ConfigFile(), true)
if err != nil {
return cfg, err
}
Expand Down Expand Up @@ -434,11 +439,6 @@ func gatherConfig() (AgentConfig, error) {
return AgentConfig{}, err
}
log, err := newErrorLogger()
if err != nil {
return AgentConfig{}, err
}
// Get process config - uses same approach as inspect output command.
// Does not contact server process to request configs.
pMap, err := getProgramsFromConfig(log, agentInfo, renderedCFG, isStandalone)
Expand Down
Loading

0 comments on commit 2cc2338

Please sign in to comment.