From e367891b672cc5e7f20867a17b71c021777388f5 Mon Sep 17 00:00:00 2001 From: "John L. Peterson (Jack)" Date: Wed, 4 Sep 2024 11:21:15 -0400 Subject: [PATCH] creates pkg/util/containers/image go module (#29024) --- cmd/serverless/dependencies_linux_amd64.txt | 1 + cmd/serverless/dependencies_linux_arm64.txt | 1 + comp/core/autodiscovery/listeners/container.go | 3 ++- .../workloadmeta/collectors/internal/docker/docker.go | 9 +++++---- .../collectors/internal/kubelet/kubelet.go | 3 ++- comp/core/workloadmeta/def/types.go | 4 ++-- go.mod | 2 ++ .../corechecks/containers/containerd/utils.go | 3 ++- pkg/collector/corechecks/containers/docker/utils.go | 3 ++- .../containers/kubelet/common/testing/utils.go | 3 ++- pkg/util/containers/image/go.mod | 11 +++++++++++ pkg/util/containers/image/go.sum | 10 ++++++++++ pkg/util/containers/{ => image}/image.go | 3 ++- pkg/util/containers/{ => image}/image_test.go | 2 +- tasks/modules.py | 1 + 15 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 pkg/util/containers/image/go.mod create mode 100644 pkg/util/containers/image/go.sum rename pkg/util/containers/{ => image}/image.go (94%) rename pkg/util/containers/{ => image}/image_test.go (99%) diff --git a/cmd/serverless/dependencies_linux_amd64.txt b/cmd/serverless/dependencies_linux_amd64.txt index c2236a0d80c05..6059520efe6e5 100644 --- a/cmd/serverless/dependencies_linux_amd64.txt +++ b/cmd/serverless/dependencies_linux_amd64.txt @@ -261,6 +261,7 @@ github.com/DataDog/datadog-agent/pkg/util/cloudproviders/gce github.com/DataDog/datadog-agent/pkg/util/clusteragent github.com/DataDog/datadog-agent/pkg/util/common github.com/DataDog/datadog-agent/pkg/util/containers +github.com/DataDog/datadog-agent/pkg/util/containers/image github.com/DataDog/datadog-agent/pkg/util/containers/metrics/provider github.com/DataDog/datadog-agent/pkg/util/dmi github.com/DataDog/datadog-agent/pkg/util/docker diff --git a/cmd/serverless/dependencies_linux_arm64.txt b/cmd/serverless/dependencies_linux_arm64.txt index 0615d268ce732..f5373b770a685 100644 --- a/cmd/serverless/dependencies_linux_arm64.txt +++ b/cmd/serverless/dependencies_linux_arm64.txt @@ -261,6 +261,7 @@ github.com/DataDog/datadog-agent/pkg/util/cloudproviders/gce github.com/DataDog/datadog-agent/pkg/util/clusteragent github.com/DataDog/datadog-agent/pkg/util/common github.com/DataDog/datadog-agent/pkg/util/containers +github.com/DataDog/datadog-agent/pkg/util/containers/image github.com/DataDog/datadog-agent/pkg/util/containers/metrics/provider github.com/DataDog/datadog-agent/pkg/util/dmi github.com/DataDog/datadog-agent/pkg/util/docker diff --git a/comp/core/autodiscovery/listeners/container.go b/comp/core/autodiscovery/listeners/container.go index 11bb0f526bdeb..f7afa5ec80cd5 100644 --- a/comp/core/autodiscovery/listeners/container.go +++ b/comp/core/autodiscovery/listeners/container.go @@ -20,6 +20,7 @@ import ( workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def" "github.com/DataDog/datadog-agent/pkg/config" "github.com/DataDog/datadog-agent/pkg/util/containers" + pkgcontainersimage "github.com/DataDog/datadog-agent/pkg/util/containers/image" "github.com/DataDog/datadog-agent/pkg/util/docker" "github.com/DataDog/datadog-agent/pkg/util/log" "github.com/DataDog/datadog-agent/pkg/util/optional" @@ -215,7 +216,7 @@ func computeContainerServiceIDs(entity string, image string, labels map[string]s ids := []string{entity} // Add Image names (long then short if different) - long, _, short, _, err := containers.SplitImageName(image) + long, _, short, _, err := pkgcontainersimage.SplitImageName(image) if err != nil { log.Warnf("error while spliting image name: %s", err) } diff --git a/comp/core/workloadmeta/collectors/internal/docker/docker.go b/comp/core/workloadmeta/collectors/internal/docker/docker.go index cf4d5e68fb270..d65673750002a 100644 --- a/comp/core/workloadmeta/collectors/internal/docker/docker.go +++ b/comp/core/workloadmeta/collectors/internal/docker/docker.go @@ -33,6 +33,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/sbom/scanner" "github.com/DataDog/datadog-agent/pkg/status/health" "github.com/DataDog/datadog-agent/pkg/util/containers" + pkgcontainersimage "github.com/DataDog/datadog-agent/pkg/util/containers/image" "github.com/DataDog/datadog-agent/pkg/util/docker" "github.com/DataDog/datadog-agent/pkg/util/kubernetes" "github.com/DataDog/datadog-agent/pkg/util/log" @@ -364,7 +365,7 @@ func extractImage(ctx context.Context, container types.ContainerJSON, resolve re ) if strings.Contains(imageSpec, "@sha256") { - name, registry, shortName, tag, err = containers.SplitImageName(imageSpec) + name, registry, shortName, tag, err = pkgcontainersimage.SplitImageName(imageSpec) if err != nil { log.Debugf("cannot split image name %q for container %q: %s", imageSpec, container.ID, err) } @@ -377,13 +378,13 @@ func extractImage(ctx context.Context, container types.ContainerJSON, resolve re return image } - name, registry, shortName, tag, err = containers.SplitImageName(resolvedImageSpec) + name, registry, shortName, tag, err = pkgcontainersimage.SplitImageName(resolvedImageSpec) if err != nil { log.Debugf("cannot split image name %q for container %q: %s", resolvedImageSpec, container.ID, err) // fallback and try to parse the original imageSpec anyway - if errors.Is(err, containers.ErrImageIsSha256) { - name, registry, shortName, tag, err = containers.SplitImageName(imageSpec) + if errors.Is(err, pkgcontainersimage.ErrImageIsSha256) { + name, registry, shortName, tag, err = pkgcontainersimage.SplitImageName(imageSpec) if err != nil { log.Debugf("cannot split image name %q for container %q: %s", imageSpec, container.ID, err) return image diff --git a/comp/core/workloadmeta/collectors/internal/kubelet/kubelet.go b/comp/core/workloadmeta/collectors/internal/kubelet/kubelet.go index c346809148780..f4dec30571972 100644 --- a/comp/core/workloadmeta/collectors/internal/kubelet/kubelet.go +++ b/comp/core/workloadmeta/collectors/internal/kubelet/kubelet.go @@ -21,6 +21,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/config/env" "github.com/DataDog/datadog-agent/pkg/errors" "github.com/DataDog/datadog-agent/pkg/util/containers" + pkgcontainersimage "github.com/DataDog/datadog-agent/pkg/util/containers/image" "github.com/DataDog/datadog-agent/pkg/util/kubernetes" "github.com/DataDog/datadog-agent/pkg/util/kubernetes/kubelet" "github.com/DataDog/datadog-agent/pkg/util/log" @@ -219,7 +220,7 @@ func (c *collector) parsePodContainers( image, err := workloadmeta.NewContainerImage(imageID, container.Image) if err != nil { - if stdErrors.Is(err, containers.ErrImageIsSha256) { + if stdErrors.Is(err, pkgcontainersimage.ErrImageIsSha256) { // try the resolved image ID if the image name in the container // status is a SHA256. this seems to happen sometimes when // pinning the image to a SHA256 diff --git a/comp/core/workloadmeta/def/types.go b/comp/core/workloadmeta/def/types.go index cad44b46ffb28..ad596604c67fa 100644 --- a/comp/core/workloadmeta/def/types.go +++ b/comp/core/workloadmeta/def/types.go @@ -20,7 +20,7 @@ import ( "github.com/DataDog/datadog-agent/comp/core/config" "github.com/DataDog/datadog-agent/pkg/languagedetection/languagemodels" langUtil "github.com/DataDog/datadog-agent/pkg/languagedetection/util" - "github.com/DataDog/datadog-agent/pkg/util/containers" + pkgcontainersimage "github.com/DataDog/datadog-agent/pkg/util/containers/image" ) // TODO(component): it might make more sense to move the store into its own @@ -277,7 +277,7 @@ func NewContainerImage(imageID string, imageName string) (ContainerImage, error) Name: imageName, } - name, registry, shortName, tag, err := containers.SplitImageName(imageName) + name, registry, shortName, tag, err := pkgcontainersimage.SplitImageName(imageName) if err != nil { return image, err } diff --git a/go.mod b/go.mod index ae1eda2c4d9f4..ded7d7e482a48 100644 --- a/go.mod +++ b/go.mod @@ -110,6 +110,7 @@ replace ( github.com/DataDog/datadog-agent/pkg/util/cache => ./pkg/util/cache github.com/DataDog/datadog-agent/pkg/util/cgroups => ./pkg/util/cgroups github.com/DataDog/datadog-agent/pkg/util/common => ./pkg/util/common + github.com/DataDog/datadog-agent/pkg/util/containers/image => ./pkg/util/containers/image github.com/DataDog/datadog-agent/pkg/util/executable => ./pkg/util/executable github.com/DataDog/datadog-agent/pkg/util/filesystem => ./pkg/util/filesystem github.com/DataDog/datadog-agent/pkg/util/flavor => ./pkg/util/flavor @@ -678,6 +679,7 @@ require ( github.com/DataDog/datadog-agent/pkg/util/backoff v0.56.0-rc.3 github.com/DataDog/datadog-agent/pkg/util/cache v0.56.0-rc.3 github.com/DataDog/datadog-agent/pkg/util/common v0.56.0-rc.3 + github.com/DataDog/datadog-agent/pkg/util/containers/image v0.56.2 github.com/DataDog/datadog-agent/pkg/util/executable v0.56.0-rc.3 github.com/DataDog/datadog-agent/pkg/util/filesystem v0.56.0-rc.3 github.com/DataDog/datadog-agent/pkg/util/flavor v0.56.0-rc.3 diff --git a/pkg/collector/corechecks/containers/containerd/utils.go b/pkg/collector/corechecks/containers/containerd/utils.go index 33adb6ac98e74..851c156bf2072 100644 --- a/pkg/collector/corechecks/containers/containerd/utils.go +++ b/pkg/collector/corechecks/containers/containerd/utils.go @@ -13,6 +13,7 @@ import ( workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/containers/generic" "github.com/DataDog/datadog-agent/pkg/util/containers" + pkgcontainersimage "github.com/DataDog/datadog-agent/pkg/util/containers/image" ) func getProcessorFilter(legacyFilter *containers.Filter, store workloadmeta.Component) generic.ContainerFilter { @@ -26,7 +27,7 @@ func getProcessorFilter(legacyFilter *containers.Filter, store workloadmeta.Comp } func getImageTags(imageName string) []string { - long, _, short, tag, err := containers.SplitImageName(imageName) + long, _, short, tag, err := pkgcontainersimage.SplitImageName(imageName) if err != nil { return []string{fmt.Sprintf("image:%s", imageName)} } diff --git a/pkg/collector/corechecks/containers/docker/utils.go b/pkg/collector/corechecks/containers/docker/utils.go index 8a17be87e82d0..500cf6cdde2d7 100644 --- a/pkg/collector/corechecks/containers/docker/utils.go +++ b/pkg/collector/corechecks/containers/docker/utils.go @@ -17,6 +17,7 @@ import ( workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/containers/generic" "github.com/DataDog/datadog-agent/pkg/util/containers" + pkgcontainersimage "github.com/DataDog/datadog-agent/pkg/util/containers/image" ) func getProcessorFilter(legacyFilter *containers.Filter, store workloadmeta.Component) generic.ContainerFilter { @@ -43,7 +44,7 @@ func getImageTagsFromContainer(taggerEntityID string, resolvedImageName string, } func getImageTags(imageName string) ([]string, error) { - long, _, short, tag, err := containers.SplitImageName(imageName) + long, _, short, tag, err := pkgcontainersimage.SplitImageName(imageName) if err != nil { return nil, err } diff --git a/pkg/collector/corechecks/containers/kubelet/common/testing/utils.go b/pkg/collector/corechecks/containers/kubelet/common/testing/utils.go index 5054431177d18..dbc422edad0af 100644 --- a/pkg/collector/corechecks/containers/kubelet/common/testing/utils.go +++ b/pkg/collector/corechecks/containers/kubelet/common/testing/utils.go @@ -23,6 +23,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/aggregator/mocksender" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/containers/kubelet/common" "github.com/DataDog/datadog-agent/pkg/util/containers" + pkgcontainersimage "github.com/DataDog/datadog-agent/pkg/util/containers/image" "github.com/DataDog/datadog-agent/pkg/util/kubernetes" "github.com/DataDog/datadog-agent/pkg/util/kubernetes/kubelet" "github.com/DataDog/datadog-agent/pkg/util/kubernetes/kubelet/mock" @@ -187,7 +188,7 @@ func StorePopulatedFromFile(store workloadmetamock.Mock, filename string, podUti image, err := workloadmeta.NewContainerImage(container.ImageID, container.Image) if err != nil { - if errors.Is(err, containers.ErrImageIsSha256) { + if errors.Is(err, pkgcontainersimage.ErrImageIsSha256) { // try the resolved image ID if the image name in the container // status is a SHA256. this seems to happen sometimes when // pinning the image to a SHA256 diff --git a/pkg/util/containers/image/go.mod b/pkg/util/containers/image/go.mod new file mode 100644 index 0000000000000..7108f9f3db1a9 --- /dev/null +++ b/pkg/util/containers/image/go.mod @@ -0,0 +1,11 @@ +module github.com/DataDog/datadog-agent/pkg/util/containers/image + +go 1.22.0 + +require github.com/stretchr/testify v1.9.0 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/pkg/util/containers/image/go.sum b/pkg/util/containers/image/go.sum new file mode 100644 index 0000000000000..60ce688a04104 --- /dev/null +++ b/pkg/util/containers/image/go.sum @@ -0,0 +1,10 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/util/containers/image.go b/pkg/util/containers/image/image.go similarity index 94% rename from pkg/util/containers/image.go rename to pkg/util/containers/image/image.go index e7cb6a0d20039..eaccc3f174460 100644 --- a/pkg/util/containers/image.go +++ b/pkg/util/containers/image/image.go @@ -3,7 +3,8 @@ // This product includes software developed at Datadog (https://www.datadoghq.com/). // Copyright 2016-present Datadog, Inc. -package containers +// Package image provides utilities to handle container images for pkg/util/containers +package image import ( "errors" diff --git a/pkg/util/containers/image_test.go b/pkg/util/containers/image/image_test.go similarity index 99% rename from pkg/util/containers/image_test.go rename to pkg/util/containers/image/image_test.go index 7574af49675f0..50a6746771f2f 100644 --- a/pkg/util/containers/image_test.go +++ b/pkg/util/containers/image/image_test.go @@ -3,7 +3,7 @@ // This product includes software developed at Datadog (https://www.datadoghq.com/). // Copyright 2016-present Datadog, Inc. -package containers +package image import ( "fmt" diff --git a/tasks/modules.py b/tasks/modules.py index 96757a0579592..39cbb81e14242 100644 --- a/tasks/modules.py +++ b/tasks/modules.py @@ -256,6 +256,7 @@ def dependency_path(self, agent_version): "pkg/util/cgroups", independent=True, condition=lambda: sys.platform == "linux", used_by_otel=True ), "pkg/util/common": GoModule("pkg/util/common", independent=True, used_by_otel=True), + "pkg/util/containers/image": GoModule("pkg/util/containers/image", independent=True, used_by_otel=True), "pkg/util/executable": GoModule("pkg/util/executable", independent=True, used_by_otel=True), "pkg/util/filesystem": GoModule("pkg/util/filesystem", independent=True, used_by_otel=True), "pkg/util/flavor": GoModule("pkg/util/flavor", independent=True),