Skip to content

Commit

Permalink
add to documentation and add latest by default logic
Browse files Browse the repository at this point in the history
  • Loading branch information
spiffyy99 committed Nov 7, 2024
1 parent 828a728 commit 3859c70
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 37 deletions.
5 changes: 3 additions & 2 deletions processor/k8sattributesprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ spec:
- --duration=10s
- --rate=1
- --otlp-attributes=k8s.container.name="telemetrygen"
image: ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen:latest
image: ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen:0.112.0@sha256:b248ef911f93ae27cbbc85056d1ffacc87fd941bbdc2ffd951b6df8df72b8096
name: telemetrygen
status:
podIP: 10.244.0.11
Expand All @@ -191,7 +191,8 @@ the processor associates the received trace to the pod, based on the connection
"k8s.pod.name": "telemetrygen-pod",
"k8s.pod.uid": "038e2267-b473-489b-b48c-46bafdb852eb",
"container.image.name": "telemetrygen",
"container.image.tag": "latest"
"container.image.tag": "0.112.0",
"container.image.repo_digests": "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:b248ef911f93ae27cbbc85056d1ffacc87fd941bbdc2ffd951b6df8df72b8096"
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion processor/k8sattributesprocessor/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
| container.id | Container ID. Usually a UUID, as for example used to identify Docker containers. The UUID might be abbreviated. Requires k8s.container.restart_count. | Any Str | false |
| container.image.name | Name of the image the container was built on. Requires container.id or k8s.container.name. | Any Str | true |
| container.image.repo_digests | Repo digests of the container image as provided by the container runtime. | Any Slice | false |
| container.image.tag | Container image tag. Requires container.id or k8s.container.name. | Any Str | true |
| container.image.tag | Container image tag. Defaults to "latest" if not provided (unless digest also in image path) Requires container.id or k8s.container.name. | Any Str | true |
| k8s.cluster.uid | Gives cluster uid identified with kube-system namespace | Any Str | false |
| k8s.container.name | The name of the Container in a Pod template. Requires container.id. | Any Str | false |
| k8s.cronjob.name | The name of the CronJob. | Any Str | false |
Expand Down
54 changes: 24 additions & 30 deletions processor/k8sattributesprocessor/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,9 +504,8 @@ func TestE2E_NamespacedRBAC(t *testing.T) {
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-traces-deployment"),
"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(shouldnotexist, ""),
"container.id": newExpectedValue(exist, ""),
"container.image.repo_digests": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"), "container.image.tag": newExpectedValue(shouldnotexist, ""),
"container.id": newExpectedValue(exist, ""),
},
},
{
Expand All @@ -528,9 +527,8 @@ func TestE2E_NamespacedRBAC(t *testing.T) {
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-metrics-deployment"),
"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(shouldnotexist, ""),
"container.id": newExpectedValue(exist, ""),
"container.image.repo_digests": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"), "container.image.tag": newExpectedValue(shouldnotexist, ""),
"container.id": newExpectedValue(exist, ""),
},
},
{
Expand All @@ -552,9 +550,8 @@ func TestE2E_NamespacedRBAC(t *testing.T) {
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-logs-deployment"),
"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(shouldnotexist, ""),
"container.id": newExpectedValue(exist, ""),
"container.image.repo_digests": newExpectedValue(regex, "ghcr.io/open-telemetry/opentelemetry-collector-contrib/telemetrygen@sha256:[0-9a-fA-f]{64}"), "container.image.tag": newExpectedValue(shouldnotexist, ""),
"container.id": newExpectedValue(exist, ""),
},
},
}
Expand Down Expand Up @@ -662,12 +659,11 @@ func TestE2E_MixRBAC(t *testing.T) {
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-traces-deployment"),
"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, "0.112.0"),
"container.id": newExpectedValue(exist, ""),
"k8s.namespace.labels.foons": newExpectedValue(equal, "barns"),
"k8s.node.labels.foo": newExpectedValue(equal, "too"),
"k8s.cluster.uid": newExpectedValue(regex, uidRe),
"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, "0.112.0"),
"container.id": newExpectedValue(exist, ""),
"k8s.namespace.labels.foons": newExpectedValue(equal, "barns"),
"k8s.node.labels.foo": newExpectedValue(equal, "too"),
"k8s.cluster.uid": newExpectedValue(regex, uidRe),
},
},
{
Expand All @@ -689,12 +685,11 @@ func TestE2E_MixRBAC(t *testing.T) {
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-metrics-deployment"),
"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, "0.112.0"),
"container.id": newExpectedValue(exist, ""),
"k8s.namespace.labels.foons": newExpectedValue(equal, "barns"),
"k8s.node.labels.foo": newExpectedValue(equal, "too"),
"k8s.cluster.uid": newExpectedValue(regex, uidRe),
"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, "0.112.0"),
"container.id": newExpectedValue(exist, ""),
"k8s.namespace.labels.foons": newExpectedValue(equal, "barns"),
"k8s.node.labels.foo": newExpectedValue(equal, "too"),
"k8s.cluster.uid": newExpectedValue(regex, uidRe),
},
},
{
Expand All @@ -716,12 +711,11 @@ func TestE2E_MixRBAC(t *testing.T) {
"k8s.labels.app": newExpectedValue(equal, "telemetrygen-"+testID+"-logs-deployment"),
"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, "0.112.0"),
"container.id": newExpectedValue(exist, ""),
"k8s.namespace.labels.foons": newExpectedValue(equal, "barns"),
"k8s.node.labels.foo": newExpectedValue(equal, "too"),
"k8s.cluster.uid": newExpectedValue(regex, uidRe),
"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, "0.112.0"),
"container.id": newExpectedValue(exist, ""),
"k8s.namespace.labels.foons": newExpectedValue(equal, "barns"),
"k8s.node.labels.foo": newExpectedValue(equal, "too"),
"k8s.cluster.uid": newExpectedValue(regex, uidRe),
},
},
}
Expand Down Expand Up @@ -818,7 +812,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(shouldnotexist, ""),
"container.image.tag": newExpectedValue(equal, "latest"),
"container.id": newExpectedValue(exist, ""),
},
},
Expand All @@ -842,7 +836,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(shouldnotexist, ""),
"container.image.tag": newExpectedValue(equal, "latest"),
"container.id": newExpectedValue(exist, ""),
},
},
Expand All @@ -866,7 +860,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(shouldnotexist, ""),
"container.image.tag": newExpectedValue(equal, "latest"),
"container.id": newExpectedValue(exist, ""),
},
},
Expand Down
7 changes: 7 additions & 0 deletions processor/k8sattributesprocessor/internal/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,8 @@ func removeUnnecessaryPodData(pod *api_v1.Pod, rules ExtractionRules) *api_v1.Po
}

