This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
/
scheduler.go
680 lines (586 loc) · 23 KB
/
scheduler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
// MIT License
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE
package scheduler
import (
"fmt"
"github.com/microsoft/hivedscheduler/pkg/algorithm"
si "github.com/microsoft/hivedscheduler/pkg/api"
"github.com/microsoft/hivedscheduler/pkg/common"
"github.com/microsoft/hivedscheduler/pkg/internal"
"github.com/microsoft/hivedscheduler/pkg/webserver"
core "k8s.io/api/core/v1"
apiErrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/runtime"
kubeInformer "k8s.io/client-go/informers"
kubeClient "k8s.io/client-go/kubernetes"
coreLister "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/klog"
ei "k8s.io/kubernetes/pkg/scheduler/api"
"sync"
"time"
)
// HivedScheduler is the scheduling framework which serves as the bridge between
// the scheduling algorithm and K8S.
// It provides the whole cluster scheduling view and the interested pod scheduling
// request from K8S to the scheduling algorithm, then it drives the scheduling
// algorithm to make the pod schedule decision, finally it will fully obey the
// decision and try its best to execute the decision to K8S.
type HivedScheduler struct {
kConfig *rest.Config
sConfig *si.Config
// Client is used to write remote objects in ApiServer.
// Remote objects are up-to-date and is writable.
//
// To read objects, it is better to use Lister instead of Client, since the
// Lister is cached.
//
// Client already has retry policy to retry for most transient failures.
// Client write failure does not mean the write does not succeed on remote, the
// failure may be due to the success response is just failed to deliver to the
// Client.
kClient kubeClient.Interface
// Informer is used to sync remote objects to local cached objects, and then
// deliver corresponding events of the object changes.
//
// The event delivery for an object is level driven instead of edge driven,
// and the object is identified by its name instead of its UID.
// For example:
// 1. Informer may not deliver any event if a create is immediately followed
// by a delete.
// 2. Informer may deliver an Update event with UID changed if a delete is
// immediately followed by a create.
//
// Platform Error Panic in Informer Callbacks will not be recovered, i.e. it will
// crash the whole process, since generally it is a ground truth failure that
// cannot be ignored and will impact the whole scheduling.
nodeInformer cache.SharedIndexInformer
podInformer cache.SharedIndexInformer
// Lister is used to read local cached objects in Informer.
// Local cached objects may be outdated and is not writable.
//
// Outdated means current local cached objects may not reflect previous Client
// remote writes.
//
// Node object provides the capacity and schedulable resource of a Node.
nodeLister coreLister.NodeLister
// Pod object provides the bound Pods and bound resource of a Node.
podLister coreLister.PodLister
// WebServer is used to interact with K8S Default Scheduler and others.
// Only responsible for unbound Pod (from Default Scheduler).
//
// Platform Error Panic in WebServer Callbacks will be recovered, since generally
// it is just one request failure that can be ignored and will not impact the
// whole scheduling.
webServer *webserver.WebServer
// SchedulerLock is used to protect the PodScheduleStatuses and its derived
// scheduling view inside the SchedulerAlgorithm.
// It also ensures the SchedulerAlgorithm.Schedule() will never be executed
// concurrently.
schedulerLock *sync.RWMutex
// PodScheduleStatuses serves as the ground truth of the scheduling view.
// It tracks and caches the PodScheduleStatus for all live (not completed)
// hived Pods, which merges the information from both SchedulerAlgorithm and
// PodInformer, so that SchedulerAlgorithm and K8S Default Scheduler can pick
// it up later.
podScheduleStatuses internal.PodScheduleStatuses
// SchedulerAlgorithm is used to make the pod schedule decision based on the
// scheduling view.
schedulerAlgorithm internal.SchedulerAlgorithm
}
func NewHivedScheduler() *HivedScheduler {
klog.Infof("Initializing " + si.ComponentName)
sConfig := si.NewConfig(si.InitRawConfig(nil))
klog.Infof("With Config: \n%v", common.ToYaml(sConfig))
kConfig := si.BuildKubeConfig(sConfig)
kClient := internal.CreateClient(kConfig)
nodeListerInformer := kubeInformer.NewSharedInformerFactory(kClient, 0).Core().V1().Nodes()
podListerInformer := kubeInformer.NewSharedInformerFactory(kClient, 0).Core().V1().Pods()
nodeInformer := nodeListerInformer.Informer()
podInformer := podListerInformer.Informer()
nodeLister := nodeListerInformer.Lister()
podLister := podListerInformer.Lister()
s := &HivedScheduler{
kConfig: kConfig,
sConfig: sConfig,
kClient: kClient,
nodeInformer: nodeInformer,
podInformer: podInformer,
nodeLister: nodeLister,
podLister: podLister,
schedulerLock: &sync.RWMutex{},
podScheduleStatuses: internal.PodScheduleStatuses{},
schedulerAlgorithm: algorithm.NewHivedAlgorithm(sConfig),
}
// Setup Informer Callbacks
s.nodeInformer.AddEventHandler(
cache.ResourceEventHandlerFuncs{
AddFunc: s.addNode,
UpdateFunc: s.updateNode,
DeleteFunc: s.deleteNode,
},
)
s.podInformer.AddEventHandler(
cache.FilteringResourceEventHandler{
FilterFunc: func(obj interface{}) bool {
pod := internal.ToPod(obj)
return internal.IsInterested(pod)
},
Handler: cache.ResourceEventHandlerFuncs{
AddFunc: s.addPod,
UpdateFunc: s.updatePod,
DeleteFunc: s.deletePod,
},
},
)
// Setup WebServer Callbacks
s.webServer = webserver.NewWebServer(
sConfig,
internal.ExtenderHandlers{
FilterHandler: s.filterRoutine,
BindHandler: s.bindRoutine,
PreemptHandler: s.preemptRoutine,
},
internal.InspectHandlers{
GetAffinityGroupsHandler: s.getAffinityGroups,
GetAffinityGroupHandler: s.getAffinityGroup,
},
)
return s
}
func (s *HivedScheduler) Run(stopCh <-chan struct{}) {
defer klog.Errorf("Stopping " + si.ComponentName)
defer runtime.HandleCrash()
klog.Infof("Recovering " + si.ComponentName)
go s.nodeInformer.Run(stopCh)
go s.podInformer.Run(stopCh)
if !cache.WaitForCacheSync(
stopCh,
s.nodeInformer.HasSynced,
s.podInformer.HasSynced) {
panic(fmt.Errorf("Failed to WaitForCacheSync"))
}
// Previous bound pods recovery completed, start to accept scheduling request.
s.webServer.AsyncRun(stopCh)
klog.Infof("Running " + si.ComponentName)
<-stopCh
}
func (s *HivedScheduler) addNode(obj interface{}) {
node := internal.ToNode(obj)
logPfx := fmt.Sprintf("[%v]: addNode: ", node.Name)
klog.Infof(logPfx + "Started")
defer internal.HandleInformerPanic(logPfx, true)
s.schedulerAlgorithm.AddNode(node)
}
func (s *HivedScheduler) updateNode(oldObj, newObj interface{}) {
oldNode := internal.ToNode(oldObj)
newNode := internal.ToNode(newObj)
// Informer may deliver an Update event with UID changed if a delete is
// immediately followed by a create, so manually decompose it.
if oldNode.UID != newNode.UID {
s.deleteNode(oldObj)
s.addNode(newObj)
return
}
logPfx := fmt.Sprintf("[%v]: updateNode: ", newNode.Name)
defer internal.HandleInformerPanic(logPfx, false)
s.schedulerAlgorithm.UpdateNode(oldNode, newNode)
}
func (s *HivedScheduler) deleteNode(obj interface{}) {
node := internal.ToNode(obj)
logPfx := fmt.Sprintf("[%v]: deleteNode: ", node.Name)
klog.Infof(logPfx + "Started")
defer internal.HandleInformerPanic(logPfx, true)
s.schedulerAlgorithm.DeleteNode(node)
}
func (s *HivedScheduler) addPod(obj interface{}) {
pod := internal.ToPod(obj)
if internal.IsBound(pod) {
s.addBoundPod(pod)
} else {
s.addUnboundPod(pod)
}
}
func (s *HivedScheduler) updatePod(oldObj, newObj interface{}) {
oldPod := internal.ToPod(oldObj)
newPod := internal.ToPod(newObj)
// Informer may deliver an Update event with UID changed if a delete is
// immediately followed by a create, so manually decompose it.
if oldPod.UID != newPod.UID {
s.deletePod(oldObj)
s.addPod(newObj)
return
}
oldBound := internal.IsBound(oldPod)
newBound := internal.IsBound(newPod)
if !oldBound && newBound {
s.addBoundPod(newPod)
} else if oldBound && !newBound {
// Unreachable
panic(fmt.Errorf(
"[%v]: Pod updated from bound to unbound: previous bound node: %v",
internal.Key(newPod), oldPod.Spec.NodeName))
}
}
func (s *HivedScheduler) deletePod(obj interface{}) {
pod := internal.ToPod(obj)
s.schedulerLock.Lock()
defer s.schedulerLock.Unlock()
logPfx := fmt.Sprintf("[%v]: deletePod: ", internal.Key(pod))
klog.Infof(logPfx + "Started")
defer internal.HandleInformerPanic(logPfx, true)
podStatus := s.podScheduleStatuses[pod.UID]
if podStatus != nil {
if internal.IsAllocated(podStatus.PodState) {
s.schedulerAlgorithm.DeleteAllocatedPod(podStatus.Pod)
}
delete(s.podScheduleStatuses, pod.UID)
}
}
func (s *HivedScheduler) addBoundPod(pod *core.Pod) {
s.schedulerLock.Lock()
defer s.schedulerLock.Unlock()
logPfx := fmt.Sprintf("[%v]: addBoundPod: ", internal.Key(pod))
klog.Infof(logPfx + "Started")
defer internal.HandleInformerPanic(logPfx, true)
podStatus := s.podScheduleStatuses[pod.UID]
if podStatus != nil {
if internal.IsAllocated(podStatus.PodState) {
// Already allocated, so the placement should never be changed again.
// So, not need to update the allocated pod.
if podStatus.PodState != internal.PodBound {
s.podScheduleStatuses[pod.UID] = &internal.PodScheduleStatus{
Pod: podStatus.Pod,
PodState: internal.PodBound,
PodScheduleResult: nil,
}
}
return
}
}
// Recover bound pod.
s.schedulerAlgorithm.AddAllocatedPod(pod)
s.podScheduleStatuses[pod.UID] = &internal.PodScheduleStatus{
Pod: pod,
PodState: internal.PodBound,
PodScheduleResult: nil,
}
}
// Also track unbound Pod to help prevent tracking and scheduling for future
// not existing and completed Pod.
func (s *HivedScheduler) addUnboundPod(pod *core.Pod) {
s.schedulerLock.Lock()
defer s.schedulerLock.Unlock()
logPfx := fmt.Sprintf("[%v]: addUnboundPod: ", internal.Key(pod))
klog.Infof(logPfx + "Started")
defer internal.HandleInformerPanic(logPfx, true)
podStatus := s.podScheduleStatuses[pod.UID]
if podStatus != nil {
// Keep the existing one.
return
}
// Receive newly unbound pod, so it must be PodWaiting.
s.podScheduleStatuses[pod.UID] = &internal.PodScheduleStatus{
Pod: pod,
PodState: internal.PodWaiting,
PodScheduleResult: nil,
}
}
// Only live (not completed) unbound hived Pods are accepted to be scheduled.
// Return unbound PodScheduleStatus.
func (s *HivedScheduler) generalScheduleAdmissionCheck(
podStatus *internal.PodScheduleStatus) *internal.PodScheduleStatus {
if podStatus == nil {
// If the pod does not exist or completed:
// The inconsistency should can be reconciled by K8S Default Scheduler.
// If the pod has not been informed to the scheduler:
// The inconsistency should can be reconciled by the scheduler PodInformer.
panic(internal.NewBadRequestError(fmt.Sprintf(
"Pod does not exist, completed or has not been informed to the scheduler")))
} else {
if podStatus.PodState == internal.PodBound {
// The inconsistency should can be reconciled by K8S Default Scheduler.
panic(internal.NewBadRequestError(fmt.Sprintf(
"Pod has already been bound to node %v",
podStatus.Pod.Spec.NodeName)))
}
}
return podStatus
}
func (s *HivedScheduler) validatePodBindInfo(
podBindInfo *si.PodBindInfo, suggestedNodes []string) error {
node := podBindInfo.Node
// Check against existing nodes
_, getErr := s.nodeLister.Get(node)
if getErr != nil {
if apiErrors.IsNotFound(getErr) {
// If the node does not exist:
// The inconsistency cannot be reconciled, the Pod should be still bound
// and then retried with another one, as normal pod deleted on node
// deleted.
// If the node has not been informed to the scheduler:
// The inconsistency should can be reconciled by the scheduler NodeInformer.
return fmt.Errorf(
"The SchedulerAlgorithm decided to bind on node %v, but the node "+
"does not exist or has not been informed to the scheduler", node)
} else {
// The inconsistency should can be reconciled by the scheduler NodeInformer.
return fmt.Errorf(
"Failed to check whether the SchedulerAlgorithm decided to bind on "+
"not existing node %v. Node cannot be got from local cache: %v",
node, getErr)
}
}
// Check against suggested nodes
if !common.StringsContains(suggestedNodes, node) {
return fmt.Errorf(
"The SchedulerAlgorithm decided to bind on node %v but the node "+
"is not within the selected nodes from K8S Default Scheduler. "+
"So, the binding is incompatible with K8S Default Scheduler.",
node)
}
return nil
}
func (s *HivedScheduler) shouldForceBind(
podStatus *internal.PodScheduleStatus, suggestedNodes []string) bool {
pod := podStatus.Pod
podBindAttempts := podStatus.PodBindAttempts
podBindInfo := podStatus.PodScheduleResult.PodBindInfo
logPfx := fmt.Sprintf("[%v]: Will force bind Pod: ", internal.Key(pod))
// Ensure the Pod can be bound according to the pod schedule decision eventually.
//
// Therefore, if the decision is really problematic based on current status,
// the Pod can fail itself or can be deleted by the GarbageCollectionController,
// then can be retried with another one, instead of potential forever binding
// or wrongly rolled back to PodWaiting.
//
// Given that the SchedulerAlgorithm should avoid make problematic decision
// based on current status, the retried Pod should can be scheduled on suitable
// placement decision eventually.
// Thus, the problematic decision can only be stale decision, i.e. only newly
// bad GPUs or newly deleted Nodes will lead Pod retried.
// For newly bad GPUs, it is like the normal behaviour that a pod will fail
// after the GPUs it runs on become unhealthy.
// For newly deleted Nodes, it is like the normal behaviour that a pod will
// be deleted by the GarbageCollectionController after the node it runs on is
// deleted.
//
// So overall keeps on binding, regardless of potential problematic decision,
// is acceptable.
if podBindAttempts >= *s.sConfig.ForcePodBindThreshold {
klog.Warningf(logPfx+
"The Pod binding has already been tried %v times which reaches the "+
"ForcePodBindThreshold %v",
podBindAttempts, *s.sConfig.ForcePodBindThreshold)
return true
} else if err := s.validatePodBindInfo(podBindInfo, suggestedNodes); err != nil {
// Proactively trigger force bind, if the pod schedule decision has already
// been detected to be probably invalid based on current status, to reduce
// the binding time.
klog.Warningf(logPfx+"%v", err)
return true
}
return false
}
// Bypass K8S Default Scheduler to directly trigger the bindRoutine, it can be
// considered as a normal shadow of the previous bindRoutine if called
// asynchronously.
func (s *HivedScheduler) forceBindExecutor(bindingPod *core.Pod) {
logPfx := fmt.Sprintf("[%v]: forceBindExecutor: ", internal.Key(bindingPod))
klog.Infof(logPfx + "Started")
defer internal.HandleWebServerPanic(nil)
defer internal.HandleRoutinePanic(logPfx)
s.bindRoutine(ei.ExtenderBindingArgs{
PodNamespace: bindingPod.Namespace,
PodName: bindingPod.Name,
PodUID: bindingPod.UID,
Node: bindingPod.Spec.NodeName,
})
}
func (s *HivedScheduler) filterRoutine(args ei.ExtenderArgs) *ei.ExtenderFilterResult {
s.schedulerLock.Lock()
defer s.schedulerLock.Unlock()
pod := args.Pod
suggestedNodes := *args.NodeNames
logPfx := fmt.Sprintf("[%v]: filterRoutine: ", internal.Key(pod))
klog.Infof(logPfx + "Started")
defer internal.HandleRoutinePanic(logPfx)
podStatus := s.generalScheduleAdmissionCheck(s.podScheduleStatuses[pod.UID])
if podStatus.PodState == internal.PodBinding {
// Insist previous bind result, since Pod binding should be idempotent, and
// it is already assumed as allocated by scheduling algorithm which cannot
// be rolled back.
bindingPod := podStatus.Pod
podStatus.PodBindAttempts++
if s.shouldForceBind(podStatus, suggestedNodes) {
go s.forceBindExecutor(bindingPod)
}
return &ei.ExtenderFilterResult{
NodeNames: &[]string{bindingPod.Spec.NodeName},
}
}
// At this point, podState must be in:
// {PodWaiting, PodPreempting}
// Carry out a new scheduling
result := s.schedulerAlgorithm.Schedule(pod, suggestedNodes)
if result.PodBindInfo != nil {
bindingPod := internal.NewBindingPod(pod, result.PodBindInfo)
// Assume binding pod as allocated, so that next scheduling does not need to
// wait current binding completed.
s.schedulerAlgorithm.AddAllocatedPod(bindingPod)
// Transition to PodBinding only after AddAllocatedPod succeeded.
s.podScheduleStatuses[pod.UID] = &internal.PodScheduleStatus{
Pod: bindingPod,
PodState: internal.PodBinding,
PodScheduleResult: &result,
}
if s.shouldForceBind(s.podScheduleStatuses[pod.UID], suggestedNodes) {
go s.forceBindExecutor(bindingPod)
}
klog.Infof(logPfx+"Pod is binding: %v", common.ToJson(result.PodBindInfo))
return &ei.ExtenderFilterResult{
NodeNames: &[]string{bindingPod.Spec.NodeName},
}
} else if result.PodPreemptInfo != nil {
s.podScheduleStatuses[pod.UID] = &internal.PodScheduleStatus{
Pod: pod,
PodState: internal.PodPreempting,
PodScheduleResult: &result,
}
// Return FailedNodes to tell K8S Default Scheduler that preemption may help.
failedNodes := map[string]string{}
for _, victim := range result.PodPreemptInfo.VictimPods {
node := victim.Spec.NodeName
if _, ok := failedNodes[node]; !ok {
failedNodes[node] = fmt.Sprintf(
"node(%v) is waiting for victim Pod(s) to be preempted: %v",
node, internal.Key(victim))
} else {
failedNodes[node] += ", " + internal.Key(victim)
}
}
klog.Infof(logPfx+"Pod is preempting: %v", common.ToJson(failedNodes))
return &ei.ExtenderFilterResult{
FailedNodes: failedNodes,
}
} else {
s.podScheduleStatuses[pod.UID] = &internal.PodScheduleStatus{
Pod: pod,
PodState: internal.PodWaiting,
PodScheduleResult: &result,
}
// Block the whole scheduling to achieve better FIFO
if *s.sConfig.WaitingPodSchedulingBlockMilliSec > 0 {
time.Sleep(time.Duration(*s.sConfig.WaitingPodSchedulingBlockMilliSec) *
time.Millisecond)
}
// Return Error to tell K8S Default Scheduler that preemption must not help.
waitReason := "Pod is waiting for preemptible or free resource to appear"
if result.PodWaitInfo != nil {
waitReason += ": " + result.PodWaitInfo.Reason
}
klog.Infof(logPfx + waitReason)
return &ei.ExtenderFilterResult{
Error: waitReason,
}
}
}
// Bind the Pod based on its corresponding bindingPod.
// Notes:
// 1. It should be idempotent since it may be called multiple times for the same
// pod. This ensures that once a specific Pod is allocated by AddAllocatedPod,
// its placement will never be changed to another one.
func (s *HivedScheduler) bindRoutine(args ei.ExtenderBindingArgs) *ei.ExtenderBindingResult {
s.schedulerLock.RLock()
defer s.schedulerLock.RUnlock()
podKey := internal.NewPodKey(args.PodNamespace, args.PodName, args.PodUID)
bindingNode := args.Node
logPfx := fmt.Sprintf("[%v]: bindRoutine: ", podKey)
klog.Infof(logPfx + "Started")
defer internal.HandleRoutinePanic(logPfx)
podStatus := s.generalScheduleAdmissionCheck(s.podScheduleStatuses[podKey.UID])
if podStatus.PodState == internal.PodBinding {
bindingPod := podStatus.Pod
if bindingPod.Spec.NodeName != bindingNode {
// The inconsistency should can be reconciled by K8S Default Scheduler.
panic(internal.NewBadRequestError(fmt.Sprintf(
"Pod binding node mismatch: expected %v, received %v",
podStatus.Pod.Spec.NodeName, bindingNode)))
}
internal.BindPod(s.kClient, bindingPod)
return &ei.ExtenderBindingResult{}
}
// At this point, podState must be in:
// {PodWaiting, PodPreempting}
// The inconsistency should can be reconciled by K8S Default Scheduler.
panic(internal.NewBadRequestError(fmt.Sprintf(
"Pod cannot be bound without a scheduling placement: "+
"Pod current scheduling state %v, received node %v",
podStatus.PodState, bindingNode)))
}
func (s *HivedScheduler) preemptRoutine(args ei.ExtenderPreemptionArgs) *ei.ExtenderPreemptionResult {
s.schedulerLock.RLock()
defer s.schedulerLock.RUnlock()
// Suggested Victims in args can be ignored.
// Preemptor and Victims can be in different namespaces.
pod := args.Pod
logPfx := fmt.Sprintf("[%v]: preemptRoutine: ", internal.Key(pod))
klog.Infof(logPfx + "Started")
defer internal.HandleRoutinePanic(logPfx)
podStatus := s.generalScheduleAdmissionCheck(s.podScheduleStatuses[pod.UID])
if podStatus.PodState == internal.PodBinding {
// The inconsistency should can be reconciled by K8S Default Scheduler.
panic(internal.NewBadRequestError(fmt.Sprintf(
"Pod has already been binding to node %v",
podStatus.Pod.Spec.NodeName)))
} else if podStatus.PodState == internal.PodPreempting {
victims := podStatus.PodScheduleResult.PodPreemptInfo.VictimPods
nodesVictims := map[string]*ei.MetaVictims{}
for _, victim := range victims {
node := victim.Spec.NodeName
if _, ok := nodesVictims[node]; !ok {
nodesVictims[node] = &ei.MetaVictims{}
}
nodeVictims := nodesVictims[node]
if nodeVictims.Pods == nil {
nodeVictims.Pods = []*ei.MetaPod{}
}
nodeVictims.Pods = append(nodeVictims.Pods,
&ei.MetaPod{UID: string(victim.UID)})
}
return &ei.ExtenderPreemptionResult{
NodeNameToMetaVictims: nodesVictims,
}
}
// At this point, podState must be in:
// {PodWaiting}
// The Pod should keep on waiting for preemptible or free resource to appear,
// so do not preempt any victim.
return &ei.ExtenderPreemptionResult{}
}
func (s *HivedScheduler) getAffinityGroups() si.AffinityGroupList {
return s.schedulerAlgorithm.GetAffinityGroups()
}
func (s *HivedScheduler) getAffinityGroup(name string) si.AffinityGroup {
return s.schedulerAlgorithm.GetAffinityGroup(name)
}