mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 02:41:25 +00:00
fix metrics kubelet_running_pod_count
This commit is contained in:
parent
30ee970640
commit
a724481f5c
@ -423,8 +423,7 @@ func getContainerState(pod *kubecontainer.Pod, cid *kubecontainer.ContainerID) p
|
||||
}
|
||||
|
||||
func updateRunningPodAndContainerMetrics(pods []*kubecontainer.Pod) {
|
||||
// Set the number of running pods in the parameter
|
||||
metrics.RunningPodCount.Set(float64(len(pods)))
|
||||
runningSandboxNum := 0
|
||||
// intermediate map to store the count of each "container_state"
|
||||
containerStateCount := make(map[string]int)
|
||||
|
||||
@ -434,10 +433,23 @@ func updateRunningPodAndContainerMetrics(pods []*kubecontainer.Pod) {
|
||||
// update the corresponding "container_state" in map to set value for the gaugeVec metrics
|
||||
containerStateCount[string(container.State)]++
|
||||
}
|
||||
|
||||
sandboxes := pod.Sandboxes
|
||||
|
||||
for _, sandbox := range sandboxes {
|
||||
if sandbox.State == kubecontainer.ContainerStateRunning {
|
||||
runningSandboxNum++
|
||||
// every pod should only have one running sandbox
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
for key, value := range containerStateCount {
|
||||
metrics.RunningContainerCount.WithLabelValues(key).Set(float64(value))
|
||||
}
|
||||
|
||||
// Set the number of running pods in the parameter
|
||||
metrics.RunningPodCount.Set(float64(runningSandboxNum))
|
||||
}
|
||||
|
||||
func (pr podRecords) getOld(id types.UID) *kubecontainer.Pod {
|
||||
|
@ -664,12 +664,21 @@ func TestRunningPodAndContainerCount(t *testing.T) {
|
||||
createTestContainer("c2", kubecontainer.ContainerStateUnknown),
|
||||
createTestContainer("c3", kubecontainer.ContainerStateUnknown),
|
||||
},
|
||||
Sandboxes: []*kubecontainer.Container{
|
||||
createTestContainer("s1", kubecontainer.ContainerStateRunning),
|
||||
createTestContainer("s2", kubecontainer.ContainerStateRunning),
|
||||
createTestContainer("s3", kubecontainer.ContainerStateUnknown),
|
||||
},
|
||||
}},
|
||||
{Pod: &kubecontainer.Pod{
|
||||
ID: "4567",
|
||||
Containers: []*kubecontainer.Container{
|
||||
createTestContainer("c1", kubecontainer.ContainerStateExited),
|
||||
},
|
||||
Sandboxes: []*kubecontainer.Container{
|
||||
createTestContainer("s1", kubecontainer.ContainerStateRunning),
|
||||
createTestContainer("s2", kubecontainer.ContainerStateExited),
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user