Skip to content

Commit 2c3c282

Browse files
authored
[AGTHEAL-97] refactor(metadata): migrate resources component to v2 (#49823)
### What does this PR do? Migrates `comp/metadata/resources` from V1 to V2 component architecture. - `component.go` → `def/component.go` (interface only) - `resourcesimpl/` → `impl/` (renamed, `Module()` removed from params.go) - New `fx/fx.go` using `fxutil.ProvideComponentConstructor` - `component_mock.go` + `resourcesimpl/resources_mock.go` → `mock/mock.go` (`MockParams` moved to mock package) - All callers updated: `bundle.go`, `hostimpl`, `dogstatsd/start`, `flare` ### Motivation Align `comp/metadata/resources` with the V2 component conventions used across the rest of the codebase (e.g. `hostgpu`, `hostsysteminfo`, `haagent`). ### Describe how you validated your changes Existing tests migrated alongside the implementation. `params_test.go` and `resources_test.go` updated to use `NewComponent`/`Requires` instead of `newResourcesProvider`/`dependencies`. ### Additional Notes `Params` keeps `optional:"true"` via `fx.In` embedding in `Requires` — callers that need to disable resources (e.g. dogstatsd) still use `fx.Supply(resourcesimpl.Disabled())`. Co-authored-by: louis.coquerelle <louis.coquerelle@datadoghq.com>
1 parent 4151c7b commit 2c3c282

15 files changed

Lines changed: 146 additions & 109 deletions

File tree

cmd/agent/subcommands/flare/command.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ import (
6262
"github.com/DataDog/datadog-agent/comp/metadata/host/hostimpl"
6363
"github.com/DataDog/datadog-agent/comp/metadata/inventoryagent/inventoryagentimpl"
6464
"github.com/DataDog/datadog-agent/comp/metadata/inventoryhost/inventoryhostimpl"
65-
"github.com/DataDog/datadog-agent/comp/metadata/resources/resourcesimpl"
65+
resourcesfx "github.com/DataDog/datadog-agent/comp/metadata/resources/fx"
6666
logscompressorfx "github.com/DataDog/datadog-agent/comp/serializer/logscompression/fx"
6767
metricscompressorfx "github.com/DataDog/datadog-agent/comp/serializer/metricscompression/fx"
6868
"github.com/DataDog/datadog-agent/pkg/config/settings"
@@ -163,7 +163,7 @@ func Commands(globalParams *command.GlobalParams) []*cobra.Command {
163163
hostimpl.Module(),
164164
inventoryhostimpl.Module(),
165165
haagentmetadatafx.Module(),
166-
resourcesimpl.Module(),
166+
resourcesfx.Module(),
167167
// inventoryagent require a serializer. Since we're not actually sending the payload to
168168
// the backend a nil will work.
169169
fx.Provide(func() serializer.MetricSerializer {

cmd/dogstatsd/subcommands/start/command.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ import (
5252
"github.com/DataDog/datadog-agent/comp/metadata/inventoryagent/inventoryagentimpl"
5353
"github.com/DataDog/datadog-agent/comp/metadata/inventoryhost"
5454
"github.com/DataDog/datadog-agent/comp/metadata/inventoryhost/inventoryhostimpl"
55-
"github.com/DataDog/datadog-agent/comp/metadata/resources"
56-
"github.com/DataDog/datadog-agent/comp/metadata/resources/resourcesimpl"
55+
resources "github.com/DataDog/datadog-agent/comp/metadata/resources/def"
56+
resourcesfx "github.com/DataDog/datadog-agent/comp/metadata/resources/fx"
57+
resourcesimpl "github.com/DataDog/datadog-agent/comp/metadata/resources/impl"
5758
"github.com/DataDog/datadog-agent/comp/metadata/runner"
5859
metadatarunnerimpl "github.com/DataDog/datadog-agent/comp/metadata/runner/runnerimpl"
5960
logscompressionfx "github.com/DataDog/datadog-agent/comp/serializer/logscompression/fx"
@@ -160,7 +161,7 @@ func RunDogstatsdFct(cliParams *CLIParams, defaultConfPath string, defaultLogFil
160161
}),
161162
fx.Supply(resourcesimpl.Disabled()),
162163
metadatarunnerimpl.Module(),
163-
resourcesimpl.Module(),
164+
resourcesfx.Module(),
164165
hostimpl.Module(),
165166
inventoryagentimpl.Module(),
166167
ipcfx.ModuleReadWrite(),

comp/metadata/bundle.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"github.com/DataDog/datadog-agent/comp/metadata/inventorychecks/inventorychecksimpl"
1717
"github.com/DataDog/datadog-agent/comp/metadata/inventoryhost/inventoryhostimpl"
1818
packagesigningfx "github.com/DataDog/datadog-agent/comp/metadata/packagesigning/fx"
19-
"github.com/DataDog/datadog-agent/comp/metadata/resources/resourcesimpl"
19+
resourcesfx "github.com/DataDog/datadog-agent/comp/metadata/resources/fx"
2020
"github.com/DataDog/datadog-agent/comp/metadata/runner/runnerimpl"
2121
securityagent "github.com/DataDog/datadog-agent/comp/metadata/securityagent/fx"
2222
systemprobe "github.com/DataDog/datadog-agent/comp/metadata/systemprobe/fx"
@@ -29,7 +29,7 @@ import (
2929
func Bundle() fxutil.BundleOptions {
3030
return fxutil.Bundle(
3131
runnerimpl.Module(),
32-
resourcesimpl.Module(),
32+
resourcesfx.Module(),
3333
hostimpl.Module(),
3434
inventoryagentimpl.Module(),
3535
inventoryhostimpl.Module(),

comp/metadata/host/hostimpl/host.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
log "github.com/DataDog/datadog-agent/comp/core/log/def"
2424
"github.com/DataDog/datadog-agent/comp/core/status"
2525
hostComp "github.com/DataDog/datadog-agent/comp/metadata/host"
26-
"github.com/DataDog/datadog-agent/comp/metadata/resources"
26+
resources "github.com/DataDog/datadog-agent/comp/metadata/resources/def"
2727
"github.com/DataDog/datadog-agent/comp/metadata/runner/runnerimpl"
2828
"github.com/DataDog/datadog-agent/pkg/config/env"
2929
configUtils "github.com/DataDog/datadog-agent/pkg/config/utils"

comp/metadata/host/hostimpl/host_test.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ import (
2121
"github.com/DataDog/datadog-agent/comp/core/hostname/hostnameimpl"
2222
log "github.com/DataDog/datadog-agent/comp/core/log/def"
2323
logmock "github.com/DataDog/datadog-agent/comp/core/log/mock"
24-
"github.com/DataDog/datadog-agent/comp/metadata/resources"
25-
"github.com/DataDog/datadog-agent/comp/metadata/resources/resourcesimpl"
24+
resourcesmock "github.com/DataDog/datadog-agent/comp/metadata/resources/mock"
2625
"github.com/DataDog/datadog-agent/pkg/serializer"
2726
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
2827
)
@@ -33,8 +32,8 @@ func TestNewHostProviderDefaultIntervals(t *testing.T) {
3332
t,
3433
fx.Provide(func() log.Component { return logmock.New(t) }),
3534
fx.Provide(func() config.Component { return config.NewMock(t) }),
36-
resourcesimpl.MockModule(),
37-
fx.Replace(resources.MockParams{Data: nil}),
35+
resourcesmock.MockModule(),
36+
fx.Replace(resourcesmock.MockParams{Data: nil}),
3837
fx.Provide(func() serializer.MetricSerializer { return nil }),
3938
hostnameimpl.MockModule(),
4039
),
@@ -120,8 +119,8 @@ func TestNewHostProviderIntervalValidation(t *testing.T) {
120119
t,
121120
fx.Provide(func() log.Component { return logmock.New(t) }),
122121
fx.Provide(func() config.Component { return config.NewMockWithOverrides(t, overrides) }),
123-
resourcesimpl.MockModule(),
124-
fx.Replace(resources.MockParams{Data: nil}),
122+
resourcesmock.MockModule(),
123+
fx.Replace(resourcesmock.MockParams{Data: nil}),
125124
fx.Provide(func() serializer.MetricSerializer { return nil }),
126125
hostnameimpl.MockModule(),
127126
),
@@ -147,8 +146,8 @@ func TestBackoffWhenEarlyIntervalEqualsCollectionInterval(t *testing.T) {
147146
ret := newHostProvider(fxutil.Test[dependencies](t,
148147
fx.Provide(func() log.Component { return logmock.New(t) }),
149148
fx.Provide(func() config.Component { return config.NewMockWithOverrides(t, overrides) }),
150-
resourcesimpl.MockModule(),
151-
fx.Replace(resources.MockParams{Data: nil}),
149+
resourcesmock.MockModule(),
150+
fx.Replace(resourcesmock.MockParams{Data: nil}),
152151
fx.Provide(func() serializer.MetricSerializer { return nil }),
153152
hostnameimpl.MockModule(),
154153
))
@@ -166,8 +165,8 @@ func TestFlareProvider(t *testing.T) {
166165
t,
167166
fx.Provide(func() log.Component { return logmock.New(t) }),
168167
fx.Provide(func() config.Component { return config.NewMock(t) }),
169-
resourcesimpl.MockModule(),
170-
fx.Replace(resources.MockParams{Data: nil}),
168+
resourcesmock.MockModule(),
169+
fx.Replace(resourcesmock.MockParams{Data: nil}),
171170
fx.Provide(func() serializer.MetricSerializer { return nil }),
172171
hostnameimpl.MockModule(),
173172
),
@@ -186,8 +185,8 @@ func TestStatusHeaderProvider(t *testing.T) {
186185
t,
187186
fx.Provide(func() log.Component { return logmock.New(t) }),
188187
fx.Provide(func() config.Component { return config.NewMock(t) }),
189-
resourcesimpl.MockModule(),
190-
fx.Replace(resources.MockParams{Data: nil}),
188+
resourcesmock.MockModule(),
189+
fx.Replace(resourcesmock.MockParams{Data: nil}),
191190
fx.Provide(func() serializer.MetricSerializer { return nil }),
192191
hostnameimpl.MockModule(),
193192
),

comp/metadata/resources/component_mock.go

Lines changed: 0 additions & 30 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
load("@rules_go//go:def.bzl", "go_library")
22

33
go_library(
4-
name = "resources",
5-
srcs = [
6-
"component.go",
7-
"component_mock.go",
8-
],
9-
importpath = "github.com/DataDog/datadog-agent/comp/metadata/resources",
4+
name = "def",
5+
srcs = ["component.go"],
6+
importpath = "github.com/DataDog/datadog-agent/comp/metadata/resources/def",
107
visibility = ["//visibility:public"],
118
)

comp/metadata/resources/fx/fx.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Unless explicitly stated otherwise all files in this repository are licensed
2+
// under the Apache License Version 2.0.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
// Copyright 2016-present Datadog, Inc.
5+
6+
// Package fx provides the fx module for the resources metadata component
7+
package fx
8+
9+
import (
10+
resourcesimpl "github.com/DataDog/datadog-agent/comp/metadata/resources/impl"
11+
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
12+
)
13+
14+
// Module defines the fx options for this component.
15+
func Module() fxutil.Module {
16+
return fxutil.Component(
17+
fxutil.ProvideComponentConstructor(resourcesimpl.NewComponent),
18+
)
19+
}

comp/metadata/resources/resourcesimpl/params.go renamed to comp/metadata/resources/impl/params.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55

66
package resourcesimpl
77

8-
import (
9-
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
10-
"go.uber.org/fx"
11-
)
12-
138
// Params defines the parameters for the metadata resources component.
149
type Params struct {
1510
// Disabled determines if the resources payload will be sent. When disabled, the Get method is still available.
@@ -22,9 +17,3 @@ func Disabled() *Params {
2217
Disabled: true,
2318
}
2419
}
25-
26-
// Module defines the fx options for this component.
27-
func Module() fxutil.Module {
28-
return fxutil.Component(
29-
fx.Provide(newResourcesProvider))
30-
}

0 commit comments

Comments
 (0)