From 5314e9ca12283a96ce20075c9cfd2e02e24405d6 Mon Sep 17 00:00:00 2001 From: Valeri Pliskin Date: Wed, 5 Mar 2025 16:53:47 +0000 Subject: [PATCH 1/3] - return nil hostgpu Payload when host doesn't have any GPU devices - inventory_payload.go: don't send nil payloads to the backend - added UTs to cover the nil payload case --- comp/metadata/hostgpu/impl/hostgpu.go | 4 +++ comp/metadata/hostgpu/impl/hostgpu_test.go | 9 ++++--- .../internal/util/inventory_payload.go | 5 ++++ .../internal/util/inventory_payload_test.go | 26 +++++++++++++++++++ 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/comp/metadata/hostgpu/impl/hostgpu.go b/comp/metadata/hostgpu/impl/hostgpu.go index c7f59541af568..cbc78165d1ad2 100644 --- a/comp/metadata/hostgpu/impl/hostgpu.go +++ b/comp/metadata/hostgpu/impl/hostgpu.go @@ -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(), diff --git a/comp/metadata/hostgpu/impl/hostgpu_test.go b/comp/metadata/hostgpu/impl/hostgpu_test.go index 088fb5819fe6f..9ebc9eb464ef5 100644 --- a/comp/metadata/hostgpu/impl/hostgpu_test.go +++ b/comp/metadata/hostgpu/impl/hostgpu_test.go @@ -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) { diff --git a/comp/metadata/internal/util/inventory_payload.go b/comp/metadata/internal/util/inventory_payload.go index 0104aa6d704cd..8171a451c6d25 100644 --- a/comp/metadata/internal/util/inventory_payload.go +++ b/comp/metadata/internal/util/inventory_payload.go @@ -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) } diff --git a/comp/metadata/internal/util/inventory_payload_test.go b/comp/metadata/internal/util/inventory_payload_test.go index d288e580026f7..66680dac9261d 100644 --- a/comp/metadata/internal/util/inventory_payload_test.go +++ b/comp/metadata/internal/util/inventory_payload_test.go @@ -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, @@ -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") +} From 9698b671ad102615ce7cb1e3f0360c68f44f8981 Mon Sep 17 00:00:00 2001 From: Valeri Pliskin Date: Wed, 5 Mar 2025 17:22:28 +0000 Subject: [PATCH 2/3] fixed linter --- comp/metadata/hostgpu/impl/hostgpu.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/comp/metadata/hostgpu/impl/hostgpu.go b/comp/metadata/hostgpu/impl/hostgpu.go index cbc78165d1ad2..ac64eb296a160 100644 --- a/comp/metadata/hostgpu/impl/hostgpu.go +++ b/comp/metadata/hostgpu/impl/hostgpu.go @@ -152,7 +152,7 @@ func (gh *gpuHost) fillData() { func (gh *gpuHost) getPayload() marshaler.JSONMarshaler { gh.fillData() - if gh.data.Devices == nil || len(gh.data.Devices) == 0 { + if gh.data == nil || len(gh.data.Devices) == 0 { return nil } From 98a6961eefdaab784d4e4c1058e70eef43c2f8c9 Mon Sep 17 00:00:00 2001 From: Valeri Pliskin Date: Wed, 5 Mar 2025 17:24:36 +0000 Subject: [PATCH 3/3] removed redundant nil check --- comp/metadata/hostgpu/impl/hostgpu.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/comp/metadata/hostgpu/impl/hostgpu.go b/comp/metadata/hostgpu/impl/hostgpu.go index ac64eb296a160..75921dd1bfff9 100644 --- a/comp/metadata/hostgpu/impl/hostgpu.go +++ b/comp/metadata/hostgpu/impl/hostgpu.go @@ -152,7 +152,7 @@ func (gh *gpuHost) fillData() { func (gh *gpuHost) getPayload() marshaler.JSONMarshaler { gh.fillData() - if gh.data == nil || len(gh.data.Devices) == 0 { + if len(gh.data.Devices) == 0 { return nil }