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

Change reported digest for containerd image layers to the rootfs diffid #33641

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 8 additions & 2 deletions comp/core/workloadmeta/collectors/internal/containerd/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,13 @@ func getLayersWithHistory(ocispecImage ocispec.Image, manifest ocispec.Manifest)
// do not correspond to a layer are appended at the end.

historyIndex := 0
for _, manifestLayer := range manifest.Layers {
for manifestIndex, manifestLayer := range manifest.Layers {
// Prefer the diffID for the current layer, but fall back to the digest as it appears in the
// manifest in the event of a mismatch.
digest := manifestLayer.Digest.String()
if manifestIndex < len(ocispecImage.RootFS.DiffIDs) {
digest = ocispecImage.RootFS.DiffIDs[manifestIndex].String()
}
// Append all empty layers encountered before a non-empty layer
for historyIndex < len(ocispecImage.History) {
history := ocispecImage.History[historyIndex]
Expand All @@ -473,7 +479,7 @@ func getLayersWithHistory(ocispecImage ocispec.Image, manifest ocispec.Manifest)
// Create and append the layer with manifest and matched history
layer := workloadmeta.ContainerImageLayer{
MediaType: manifestLayer.MediaType,
Digest: manifestLayer.Digest.String(),
Digest: digest,
SizeBytes: manifestLayer.Size,
URLs: manifestLayer.URLs,
History: history,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ func TestGetLayersWithHistory(t *testing.T) {
Comment: "empty5",
},
},
RootFS: ocispec.RootFS{
DiffIDs: []digest.Digest{
digest.FromString("abc"),
digest.FromString("def"),
digest.FromString("ghi"),
digest.FromString("jkl"),
},
},
}

manifest := ocispec.Manifest{
Expand Down Expand Up @@ -144,7 +152,7 @@ func TestGetLayersWithHistory(t *testing.T) {
assert.Equal(t, []workloadmeta.ContainerImageLayer{
{
MediaType: ocispec.MediaTypeImageLayer,
Digest: digest.FromString("foo").String(),
Digest: digest.FromString("abc").String(),
SizeBytes: 1,
History: &ocispec.History{
Comment: "not-empty1",
Expand All @@ -165,7 +173,7 @@ func TestGetLayersWithHistory(t *testing.T) {
},
{
MediaType: ocispec.MediaTypeImageLayer,
Digest: digest.FromString("bar").String(),
Digest: digest.FromString("def").String(),
SizeBytes: 2,
History: &ocispec.History{
Comment: "not-empty2",
Expand All @@ -180,7 +188,7 @@ func TestGetLayersWithHistory(t *testing.T) {
},
{
MediaType: ocispec.MediaTypeImageLayer,
Digest: digest.FromString("baz").String(),
Digest: digest.FromString("ghi").String(),
SizeBytes: 3,
History: &ocispec.History{
Comment: "not-empty3",
Expand All @@ -201,7 +209,7 @@ func TestGetLayersWithHistory(t *testing.T) {
},
{
MediaType: ocispec.MediaTypeImageLayer,
Digest: digest.FromString("bow").String(),
Digest: digest.FromString("jkl").String(),
SizeBytes: 4,
},
}, layers)
Expand Down
Loading