Skip to content

Commit

Permalink
Migrate pkg/tagger to component (#21314)
Browse files Browse the repository at this point in the history
* migrate to comp

* create taggerclient

* migrate rest of pkg/tagger to comp/core/tagger

* update unit test

* migrate cmd to tagger component

* migrate tagger in collector/test/cmd

* fix unit test

* fix unit test

* fix security resolver and log agent

* migrate capture tagger in dogstatsdsettaggerstate

* fix integration test

* fix docker_listener_test integration test

* fix linter

* fix revive linter

* fix duplicate comment

* merge

* address comment: add legacy init to remote tagger, create mock module

* use param constructor for each agentType

* use onhook ctx instead of deps context

* fix bug: removing duplicated tagger.module

* fix docker listener test: using main ctx

* address comment

* address comment to reduce duplicate param constructor function

* Update cmd/agent/common/loader.go

Co-authored-by: Cedric Lamoriniere <[email protected]>

* fix linter

* Add tagger module to agent flare and diagnose

* merge conflict

---------

Co-authored-by: Cedric Lamoriniere <[email protected]>
  • Loading branch information
zhuminyi and clamoriniere authored Jan 23, 2024
1 parent 5b4beaa commit a958bf5
Show file tree
Hide file tree
Showing 153 changed files with 1,088 additions and 773 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@
/comp/checks/agentcrashdetect @DataDog/windows-kernel-integrations
/comp/checks/winregistry @DataDog/windows-agent
/comp/core/sysprobeconfig @DataDog/ebpf-platform
/comp/core/tagger @DataDog/container-integrations
/comp/core/workloadmeta @DataDog/container-integrations
/comp/dogstatsd/statsd @DataDog/agent-shared-components
/comp/forwarder/orchestrator @DataDog/agent-metrics-logs
Expand Down Expand Up @@ -350,7 +351,6 @@
/pkg/remoteconfig/ @DataDog/remote-config
/pkg/runtime/ @DataDog/agent-shared-components
/pkg/serializer/ @DataDog/agent-metrics-logs
/pkg/tagger/ @DataDog/container-integrations
/pkg/tagset/ @DataDog/agent-shared-components
/pkg/updater/ @DataDog/fleet
/pkg/util/ @DataDog/agent-shared-components
Expand Down
31 changes: 2 additions & 29 deletions cmd/agent/common/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,49 +19,22 @@ import (
"github.com/DataDog/datadog-agent/pkg/collector"
"github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/sbom/scanner"
"github.com/DataDog/datadog-agent/pkg/tagger"
"github.com/DataDog/datadog-agent/pkg/tagger/local"
"github.com/DataDog/datadog-agent/pkg/tagger/remote"
"github.com/DataDog/datadog-agent/pkg/util/log"
)

// GetWorkloadmetaInit provides the InitHelper for workloadmeta so it can be injected as a Param
// at workloadmeta comp fx injection.
func GetWorkloadmetaInit() workloadmeta.InitHelper {
return workloadmeta.InitHelper(func(ctx context.Context, wm workloadmeta.Component) error {
var t tagger.Tagger
var e error
if config.IsCLCRunner() {
options, err := remote.CLCRunnerOptions()
if err != nil {
e = fmt.Errorf("unable to configure the remote tagger: %s", err)
t = local.NewFakeTagger()
} else if options.Disabled {
// TODO(components): log the remote tagger being disabled.
// wm.log.Info("remote tagger is disabled")
t = local.NewFakeTagger()
} else {
t = remote.NewTagger(options)
}
} else {
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)
return fmt.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)
}

return e
return nil
})
}

Expand Down
7 changes: 6 additions & 1 deletion cmd/agent/subcommands/diagnose/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/comp/core/log"
"github.com/DataDog/datadog-agent/comp/core/log/logimpl"
"github.com/DataDog/datadog-agent/comp/core/tagger"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta/collectors"
"github.com/DataDog/datadog-agent/pkg/api/util"
Expand Down Expand Up @@ -95,6 +96,7 @@ func Commands(globalParams *command.GlobalParams) []*cobra.Command {
}
}),
workloadmeta.OptionalModule(),
tagger.OptionalModule(),
diagnosesendermanagerimpl.Module(),
)
},
Expand Down Expand Up @@ -223,7 +225,10 @@ This command print the package-signing metadata payload. This payload is used by
return []*cobra.Command{diagnoseCommand}
}

