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 (#21034)

[workloadmeta] pass expected context to InitHelper + cleanup
  • Loading branch information
truthbk authored Nov 23, 2023
1 parent f432302 commit 8c628de
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 20 deletions.
18 changes: 10 additions & 8 deletions cmd/agent/common/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,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 @@ -57,7 +66,7 @@ func GetWorkloadmetaInit() workloadmeta.InitHelper {

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

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

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 @@ -254,7 +254,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, secretResolver, config.GetString("confd_path"))
common.LoadComponents(senderManager, secretResolver, 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 @@ -327,11 +327,9 @@ func getSharedFxOption() fx.Option {
fx.Invoke(func(lc fx.Lifecycle, demultiplexer demultiplexer.Component, _ workloadmeta.Component, secretResolver secrets.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, secretResolver, pkgconfig.Datadog.GetString("confd_path"))
common.LoadComponents(demultiplexer, secretResolver, 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 @@ -133,7 +133,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, secretResolver, pkgconfig.Datadog.GetString("confd_path"))
common.LoadComponents(demultiplexer, secretResolver, 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 @@ -258,7 +258,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, secretResolver, pkgconfig.Datadog.GetString("confd_path"))
common.LoadComponents(demultiplexer, secretResolver, 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 @@ -205,7 +205,7 @@ func run(config config.Component, cliParams *cliParams, demultiplexer demultiple
return nil
}

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

// Create the CheckScheduler, but do not attach it to
Expand Down
2 changes: 1 addition & 1 deletion pkg/diagnose/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func diagnoseChecksInCLIProcess(diagCfg diagnosis.Config, senderManager diagnose
secretResolver := secretsimpl.GetInstance()

// Initializing the aggregator with a flush interval of 0 (to disable the flush goroutines)
common.LoadComponents(context.Background(), senderManagerInstance, secretResolver, pkgconfig.Datadog.GetString("confd_path"))
common.LoadComponents(senderManagerInstance, secretResolver, 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 8c628de

Please sign in to comment.