NewSchedulerFromInterface implementation

This commit is contained in:
jayunit100
2017-01-20 14:32:09 -05:00
parent 54ff406702
commit 8439f81f2d
3 changed files with 86 additions and 46 deletions

View File

@@ -51,6 +51,10 @@ type Scheduler struct {
config *Config
}
func (sched *Scheduler) StopEverything() {
close(sched.config.StopEverything)
}
// These are the functions which need to be provided in order to build a Scheduler configuration.
// An implementation of this can be seen in factory.go.
type Configurator interface {
@@ -78,6 +82,7 @@ type Configurator interface {
CreateFromKeys(predicateKeys, priorityKeys sets.String, extenders []algorithm.SchedulerExtender) (*Config, error)
}
// TODO over time we should make this struct a hidden implementation detail of the scheduler.
type Config struct {
// It is expected that changes made via SchedulerCache will be observed
// by NodeLister and Algorithm.
@@ -108,6 +113,7 @@ type Config struct {
}
// New returns a new scheduler.
// TODO replace this with NewFromConfigurator.
func New(c *Config) *Scheduler {
s := &Scheduler{
config: c,
@@ -116,6 +122,25 @@ func New(c *Config) *Scheduler {
return s
}
// NewFromConfigurator returns a new scheduler that is created entirely by the Configurator. Assumes Create() is implemented.
// Supports intermediate Config mutation for now if you provide modifier functions which will run after Config is created.
func NewFromConfigurator(c Configurator, modifiers ...func(c *Config)) (*Scheduler, error) {
cfg, err := c.Create()
if err != nil {
return nil, err
}
// Mutate it if any functions were provided, changes might be required for certain types of tests (i.e. change the recorder).
for _, modifier := range modifiers {
modifier(cfg)
}
// From this point on the config is immutable to the outside.
s := &Scheduler{
config: cfg,
}
metrics.Register()
return s, nil
}
// Run begins watching and scheduling. It starts a goroutine and returns immediately.
func (s *Scheduler) Run() {
go wait.Until(s.scheduleOne, 0, s.config.StopEverything)