@@ -18,6 +18,7 @@ package util
18
18
19
19
import (
20
20
"fmt"
21
+ "sync"
21
22
22
23
"k8s.io/api/core/v1"
23
24
"k8s.io/apimachinery/pkg/labels"
@@ -30,6 +31,35 @@ import (
30
31
// PodLister is used in predicate and nodeorder plugin
31
32
type PodLister struct {
32
33
Session * framework.Session
34
+
35
+ podLock sync.RWMutex
36
+ UpdatedPod map [api.TaskID ]* v1.Pod
37
+ }
38
+
39
+ func NewPodLister (ssn * framework.Session ) * PodLister {
40
+ return & PodLister {
41
+ Session : ssn ,
42
+ UpdatedPod : make (map [api.TaskID ]* v1.Pod ),
43
+ }
44
+ }
45
+
46
+ func (pl * PodLister ) updateTask (task * api.TaskInfo ) * v1.Pod {
47
+ pl .podLock .RLock ()
48
+ pod , found := pl .UpdatedPod [task .UID ]
49
+ pl .podLock .RUnlock ()
50
+
51
+ if ! found {
52
+ pod = task .Pod .DeepCopy ()
53
+ pod .Spec .NodeName = task .NodeName
54
+
55
+ pl .podLock .Lock ()
56
+ pl .UpdatedPod [task .UID ] = pod
57
+ pl .podLock .Unlock ()
58
+ } else {
59
+ pod .Spec .NodeName = task .NodeName
60
+ }
61
+
62
+ return pod
33
63
}
34
64
35
65
// List method is used to list all the pods
@@ -44,8 +74,7 @@ func (pl *PodLister) List(selector labels.Selector) ([]*v1.Pod, error) {
44
74
for _ , task := range tasks {
45
75
if selector .Matches (labels .Set (task .Pod .Labels )) {
46
76
if task .NodeName != task .Pod .Spec .NodeName {
47
- pod := task .Pod .DeepCopy ()
48
- pod .Spec .NodeName = task .NodeName
77
+ pod := pl .updateTask (task )
49
78
pods = append (pods , pod )
50
79
} else {
51
80
pods = append (pods , task .Pod )
@@ -70,8 +99,7 @@ func (pl *PodLister) FilteredList(podFilter algorithm.PodFilter, selector labels
70
99
for _ , task := range tasks {
71
100
if podFilter (task .Pod ) && selector .Matches (labels .Set (task .Pod .Labels )) {
72
101
if task .NodeName != task .Pod .Spec .NodeName {
73
- pod := task .Pod .DeepCopy ()
74
- pod .Spec .NodeName = task .NodeName
102
+ pod := pl .updateTask (task )
75
103
pods = append (pods , pod )
76
104
} else {
77
105
pods = append (pods , task .Pod )
0 commit comments