func cmdDiagnose(cliParams *cliParams, senderManager diagnosesendermanager.Component, _ optional.Option[workloadmeta.Component]) error {
func cmdDiagnose(cliParams *cliParams,
senderManager diagnosesendermanager.Component,
_ optional.Option[workloadmeta.Component],
_ optional.Option[tagger.Component]) error {
diagCfg := diagnosis.Config{
Verbose: cliParams.verbose,
RunLocal: cliParams.runLocal,
Expand Down
5 changes: 4 additions & 1 deletion cmd/agent/subcommands/flare/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/DataDog/datadog-agent/comp/core/secrets"
"github.com/DataDog/datadog-agent/comp/core/sysprobeconfig"
"github.com/DataDog/datadog-agent/comp/core/sysprobeconfig/sysprobeconfigimpl"
"github.com/DataDog/datadog-agent/comp/core/tagger"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta/collectors"
"github.com/DataDog/datadog-agent/comp/metadata/host/hostimpl"
Expand Down Expand Up @@ -109,6 +110,7 @@ func Commands(globalParams *command.GlobalParams) []*cobra.Command {
}
}),
workloadmeta.OptionalModule(),
tagger.OptionalModule(),
flare.Module(),
diagnosesendermanagerimpl.Module(),
// We need inventoryagent to fill the status page generated by the flare.
Expand Down Expand Up @@ -231,7 +233,8 @@ func makeFlare(flareComp flare.Component,
config config.Component,
_ sysprobeconfig.Component,
cliParams *cliParams,
_ optional.Option[workloadmeta.Component]) error {
_ optional.Option[workloadmeta.Component],
_ optional.Option[tagger.Component]) error {
var (
profile flare.ProfileData
err error
Expand Down
8 changes: 5 additions & 3 deletions cmd/agent/subcommands/jmx/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/DataDog/datadog-agent/comp/core/flare"
"github.com/DataDog/datadog-agent/comp/core/log/logimpl"
"github.com/DataDog/datadog-agent/comp/core/secrets"
"github.com/DataDog/datadog-agent/comp/core/tagger"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta/collectors"
"github.com/DataDog/datadog-agent/comp/dogstatsd/replay"
Expand Down Expand Up @@ -123,7 +124,6 @@ func Commands(globalParams *command.GlobalParams) []*cobra.Command {
}),
workloadmeta.Module(),
apiimpl.Module(),

// TODO(components): this is a temporary hack as the StartServer() method of the API package was previously called with nil arguments
// This highlights the fact that the API Server created by JMX (through ExecJmx... function) should be different from the ones created
// in others commands such as run.
Expand All @@ -137,6 +137,8 @@ func Commands(globalParams *command.GlobalParams) []*cobra.Command {
fx.Provide(func() demultiplexer.Component { return nil }),
fx.Provide(func() inventorychecks.Component { return nil }),
fx.Provide(func() packagesigning.Component { return nil }),
fx.Provide(tagger.NewTaggerParamsForCoreAgent),
tagger.Module(),
)
}

Expand Down Expand Up @@ -265,7 +267,7 @@ func disableCmdPort() {

// runJmxCommandConsole sets up the common utils necessary for JMX, and executes the command
// with the Console reporter
func runJmxCommandConsole(config config.Component, cliParams *cliParams, wmeta workloadmeta.Component, diagnoseSendermanager diagnosesendermanager.Component, secretResolver secrets.Component, agentAPI internalAPI.Component) error {
func runJmxCommandConsole(config config.Component, cliParams *cliParams, wmeta workloadmeta.Component, taggerComp tagger.Component, diagnoseSendermanager diagnosesendermanager.Component, secretResolver secrets.Component, agentAPI internalAPI.Component) error {
// This prevents log-spam from "comp/core/workloadmeta/collectors/internal/remote/process_collector/process_collector.go"
// It appears that this collector creates some contention in AD.
// Disabling it is both more efficient and gets rid of this log spam
Expand Down Expand Up @@ -304,7 +306,7 @@ func runJmxCommandConsole(config config.Component, cliParams *cliParams, wmeta w
return err
}

err = standalone.ExecJMXCommandConsole(cliParams.command, cliParams.cliSelectedChecks, cliParams.jmxLogLevel, allConfigs, wmeta, diagnoseSendermanager, agentAPI)
err = standalone.ExecJMXCommandConsole(cliParams.command, cliParams.cliSelectedChecks, cliParams.jmxLogLevel, allConfigs, wmeta, taggerComp, diagnoseSendermanager, agentAPI)

if runtime.GOOS == "windows" {
standalone.PrintWindowsUserWarning("jmx")
Expand Down
10 changes: 8 additions & 2 deletions cmd/agent/subcommands/run/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/DataDog/datadog-agent/comp/core/secrets"
"github.com/DataDog/datadog-agent/comp/core/sysprobeconfig"
"github.com/DataDog/datadog-agent/comp/core/sysprobeconfig/sysprobeconfigimpl"
"github.com/DataDog/datadog-agent/comp/core/tagger"
"github.com/DataDog/datadog-agent/comp/core/telemetry"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta/collectors"
Expand Down Expand Up @@ -171,6 +172,7 @@ func run(log log.Component,
serverDebug dogstatsddebug.Component,
forwarder defaultforwarder.Component,
wmeta workloadmeta.Component,
taggerComp tagger.Component,
rcclient rcclient.Component,
_ runner.Component,
demultiplexer demultiplexer.Component,
Expand Down Expand Up @@ -237,6 +239,7 @@ func run(log log.Component,
server,
serverDebug,
wmeta,
taggerComp,
rcclient,
logsAgent,
forwarder,
Expand Down Expand Up @@ -284,6 +287,8 @@ func getSharedFxOption() fx.Option {
dogstatsd.Bundle(),
otelcol.Bundle(),
rcclient.Module(),
fx.Provide(tagger.NewTaggerParamsForCoreAgent),
tagger.Module(),

// TODO: (components) - some parts of the agent (such as the logs agent) implicitly depend on the global state
// set up by LoadComponents. In order for components to use lifecycle hooks that also depend on this global state, we
Expand All @@ -292,10 +297,9 @@ func getSharedFxOption() fx.Option {
// Workloadmeta component needs to be initialized before this hook is executed, and thus is included
// in the function args to order the execution. This pattern might be worth revising because it is
// error prone.
fx.Invoke(func(lc fx.Lifecycle, demultiplexer demultiplexer.Component, _ workloadmeta.Component, secretResolver secrets.Component) {
fx.Invoke(func(lc fx.Lifecycle, demultiplexer demultiplexer.Component, _ workloadmeta.Component, _ tagger.Component, secretResolver secrets.Component) {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {

// create and setup the Autoconfig instance
common.LoadComponents(demultiplexer, secretResolver, pkgconfig.Datadog.GetString("confd_path"))
return nil
Expand Down Expand Up @@ -338,6 +342,7 @@ func startAgent(
server dogstatsdServer.Component,
serverDebug dogstatsddebug.Component,
wmeta workloadmeta.Component,
taggerComp tagger.Component,
rcclient rcclient.Component,
logsAgent optional.Option[logsAgent.Component],
_ defaultforwarder.Component,
Expand Down Expand Up @@ -489,6 +494,7 @@ func startAgent(
if err = agentAPI.StartServer(
configService,
wmeta,
taggerComp,
logsAgent,
demultiplexer,
); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions cmd/agent/subcommands/run/command_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/DataDog/datadog-agent/comp/core/secrets"
"github.com/DataDog/datadog-agent/comp/core/sysprobeconfig"
"github.com/DataDog/datadog-agent/comp/core/sysprobeconfig/sysprobeconfigimpl"
"github.com/DataDog/datadog-agent/comp/core/tagger"
"github.com/DataDog/datadog-agent/comp/core/telemetry"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
"github.com/DataDog/datadog-agent/comp/dogstatsd/replay"
Expand Down Expand Up @@ -87,6 +88,7 @@ func StartAgentWithDefaults(ctxChan <-chan context.Context) (<-chan error, error
_ replay.Component,
serverDebug dogstatsddebug.Component,
wmeta workloadmeta.Component,
taggerComp tagger.Component,
rcclient rcclient.Component,
forwarder defaultforwarder.Component,
logsAgent optional.Option[logsAgent.Component],
Expand Down Expand Up @@ -115,6 +117,7 @@ func StartAgentWithDefaults(ctxChan <-chan context.Context) (<-chan error, error
server,
serverDebug,
wmeta,
taggerComp,
rcclient,
logsAgent,
forwarder,
Expand Down
7 changes: 5 additions & 2 deletions cmd/cluster-agent-cloudfoundry/subcommands/run/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/DataDog/datadog-agent/comp/core/log"
"github.com/DataDog/datadog-agent/comp/core/log/logimpl"
"github.com/DataDog/datadog-agent/comp/core/secrets"
"github.com/DataDog/datadog-agent/comp/core/tagger"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta/collectors"
"github.com/DataDog/datadog-agent/comp/forwarder"
Expand Down Expand Up @@ -83,14 +84,16 @@ func Commands(globalParams *command.GlobalParams) []*cobra.Command {
InitHelper: common.GetWorkloadmetaInit(),
}), // TODO(components): check what this must be for cluster-agent-cloudfoundry
workloadmeta.Module(),
fx.Provide(tagger.NewTaggerParams),
tagger.Module(),
)
},
}

return []*cobra.Command{startCmd}
}

func run(log log.Component, demultiplexer demultiplexer.Component, wmeta workloadmeta.Component, secretResolver secrets.Component) error {
func run(log log.Component, taggerComp tagger.Component, demultiplexer demultiplexer.Component, wmeta workloadmeta.Component, secretResolver secrets.Component) error {
mainCtx, mainCtxCancel := context.WithCancel(context.Background())
defer mainCtxCancel() // Calling cancel twice is safe

Expand Down Expand Up @@ -146,7 +149,7 @@ func run(log log.Component, demultiplexer demultiplexer.Component, wmeta workloa
// start the autoconfig, this will immediately run any configured check
common.AC.LoadAndRun(mainCtx)

if err = api.StartServer(wmeta, demultiplexer); err != nil {
if err = api.StartServer(wmeta, taggerComp, demultiplexer); err != nil {
return log.Errorf("Error while starting agent API, exiting: %v", err)
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/cluster-agent/api/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"github.com/DataDog/datadog-agent/cmd/agent/common/path"
"github.com/DataDog/datadog-agent/cmd/agent/common/signals"
"github.com/DataDog/datadog-agent/comp/api/api/apiimpl/response"
"github.com/DataDog/datadog-agent/comp/core/tagger"
"github.com/DataDog/datadog-agent/comp/core/tagger/collectors"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
"github.com/DataDog/datadog-agent/pkg/aggregator/sender"
"github.com/DataDog/datadog-agent/pkg/autodiscovery"
Expand All @@ -29,8 +31,6 @@ import (
"github.com/DataDog/datadog-agent/pkg/flare"
clusteragentStatus "github.com/DataDog/datadog-agent/pkg/status/clusteragent"
"github.com/DataDog/datadog-agent/pkg/status/health"
"github.com/DataDog/datadog-agent/pkg/tagger"
"github.com/DataDog/datadog-agent/pkg/tagger/collectors"
"github.com/DataDog/datadog-agent/pkg/util/hostname"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/version"
Expand Down
2 changes: 1 addition & 1 deletion cmd/cluster-agent/api/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

pbgo "github.com/DataDog/datadog-agent/pkg/proto/pbgo/core"

taggerserver "github.com/DataDog/datadog-agent/pkg/tagger/server"
taggerserver "github.com/DataDog/datadog-agent/comp/core/tagger/server"
)

type serverSecure struct {
Expand Down
8 changes: 4 additions & 4 deletions cmd/cluster-agent/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ import (

"github.com/DataDog/datadog-agent/cmd/cluster-agent/api/agent"
v1 "github.com/DataDog/datadog-agent/cmd/cluster-agent/api/v1"
"github.com/DataDog/datadog-agent/comp/core/tagger"
taggerserver "github.com/DataDog/datadog-agent/comp/core/tagger/server"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
"github.com/DataDog/datadog-agent/pkg/aggregator/sender"
"github.com/DataDog/datadog-agent/pkg/api/security"
"github.com/DataDog/datadog-agent/pkg/api/util"
"github.com/DataDog/datadog-agent/pkg/config"
pb "github.com/DataDog/datadog-agent/pkg/proto/pbgo/core"
"github.com/DataDog/datadog-agent/pkg/tagger"
taggerserver "github.com/DataDog/datadog-agent/pkg/tagger/server"
grpcutil "github.com/DataDog/datadog-agent/pkg/util/grpc"
)

Expand All @@ -47,7 +47,7 @@ var (
)

// StartServer creates the router and starts the HTTP server
func StartServer(w workloadmeta.Component, senderManager sender.DiagnoseSenderManager) error {
func StartServer(w workloadmeta.Component, taggerComp tagger.Component, senderManager sender.DiagnoseSenderManager) error {
// create the root HTTP router
router = mux.NewRouter()
apiRouter = router.PathPrefix("/api/v1").Subrouter()
Expand Down Expand Up @@ -123,7 +123,7 @@ func StartServer(w workloadmeta.Component, senderManager sender.DiagnoseSenderMa

grpcSrv := grpc.NewServer(opts...)
pb.RegisterAgentSecureServer(grpcSrv, &serverSecure{
taggerServer: taggerserver.NewServer(tagger.GetDefaultTagger()),
taggerServer: taggerserver.NewServer(taggerComp),
})

timeout := config.Datadog.GetDuration("cluster_agent.server.idle_timeout_seconds") * time.Second
Expand Down
7 changes: 5 additions & 2 deletions cmd/cluster-agent/subcommands/start/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/DataDog/datadog-agent/comp/core/log"
"github.com/DataDog/datadog-agent/comp/core/log/logimpl"
"github.com/DataDog/datadog-agent/comp/core/secrets"
"github.com/DataDog/datadog-agent/comp/core/tagger"
"github.com/DataDog/datadog-agent/comp/core/telemetry"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta/collectors"
Expand Down Expand Up @@ -125,14 +126,16 @@ func Commands(globalParams *command.GlobalParams) []*cobra.Command {
}), // TODO(components): check what this must be for cluster-agent-cloudfoundry
fx.Supply(context.Background()),
workloadmeta.Module(),
fx.Provide(tagger.NewTaggerParams),
tagger.Module(),
)
},
}

return []*cobra.Command{startCmd}
}

func start(log log.Component, config config.Component, telemetry telemetry.Component, demultiplexer demultiplexer.Component, wmeta workloadmeta.Component, secretResolver secrets.Component) error {
func start(log log.Component, config config.Component, taggerComp tagger.Component, telemetry telemetry.Component, demultiplexer demultiplexer.Component, wmeta workloadmeta.Component, secretResolver secrets.Component) error {
stopCh := make(chan struct{})

mainCtx, mainCtxCancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -205,7 +208,7 @@ func start(log log.Component, config config.Component, telemetry telemetry.Compo
}

// Starting server early to ease investigations
if err := api.StartServer(wmeta, demultiplexer); err != nil {
if err := api.StartServer(wmeta, taggerComp, demultiplexer); err != nil {
return fmt.Errorf("Error while starting agent API, exiting: %v", err)
}

Expand Down
Loading

0 comments on commit a958bf5

Please sign in to comment.