@@ -64,36 +64,66 @@ func GetNode(nodename string) (*v1.Node, error) {
6464 return n , err
6565}
6666
67- func GetPendingPod (node string ) (* v1.Pod , error ) {
67+ func GetPendingPod (node string , gpuAmount int ) (* v1.Pod , error ) {
6868 podList , err := lock .GetClient ().CoreV1 ().Pods ("" ).List (context .Background (), metav1.ListOptions {})
6969 if err != nil {
7070 return nil , err
7171 }
7272
73- oldestPod := getOldestPod (podList .Items , node )
73+ oldestPod := getOldestPod (podList .Items , node , gpuAmount )
7474 if oldestPod == nil {
7575 return nil , fmt .Errorf ("cannot get valid pod" )
7676 }
7777
7878 return oldestPod , nil
7979}
8080
81- func getOldestPod (pods []v1.Pod , nodename string ) * v1.Pod {
81+ //func printPodList(pods []v1.Pod, kubeletRequestAmount int) {
82+ // for _, pod := range pods {
83+ // _, length := DecodePodDevices(pod.Annotations[AssignedIDsToAllocateAnnotations])
84+ // klog.V(4).Infof("pod %s, node %s, kubelet request %d, pod annotation gpu amount %d, annotations: %s", pod.Name,
85+ // pod.Spec.NodeName, kubeletRequestAmount, length, pod.Annotations[AssignedIDsToAllocateAnnotations])
86+ // }
87+ //}
88+
89+ func getOldestPod (pods []v1.Pod , nodename string , gpuAmount int ) * v1.Pod {
8290 if len (pods ) == 0 {
8391 return nil
8492 }
85- oldest := pods [0 ]
93+ //printPodList(pods, gpuAmount)
94+ var oldest v1.Pod
95+ found := false
96+ filteredPods := []v1.Pod {}
8697 for _ , pod := range pods {
8798 if pod .Annotations [AssignedNodeAnnotations ] == nodename {
88- klog .V (4 ).Infof ("pod %s, predicate time: %s" , pod .Name , pod .Annotations [AssignedTimeAnnotations ])
89- if getPredicateTimeFromPodAnnotation (& oldest ) > getPredicateTimeFromPodAnnotation (& pod ) {
99+ // Need to ensure that the pod being handled has the same number of devices as the node requesting
100+ pdevices , deviceCount := DecodePodDevices (pod .Annotations [AssignedIDsToAllocateAnnotations ])
101+ klog .V (3 ).Infof ("GetOldestPod -- Pod name: %s, Kubelet request gpuAmount %d, " +
102+ "pod request gpuAmount %d" , pod .Name , gpuAmount , len (pdevices ))
103+ if deviceCount == gpuAmount {
104+ found = true
105+ filteredPods = append (filteredPods , pod )
90106 oldest = pod
91107 }
92108 }
93109 }
94- klog .V (4 ).Infof ("oldest pod %#v, predicate time: %#v" , oldest .Name ,
95- oldest .Annotations [AssignedTimeAnnotations ])
96- return & oldest
110+
111+ //klog.V(4).Infof("filteredPods %v", filteredPods)
112+
113+ for _ , pod := range filteredPods {
114+ //klog.V(4).Infof("pod %s, predicate time: %s", pod.Name, pod.Annotations[AssignedTimeAnnotations])
115+ if getPredicateTimeFromPodAnnotation (& oldest ) > getPredicateTimeFromPodAnnotation (& pod ) {
116+ oldest = pod
117+ }
118+ }
119+
120+ if found {
121+ klog .V (4 ).Infof ("GetOldestPod -- oldest pod %#v, predicate time: %#v" , oldest .Name ,
122+ oldest .Annotations [AssignedTimeAnnotations ])
123+ return & oldest
124+ }
125+ return nil
126+
97127}
98128
99129func getPredicateTimeFromPodAnnotation (pod * v1.Pod ) uint64 {
@@ -153,9 +183,8 @@ func EncodeContainerDevices(cd ContainerDevices) string {
153183 for _ , val := range cd {
154184 tmp += val .UUID + "," + val .Type + "," + strconv .Itoa (int (val .Usedmem )) + "," + strconv .Itoa (int (val .Usedcores )) + ":"
155185 }
156- fmt . Println ("Encoded container Devices=" , tmp )
186+ //klog.V(4).Infoln ("Encoded container Devices=", tmp)
157187 return tmp
158- //return strings.Join(cd, ",")
159188}
160189
161190func EncodePodDevices (pd PodDevices ) string {
@@ -166,18 +195,20 @@ func EncodePodDevices(pd PodDevices) string {
166195 return strings .Join (ss , ";" )
167196}
168197
169- func DecodeContainerDevices (str string ) ContainerDevices {
198+ func DecodeContainerDevices (str string ) (c ContainerDevices , count int ) {
199+ deviceCount := 0
170200 if len (str ) == 0 {
171- return ContainerDevices {}
201+ return ContainerDevices {}, 0
172202 }
173203 cd := strings .Split (str , ":" )
174204 contdev := ContainerDevices {}
175205 tmpdev := ContainerDevice {}
176206 if len (str ) == 0 {
177- return contdev
207+ return contdev , 0
178208 }
179209 for _ , val := range cd {
180210 if strings .Contains (val , "," ) {
211+ deviceCount ++
181212 tmpstr := strings .Split (val , "," )
182213 tmpdev .UUID = tmpstr [0 ]
183214 tmpdev .Type = tmpstr [1 ]
@@ -186,27 +217,31 @@ func DecodeContainerDevices(str string) ContainerDevices {
186217 devcores , _ := strconv .ParseInt (tmpstr [3 ], 10 , 32 )
187218 tmpdev .Usedcores = int32 (devcores )
188219 contdev = append (contdev , tmpdev )
220+ klog .V (4 ).Infoln ("val=" , val )
189221 }
190222 }
191- return contdev
223+ //klog.V(4).Infoln("contdev=", contdev)
224+ return contdev , deviceCount
192225}
193226
194- func DecodePodDevices (str string ) PodDevices {
195- klog .Infoln ("DecodePodDevices=" , str )
227+ func DecodePodDevices (str string ) (PodDevices , int ) {
228+ klog .V (4 ).Infoln ("DecodePodDevices=" , str )
229+ length := 0
196230 if len (str ) == 0 {
197- return PodDevices {}
231+ return PodDevices {}, 0
198232 }
199233 var pd PodDevices
200234 for _ , s := range strings .Split (str , ";" ) {
201- cd := DecodeContainerDevices (s )
235+ cd , count := DecodeContainerDevices (s )
202236 pd = append (pd , cd )
237+ length += count
203238 }
204- return pd
239+ return pd , length
205240}
206241
207242func GetNextDeviceRequest (dtype string , p v1.Pod ) (v1.Container , ContainerDevices , error ) {
208- pdevices := DecodePodDevices (p .Annotations [AssignedIDsToAllocateAnnotations ])
209- klog .Infoln ("pdevices=" , pdevices , "p.name" , p .Name , "annos" , p .Annotations )
243+ pdevices , _ := DecodePodDevices (p .Annotations [AssignedIDsToAllocateAnnotations ])
244+ klog .V ( 4 ). Infoln ("pdevices=" , pdevices , "p.name" , p .Name , "annos" , p .Annotations )
210245 res := ContainerDevices {}
211246 for idx , val := range pdevices {
212247 found := false
@@ -224,7 +259,7 @@ func GetNextDeviceRequest(dtype string, p v1.Pod) (v1.Container, ContainerDevice
224259}
225260
226261func EraseNextDeviceTypeFromAnnotation (dtype string , p v1.Pod ) error {
227- pdevices := DecodePodDevices (p .Annotations [AssignedIDsToAllocateAnnotations ])
262+ pdevices , _ := DecodePodDevices (p .Annotations [AssignedIDsToAllocateAnnotations ])
228263 res := PodDevices {}
229264 found := false
230265 for _ , val := range pdevices {
@@ -248,9 +283,9 @@ func EraseNextDeviceTypeFromAnnotation(dtype string, p v1.Pod) error {
248283 }
249284 }
250285 klog .Infoln ("After erase res=" , res )
251- newannos := make (map [string ]string )
252- newannos [AssignedIDsToAllocateAnnotations ] = EncodePodDevices (res )
253- return PatchPodAnnotations (& p , newannos )
286+ newAnnos := make (map [string ]string )
287+ newAnnos [AssignedIDsToAllocateAnnotations ] = EncodePodDevices (res )
288+ return PatchPodAnnotations (& p , newAnnos )
254289}
255290
256291func PodAllocationTrySuccess (nodeName string , pod * v1.Pod ) {
0 commit comments