Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GPU: hostgpu payload: filter out hosts with no GPUs #34790

Merged
merged 3 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions comp/metadata/hostgpu/impl/hostgpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ func (gh *gpuHost) fillData() {
func (gh *gpuHost) getPayload() marshaler.JSONMarshaler {
gh.fillData()

if gh.data.Devices == nil || len(gh.data.Devices) == 0 {
return nil
}

return &Payload{
Hostname: gh.hostname,
Timestamp: time.Now().UnixNano(),
Expand Down
9 changes: 5 additions & 4 deletions comp/metadata/hostgpu/impl/hostgpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,17 @@ func TestGetPayload(t *testing.T) {
},
}

p := gh.getPayload().(*Payload)
p, ok := gh.getPayload().(*Payload)
assert.True(t, ok)
assert.Equal(t, expectedMetadata, p.Metadata)
}

func TestGetPayloadError(t *testing.T) {
func TestGetEmptyPayload(t *testing.T) {
gh := getTestInventoryHost(t)
gh.wmeta = &wmsErrorMock{}

p := gh.getPayload().(*Payload)
assert.Equal(t, &hostGPUMetadata{}, p.Metadata)
p := gh.getPayload()
assert.Nil(t, p)
}

func TestFlareProviderFilename(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions comp/metadata/internal/util/inventory_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ func (i *InventoryPayload) collect(_ context.Context) time.Duration {
i.LastCollect = time.Now()

p := i.getPayload()
// If the payload is nil, we don't want to send it to the backend.
if p == nil {
i.log.Debugf("inventory payload is nil, skipping submission")
return i.MinInterval
}
if err := i.serializer.SendMetadata(p); err != nil {
i.log.Errorf("unable to submit inventories payload, %s", err)
}
Expand Down
26 changes: 26 additions & 0 deletions comp/metadata/internal/util/inventory_payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ func getTestInventoryPayload(t *testing.T, confOverrides map[string]any) *Invent
return &i
}

func getEmptyInventoryPayload(t *testing.T, confOverrides map[string]any) *InventoryPayload {
i := CreateInventoryPayload(
fxutil.Test[config.Component](t, config.MockModule(), fx.Replace(config.MockParams{Overrides: confOverrides})),
logmock.New(t),
serializermock.NewMetricSerializer(t),
func() marshaler.JSONMarshaler { return nil },
"testempty.json",
)
return &i
}

func TestEnabled(t *testing.T) {
i := getTestInventoryPayload(t, map[string]any{
"inventories_enabled": true,
Expand Down Expand Up @@ -222,3 +233,18 @@ func TestCollect(t *testing.T) {
i.serializer.(*serializermock.MetricSerializer).AssertExpectations(t)
assert.False(t, i.forceRefresh.Load())
}

func TestCollectEmptyPayload(t *testing.T) {
i := getEmptyInventoryPayload(t, nil)

// Ensure calls to collect do not fail the check for createdAt
i.createdAt = time.Now().Add(-2 * time.Minute)
// Make sure the minInterval between two payload has expired
i.LastCollect = time.Now().Add(-1 * time.Hour)

now := time.Now()
interval := i.collect(context.Background())
assert.Equal(t, defaultMinInterval, interval)
assert.False(t, i.LastCollect.Before(now))
i.serializer.(*serializermock.MetricSerializer).AssertNotCalled(t, "SendMetadata")
}