Skip to content

Commit 51f4fe0

Browse files
committed
CA: optimized pod list and classification
1 parent 9e22656 commit 51f4fe0

File tree

2 files changed

+19
-47
lines changed

2 files changed

+19
-47
lines changed

cluster-autoscaler/core/static_autoscaler.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,11 +1162,7 @@ func listPods(podLister kube_util.PodLister, bypassedSchedulers map[string]bool)
11621162
klog.Errorf("Failed to list pods: %v", err)
11631163
return nil, nil, nil, err
11641164
}
1165-
scheduled = kube_util.ScheduledPods(pods)
1166-
unschedulable = kube_util.UnschedulablePods(pods)
1167-
if len(bypassedSchedulers) > 0 {
1168-
unprocessed = kube_util.SchedulerUnprocessedPods(pods, bypassedSchedulers)
1169-
}
1165+
scheduled, unschedulable, unprocessed = kube_util.FilterPodsBySchedulability(pods, bypassedSchedulers)
11701166
// Skip logging in case of the boring scenario, when all pods are scheduled.
11711167
if len(pods) != len(scheduled) {
11721168
ignored := len(pods) - len(scheduled) - len(unschedulable) - len(unprocessed)

cluster-autoscaler/utils/kubernetes/listers.go

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func (r listerRegistryImpl) StatefulSetLister() v1appslister.StatefulSetLister {
139139
}
140140

141141
// PodLister lists all pods.
142-
// To filter out the scheduled or unschedulable pods the helper methods ScheduledPods and UnschedulablePods should be used.
142+
// To filter out scheduled, unschedulable, or unprocessed pods the helper method FilterPodsBySchedulability should be used.
143143
type PodLister interface {
144144
List() ([]*apiv1.Pod, error)
145145
}
@@ -156,19 +156,6 @@ func isDeleted(pod *apiv1.Pod) bool {
156156
return pod.GetDeletionTimestamp() != nil
157157
}
158158

159-
// isUnschedulable checks whether a pod is unschedulable or not
160-
// This method doesn't check for nil ptr, it's the responsibility of the caller
161-
func isUnschedulable(pod *apiv1.Pod) bool {
162-
if isScheduled(pod) || isDeleted(pod) {
163-
return false
164-
}
165-
_, condition := podv1.GetPodCondition(&pod.Status, apiv1.PodScheduled)
166-
if condition == nil || condition.Status != apiv1.ConditionFalse || condition.Reason != apiv1.PodReasonUnschedulable {
167-
return false
168-
}
169-
return true
170-
}
171-
172159
// ScheduledPods is a helper method that returns all scheduled pods from given pod list.
173160
func ScheduledPods(allPods []*apiv1.Pod) []*apiv1.Pod {
174161
var scheduledPods []*apiv1.Pod
@@ -181,27 +168,28 @@ func ScheduledPods(allPods []*apiv1.Pod) []*apiv1.Pod {
181168
return scheduledPods
182169
}
183170

184-
// SchedulerUnprocessedPods is a helper method that returns all pods which are not yet processed by the specified bypassed schedulers
185-
func SchedulerUnprocessedPods(allPods []*apiv1.Pod, bypassedSchedulers map[string]bool) []*apiv1.Pod {
186-
var unprocessedPods []*apiv1.Pod
187-
171+
// FilterPodsBySchedulability is a helper method that returns all scheduled and unschedulable pods from given pod list.
172+
func FilterPodsBySchedulability(allPods []*apiv1.Pod, bypassedSchedulers map[string]bool) (scheduled, unschedulable, unprocessed []*apiv1.Pod) {
188173
for _, pod := range allPods {
189-
if canBypass := bypassedSchedulers[pod.Spec.SchedulerName]; !canBypass {
174+
if isScheduled(pod) {
175+
scheduled = append(scheduled, pod)
190176
continue
191-
}
192-
// Make sure it's not scheduled or deleted
193-
if isScheduled(pod) || isDeleted(pod) || isUnschedulable(pod) {
177+
} else if isDeleted(pod) {
194178
continue
195-
}
196-
// Make sure that if it's not scheduled it's either
197-
// Not processed (condition is nil)
198-
// Or Reason is empty (not schedulerError, terminated, ...etc)
199-
_, condition := podv1.GetPodCondition(&pod.Status, apiv1.PodScheduled)
200-
if condition == nil || (condition.Status == apiv1.ConditionFalse && condition.Reason == "") {
201-
unprocessedPods = append(unprocessedPods, pod)
179+
} else {
180+
_, condition := podv1.GetPodCondition(&pod.Status, apiv1.PodScheduled)
181+
if !(condition == nil || condition.Status != apiv1.ConditionFalse || condition.Reason != apiv1.PodReasonUnschedulable) {
182+
unschedulable = append(unschedulable, pod)
183+
} else {
184+
if canBypass := bypassedSchedulers[pod.Spec.SchedulerName]; canBypass {
185+
if condition == nil || (condition.Status == apiv1.ConditionFalse && condition.Reason == "") {
186+
unprocessed = append(unprocessed, pod)
187+
}
188+
}
189+
}
202190
}
203191
}
204-
return unprocessedPods
192+
return scheduled, unschedulable, unprocessed
205193
}
206194

207195
// SchedulingGatedPods is a helper method that returns all pods which has scheduling gate
@@ -228,18 +216,6 @@ func isSchedulingGated(pod *apiv1.Pod) bool {
228216
return false
229217
}
230218

231-
// UnschedulablePods is a helper method that returns all unschedulable pods from given pod list.
232-
func UnschedulablePods(allPods []*apiv1.Pod) []*apiv1.Pod {
233-
var unschedulablePods []*apiv1.Pod
234-
for _, pod := range allPods {
235-
if !isUnschedulable(pod) {
236-
continue
237-
}
238-
unschedulablePods = append(unschedulablePods, pod)
239-
}
240-
return unschedulablePods
241-
}
242-
243219
// AllPodLister lists all pods.
244220
type AllPodLister struct {
245221
podLister v1lister.PodLister

0 commit comments

Comments
 (0)