Skip to content

Commit 4762985

Browse files
committed
kubelet: guarantee at most only one cinfo per containerID
The trailing for loop removed within this PR can include active and inactive references. This modification will gauarantee at most only one reference per container is returned.
1 parent a380c29 commit 4762985

File tree

2 files changed

+4
-15
lines changed

2 files changed

+4
-15
lines changed

pkg/kubelet/stats/cadvisor_stats_provider.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -329,17 +329,12 @@ func removeTerminatedContainerInfo(containerInfo map[string]cadvisorapiv2.Contai
329329
continue
330330
}
331331
sort.Sort(ByCreationTime(refs))
332-
i := 0
333-
for ; i < len(refs); i++ {
332+
for i := len(refs) - 1; i >= 0; i-- {
334333
if hasMemoryAndCPUInstUsage(&refs[i].cinfo) {
335-
// Stops removing when we first see an info with non-zero
336-
// CPU/Memory usage.
334+
result[refs[i].cgroup] = refs[i].cinfo
337335
break
338336
}
339337
}
340-
for ; i < len(refs); i++ {
341-
result[refs[i].cgroup] = refs[i].cinfo
342-
}
343338
}
344339
return result
345340
}

pkg/kubelet/stats/cadvisor_stats_provider_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,10 @@ func TestRemoveTerminatedContainerInfo(t *testing.T) {
6262
// The latest containers, which should be in the results.
6363
"/pod0-i": getTestContainerInfo(seedPod0Infra, pName0, namespace, leaky.PodInfraContainerName),
6464
"/pod0-c0": getTestContainerInfo(seedPod0Container0, pName0, namespace, cName00),
65-
66-
// Duplicated containers with non-zero CPU and memory usage. This case
67-
// shouldn't happen unless something goes wrong, but we want to test
68-
// that the metrics reporting logic works in this scenario.
69-
"/pod0-i-duplicated": getTestContainerInfo(seedPod0Infra, pName0, namespace, leaky.PodInfraContainerName),
70-
"/pod0-c0-duplicated": getTestContainerInfo(seedPod0Container0, pName0, namespace, cName00),
7165
}
7266
output := removeTerminatedContainerInfo(infos)
73-
assert.Len(t, output, 4)
74-
for _, c := range []string{"/pod0-i", "/pod0-c0", "/pod0-i-duplicated", "/pod0-c0-duplicated"} {
67+
assert.Len(t, output, 2)
68+
for _, c := range []string{"/pod0-i", "/pod0-c0"} {
7569
if _, found := output[c]; !found {
7670
t.Errorf("%q is expected to be in the output\n", c)
7771
}

0 commit comments

Comments
 (0)