Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter pods by node name and improve oldest pod selection logic #37

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 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,23 +81,45 @@ func getOldestPod(pods []v1.Pod, nodename string) *v1.Pod {
if len(pods) == 0 {
return nil
}
oldest := pods[0]
for _, pod := range pods {
if pod.Annotations[AssignedNodeAnnotations] == nodename {
klog.V(4).Infof("pod %s, predicate time: %s", pod.Name, pod.Annotations[AssignedTimeAnnotations])
if getPredicateTimeFromPodAnnotation(&oldest) > getPredicateTimeFromPodAnnotation(&pod) {
oldest = pod
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
}
}
if assignedNodeAnnotations, ok := pod.Annotations[AssignedNodeAnnotations]; !ok {
continue
} else {
if strings.Compare(assignedNodeAnnotations, nodename) != 0 {
continue
}
}
klog.V(4).Infof("pod %s, predicate time: %s", pod.Name, pod.Annotations[AssignedTimeAnnotations])
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
Loading