Skip to content

Commit

Permalink
Add the plumbing in the agent forwarder to submit container images an…
Browse files Browse the repository at this point in the history
…d SBOM
  • Loading branch information
L3n41c committed Jan 10, 2023
1 parent 9e511a0 commit 6f4e092
Show file tree
Hide file tree
Showing 26 changed files with 649 additions and 27 deletions.
3 changes: 3 additions & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ core,code.cloudfoundry.org/lager,Apache-2.0,"Copyright (c) 2016-Present CloudFou
core,code.cloudfoundry.org/tlsconfig,Apache-2.0,"Copyright (c) 2016-Present CloudFoundry.org Foundation, Inc. All Rights Reserved."
core,contrib.go.opencensus.io/exporter/prometheus,Apache-2.0,"Copyright 2020, OpenCensus Authors"
core,github.com/AlekSi/pointer,MIT,Copyright (c) 2015 Alexey Palazhchenko
core,github.com/DataDog/agent-payload/v5/contimage,BSD-3-Clause,"Copyright (c) 2017, Datadog, Inc"
core,github.com/DataDog/agent-payload/v5/contlcycle,BSD-3-Clause,"Copyright (c) 2017, Datadog, Inc"
core,github.com/DataDog/agent-payload/v5/cyclonedx_v1_4,BSD-3-Clause,"Copyright (c) 2017, Datadog, Inc"
core,github.com/DataDog/agent-payload/v5/gogen,BSD-3-Clause,"Copyright (c) 2017, Datadog, Inc"
core,github.com/DataDog/agent-payload/v5/process,BSD-3-Clause,"Copyright (c) 2017, Datadog, Inc"
core,github.com/DataDog/agent-payload/v5/sbom,BSD-3-Clause,"Copyright (c) 2017, Datadog, Inc"
core,github.com/DataDog/aptly/aptly,MIT,"* Alexander Guy (https://github.com/alexanderguy) | * Andre Roth (https://github.com/neolynx) | * Andrea Bernardo Ciddio (https://github.com/bcandrea) | * Andrey Smirnov (https://github.com/smira) | * Artem Smirnov (https://github.com/urpylka) | * Benj Fassbind (https://github.com/randombenj) | * Benoit Foucher (https://github.com/bentoi) | * Charles Hsu (https://github.com/charz) | * Chris Read (https://github.com/cread) | * Chuan Liu (https://github.com/chuan) | * Clemens Rabe (https://github.com/seeraven) | * Dmitrii Kashin (https://github.com/freehck) | * Don Kuntz (https://github.com/dkuntz2) | * Geoffrey Thomas (https://github.com/geofft) | * Harald Sitter (https://github.com/apachelogger) | * Ivan Kurnosov (https://github.com/zerkms) | * Johannes Layher (https://github.com/jola5) | * Joshua Colson (https://github.com/freakinhippie) | * Lorenzo Bolla (https://github.com/lbolla) | * Ludovico Cavedon (https://github.com/cavedon) | * Markus Muellner (https://github.com/mmianl) | * Matt Martyn (https://github.com/MMartyn) | * Maximilian Stein (https://github.com/steinymity) | * Michael Koval (https://github.com/mkoval) | * Nabil Bendafi (https://github.com/nabilbendafi) | * Oliver Sauder (https://github.com/sliverc) | * Paul Krohn (https://github.com/paul-krohn) | * Petr Jediny (https://github.com/pjediny) | * Phil Frost (https://github.com/bitglue) | * Raphael Medaer (https://github.com/rmedaer) | * Raul Benencia (https://github.com/rul) | * Rohan Garg (https://github.com/shadeslayer) | * Russ Allbery (https://github.com/rra) | * Russell Greene (https://github.com/russelltg) | * Ryan Uber (https://github.com/ryanuber) | * Samuel Mutel (https://github.com/smutel) | * Sebastien Badia (https://github.com/sbadia) | * Sebastien Binet (https://github.com/sbinet) | * Shengjing Zhu (https://github.com/zhsj) | * Simon Aquino (https://github.com/queeno) | * Strajan Sebastian (https://github.com/strajansebastian) | * Sylvain Baubeau (https://github.com/lebauce) | * Szymon Sobik (https://github.com/sobczyk) | * TJ Merritt (https://github.com/tjmerritt) | * Vincent Batoufflet (https://github.com/vbatoufflet) | * Vincent Bernat (https://github.com/vincentbernat) | * William Manley (https://github.com/wmanley) | * x539 (https://github.com/x539) | Copyright 2013-2015 aptly authors. All rights reserved | List of contributors, in chronological order:"
core,github.com/DataDog/aptly/database,MIT,"* Alexander Guy (https://github.com/alexanderguy) | * Andre Roth (https://github.com/neolynx) | * Andrea Bernardo Ciddio (https://github.com/bcandrea) | * Andrey Smirnov (https://github.com/smira) | * Artem Smirnov (https://github.com/urpylka) | * Benj Fassbind (https://github.com/randombenj) | * Benoit Foucher (https://github.com/bentoi) | * Charles Hsu (https://github.com/charz) | * Chris Read (https://github.com/cread) | * Chuan Liu (https://github.com/chuan) | * Clemens Rabe (https://github.com/seeraven) | * Dmitrii Kashin (https://github.com/freehck) | * Don Kuntz (https://github.com/dkuntz2) | * Geoffrey Thomas (https://github.com/geofft) | * Harald Sitter (https://github.com/apachelogger) | * Ivan Kurnosov (https://github.com/zerkms) | * Johannes Layher (https://github.com/jola5) | * Joshua Colson (https://github.com/freakinhippie) | * Lorenzo Bolla (https://github.com/lbolla) | * Ludovico Cavedon (https://github.com/cavedon) | * Markus Muellner (https://github.com/mmianl) | * Matt Martyn (https://github.com/MMartyn) | * Maximilian Stein (https://github.com/steinymity) | * Michael Koval (https://github.com/mkoval) | * Nabil Bendafi (https://github.com/nabilbendafi) | * Oliver Sauder (https://github.com/sliverc) | * Paul Krohn (https://github.com/paul-krohn) | * Petr Jediny (https://github.com/pjediny) | * Phil Frost (https://github.com/bitglue) | * Raphael Medaer (https://github.com/rmedaer) | * Raul Benencia (https://github.com/rul) | * Rohan Garg (https://github.com/shadeslayer) | * Russ Allbery (https://github.com/rra) | * Russell Greene (https://github.com/russelltg) | * Ryan Uber (https://github.com/ryanuber) | * Samuel Mutel (https://github.com/smutel) | * Sebastien Badia (https://github.com/sbadia) | * Sebastien Binet (https://github.com/sbinet) | * Shengjing Zhu (https://github.com/zhsj) | * Simon Aquino (https://github.com/queeno) | * Strajan Sebastian (https://github.com/strajansebastian) | * Sylvain Baubeau (https://github.com/lebauce) | * Szymon Sobik (https://github.com/sobczyk) | * TJ Merritt (https://github.com/tjmerritt) | * Vincent Batoufflet (https://github.com/vbatoufflet) | * Vincent Bernat (https://github.com/vincentbernat) | * William Manley (https://github.com/wmanley) | * x539 (https://github.com/x539) | Copyright 2013-2015 aptly authors. All rights reserved | List of contributors, in chronological order:"
core,github.com/DataDog/aptly/database/goleveldb,MIT,"* Alexander Guy (https://github.com/alexanderguy) | * Andre Roth (https://github.com/neolynx) | * Andrea Bernardo Ciddio (https://github.com/bcandrea) | * Andrey Smirnov (https://github.com/smira) | * Artem Smirnov (https://github.com/urpylka) | * Benj Fassbind (https://github.com/randombenj) | * Benoit Foucher (https://github.com/bentoi) | * Charles Hsu (https://github.com/charz) | * Chris Read (https://github.com/cread) | * Chuan Liu (https://github.com/chuan) | * Clemens Rabe (https://github.com/seeraven) | * Dmitrii Kashin (https://github.com/freehck) | * Don Kuntz (https://github.com/dkuntz2) | * Geoffrey Thomas (https://github.com/geofft) | * Harald Sitter (https://github.com/apachelogger) | * Ivan Kurnosov (https://github.com/zerkms) | * Johannes Layher (https://github.com/jola5) | * Joshua Colson (https://github.com/freakinhippie) | * Lorenzo Bolla (https://github.com/lbolla) | * Ludovico Cavedon (https://github.com/cavedon) | * Markus Muellner (https://github.com/mmianl) | * Matt Martyn (https://github.com/MMartyn) | * Maximilian Stein (https://github.com/steinymity) | * Michael Koval (https://github.com/mkoval) | * Nabil Bendafi (https://github.com/nabilbendafi) | * Oliver Sauder (https://github.com/sliverc) | * Paul Krohn (https://github.com/paul-krohn) | * Petr Jediny (https://github.com/pjediny) | * Phil Frost (https://github.com/bitglue) | * Raphael Medaer (https://github.com/rmedaer) | * Raul Benencia (https://github.com/rul) | * Rohan Garg (https://github.com/shadeslayer) | * Russ Allbery (https://github.com/rra) | * Russell Greene (https://github.com/russelltg) | * Ryan Uber (https://github.com/ryanuber) | * Samuel Mutel (https://github.com/smutel) | * Sebastien Badia (https://github.com/sbadia) | * Sebastien Binet (https://github.com/sbinet) | * Shengjing Zhu (https://github.com/zhsj) | * Simon Aquino (https://github.com/queeno) | * Strajan Sebastian (https://github.com/strajansebastian) | * Sylvain Baubeau (https://github.com/lebauce) | * Szymon Sobik (https://github.com/sobczyk) | * TJ Merritt (https://github.com/tjmerritt) | * Vincent Batoufflet (https://github.com/vbatoufflet) | * Vincent Bernat (https://github.com/vincentbernat) | * William Manley (https://github.com/wmanley) | * x539 (https://github.com/x539) | Copyright 2013-2015 aptly authors. All rights reserved | List of contributors, in chronological order:"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ require (
code.cloudfoundry.org/bbs v0.0.0-20200403215808-d7bc971db0db
code.cloudfoundry.org/garden v0.0.0-20210208153517-580cadd489d2
code.cloudfoundry.org/lager v2.0.0+incompatible
github.com/DataDog/agent-payload/v5 v5.0.52
github.com/DataDog/agent-payload/v5 v5.0.61
github.com/DataDog/datadog-agent/pkg/obfuscate v0.42.0-rc.3
github.com/DataDog/datadog-agent/pkg/otlp/model v0.42.0-rc.3
github.com/DataDog/datadog-agent/pkg/quantile v0.42.0-rc.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 86 additions & 0 deletions pkg/aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ var (
aggregatorEventPlatformEventsErrors = expvar.Map{}
aggregatorContainerLifecycleEvents = expvar.Int{}
aggregatorContainerLifecycleEventsErrors = expvar.Int{}
aggregatorContainerImages = expvar.Int{}
aggregatorContainerImagesErrors = expvar.Int{}
aggregatorSBOM = expvar.Int{}
aggregatorSBOMErrors = expvar.Int{}

tlmFlush = telemetry.NewCounter("aggregator", "flush",
[]string{"data_type", "state"}, "Number of metrics/service checks/events flushed")
Expand Down Expand Up @@ -179,6 +183,10 @@ func init() {
aggregatorExpvars.Set("EventPlatformEventsErrors", &aggregatorEventPlatformEventsErrors)
aggregatorExpvars.Set("ContainerLifecycleEvents", &aggregatorContainerLifecycleEvents)
aggregatorExpvars.Set("ContainerLifecycleEventsErrors", &aggregatorContainerLifecycleEventsErrors)
aggregatorExpvars.Set("ContainerImages", &aggregatorContainerImages)
aggregatorExpvars.Set("ContainerImagesErrors", &aggregatorContainerImagesErrors)
aggregatorExpvars.Set("SBOM", &aggregatorSBOM)
aggregatorExpvars.Set("SBOMErrors", &aggregatorSBOMErrors)

contextsByMtypeMap := expvar.Map{}
aggregatorDogstatsdContextsByMtype = make([]expvar.Int, int(metrics.NumMetricTypes))
Expand Down Expand Up @@ -212,6 +220,16 @@ type BufferedAggregator struct {
contLcycleStopper chan struct{}
contLcycleDequeueOnce sync.Once

contImageIn chan senderContainerImage
contImageBuffer chan senderContainerImage
contImageStopper chan struct{}
contImageDequeueOnce sync.Once

sbomIn chan senderSBOM
sbomBuffer chan senderSBOM
sbomStopper chan struct{}
sbomDequeueOnce sync.Once

// metricSamplePool is a pool of slices of metric sample to avoid allocations.
// Used by the Dogstatsd Batcher.
MetricSamplePool *metrics.MetricSamplePool
Expand Down Expand Up @@ -290,6 +308,14 @@ func NewBufferedAggregator(s serializer.MetricSerializer, eventPlatformForwarder
contLcycleBuffer: make(chan senderContainerLifecycleEvent, bufferSize),
contLcycleStopper: make(chan struct{}),

contImageIn: make(chan senderContainerImage, bufferSize),
contImageBuffer: make(chan senderContainerImage, bufferSize),
contImageStopper: make(chan struct{}),

sbomIn: make(chan senderSBOM, bufferSize),
sbomBuffer: make(chan senderSBOM, bufferSize),
sbomStopper: make(chan struct{}),

tagsStore: tagsStore,
checkSamplers: make(map[check.ID]*CheckSampler),
flushInterval: flushInterval,
Expand Down Expand Up @@ -785,6 +811,12 @@ func (agg *BufferedAggregator) run() {
case event := <-agg.contLcycleIn:
aggregatorContainerLifecycleEvents.Add(1)
agg.handleContainerLifecycleEvent(event)
case event := <-agg.contImageIn:
aggregatorContainerImages.Add(1)
agg.handleContainerImage(event)
case event := <-agg.sbomIn:
aggregatorSBOM.Add(1)
agg.handleSBOM(event)
}
}
}
Expand All @@ -805,6 +837,38 @@ func (agg *BufferedAggregator) dequeueContainerLifecycleEvents() {
}
}

// dequeueContainerImages consumes buffered container image.
// It is blocking so it should be started in its own routine and only one instance should be started.
func (agg *BufferedAggregator) dequeueContainerImages() {
for {
select {
case event := <-agg.contImageBuffer:
if err := agg.serializer.SendContainerImage(event.msgs, agg.hostname); err != nil {
aggregatorContainerImagesErrors.Add(1)
log.Warnf("Error submitting container image data: %v", err)
}
case <-agg.contImageStopper:
return
}
}
}

// dequeueSBOM consumes buffered SBOM.
// It is blocking so it should be started in its own routine and only one instance should be started.
func (agg *BufferedAggregator) dequeueSBOM() {
for {
select {
case event := <-agg.sbomBuffer:
if err := agg.serializer.SendSBOM(event.msgs, agg.hostname); err != nil {
aggregatorSBOMErrors.Add(1)
log.Warnf("Error submitting SBOM data: %v", err)
}
case <-agg.sbomStopper:
return
}
}
}

// handleContainerLifecycleEvent forwards container lifecycle events to the buffering channel.
func (agg *BufferedAggregator) handleContainerLifecycleEvent(event senderContainerLifecycleEvent) {
select {
Expand All @@ -816,6 +880,28 @@ func (agg *BufferedAggregator) handleContainerLifecycleEvent(event senderContain
}
}

// handleContainerImage forwards container image to the buffering channel.
func (agg *BufferedAggregator) handleContainerImage(event senderContainerImage) {
select {
case agg.contImageBuffer <- event:
return
default:
aggregatorContainerImagesErrors.Add(1)
log.Warn("Container image channel is full")
}
}

// handleSBOM forwards SBOM to the buffering channel.
func (agg *BufferedAggregator) handleSBOM(event senderSBOM) {
select {
case agg.sbomBuffer <- event:
return
default:
aggregatorSBOMErrors.Add(1)
log.Warn("SBOM channel is full")
}
}

// tags returns the list of tags that should be added to the agent telemetry metrics
// Container agent tags may be missing in the first seconds after agent startup
func (agg *BufferedAggregator) tags(withVersion bool) []string {
Expand Down
Loading

0 comments on commit 6f4e092

Please sign in to comment.