Skip to content

Commit

Permalink
fix metrics kubelet_running_pod_count
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaofei.sun committed Jul 31, 2020
1 parent 30ee970 commit a724481
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pkg/kubelet/pleg/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions pkg/kubelet/pleg/generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
}},
}

Expand Down

0 comments on commit a724481

Please sign in to comment.