-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CONTINT-4545] Fix <missing> docker image layer digests from the dock…
…er collector (#33384)
- Loading branch information
1 parent
04e2e0a
commit 8b33915
Showing
3 changed files
with
232 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
comp/core/workloadmeta/collectors/internal/docker/docker_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
//go:build docker | ||
|
||
package docker | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/api/types/image" | ||
v1 "github.com/opencontainers/image-spec/specs-go/v1" | ||
"github.com/stretchr/testify/assert" | ||
|
||
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def" | ||
) | ||
|
||
func Test_LayersFromDockerHistoryAndInspect(t *testing.T) { | ||
var emptySize int64 | ||
var noDiffCmd = "ENV var=dummy" | ||
|
||
var nonEmptySize int64 = 1 | ||
var cmd = "COPY dummy.sh ." | ||
|
||
var baseTimeUnix int64 | ||
var baseTime = time.Unix(baseTimeUnix, 0) | ||
|
||
var layerID = "dummy id" | ||
|
||
tests := []struct { | ||
name string | ||
history []image.HistoryResponseItem | ||
inspect types.ImageInspect | ||
expected []workloadmeta.ContainerImageLayer | ||
}{ | ||
{ | ||
name: "Layer with CreatedBy and positive Size is assigned a digest", | ||
history: []image.HistoryResponseItem{ | ||
{ | ||
Size: nonEmptySize, | ||
CreatedBy: cmd, | ||
Created: baseTimeUnix, | ||
}, | ||
}, | ||
inspect: types.ImageInspect{ | ||
RootFS: types.RootFS{ | ||
Layers: []string{layerID}, | ||
}, | ||
}, | ||
expected: []workloadmeta.ContainerImageLayer{ | ||
{ | ||
Digest: layerID, | ||
SizeBytes: nonEmptySize, | ||
History: &v1.History{ | ||
Created: &baseTime, | ||
CreatedBy: cmd, | ||
EmptyLayer: false, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Inherited layer with no CreatedBy and no Size is detected and is assigned a digest", | ||
history: []image.HistoryResponseItem{ | ||
{ | ||
Size: emptySize, | ||
Created: baseTimeUnix, | ||
}, | ||
}, | ||
inspect: types.ImageInspect{ | ||
RootFS: types.RootFS{ | ||
Layers: []string{layerID}, | ||
}, | ||
}, | ||
expected: []workloadmeta.ContainerImageLayer{ | ||
{ | ||
Digest: layerID, | ||
SizeBytes: emptySize, | ||
History: &v1.History{ | ||
Created: &baseTime, | ||
EmptyLayer: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Layer with CreatedBy and empty Size is NOT assigned a digest", | ||
history: []image.HistoryResponseItem{ | ||
{ | ||
Size: emptySize, | ||
CreatedBy: noDiffCmd, | ||
Created: baseTimeUnix, | ||
}, | ||
}, | ||
inspect: types.ImageInspect{ | ||
RootFS: types.RootFS{ | ||
Layers: []string{layerID}, | ||
}, | ||
}, | ||
expected: []workloadmeta.ContainerImageLayer{ | ||
{ | ||
SizeBytes: emptySize, | ||
History: &v1.History{ | ||
CreatedBy: noDiffCmd, | ||
Created: &baseTime, | ||
EmptyLayer: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Mix of layers with and without digests are merged in the proper order", | ||
history: []image.HistoryResponseItem{ | ||
{ // "2" in the expected field | ||
Size: nonEmptySize, | ||
Created: baseTimeUnix, | ||
CreatedBy: cmd, | ||
}, | ||
{ | ||
Size: emptySize, | ||
Created: baseTimeUnix, | ||
CreatedBy: noDiffCmd, | ||
}, | ||
{ // "1" in the expected field | ||
Size: emptySize, | ||
Created: baseTimeUnix, | ||
}, | ||
}, | ||
inspect: types.ImageInspect{ | ||
RootFS: types.RootFS{ | ||
Layers: []string{"1", "2"}, | ||
}, | ||
}, | ||
expected: []workloadmeta.ContainerImageLayer{ | ||
{ | ||
Digest: "1", | ||
SizeBytes: emptySize, | ||
History: &v1.History{ | ||
Created: &baseTime, | ||
EmptyLayer: true, | ||
}, | ||
}, | ||
{ | ||
SizeBytes: emptySize, | ||
History: &v1.History{ | ||
Created: &baseTime, | ||
CreatedBy: noDiffCmd, | ||
EmptyLayer: true, | ||
}, | ||
}, | ||
{ | ||
Digest: "2", | ||
SizeBytes: nonEmptySize, | ||
History: &v1.History{ | ||
Created: &baseTime, | ||
CreatedBy: cmd, | ||
EmptyLayer: false, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Number of inspect layers exceeds history layers breaks our assumption and results in no layers returned", | ||
history: []image.HistoryResponseItem{ | ||
{ | ||
Size: nonEmptySize, | ||
CreatedBy: cmd, | ||
Created: baseTimeUnix, | ||
}, | ||
}, | ||
inspect: types.ImageInspect{ | ||
RootFS: types.RootFS{ | ||
Layers: []string{layerID, layerID}, | ||
}, | ||
}, | ||
expected: []workloadmeta.ContainerImageLayer{}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
layers := layersFromDockerHistoryAndInspect(tt.history, tt.inspect) | ||
assert.ElementsMatchf(t, tt.expected, layers, "Expected layers and actual layers returned do not match") | ||
}) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
releasenotes/notes/fix-missing-docker-runtime-image-layer-digests-ab42e0cd0d2bd16d.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Each section from every release note are combined when the | ||
# CHANGELOG.rst is rendered. So the text needs to be worded so that | ||
# it does not depend on any information only available in another | ||
# section. This may mean repeating some details, but each section | ||
# must be readable independently of the other. | ||
# | ||
# Each section note must be formatted as reStructuredText. | ||
--- | ||
enhancements: | ||
- | | ||
Image layer digests will no longer report as "<missing>" from Docker runtimes. |