From 4576c366fa513984dc7b280fb73efe1d099995c4 Mon Sep 17 00:00:00 2001 From: cybergeek2077 Date: Tue, 19 Nov 2024 10:44:16 +0800 Subject: [PATCH] filter pods by node name and improve oldest pod selection logic Signed-off-by: cybergeek2077 --- pkg/plugin/vgpu/util/util.go | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/pkg/plugin/vgpu/util/util.go b/pkg/plugin/vgpu/util/util.go index 9f8aac2..eaeae99 100644 --- a/pkg/plugin/vgpu/util/util.go +++ b/pkg/plugin/vgpu/util/util.go @@ -59,7 +59,12 @@ func GetNode(nodename string) (*v1.Node, error) { } func GetPendingPod(node string) (*v1.Pod, error) { - podList, err := lock.GetClient().CoreV1().Pods("").List(context.Background(), metav1.ListOptions{}) + // filter pods for this node. + selector := fmt.Sprintf("spec.nodeName=%s", node) + podListOptions := metav1.ListOptions{ + FieldSelector: selector, + } + podList, err := lock.GetClient().CoreV1().Pods("").List(context.Background(), podListOptions) if err != nil { return nil, err } @@ -76,21 +81,38 @@ func getOldestPod(pods []v1.Pod) *v1.Pod { if len(pods) == 0 { return nil } - oldest := pods[0] - for _, pod := range pods { + var oldest *v1.Pod = nil + for i, pod := range pods { + if pod.Status.Phase != v1.PodPending { + continue + } + if _, ok := pod.Annotations[BindTimeAnnotations]; !ok { + continue + } + if phase, ok := pod.Annotations[DeviceBindPhase]; !ok { + continue + } else { + if strings.Compare(phase, DeviceBindAllocating) != 0 { + continue + } + } klog.V(4).Infof("pod %s, predicate time: %s", pod.Name, pod.Annotations[AssignedTimeAnnotations]) - if getPredicateTimeFromPodAnnotation(&oldest) > getPredicateTimeFromPodAnnotation(&pod) { - oldest = pod + if oldest == nil || getPredicateTimeFromPodAnnotation(oldest) > getPredicateTimeFromPodAnnotation(&pod) { + oldest = &pods[i] } } + if oldest == nil { + klog.Warningf("no pod has predicate time") + return nil + } klog.V(4).Infof("oldest pod %#v, predicate time: %#v", oldest.Name, oldest.Annotations[AssignedTimeAnnotations]) annotation := map[string]string{AssignedTimeAnnotations: strconv.FormatUint(math.MaxUint64, 10)} - if err := PatchPodAnnotations(&oldest, annotation); err != nil { + if err := PatchPodAnnotations(oldest, annotation); err != nil { klog.Errorf("update pod %s failed, err: %v", oldest.Name, err) return nil } - return &oldest + return oldest } func getPredicateTimeFromPodAnnotation(pod *v1.Pod) uint64 {