Skip to content

Commit

Permalink
[workloadmeta] create SBOM global scanner in the InitHelper so its av…
Browse files Browse the repository at this point in the history
…ailable to wm

[workloadmeta] pass expected context to InitHelper + cleanup
  • Loading branch information
truthbk committed Nov 22, 2023
1 parent 6f13d7c commit 97db87b
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 19 deletions.
18 changes: 10 additions & 8 deletions cmd/agent/common/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ func GetWorkloadmetaInit() workloadmeta.InitHelper {
t = local.NewTagger(wm)
}

// SBOM scanner needs to be called here as initialization is required prior to the
// catalog getting instantiated and initialized.
sbomScanner, err := scanner.CreateGlobalScanner(config.Datadog)
if err != nil {
log.Errorf("failed to create SBOM scanner: %s", err)
} else if sbomScanner != nil {
sbomScanner.Start(ctx)
}

tagger.SetDefaultTagger(t)
if err := tagger.Init(ctx); err != nil {
e = fmt.Errorf("failed to start the tagger: %s", err)
Expand All @@ -56,7 +65,7 @@ func GetWorkloadmetaInit() workloadmeta.InitHelper {

// LoadComponents configures several common Agent components:
// tagger, collector, scheduler and autodiscovery
func LoadComponents(ctx context.Context, senderManager sender.SenderManager, confdPath string) {
func LoadComponents(senderManager sender.SenderManager, confdPath string) {

confSearchPaths := []string{
confdPath,
Expand All @@ -73,13 +82,6 @@ func LoadComponents(ctx context.Context, senderManager sender.SenderManager, con
// because of subscription to metadata store.
AC = setupAutoDiscovery(confSearchPaths, scheduler.NewMetaScheduler())

sbomScanner, err := scanner.CreateGlobalScanner(config.Datadog)
if err != nil {
log.Errorf("failed to create SBOM scanner: %s", err)
} else if sbomScanner != nil {
sbomScanner.Start(ctx)
}

// create the Collector instance and start all the components
// NOTICE: this will also setup the Python environment, if available
Coll = collector.NewCollector(senderManager, GetPythonPaths()...)
Expand Down
2 changes: 1 addition & 1 deletion cmd/agent/subcommands/jmx/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func runJmxCommandConsole(config config.Component, cliParams *cliParams, wmeta w
}
// The Autoconfig instance setup happens in the workloadmeta start hook
// create and setup the Collector and others.
common.LoadComponents(context.Background(), senderManager, config.GetString("confd_path"))
common.LoadComponents(senderManager, config.GetString("confd_path"))
common.AC.LoadAndRun(context.Background())

// Create the CheckScheduler, but do not attach it to
Expand Down
4 changes: 1 addition & 3 deletions cmd/agent/subcommands/run/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,9 @@ func getSharedFxOption() fx.Option {
fx.Invoke(func(lc fx.Lifecycle, demultiplexer demultiplexer.Component, _ workloadmeta.Component) {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
// Main context passed to components
mainCtx, _ := pkgcommon.GetMainCtxCancel()

// create and setup the Autoconfig instance
common.LoadComponents(mainCtx, demultiplexer, pkgconfig.Datadog.GetString("confd_path"))
common.LoadComponents(demultiplexer, pkgconfig.Datadog.GetString("confd_path"))
return nil
},
OnStop: func(ctx context.Context) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/cluster-agent-cloudfoundry/subcommands/run/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func run(log log.Component, demultiplexer demultiplexer.Component, wmeta workloa
// create and setup the Autoconfig instance
// The Autoconfig instance setup happens in the workloadmeta start hook
// create and setup the Collector and others.
common.LoadComponents(mainCtx, demultiplexer, pkgconfig.Datadog.GetString("confd_path"))
common.LoadComponents(demultiplexer, pkgconfig.Datadog.GetString("confd_path"))

// Set up check collector
common.AC.AddScheduler("check", collector.InitCheckScheduler(common.Coll, demultiplexer), true)
Expand Down
2 changes: 1 addition & 1 deletion cmd/cluster-agent/subcommands/start/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func start(log log.Component, config config.Component, telemetry telemetry.Compo
// create and setup the Autoconfig instance
// The Autoconfig instance setup happens in the workloadmeta start hook
// create and setup the Collector and others.
common.LoadComponents(mainCtx, demultiplexer, pkgconfig.Datadog.GetString("confd_path"))
common.LoadComponents(demultiplexer, pkgconfig.Datadog.GetString("confd_path"))

// Set up check collector
common.AC.AddScheduler("check", collector.InitCheckScheduler(common.Coll, demultiplexer), true)
Expand Down
10 changes: 6 additions & 4 deletions comp/core/workloadmeta/workloadmeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,18 @@ func newWorkloadMeta(deps dependencies) Component {

var err error

// Main context passed to components
// TODO(components): this mainCtx should probably be replaced by the
// context provided to the OnStart hook.
mainCtx, _ := common.GetMainCtxCancel()

// create and setup the Autoconfig instance
if deps.Params.InitHelper != nil {
err = deps.Params.InitHelper(c, wm)
err = deps.Params.InitHelper(mainCtx, wm)
if err != nil {
return err
}
}

// Main context passed to components
mainCtx, _ := common.GetMainCtxCancel()
wm.Start(mainCtx)
return nil
}})
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/subcommands/check/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func run(config config.Component, cliParams *cliParams, demultiplexer demultiple
return nil
}

common.LoadComponents(context.Background(), demultiplexer, pkgconfig.Datadog.GetString("confd_path"))
common.LoadComponents(demultiplexer, pkgconfig.Datadog.GetString("confd_path"))
common.AC.LoadAndRun(context.Background())

// Create the CheckScheduler, but do not attach it to
Expand Down

0 comments on commit 97db87b

Please sign in to comment.