Skip to content

Commit ee72e02

Browse files
authored
Merge pull request kubernetes#85983 from xiaoanyunfei/bugfix/fix-metric-running-pod-num
fix metrics kubelet_running_pod_count
2 parents 30ee970 + a724481 commit ee72e02

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

pkg/kubelet/pleg/generic.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,7 @@ func getContainerState(pod *kubecontainer.Pod, cid *kubecontainer.ContainerID) p
423423
}
424424

425425
func updateRunningPodAndContainerMetrics(pods []*kubecontainer.Pod) {
426-
// Set the number of running pods in the parameter
427-
metrics.RunningPodCount.Set(float64(len(pods)))
426+
runningSandboxNum := 0
428427
// intermediate map to store the count of each "container_state"
429428
containerStateCount := make(map[string]int)
430429

@@ -434,10 +433,23 @@ func updateRunningPodAndContainerMetrics(pods []*kubecontainer.Pod) {
434433
// update the corresponding "container_state" in map to set value for the gaugeVec metrics
435434
containerStateCount[string(container.State)]++
436435
}
436+
437+
sandboxes := pod.Sandboxes
438+
439+
for _, sandbox := range sandboxes {
440+
if sandbox.State == kubecontainer.ContainerStateRunning {
441+
runningSandboxNum++
442+
// every pod should only have one running sandbox
443+
break
444+
}
445+
}
437446
}
438447
for key, value := range containerStateCount {
439448
metrics.RunningContainerCount.WithLabelValues(key).Set(float64(value))
440449
}
450+
451+
// Set the number of running pods in the parameter
452+
metrics.RunningPodCount.Set(float64(runningSandboxNum))
441453
}
442454

443455
func (pr podRecords) getOld(id types.UID) *kubecontainer.Pod {

pkg/kubelet/pleg/generic_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,12 +664,21 @@ func TestRunningPodAndContainerCount(t *testing.T) {
664664
createTestContainer("c2", kubecontainer.ContainerStateUnknown),
665665
createTestContainer("c3", kubecontainer.ContainerStateUnknown),
666666
},
667+
Sandboxes: []*kubecontainer.Container{
668+
createTestContainer("s1", kubecontainer.ContainerStateRunning),
669+
createTestContainer("s2", kubecontainer.ContainerStateRunning),
670+
createTestContainer("s3", kubecontainer.ContainerStateUnknown),
671+
},
667672
}},
668673
{Pod: &kubecontainer.Pod{
669674
ID: "4567",
670675
Containers: []*kubecontainer.Container{
671676
createTestContainer("c1", kubecontainer.ContainerStateExited),
672677
},
678+
Sandboxes: []*kubecontainer.Container{
679+
createTestContainer("s1", kubecontainer.ContainerStateRunning),
680+
createTestContainer("s2", kubecontainer.ContainerStateExited),
681+
},
673682
}},
674683
}
675684

0 commit comments

Comments
 (0)