Skip to content

Commit 4576c36

Browse files
committed
filter pods by node name and improve oldest pod selection logic
Signed-off-by: cybergeek2077 <zhaoshen@buaa.edu.cn>
1 parent 680cd35 commit 4576c36

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

pkg/plugin/vgpu/util/util.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ func GetNode(nodename string) (*v1.Node, error) {
5959
}
6060

6161
func GetPendingPod(node string) (*v1.Pod, error) {
62-
podList, err := lock.GetClient().CoreV1().Pods("").List(context.Background(), metav1.ListOptions{})
62+
// filter pods for this node.
63+
selector := fmt.Sprintf("spec.nodeName=%s", node)
64+
podListOptions := metav1.ListOptions{
65+
FieldSelector: selector,
66+
}
67+
podList, err := lock.GetClient().CoreV1().Pods("").List(context.Background(), podListOptions)
6368
if err != nil {
6469
return nil, err
6570
}
@@ -76,21 +81,38 @@ func getOldestPod(pods []v1.Pod) *v1.Pod {
7681
if len(pods) == 0 {
7782
return nil
7883
}
79-
oldest := pods[0]
80-
for _, pod := range pods {
84+
var oldest *v1.Pod = nil
85+
for i, pod := range pods {
86+
if pod.Status.Phase != v1.PodPending {
87+
continue
88+
}
89+
if _, ok := pod.Annotations[BindTimeAnnotations]; !ok {
90+
continue
91+
}
92+
if phase, ok := pod.Annotations[DeviceBindPhase]; !ok {
93+
continue
94+
} else {
95+
if strings.Compare(phase, DeviceBindAllocating) != 0 {
96+
continue
97+
}
98+
}
8199
klog.V(4).Infof("pod %s, predicate time: %s", pod.Name, pod.Annotations[AssignedTimeAnnotations])
82-
if getPredicateTimeFromPodAnnotation(&oldest) > getPredicateTimeFromPodAnnotation(&pod) {
83-
oldest = pod
100+
if oldest == nil || getPredicateTimeFromPodAnnotation(oldest) > getPredicateTimeFromPodAnnotation(&pod) {
101+
oldest = &pods[i]
84102
}
85103
}
104+
if oldest == nil {
105+
klog.Warningf("no pod has predicate time")
106+
return nil
107+
}
86108
klog.V(4).Infof("oldest pod %#v, predicate time: %#v", oldest.Name,
87109
oldest.Annotations[AssignedTimeAnnotations])
88110
annotation := map[string]string{AssignedTimeAnnotations: strconv.FormatUint(math.MaxUint64, 10)}
89-
if err := PatchPodAnnotations(&oldest, annotation); err != nil {
111+
if err := PatchPodAnnotations(oldest, annotation); err != nil {
90112
klog.Errorf("update pod %s failed, err: %v", oldest.Name, err)
91113
return nil
92114
}
93-
return &oldest
115+
return oldest
94116
}
95117

96118
func getPredicateTimeFromPodAnnotation(pod *v1.Pod) uint64 {

0 commit comments

Comments
 (0)