Skip to content

Commit

Permalink
creates pkg/util/containers/image go module (#29024)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackgopack4 authored Sep 4, 2024
1 parent 27bc906 commit e367891
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 13 deletions.
1 change: 1 addition & 0 deletions cmd/serverless/dependencies_linux_amd64.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cmd/serverless/dependencies_linux_arm64.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion comp/core/autodiscovery/listeners/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 5 additions & 4 deletions comp/core/workloadmeta/collectors/internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions comp/core/workloadmeta/def/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pkg/collector/corechecks/containers/containerd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/collector/corechecks/containers/docker/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions pkg/util/containers/image/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
10 changes: 10 additions & 0 deletions pkg/util/containers/image/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions tasks/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit e367891

Please sign in to comment.