Merge pull request #85983 from xiaoanyunfei/bugfix/fix-metric-running-pod-num

fix metrics kubelet_running_pod_count
This commit is contained in:
Kubernetes Prow Robot 2020-07-31 03:07:06 -07:00 committed by GitHub
commit ee72e02907
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View File

@ -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 {

View File

@ -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),
},
}},
}