Skip to content

Commit

Permalink
filter pods by node name and improve oldest pod selection logic
Browse files Browse the repository at this point in the history
Signed-off-by: cybergeek2077 <[email protected]>
  • Loading branch information
cybergeek2077 committed Nov 19, 2024
1 parent 680cd35 commit 4576c36
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions pkg/plugin/vgpu/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down

0 comments on commit 4576c36

Please sign in to comment.