@@ -59,7 +59,12 @@ func GetNode(nodename string) (*v1.Node, error) {
5959}
6060
6161func 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
96118func getPredicateTimeFromPodAnnotation (pod * v1.Pod ) uint64 {
0 commit comments