Skip to content

Commit

Permalink
Fix the setting of host field in contlcycle, contimage and sbom events
Browse files Browse the repository at this point in the history
  • Loading branch information
L3n41c committed Oct 11, 2023
1 parent 915f5e1 commit 0ead811
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
8 changes: 8 additions & 0 deletions pkg/collector/corechecks/containerimage/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package containerimage

import (
"context"
"strings"
"time"

Expand All @@ -14,6 +15,7 @@ import (
"github.com/DataDog/datadog-agent/pkg/tagger"
"github.com/DataDog/datadog-agent/pkg/tagger/collectors"
queue "github.com/DataDog/datadog-agent/pkg/util/aggregatingqueue"
"github.com/DataDog/datadog-agent/pkg/util/hostname"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/workloadmeta"

Expand All @@ -32,10 +34,16 @@ type processor struct {
}

func newProcessor(sender sender.Sender, maxNbItem int, maxRetentionTime time.Duration) *processor {
hname, err := hostname.Get(context.TODO())
if err != nil {
log.Warnf("Error getting hostname: %v", err)
}

return &processor{
queue: queue.NewQueue(maxNbItem, maxRetentionTime, func(images []*model.ContainerImage) {
encoded, err := proto.Marshal(&model.ContainerImagePayload{
Version: "v1",
Host: hname,
Source: &sourceAgent,
Images: images,
})
Expand Down
13 changes: 12 additions & 1 deletion pkg/collector/corechecks/containerlifecycle/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
package containerlifecycle

import (
"context"
"fmt"

model "github.com/DataDog/agent-payload/v5/contlcycle"

types "github.com/DataDog/datadog-agent/pkg/containerlifecycle"
"github.com/DataDog/datadog-agent/pkg/util/hostname"
"github.com/DataDog/datadog-agent/pkg/util/log"
)

type event interface {
Expand Down Expand Up @@ -80,7 +83,15 @@ func (e *eventTransformer) withOwnerID(id string) {
}

func (e *eventTransformer) toPayloadModel() (*model.EventsPayload, error) {
payload := &model.EventsPayload{Version: types.PayloadV1}
hname, err := hostname.Get(context.TODO())
if err != nil {
log.Warnf("Error getting hostname: %v", err)
}

payload := &model.EventsPayload{
Version: types.PayloadV1,
Host: hname,
}
kind, err := e.kind(e.objectKind)
if err != nil {
return nil, err
Expand Down
12 changes: 8 additions & 4 deletions pkg/collector/corechecks/sbom/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package sbom

import (
"context"
"errors"
"os"
"strings"
Expand All @@ -20,10 +21,10 @@ import (
"github.com/DataDog/datadog-agent/pkg/sbom"
"github.com/DataDog/datadog-agent/pkg/sbom/collectors/host"
sbomscanner "github.com/DataDog/datadog-agent/pkg/sbom/scanner"
"github.com/DataDog/datadog-agent/pkg/security/utils"
"github.com/DataDog/datadog-agent/pkg/tagger"
"github.com/DataDog/datadog-agent/pkg/tagger/collectors"
queue "github.com/DataDog/datadog-agent/pkg/util/aggregatingqueue"
"github.com/DataDog/datadog-agent/pkg/util/hostname"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/workloadmeta"

Expand Down Expand Up @@ -59,16 +60,19 @@ func newProcessor(workloadmetaStore workloadmeta.Store, sender sender.Sender, ma
if sbomScanner == nil {
return nil, errors.New("failed to get global SBOM scanner")
}
hostname, _ := utils.GetHostname()
hname, err := hostname.Get(context.TODO())
if err != nil {
log.Warnf("Error getting hostname: %v", err)
}

return &processor{
queue: queue.NewQueue(maxNbItem, maxRetentionTime, func(entities []*model.SBOMEntity) {
encoded, err := proto.Marshal(&model.SBOMPayload{
Version: 1,
Host: hname,
Source: &sourceAgent,
Entities: entities,
DdEnv: &envVarEnv,
Host: hostname,
})
if err != nil {
log.Errorf("Unable to encode message: %+v", err)
Expand All @@ -83,7 +87,7 @@ func newProcessor(workloadmetaStore workloadmeta.Store, sender sender.Sender, ma
sbomScanner: sbomScanner,
hostSBOM: hostSBOM,
hostScanOpts: hostScanOpts,
hostname: hostname,
hostname: hname,
hostHeartbeatValidity: hostHeartbeatValidity,
}, nil
}
Expand Down

0 comments on commit 0ead811

Please sign in to comment.