feat(scheduler): use context in the scheduler package

+ Use context instead of stopCh
+ Add context to the scheduling framework interface
This commit is contained in:
draveness
2019-08-28 19:12:02 +08:00
parent f7091992c0
commit 47a6c5b693
42 changed files with 426 additions and 352 deletions

View File

@@ -17,6 +17,7 @@ limitations under the License.
package util
import (
"context"
"net/http"
"net/http/httptest"
@@ -59,30 +60,31 @@ func StartApiserver() (string, ShutdownFunc) {
// StartScheduler configures and starts a scheduler given a handle to the clientSet interface
// and event broadcaster. It returns the running scheduler and the shutdown function to stop it.
func StartScheduler(clientSet clientset.Interface) (*scheduler.Scheduler, coreinformers.PodInformer, ShutdownFunc) {
ctx, cancel := context.WithCancel(context.Background())
informerFactory := informers.NewSharedInformerFactory(clientSet, 0)
podInformer := informerFactory.Core().V1().Pods()
stopCh := make(chan struct{})
evtBroadcaster := events.NewBroadcaster(&events.EventSinkImpl{
Interface: clientSet.EventsV1beta1().Events("")})
evtBroadcaster.StartRecordingToSink(stopCh)
evtBroadcaster.StartRecordingToSink(ctx.Done())
recorder := evtBroadcaster.NewRecorder(
legacyscheme.Scheme,
v1.DefaultSchedulerName,
)
sched, err := createScheduler(clientSet, informerFactory, podInformer, recorder, stopCh)
sched, err := createScheduler(clientSet, informerFactory, podInformer, recorder, ctx.Done())
if err != nil {
klog.Fatalf("Error creating scheduler: %v", err)
}
informerFactory.Start(stopCh)
sched.Run()
informerFactory.Start(ctx.Done())
go sched.Run(ctx)
shutdownFunc := func() {
klog.Infof("destroying scheduler")
close(stopCh)
cancel()
klog.Infof("destroyed scheduler")
}
return sched, podInformer, shutdownFunc