Skip to content

Commit

Permalink
fix: use k8s container image parser
Browse files Browse the repository at this point in the history
  • Loading branch information
rogercoll committed Nov 8, 2024
1 parent e47aa1a commit ca69a80
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 58 deletions.
25 changes: 6 additions & 19 deletions internal/common/docker/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@ package docker // import "github.com/open-telemetry/opentelemetry-collector-cont

import (
"errors"
"regexp"

"go.uber.org/zap"
)

var (
extractImageRegexp = regexp.MustCompile(`^(?P<repository>([^/\s]+/)?([^:\s]+))(:(?P<tag>[^@\s]+))?(@sha256:(?P<sha256>\d+))?$`)
"k8s.io/kubernetes/pkg/util/parsers"
)

type ImageRef struct {
Repository string
Tag string
SHA256 string
Digest string
}

// ParseImageName extracts image repository and tag from a combined image reference
Expand All @@ -27,24 +23,15 @@ func ParseImageName(image string) (ImageRef, error) {
return ImageRef{}, errors.New("empty image")
}

match := extractImageRegexp.FindStringSubmatch(image)
if len(match) == 0 {
return ImageRef{}, errors.New("failed to match regex against image")
}

tag := "latest"
if foundTag := match[extractImageRegexp.SubexpIndex("tag")]; foundTag != "" {
tag = foundTag
repository, tag, digest, err := parsers.ParseImageName(image)
if err != nil {
return ImageRef{}, errors.New("failed to parse image")
}

repository := match[extractImageRegexp.SubexpIndex("repository")]

hash := match[extractImageRegexp.SubexpIndex("sha256")]

return ImageRef{
Repository: repository,
Tag: tag,
SHA256: hash,
Digest: digest,
}, nil
}

Expand Down
50 changes: 30 additions & 20 deletions internal/common/docker/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestDockerImageToElements(t *testing.T) {
args args
wantRepository string
wantTag string
wantSHA256 string
wantDigest string
wantErr bool
}{
{
Expand All @@ -26,7 +26,7 @@ func TestDockerImageToElements(t *testing.T) {
},
wantRepository: "",
wantTag: "",
wantSHA256: "",
wantDigest: "",
wantErr: true,
},
{
Expand All @@ -36,57 +36,67 @@ func TestDockerImageToElements(t *testing.T) {
},
wantRepository: "",
wantTag: "",
wantSHA256: "",
wantDigest: "",
wantErr: true,
},
{
name: "image with sha256 hash",
args: args{
image: "alpine:test@sha256:00000000000000",
image: "alpine:test@sha256:22eeed9e66facc651d4344ef7d9ce912fccff5bb3969e745eed3ab953f309534",
},
wantRepository: "alpine",
wantRepository: "docker.io/library/alpine",
wantTag: "test",
wantSHA256: "00000000000000",
wantDigest: "sha256:22eeed9e66facc651d4344ef7d9ce912fccff5bb3969e745eed3ab953f309534",
wantErr: false,
},
{
name: "image with sha256 hash and no tag",
args: args{
image: "alpine@sha256:22eeed9e66facc651d4344ef7d9ce912fccff5bb3969e745eed3ab953f309534",
},
wantRepository: "docker.io/library/alpine",
wantTag: "",
wantDigest: "sha256:22eeed9e66facc651d4344ef7d9ce912fccff5bb3969e745eed3ab953f309534",
wantErr: false,
},
{
name: "shorthand only",
args: args{
image: "alpine",
},
wantRepository: "alpine",
wantRepository: "docker.io/library/alpine",
wantTag: "latest",
wantSHA256: "",
wantDigest: "",
wantErr: false,
},
{
name: "shorthand with tag",
args: args{
image: "alpine:v1.0.0",
},
wantRepository: "alpine",
wantRepository: "docker.io/library/alpine",
wantTag: "v1.0.0",
wantSHA256: "",
wantDigest: "",
wantErr: false,
},
{
name: "repository without registry and tag",
args: args{
image: "alpine/alpine",
},
wantRepository: "alpine/alpine",
wantRepository: "docker.io/alpine/alpine",
wantTag: "latest",
wantSHA256: "",
wantDigest: "",
wantErr: false,
},
{
name: "repository without registry",
args: args{
image: "alpine/alpine:2.0.0",
},
wantRepository: "alpine/alpine",
wantRepository: "docker.io/alpine/alpine",
wantTag: "2.0.0",
wantSHA256: "",
wantDigest: "",
wantErr: false,
},
{
Expand All @@ -96,7 +106,7 @@ func TestDockerImageToElements(t *testing.T) {
},
wantRepository: "example.com/alpine/alpine",
wantTag: "latest",
wantSHA256: "",
wantDigest: "",
wantErr: false,
},
{
Expand All @@ -106,7 +116,7 @@ func TestDockerImageToElements(t *testing.T) {
},
wantRepository: "example.com/alpine/alpine",
wantTag: "1",
wantSHA256: "",
wantDigest: "",
wantErr: false,
},
{
Expand All @@ -116,7 +126,7 @@ func TestDockerImageToElements(t *testing.T) {
},
wantRepository: "example.com:3000/alpine/alpine",
wantTag: "latest",
wantSHA256: "",
wantDigest: "",
wantErr: false,
},
{
Expand All @@ -126,7 +136,7 @@ func TestDockerImageToElements(t *testing.T) {
},
wantRepository: "example.com:3000/alpine/alpine",
wantTag: "test",
wantSHA256: "",
wantDigest: "",
wantErr: false,
},
}
Expand All @@ -143,8 +153,8 @@ func TestDockerImageToElements(t *testing.T) {
if image.Tag != tt.wantTag {
t.Errorf("ParseImageName() tag = %v, want %v", image.Tag, tt.wantTag)
}
if image.SHA256 != tt.wantSHA256 {
t.Errorf("ParseImageName() hash = %v, want %v", image.SHA256, tt.wantSHA256)
if image.Digest != tt.wantDigest {
t.Errorf("ParseImageName() hash = %v, want %v", image.Digest, tt.wantDigest)
}
})
}
Expand Down
7 changes: 5 additions & 2 deletions internal/common/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ require (
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
go.uber.org/multierr v1.11.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/kubernetes v1.31.2 // indirect
)

