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 scheduler
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
@@ -67,7 +68,8 @@ type testContext struct {
clientSet *clientset.Clientset
informerFactory informers.SharedInformerFactory
scheduler *scheduler.Scheduler
stopCh chan struct{}
ctx context.Context
cancelFn context.CancelFunc
}
func createAlgorithmSourceFromPolicy(policy *schedulerapi.Policy, clientSet clientset.Interface) schedulerconfig.SchedulerAlgorithmSource {
@@ -93,8 +95,10 @@ func createAlgorithmSourceFromPolicy(policy *schedulerapi.Policy, clientSet clie
// initTestMasterAndScheduler initializes a test environment and creates a master with default
// configuration.
func initTestMaster(t *testing.T, nsPrefix string, admission admission.Interface) *testContext {
ctx, cancelFunc := context.WithCancel(context.Background())
context := testContext{
stopCh: make(chan struct{}),
ctx: ctx,
cancelFn: cancelFunc,
}
// 1. Create master
@@ -187,7 +191,7 @@ func initTestSchedulerWithOptions(
podInformer,
recorder,
algorithmSrc,
context.stopCh,
context.ctx.Done(),
opts...,
)
@@ -207,7 +211,7 @@ func initTestSchedulerWithOptions(
context.informerFactory.Start(context.scheduler.StopEverything)
context.informerFactory.WaitForCacheSync(context.scheduler.StopEverything)
context.scheduler.Run()
go context.scheduler.Run(context.ctx)
return context
}
@@ -261,7 +265,7 @@ func initTestDisablePreemption(t *testing.T, nsPrefix string) *testContext {
// at the end of a test.
func cleanupTest(t *testing.T, context *testContext) {
// Kill the scheduler.
close(context.stopCh)
context.cancelFn()
// Cleanup nodes.
context.clientSet.CoreV1().Nodes().DeleteCollection(nil, metav1.ListOptions{})
framework.DeleteTestingNamespace(context.ns, context.httpServer, t)