-
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.
Add repo digest to workloadmeta.container from docker runtime collect…
…ion (#23045) Add repo digest to workloadmeta.container from docker runtime collection
- Loading branch information
Showing
5 changed files
with
147 additions
and
120 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
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
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
70 changes: 70 additions & 0 deletions
70
comp/core/workloadmeta/collectors/util/image_metadata_util.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,70 @@ | ||
// 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 2023-present Datadog, Inc. | ||
|
||
// Package util contains utility functions for image metadata collection | ||
package util | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/DataDog/datadog-agent/comp/core/workloadmeta" | ||
"github.com/DataDog/datadog-agent/pkg/util/log" | ||
) | ||
|
||
const ( | ||
// SHA256 is the prefix used by containerd for the repo digest | ||
SHA256 = "@sha256:" | ||
) | ||
|
||
// ExtractRepoDigestFromImage extracts the repoDigest from workloadmeta store if it exists and unique | ||
// the format of repoDigest is "registry/repo@sha256:digest", the format of return value is "sha256:digest" | ||
func ExtractRepoDigestFromImage(imageID string, imageRegistry string, store workloadmeta.Component) string { | ||
existingImgMetadata, err := store.GetImage(imageID) | ||
if err == nil { | ||
// If there is only one repoDigest, return it | ||
if len(existingImgMetadata.RepoDigests) == 1 { | ||
_, _, digest := parseRepoDigest(existingImgMetadata.RepoDigests[0]) | ||
return digest | ||
} | ||
// If there are multiple repoDigests, we should find the one that matches the current container's registry | ||
for _, repoDigest := range existingImgMetadata.RepoDigests { | ||
registry, _, digest := parseRepoDigest(repoDigest) | ||
if registry == imageRegistry { | ||
return digest | ||
} | ||
} | ||
log.Debugf("cannot get matched registry in repodigests for image %s", imageID) | ||
} else { | ||
// TODO: we should handle the case when the image metadata is not found in the store | ||
// For example, there could be a rare race condition when collection of image metadata is not finished yet | ||
// In this case, we can either call containerd or docker directly or get it from knownImages in the local cache | ||
log.Debugf("cannot get image metadata for image %s: %s", imageID, err) | ||
} | ||
return "" | ||
} | ||
|
||
// parseRepoDigest extracts registry, repository, digest from repoDigest (in the format of "registry/repo@sha256:digest") | ||
func parseRepoDigest(repoDigest string) (string, string, string) { | ||
var registry, repository, digest string | ||
imageName := repoDigest | ||
|
||
digestStart := strings.Index(repoDigest, SHA256) | ||
if digestStart != -1 { | ||
digest = repoDigest[digestStart+len("@"):] | ||
imageName = repoDigest[:digestStart] | ||
} | ||
|
||
registryEnd := strings.Index(imageName, "/") | ||
// e.g <registry>/<repository> | ||
if registryEnd != -1 { | ||
registry = imageName[:registryEnd] | ||
repository = imageName[registryEnd+len("/"):] | ||
// e.g <registry> | ||
} else { | ||
registry = imageName | ||
} | ||
|
||
return registry, repository, digest | ||
} |
70 changes: 70 additions & 0 deletions
70
comp/core/workloadmeta/collectors/util/image_metadata_util_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,70 @@ | ||
// 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 2023-present Datadog, Inc. | ||
|
||
// Package util contains utility functions for image metadata collection | ||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParseRepoDigest(t *testing.T) { | ||
for _, tc := range []struct { | ||
repoDigest string | ||
registry string | ||
repository string | ||
digest string | ||
}{ | ||
{ | ||
repoDigest: "727006795293.dkr.ecr.us-east-1.amazonaws.com/spidly@sha256:fce79f86f7a3b9c660112da8484a8f5858a7da9e703892ba04c6f045da714300", | ||
registry: "727006795293.dkr.ecr.us-east-1.amazonaws.com", | ||
repository: "spidly", | ||
digest: "sha256:fce79f86f7a3b9c660112da8484a8f5858a7da9e703892ba04c6f045da714300", | ||
}, | ||
{ | ||
repoDigest: "docker.io/library/docker@sha256:b813c414ee36b8a2c44b45295698df6bdc3bdee4a435481dbb892e1b44e09d3b", | ||
registry: "docker.io", | ||
repository: "library/docker", | ||
digest: "sha256:b813c414ee36b8a2c44b45295698df6bdc3bdee4a435481dbb892e1b44e09d3b", | ||
}, | ||
{ | ||
repoDigest: "eu.gcr.io/datadog-staging/logs-event-store-api@sha256:747bd4fc36f3f263b5dcb9df907b98489a4cb46d636c223dc29f1fb7f9405070", | ||
registry: "eu.gcr.io", | ||
repository: "datadog-staging/logs-event-store-api", | ||
digest: "sha256:747bd4fc36f3f263b5dcb9df907b98489a4cb46d636c223dc29f1fb7f9405070", | ||
}, | ||
{ | ||
repoDigest: "registry.ddbuild.io/apm-integrations-testing/handmade/postgres", | ||
registry: "registry.ddbuild.io", | ||
repository: "apm-integrations-testing/handmade/postgres", | ||
digest: "", | ||
}, | ||
{ | ||
repoDigest: "registry.ddbuild.io/apm-integrations-testing/handmade/postgres@sha256:747bd4fc36f3f263b5dcb9df907b98489a4cb46d636c223dc29f1fb7f9405070", | ||
registry: "registry.ddbuild.io", | ||
repository: "apm-integrations-testing/handmade/postgres", | ||
digest: "sha256:747bd4fc36f3f263b5dcb9df907b98489a4cb46d636c223dc29f1fb7f9405070", | ||
}, | ||
{ | ||
repoDigest: "@sha256:747bd4fc36f3f263b5dcb9df907b98489a4cb46d636c223dc29f1fb7f9405070", | ||
registry: "", | ||
repository: "", | ||
digest: "sha256:747bd4fc36f3f263b5dcb9df907b98489a4cb46d636c223dc29f1fb7f9405070", | ||
}, | ||
{ | ||
repoDigest: "docker.io@sha256:747bd4fc36f3f263b5dcb9df907b98489a4cb46d636c223dc29f1fb7f9405070", | ||
registry: "docker.io", | ||
repository: "", | ||
digest: "sha256:747bd4fc36f3f263b5dcb9df907b98489a4cb46d636c223dc29f1fb7f9405070", | ||
}, | ||
} { | ||
registry, repository, digest := parseRepoDigest(tc.repoDigest) | ||
assert.Equal(t, tc.registry, registry) | ||
assert.Equal(t, tc.repository, repository) | ||
assert.Equal(t, tc.digest, digest) | ||
} | ||
} |