Skip to content

Commit 275449c

Browse files
authored
fix(receiver/k8s_cluster): Include non-ready containers (open-telemetry#43149)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description This PR fixes an issue where the receiver was ignoring non-ready containers with an empty ContainerID, resulting in incomplete data collection for the newly introduced metrics `k8s.container.status.state` and `k8s.container.status.reason`. <!-- Issue number (e.g. #1234) or full URL to issue, if applicable. --> #### Link to tracking issue Fixes open-telemetry#43147 <!--Describe what testing was performed and which tests were added.--> #### Testing Updated `TestTransform` <!--Describe the documentation added.--> #### Documentation <!--Please delete paragraphs that you did not use before submitting.-->
1 parent 3cdf484 commit 275449c

File tree

3 files changed

+83
-7
lines changed

3 files changed

+83
-7
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: receiver/k8s_cluster
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Fix for k8sclusterreceiver to handle empty containerID in ContainerStatus
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [43147]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

receiver/k8sclusterreceiver/internal/pod/pods.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ func Transform(pod *corev1.Pod) *corev1.Pod {
5151
}
5252
for i := range pod.Status.ContainerStatuses {
5353
cs := &pod.Status.ContainerStatuses[i]
54-
if cs.ContainerID == "" {
55-
continue
56-
}
5754
newPod.Status.ContainerStatuses = append(newPod.Status.ContainerStatuses, corev1.ContainerStatus{
5855
Name: cs.Name,
5956
Image: cs.Image,

receiver/k8sclusterreceiver/internal/pod/pods_test.go

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,8 @@ func podWithOwnerReference(kind string) *corev1.Pod {
354354
}
355355

356356
func TestTransform(t *testing.T) {
357-
containerState := corev1.ContainerState{Running: &corev1.ContainerStateRunning{StartedAt: v1.Now()}}
357+
waitingContainerState := corev1.ContainerState{Waiting: &corev1.ContainerStateWaiting{Reason: "ImagePullBackOff"}}
358+
runningContainerState := corev1.ContainerState{Running: &corev1.ContainerStateRunning{StartedAt: v1.Now()}}
358359
originalPod := &corev1.Pod{
359360
ObjectMeta: v1.ObjectMeta{
360361
Name: "my-pod",
@@ -384,6 +385,34 @@ func TestTransform(t *testing.T) {
384385
FSGroup: func() *int64 { gid := int64(3000); return &gid }(),
385386
},
386387
Containers: []corev1.Container{
388+
{
389+
Name: "my-failing-container",
390+
Image: "redis:latest",
391+
ImagePullPolicy: corev1.PullAlways,
392+
Ports: []corev1.ContainerPort{
393+
{
394+
Name: "http",
395+
ContainerPort: 6379,
396+
Protocol: corev1.ProtocolTCP,
397+
},
398+
},
399+
Env: []corev1.EnvVar{
400+
{
401+
Name: "MY_ENV",
402+
Value: "my-value",
403+
},
404+
},
405+
Resources: corev1.ResourceRequirements{
406+
Requests: corev1.ResourceList{
407+
corev1.ResourceCPU: resource.MustParse("500m"),
408+
corev1.ResourceMemory: resource.MustParse("1Gi"),
409+
},
410+
Limits: corev1.ResourceList{
411+
corev1.ResourceCPU: resource.MustParse("1"),
412+
corev1.ResourceMemory: resource.MustParse("2Gi"),
413+
},
414+
},
415+
},
387416
{
388417
Name: "my-container",
389418
Image: "nginx:latest",
@@ -427,17 +456,19 @@ func TestTransform(t *testing.T) {
427456
StartTime: &v1.Time{Time: v1.Now().Add(-5 * time.Minute)},
428457
ContainerStatuses: []corev1.ContainerStatus{
429458
{
430-
Name: "invalid-container",
459+
Name: "my-failing-container",
431460
Image: "redis:latest",
432461
RestartCount: 1,
462+
Ready: false,
463+
State: waitingContainerState,
433464
},
434465
{
435466
Name: "my-container",
436467
Image: "nginx:latest",
437468
ContainerID: "abc12345",
438469
RestartCount: 2,
439470
Ready: true,
440-
State: containerState,
471+
State: runningContainerState,
441472
},
442473
},
443474
},
@@ -454,6 +485,19 @@ func TestTransform(t *testing.T) {
454485
Spec: corev1.PodSpec{
455486
NodeName: "node-1",
456487
Containers: []corev1.Container{
488+
{
489+
Name: "my-failing-container",
490+
Resources: corev1.ResourceRequirements{
491+
Requests: corev1.ResourceList{
492+
corev1.ResourceCPU: resource.MustParse("500m"),
493+
corev1.ResourceMemory: resource.MustParse("1Gi"),
494+
},
495+
Limits: corev1.ResourceList{
496+
corev1.ResourceCPU: resource.MustParse("1"),
497+
corev1.ResourceMemory: resource.MustParse("2Gi"),
498+
},
499+
},
500+
},
457501
{
458502
Name: "my-container",
459503
Resources: corev1.ResourceRequirements{
@@ -473,13 +517,21 @@ func TestTransform(t *testing.T) {
473517
Phase: corev1.PodRunning,
474518
Reason: "Evicted",
475519
ContainerStatuses: []corev1.ContainerStatus{
520+
{
521+
Name: "my-failing-container",
522+
Image: "redis:latest",
523+
ContainerID: "",
524+
RestartCount: 1,
525+
Ready: false,
526+
State: waitingContainerState,
527+
},
476528
{
477529
Name: "my-container",
478530
Image: "nginx:latest",
479531
ContainerID: "abc12345",
480532
RestartCount: 2,
481533
Ready: true,
482-
State: containerState,
534+
State: runningContainerState,
483535
},
484536
},
485537
},

0 commit comments

Comments
 (0)