// TODO: once we switch everything over to Cobra commands, we can go back to calling // utilflag.InitFlags() (by removing its pflag.Parse() call). For now, we have to set the // normalize func and add the go flag set by hand. pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc) // utilflag.InitFlags() logs.InitLogs() defer logs.FlushLogs()
// 启动所有 informers. go cc.PodInformer.Informer().Run(stopCh) cc.InformerFactory.Start(stopCh)
// Wait for all caches to sync before scheduling. cc.InformerFactory.WaitForCacheSync(stopCh) controller.WaitForCacheSync("scheduler", stopCh, cc.PodInformer.Informer().HasSynced)
// Prepare a reusable runCommand function. run := func(ctx context.Context) { sched.Run() <-ctx.Done() }
ctx, cancel := context.WithCancel(context.TODO()) // TODO once Run() accepts a context, it should be used here defer cancel()
// Leader election is disabled, so runCommand inline until done. run(ctx) return fmt.Errorf("finished without leader elect") }
// scheduleOne does the entire scheduling workflow for a single pod. It is serialized on the scheduling algorithm's host fitting. func (sched *Scheduler) scheduleOne() {
// 从待调度队列中取出一个待调度的pod,此处无数据时会阻塞 pod := sched.config.NextPod() // Synchronously attempt to find a fit for the pod. start := time.Now() // 执行调度方法,返回调度结果 scheduleResult, err := sched.schedule(pod) // 如果调度出错,则可能进入抢占逻辑。 // 无论什么类型错误,进入if后都会return,结束本轮调度 if err != nil { // 如果错误类型是 FitError if fitError, ok := err.(*core.FitError); ok { // 如果不允许抢占,则报错 if !util.PodPriorityEnabled() || sched.config.DisablePreemption { klog.V(3).Infof("Pod priority feature is not enabled or preemption is disabled by scheduler configuration." + " No preemption is performed.") } else { // 否则,进入抢占 preemptionStartTime := time.Now() sched.preempt(pod, fitError) } } else { // 否则,报错 klog.Errorf("error selecting node for pod: %v", err) metrics.PodScheduleErrors.Inc() } return } // ... 省略调度成功后绑定volume等操作 }
trace.Step("Prioritizing") startPriorityEvalTime := time.Now() // When only one node after predicate, just use it. iflen(filteredNodes) == 1 { return ScheduleResult{ SuggestedHost: filteredNodes[0].Name, EvaluatedNodes: 1 + len(failedPredicateMap), FeasibleNodes: 1, }, nil }