Skip to content

Commit a93f803

Browse files
authored
Merge pull request kubernetes#75501 from Huang-Wei/scheduler-metrics
scheduler: add metrics to record number of pending pods in different queues
2 parents 1cca3f9 + 63c3a61 commit a93f803

File tree

10 files changed

+457
-75
lines changed

10 files changed

+457
-75
lines changed

pkg/controller/.import-restrictions

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@
258258
"k8s.io/kubernetes/pkg/fieldpath",
259259
"k8s.io/kubernetes/pkg/scheduler/volumebinder",
260260
"k8s.io/kubernetes/pkg/util/resizefs",
261-
"k8s.io/kubernetes/pkg/apis/apps"
261+
"k8s.io/kubernetes/pkg/apis/apps",
262+
"k8s.io/kubernetes/pkg/scheduler/metrics"
262263
]
263264
},
264265
{

pkg/scheduler/internal/queue/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ go_library(
1212
"//pkg/api/v1/pod:go_default_library",
1313
"//pkg/scheduler/algorithm/predicates:go_default_library",
1414
"//pkg/scheduler/algorithm/priorities/util:go_default_library",
15+
"//pkg/scheduler/metrics:go_default_library",
1516
"//pkg/scheduler/util:go_default_library",
1617
"//staging/src/k8s.io/api/core/v1:go_default_library",
1718
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
@@ -31,11 +32,13 @@ go_test(
3132
embed = [":go_default_library"],
3233
deps = [
3334
"//pkg/api/v1/pod:go_default_library",
35+
"//pkg/scheduler/metrics:go_default_library",
3436
"//pkg/scheduler/util:go_default_library",
3537
"//staging/src/k8s.io/api/core/v1:go_default_library",
3638
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
3739
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
3840
"//staging/src/k8s.io/apimachinery/pkg/util/clock:go_default_library",
41+
"//vendor/github.com/prometheus/client_model/go:go_default_library",
3942
],
4043
)
4144

pkg/scheduler/internal/queue/scheduling_queue.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
4343
"k8s.io/kubernetes/pkg/scheduler/algorithm/predicates"
4444
priorityutil "k8s.io/kubernetes/pkg/scheduler/algorithm/priorities/util"
45+
"k8s.io/kubernetes/pkg/scheduler/metrics"
4546
"k8s.io/kubernetes/pkg/scheduler/util"
4647
)
4748

@@ -283,13 +284,13 @@ func NewPriorityQueueWithClock(stop <-chan struct{}, clock util.Clock) *Priority
283284
clock: clock,
284285
stop: stop,
285286
podBackoff: NewPodBackoffMap(1*time.Second, 10*time.Second),
286-
activeQ: util.NewHeap(podInfoKeyFunc, activeQComp),
287-
unschedulableQ: newUnschedulablePodsMap(),
287+
activeQ: util.NewHeapWithRecorder(podInfoKeyFunc, activeQComp, metrics.NewActivePodsRecorder()),
288+
unschedulableQ: newUnschedulablePodsMap(metrics.NewUnschedulablePodsRecorder()),
288289
nominatedPods: newNominatedPodMap(),
289290
moveRequestCycle: -1,
290291
}
291292
pq.cond.L = &pq.lock
292-
pq.podBackoffQ = util.NewHeap(podInfoKeyFunc, pq.podsCompareBackoffCompleted)
293+
pq.podBackoffQ = util.NewHeapWithRecorder(podInfoKeyFunc, pq.podsCompareBackoffCompleted, metrics.NewBackoffPodsRecorder())
293294

294295
pq.run()
295296

@@ -777,16 +778,27 @@ type UnschedulablePodsMap struct {
777778
// podInfoMap is a map key by a pod's full-name and the value is a pointer to the podInfo.
778779
podInfoMap map[string]*podInfo
779780
keyFunc func(*v1.Pod) string
781+
// metricRecorder updates the counter when elements of an unschedulablePodsMap
782+
// get added or removed, and it does nothing if it's nil
783+
metricRecorder metrics.MetricRecorder
780784
}
781785

782786
// Add adds a pod to the unschedulable podInfoMap.
783787
func (u *UnschedulablePodsMap) addOrUpdate(pInfo *podInfo) {
784-
u.podInfoMap[u.keyFunc(pInfo.pod)] = pInfo
788+
podID := u.keyFunc(pInfo.pod)
789+
if _, exists := u.podInfoMap[podID]; !exists && u.metricRecorder != nil {
790+
u.metricRecorder.Inc()
791+
}
792+
u.podInfoMap[podID] = pInfo
785793
}
786794

787795
// Delete deletes a pod from the unschedulable podInfoMap.
788796
func (u *UnschedulablePodsMap) delete(pod *v1.Pod) {
789-
delete(u.podInfoMap, u.keyFunc(pod))
797+
podID := u.keyFunc(pod)
798+
if _, exists := u.podInfoMap[podID]; exists && u.metricRecorder != nil {
799+
u.metricRecorder.Dec()
800+
}
801+
delete(u.podInfoMap, podID)
790802
}
791803

792804
// Get returns the podInfo if a pod with the same key as the key of the given "pod"
@@ -802,13 +814,17 @@ func (u *UnschedulablePodsMap) get(pod *v1.Pod) *podInfo {
802814
// Clear removes all the entries from the unschedulable podInfoMap.
803815
func (u *UnschedulablePodsMap) clear() {
804816
u.podInfoMap = make(map[string]*podInfo)
817+
if u.metricRecorder != nil {
818+
u.metricRecorder.Clear()
819+
}
805820
}
806821

807822
// newUnschedulablePodsMap initializes a new object of UnschedulablePodsMap.
808-
func newUnschedulablePodsMap() *UnschedulablePodsMap {
823+
func newUnschedulablePodsMap(metricRecorder metrics.MetricRecorder) *UnschedulablePodsMap {
809824
return &UnschedulablePodsMap{
810-
podInfoMap: make(map[string]*podInfo),
811-
keyFunc: util.GetPodFullName,
825+
podInfoMap: make(map[string]*podInfo),
826+
keyFunc: util.GetPodFullName,
827+
metricRecorder: metricRecorder,
812828
}
813829
}
814830

0 commit comments

Comments
 (0)