retract (
Expand Down
8 changes: 8 additions & 0 deletions internal/common/go.sum

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

4 changes: 1 addition & 3 deletions receiver/k8sclusterreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ require (
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/distribution/reference v0.5.0 // indirect
Expand Down Expand Up @@ -110,7 +108,6 @@ require (
go.opentelemetry.io/otel/sdk v1.31.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.31.0 // indirect
go.opentelemetry.io/otel/trace v1.31.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.30.0 // indirect
Expand All @@ -131,6 +128,7 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/kubernetes v1.31.2 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
Expand Down
12 changes: 7 additions & 5 deletions receiver/k8sclusterreceiver/go.sum

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

4 changes: 3 additions & 1 deletion receiver/k8sclusterreceiver/internal/container/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ func RecordSpecMetrics(logger *zap.Logger, mb *imetadata.MetricsBuilder, c corev
docker.LogParseError(err, imageStr, logger)
} else {
rb.SetContainerImageName(image.Repository)
rb.SetContainerImageTag(image.Tag)
if image.Tag != "" {
rb.SetContainerImageTag(image.Tag)
}
}
mb.EmitForResource(imetadata.WithResource(rb.Emit()))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ resourceMetrics:
- key: container.image.name
value:
stringValue: docker.io/otel/test-image
- key: container.image.tag
value:
stringValue: latest
- key: k8s.container.name
value:
stringValue: container-name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ resourceMetrics:
stringValue: container-id
- key: container.image.name
value:
stringValue: container-image-name
- key: container.image.tag
value:
stringValue: latest
stringValue: docker.io/otel/test-image
- key: k8s.container.name
value:
stringValue: container-name
Expand Down
2 changes: 1 addition & 1 deletion receiver/k8sclusterreceiver/internal/testutils/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func NewEvictedTerminatedPodStatusWithContainer(containerName, containerID strin
Name: containerName,
Ready: true,
RestartCount: 3,
Image: "container-image-name",
Image: "docker.io/otel/test-image@sha256:22eeed9e66facc651d4344ef7d9ce912fccff5bb3969e745eed3ab953f309534",
ContainerID: containerID,
State: corev1.ContainerState{
Terminated: &corev1.ContainerStateTerminated{},
Expand Down

0 comments on commit ca69a80

Please sign in to comment.