Skip to content

Commit 0bc7f60

Browse files
authored
Merge pull request kubernetes-retired#673 from hex108/shadow
Do not create PodGroup and Job for task whose scheduler is not kube-b…
2 parents 962f323 + f2063ad commit 0bc7f60

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

pkg/scheduler/cache/cache.go

+3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ type SchedulerCache struct {
7474
kbclient *kbver.Clientset
7575

7676
defaultQueue string
77+
// schedulerName is the name for kube-batch scheduler
78+
schedulerName string
7779

7880
podInformer infov1.PodInformer
7981
nodeInformer infov1.NodeInformer
@@ -189,6 +191,7 @@ func newSchedulerCache(config *rest.Config, schedulerName string, defaultQueue s
189191
kubeclient: kubernetes.NewForConfigOrDie(config),
190192
kbclient: kbver.NewForConfigOrDie(config),
191193
defaultQueue: defaultQueue,
194+
schedulerName: schedulerName,
192195
}
193196

194197
// Prepare event clients.

pkg/scheduler/cache/cache_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,53 @@ func TestAddNode(t *testing.T) {
257257
}
258258
}
259259
}
260+
261+
func TestGetOrCreateJob(t *testing.T) {
262+
owner1 := buildOwnerReference("j1")
263+
owner2 := buildOwnerReference("j2")
264+
265+
pod1 := buildPod("c1", "p1", "n1", v1.PodRunning, buildResourceList("1000m", "1G"),
266+
[]metav1.OwnerReference{owner1}, make(map[string]string))
267+
pi1 := api.NewTaskInfo(pod1)
268+
pi1.Job = "j1" // The job name is set by cache.
269+
270+
pod2 := buildPod("c1", "p2", "n1", v1.PodRunning, buildResourceList("1000m", "1G"),
271+
[]metav1.OwnerReference{owner2}, make(map[string]string))
272+
pod2.Spec.SchedulerName = "kube-batch"
273+
pi2 := api.NewTaskInfo(pod2)
274+
275+
pod3 := buildPod("c3", "p3", "n1", v1.PodRunning, buildResourceList("1000m", "1G"),
276+
[]metav1.OwnerReference{owner2}, make(map[string]string))
277+
pi3 := api.NewTaskInfo(pod3)
278+
279+
cache := &SchedulerCache{
280+
Nodes: make(map[string]*api.NodeInfo),
281+
Jobs: make(map[api.JobID]*api.JobInfo),
282+
schedulerName: "kube-batch",
283+
}
284+
285+
tests := []struct {
286+
task *api.TaskInfo
287+
gotJob bool // whether getOrCreateJob will return job for corresponding task
288+
}{
289+
{
290+
task: pi1,
291+
gotJob: true,
292+
},
293+
{
294+
task: pi2,
295+
gotJob: true,
296+
},
297+
{
298+
task: pi3,
299+
gotJob: false,
300+
},
301+
}
302+
for i, test := range tests {
303+
result := cache.getOrCreateJob(test.task) != nil
304+
if result != test.gotJob {
305+
t.Errorf("case %d: \n expected %t, \n got %t \n",
306+
i, test.gotJob, result)
307+
}
308+
}
309+
}

pkg/scheduler/cache/event_handlers.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,15 @@ func isTerminated(status kbapi.TaskStatus) bool {
3838
return status == kbapi.Succeeded || status == kbapi.Failed
3939
}
4040

41+
// getOrCreateJob will return corresponding Job for pi if it exists, or it will create a Job and return it if
42+
// pi.Pod.Spec.SchedulerName is same as kube-batch scheduler's name, otherwise it will return nil.
4143
func (sc *SchedulerCache) getOrCreateJob(pi *kbapi.TaskInfo) *kbapi.JobInfo {
4244
if len(pi.Job) == 0 {
45+
if pi.Pod.Spec.SchedulerName != sc.schedulerName {
46+
glog.V(4).Infof("Pod %s/%s will not not scheduled by %s, skip creating PodGroup and Job for it",
47+
pi.Pod.Namespace, pi.Pod.Name, sc.schedulerName)
48+
return nil
49+
}
4350
pb := createShadowPodGroup(pi.Pod)
4451
pi.Job = kbapi.JobID(pb.Name)
4552

@@ -62,7 +69,9 @@ func (sc *SchedulerCache) getOrCreateJob(pi *kbapi.TaskInfo) *kbapi.JobInfo {
6269

6370
func (sc *SchedulerCache) addTask(pi *kbapi.TaskInfo) error {
6471
job := sc.getOrCreateJob(pi)
65-
job.AddTaskInfo(pi)
72+
if job != nil {
73+
job.AddTaskInfo(pi)
74+
}
6675

6776
if len(pi.NodeName) != 0 {
6877
if _, found := sc.Nodes[pi.NodeName]; !found {

0 commit comments

Comments
 (0)