// parseAttributesFromImage parses the image name and tag for differently-formatted image names.
// returns "latest" as the default if tag not present. also checks if the image contains a digest.
// if it does, no latest tag is assumed.
func parseNameAndTagFromImage(image string) (name, tag string, err error) {
ref, err := reference.Parse(image)
if err != nil {
Expand All @@ -647,6 +649,11 @@ func parseNameAndTagFromImage(image string) (name, tag string, err error) {
if taggedRef, ok := namedRef.(reference.Tagged); ok {
tag = taggedRef.Tag()
}
if tag == "" {
if digestedRef, ok := namedRef.(reference.Digested); !ok || digestedRef.String() == "" {
tag = "latest"
}
}
return
}

Expand Down
6 changes: 3 additions & 3 deletions processor/k8sattributesprocessor/internal/kube/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,7 @@ func Test_extractPodContainersAttributes(t *testing.T) {
InitContainers: []api_v1.Container{
{
Name: "init_container",
Image: "test/init-image:1.0.2",
Image: "test/init-image",
},
},
},
Expand Down Expand Up @@ -1722,7 +1722,7 @@ func Test_extractPodContainersAttributes(t *testing.T) {
},
"init-container-id-789": {
ImageName: "test/init-image",
ImageTag: "1.0.2",
ImageTag: "latest",
Statuses: map[int]ContainerStatus{
0: {ContainerID: "init-container-id-789", ImageRepoDigest: "ghcr.io/initimage1@sha256:42e8ba40f9f70d604684c3a2a0ed321206b7e2e3509fdb2c8836d34f2edfb57b"},
},
Expand Down Expand Up @@ -1751,7 +1751,7 @@ func Test_extractPodContainersAttributes(t *testing.T) {
},
"init_container": {
ImageName: "test/init-image",
ImageTag: "1.0.2",
ImageTag: "latest",
Statuses: map[int]ContainerStatus{
0: {ContainerID: "init-container-id-789", ImageRepoDigest: "ghcr.io/initimage1@sha256:42e8ba40f9f70d604684c3a2a0ed321206b7e2e3509fdb2c8836d34f2edfb57b"},
},
Expand Down
2 changes: 1 addition & 1 deletion processor/k8sattributesprocessor/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ resource_attributes:
type: slice
enabled: false
container.image.tag:
description: Container image tag. Requires container.id or k8s.container.name.
description: Container image tag. Defaults to "latest" if not provided (unless digest also in image path) Requires container.id or k8s.container.name.
type: string
enabled: true

Expand Down

0 comments on commit 3859c70

Please sign in to comment.