mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 20:24:09 +00:00
Merge pull request #85983 from xiaoanyunfei/bugfix/fix-metric-running-pod-num
fix metrics kubelet_running_pod_count
This commit is contained in:
commit
ee72e02907
@ -423,8 +423,7 @@ func getContainerState(pod *kubecontainer.Pod, cid *kubecontainer.ContainerID) p
|
|||||||
}
|
}
|
||||||
|
|
||||||
func updateRunningPodAndContainerMetrics(pods []*kubecontainer.Pod) {
|
func updateRunningPodAndContainerMetrics(pods []*kubecontainer.Pod) {
|
||||||
// Set the number of running pods in the parameter
|
runningSandboxNum := 0
|
||||||
metrics.RunningPodCount.Set(float64(len(pods)))
|
|
||||||
// intermediate map to store the count of each "container_state"
|
// intermediate map to store the count of each "container_state"
|
||||||
containerStateCount := make(map[string]int)
|
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
|
// update the corresponding "container_state" in map to set value for the gaugeVec metrics
|
||||||
containerStateCount[string(container.State)]++
|
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 {
|
for key, value := range containerStateCount {
|
||||||
metrics.RunningContainerCount.WithLabelValues(key).Set(float64(value))
|
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 {
|
func (pr podRecords) getOld(id types.UID) *kubecontainer.Pod {
|
||||||
|
@ -664,12 +664,21 @@ func TestRunningPodAndContainerCount(t *testing.T) {
|
|||||||
createTestContainer("c2", kubecontainer.ContainerStateUnknown),
|
createTestContainer("c2", kubecontainer.ContainerStateUnknown),
|
||||||
createTestContainer("c3", kubecontainer.ContainerStateUnknown),
|
createTestContainer("c3", kubecontainer.ContainerStateUnknown),
|
||||||
},
|
},
|
||||||
|
Sandboxes: []*kubecontainer.Container{
|
||||||
|
createTestContainer("s1", kubecontainer.ContainerStateRunning),
|
||||||
|
createTestContainer("s2", kubecontainer.ContainerStateRunning),
|
||||||
|
createTestContainer("s3", kubecontainer.ContainerStateUnknown),
|
||||||
|
},
|
||||||
}},
|
}},
|
||||||
{Pod: &kubecontainer.Pod{
|
{Pod: &kubecontainer.Pod{
|
||||||
ID: "4567",
|
ID: "4567",
|
||||||
Containers: []*kubecontainer.Container{
|
Containers: []*kubecontainer.Container{
|
||||||
createTestContainer("c1", kubecontainer.ContainerStateExited),
|
createTestContainer("c1", kubecontainer.ContainerStateExited),
|
||||||
},
|
},
|
||||||
|
Sandboxes: []*kubecontainer.Container{
|
||||||
|
createTestContainer("s1", kubecontainer.ContainerStateRunning),
|
||||||
|
createTestContainer("s2", kubecontainer.ContainerStateExited),
|
||||||
|
},
|
||||||
}},
|
}},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user