Skip to content

Commit

Permalink
[CONTINT-4499] Include empty layers in container image metadata on co…
Browse files Browse the repository at this point in the history
…ntainerd (DataDog#31384)
  • Loading branch information
ewoodthomas authored Nov 25, 2024
1 parent 942529b commit 84c20fb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 18 deletions.
50 changes: 35 additions & 15 deletions comp/core/workloadmeta/collectors/internal/containerd/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,37 +441,57 @@ func extractPlatform(platform *ocispec.Platform, outImage *workloadmeta.Containe
func getLayersWithHistory(ocispecImage ocispec.Image, manifest ocispec.Manifest) []workloadmeta.ContainerImageLayer {
var layers []workloadmeta.ContainerImageLayer

// The layers in the manifest don't include the history, and the only way to
// match the history with each layer is to rely on the order and take into
// account that some history objects don't have an associated layer
// (emptyLayer = true).
// History is optional in OCI Spec, so we have no guarantee to be able to get it.
// If history is present, we use it to associate additional metadata with each layer.
// Layers marked as "empty" in history are appended before processing the
// corresponding layer. History is optional in the OCI specification, so if no history is available,
// the function still processes all layers. Any remaining empty layers in history that
// do not correspond to a layer are appended at the end.

historyIndex := 0
for _, manifestLayer := range manifest.Layers {
// Look for next history point with emptyLayer = false
historyFound := false
for ; historyIndex < len(ocispecImage.History); historyIndex++ {
if !ocispecImage.History[historyIndex].EmptyLayer {
historyFound = true
// Append all empty layers encountered before a non-empty layer
for historyIndex < len(ocispecImage.History) {
history := ocispecImage.History[historyIndex]
if history.EmptyLayer {
layers = append(layers, workloadmeta.ContainerImageLayer{
History: &history,
})
historyIndex++
} else {
// Stop at the first non-empty layer
break
}
}

// Match the non-empty history to this manifest layer, if available
var history *ocispec.History
if historyIndex < len(ocispecImage.History) {
history = &ocispecImage.History[historyIndex]
historyIndex++
}

// Create and append the layer with manifest and matched history
layer := workloadmeta.ContainerImageLayer{
MediaType: manifestLayer.MediaType,
Digest: manifestLayer.Digest.String(),
SizeBytes: manifestLayer.Size,
URLs: manifestLayer.URLs,
History: history,
}
if historyFound {
layer.History = &ocispecImage.History[historyIndex]
historyIndex++
}

layers = append(layers, layer)
}

// Append any remaining empty layers after processing all manifest layers
for historyIndex < len(ocispecImage.History) {
history := ocispecImage.History[historyIndex]
if history.EmptyLayer {
layers = append(layers, workloadmeta.ContainerImageLayer{
History: &history,
})
}
historyIndex++
}

return layers
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,56 @@ func TestGetLayersWithHistory(t *testing.T) {
Digest: digest.FromString("foo").String(),
SizeBytes: 1,
History: &ocispec.History{
Comment: "not-empty1",
Comment: "not-empty1",
EmptyLayer: false,
},
},
{
History: &ocispec.History{
Comment: "empty1",
EmptyLayer: true,
},
},
{
History: &ocispec.History{
Comment: "empty2",
EmptyLayer: true,
},
},
{
MediaType: ocispec.MediaTypeImageLayer,
Digest: digest.FromString("bar").String(),
SizeBytes: 2,
History: &ocispec.History{
Comment: "not-empty2",
Comment: "not-empty2",
EmptyLayer: false,
},
},
{
History: &ocispec.History{
Comment: "empty3",
EmptyLayer: true,
},
},
{
MediaType: ocispec.MediaTypeImageLayer,
Digest: digest.FromString("baz").String(),
SizeBytes: 3,
History: &ocispec.History{
Comment: "not-empty3",
Comment: "not-empty3",
EmptyLayer: false,
},
},
{
History: &ocispec.History{
Comment: "empty4",
EmptyLayer: true,
},
},
{
History: &ocispec.History{
Comment: "empty5",
EmptyLayer: true,
},
},
{
Expand Down

0 comments on commit 84c20fb

Please sign in to comment.