-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[processor/k8sattributes] Support name:tag@digest image name format #36145
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: bug_fix | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: processor/k8sattribute | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: fixes parsing of k8s image names to support images with tags and digests. | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [36131] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [user] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,7 @@ func newExpectedValue(mode int, value string) *expectedValue { | |
|
||
// TestE2E_ClusterRBAC tests the k8s attributes processor in a k8s cluster with the collector's service account having | ||
// cluster-wide permissions to list/watch namespaces, nodes, pods and replicasets. The config in the test does not | ||
// set filter::namespace. | ||
// set filter::namespace, and the telemetrygen image has a latest tag but no digest. | ||
// The test requires a prebuilt otelcontribcol image uploaded to a kind k8s cluster defined in | ||
// `/tmp/kube-config-otelcol-e2e-testing`. Run the following command prior to running the test locally: | ||
// | ||
|
@@ -432,7 +432,8 @@ func TestE2E_ClusterRBAC(t *testing.T) { | |
} | ||
} | ||
|
||
// Test with `filter::namespace` set and only role binding to collector's SA. We can't get node and namespace labels/annotations. | ||
// Test with `filter::namespace` set and only role binding to collector's SA. We can't get node and namespace labels/annotations, | ||
// and the telemetrygen image has a digest but no tag. | ||
func TestE2E_NamespacedRBAC(t *testing.T) { | ||
|
||
testDir := filepath.Join("testdata", "e2e", "namespacedrbac") | ||
|
@@ -504,7 +505,7 @@ func TestE2E_NamespacedRBAC(t *testing.T) { | |
"k8s.container.name": newExpectedValue(equal, "telemetrygen"), | ||
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"), | ||
"container.image.repo_digests": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"), | ||
"container.image.tag": newExpectedValue(equal, "latest"), | ||
"container.image.tag": newExpectedValue(shouldnotexist, ""), | ||
"container.id": newExpectedValue(exist, ""), | ||
}, | ||
}, | ||
|
@@ -528,7 +529,7 @@ func TestE2E_NamespacedRBAC(t *testing.T) { | |
"k8s.container.name": newExpectedValue(equal, "telemetrygen"), | ||
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"), | ||
"container.image.repo_digests": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"), | ||
"container.image.tag": newExpectedValue(equal, "latest"), | ||
"container.image.tag": newExpectedValue(shouldnotexist, ""), | ||
"container.id": newExpectedValue(exist, ""), | ||
}, | ||
}, | ||
|
@@ -552,7 +553,7 @@ func TestE2E_NamespacedRBAC(t *testing.T) { | |
"k8s.container.name": newExpectedValue(equal, "telemetrygen"), | ||
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"), | ||
"container.image.repo_digests": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"), | ||
"container.image.tag": newExpectedValue(equal, "latest"), | ||
"container.image.tag": newExpectedValue(shouldnotexist, ""), | ||
"container.id": newExpectedValue(exist, ""), | ||
}, | ||
}, | ||
|
@@ -575,7 +576,7 @@ func TestE2E_NamespacedRBAC(t *testing.T) { | |
} | ||
|
||
// Test with `filter::namespace` set, role binding for namespace-scoped objects (pod, replicaset) and clusterrole | ||
// binding for node and namespace objects. | ||
// binding for node and namespace objects, and the telemetrygen image has a tag and digest. | ||
func TestE2E_MixRBAC(t *testing.T) { | ||
|
||
testDir := filepath.Join("testdata", "e2e", "mixrbac") | ||
|
@@ -662,7 +663,7 @@ func TestE2E_MixRBAC(t *testing.T) { | |
"k8s.container.name": newExpectedValue(equal, "telemetrygen"), | ||
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"), | ||
"container.image.repo_digests": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"), | ||
"container.image.tag": newExpectedValue(equal, "latest"), | ||
"container.image.tag": newExpectedValue(equal, "0.112.0"), | ||
"container.id": newExpectedValue(exist, ""), | ||
"k8s.namespace.labels.foons": newExpectedValue(equal, "barns"), | ||
"k8s.node.labels.foo": newExpectedValue(equal, "too"), | ||
|
@@ -689,7 +690,7 @@ func TestE2E_MixRBAC(t *testing.T) { | |
"k8s.container.name": newExpectedValue(equal, "telemetrygen"), | ||
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"), | ||
"container.image.repo_digests": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"), | ||
"container.image.tag": newExpectedValue(equal, "latest"), | ||
"container.image.tag": newExpectedValue(equal, "0.112.0"), | ||
"container.id": newExpectedValue(exist, ""), | ||
"k8s.namespace.labels.foons": newExpectedValue(equal, "barns"), | ||
"k8s.node.labels.foo": newExpectedValue(equal, "too"), | ||
|
@@ -716,7 +717,7 @@ func TestE2E_MixRBAC(t *testing.T) { | |
"k8s.container.name": newExpectedValue(equal, "telemetrygen"), | ||
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"), | ||
"container.image.repo_digests": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"), | ||
"container.image.tag": newExpectedValue(equal, "latest"), | ||
"container.image.tag": newExpectedValue(equal, "0.112.0"), | ||
"container.id": newExpectedValue(exist, ""), | ||
"k8s.namespace.labels.foons": newExpectedValue(equal, "barns"), | ||
"k8s.node.labels.foo": newExpectedValue(equal, "too"), | ||
|
@@ -745,7 +746,8 @@ func TestE2E_MixRBAC(t *testing.T) { | |
// While `k8s.pod.ip` is not set in `k8sattributes:extract:metadata` and the `pod_association` is not `connection` | ||
// we expect that the `k8s.pod.ip` metadata is not added. | ||
// While `container.image.repo_digests` is not set in `k8sattributes::extract::metadata`, we expect | ||
// that the `container.image.repo_digests` metadata is not added | ||
// that the `container.image.repo_digests` metadata is not added. | ||
// The telemetrygen image has neither a tag nor digest (implicitly latest version) | ||
func TestE2E_NamespacedRBACNoPodIP(t *testing.T) { | ||
testDir := filepath.Join("testdata", "e2e", "namespaced_rbac_no_pod_ip") | ||
|
||
|
@@ -816,7 +818,7 @@ func TestE2E_NamespacedRBACNoPodIP(t *testing.T) { | |
"k8s.container.name": newExpectedValue(equal, "telemetrygen"), | ||
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"), | ||
"container.image.repo_digests": newExpectedValue(shouldnotexist, ""), | ||
"container.image.tag": newExpectedValue(equal, "latest"), | ||
"container.image.tag": newExpectedValue(shouldnotexist, ""), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we honor that |
||
"container.id": newExpectedValue(exist, ""), | ||
}, | ||
}, | ||
|
@@ -840,7 +842,7 @@ func TestE2E_NamespacedRBACNoPodIP(t *testing.T) { | |
"k8s.container.name": newExpectedValue(equal, "telemetrygen"), | ||
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"), | ||
"container.image.repo_digests": newExpectedValue(shouldnotexist, ""), | ||
"container.image.tag": newExpectedValue(equal, "latest"), | ||
"container.image.tag": newExpectedValue(shouldnotexist, ""), | ||
"container.id": newExpectedValue(exist, ""), | ||
}, | ||
}, | ||
|
@@ -864,7 +866,7 @@ func TestE2E_NamespacedRBACNoPodIP(t *testing.T) { | |
"k8s.container.name": newExpectedValue(equal, "telemetrygen"), | ||
"container.image.name": newExpectedValue(equal, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen"), | ||
"container.image.repo_digests": newExpectedValue(shouldnotexist, ""), | ||
"container.image.tag": newExpectedValue(equal, "latest"), | ||
"container.image.tag": newExpectedValue(shouldnotexist, ""), | ||
"container.id": newExpectedValue(exist, ""), | ||
}, | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ package kube // import "github.com/open-telemetry/opentelemetry-collector-contri | |
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
@@ -632,6 +633,23 @@ func removeUnnecessaryPodData(pod *api_v1.Pod, rules ExtractionRules) *api_v1.Po | |
return &transformedPod | ||
} | ||
|
||
// parseAttributesFromImage parses the image name and tag for differently-formatted image names. | ||
func parseNameAndTagFromImage(image string) (name, tag string, err error) { | ||
ref, err := reference.Parse(image) | ||
if err != nil { | ||
return | ||
} | ||
namedRef, ok := ref.(reference.Named) | ||
if !ok { | ||
return "", "", errors.New("cannot retrieve image name") | ||
} | ||
name = namedRef.Name() | ||
if taggedRef, ok := namedRef.(reference.Tagged); ok { | ||
tag = taggedRef.Tag() | ||
} | ||
return | ||
} | ||
|
||
func (c *WatchClient) extractPodContainersAttributes(pod *api_v1.Pod) PodContainers { | ||
containers := PodContainers{ | ||
ByID: map[string]*Container{}, | ||
|
@@ -643,16 +661,14 @@ func (c *WatchClient) extractPodContainersAttributes(pod *api_v1.Pod) PodContain | |
if c.Rules.ContainerImageName || c.Rules.ContainerImageTag { | ||
for _, spec := range append(pod.Spec.Containers, pod.Spec.InitContainers...) { | ||
container := &Container{} | ||
nameTagSep := strings.LastIndex(spec.Image, ":") | ||
if c.Rules.ContainerImageName { | ||
if nameTagSep > 0 { | ||
container.ImageName = spec.Image[:nameTagSep] | ||
} else { | ||
container.ImageName = spec.Image | ||
name, tag, err := parseNameAndTagFromImage(spec.Image) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we reflect this logic in the documentation as well? Something like what was described at #36131 (comment) |
||
if err == nil { | ||
if c.Rules.ContainerImageName { | ||
container.ImageName = name | ||
} | ||
if c.Rules.ContainerImageTag { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏽 Since we are on it, I also wonder if we should override the |
||
container.ImageTag = tag | ||
} | ||
} | ||
if c.Rules.ContainerImageTag && nameTagSep > 0 { | ||
container.ImageTag = spec.Image[nameTagSep+1:] | ||
} | ||
containers.ByName[spec.Name] = container | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we preserve this? Unless it is a wrong assumption that we want to fix.
FWIWI a container with
image: busybox
in the spec will produce a running container withimage: docker.io/library/busybox:latest
so technically I think that puttinglatest
when the tag is absent in the spec, is correct.