forked from volcano-sh/volcano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.go
106 lines (87 loc) · 2.85 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
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package scheduler
import (
"time"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/rest"
"k8s.io/klog"
schedcache "volcano.sh/volcano/pkg/scheduler/cache"
"volcano.sh/volcano/pkg/scheduler/conf"
"volcano.sh/volcano/pkg/scheduler/framework"
"volcano.sh/volcano/pkg/scheduler/metrics"
)
// Scheduler watches for new unscheduled pods for volcano. It attempts to find
// nodes that they fit on and writes bindings back to the api server.
type Scheduler struct {
cache schedcache.Cache
config *rest.Config
actions []framework.Action
plugins []conf.Tier
configurations []conf.Configuration
schedulerConf string
schedulePeriod time.Duration
}
// NewScheduler returns a scheduler
func NewScheduler(
config *rest.Config,
schedulerName string,
conf string,
period time.Duration,
defaultQueue string,
) (*Scheduler, error) {
scheduler := &Scheduler{
config: config,
schedulerConf: conf,
cache: schedcache.New(config, schedulerName, defaultQueue),
schedulePeriod: period,
}
return scheduler, nil
}
// Run runs the Scheduler
func (pc *Scheduler) Run(stopCh <-chan struct{}) {
// Start cache for policy.
go pc.cache.Run(stopCh)
pc.cache.WaitForCacheSync(stopCh)
go wait.Until(pc.runOnce, pc.schedulePeriod, stopCh)
}
func (pc *Scheduler) runOnce() {
klog.V(4).Infof("Start scheduling ...")
scheduleStartTime := time.Now()
defer klog.V(4).Infof("End scheduling ...")
defer metrics.UpdateE2eDuration(metrics.Duration(scheduleStartTime))
pc.loadSchedulerConf()
ssn := framework.OpenSession(pc.cache, pc.plugins, pc.configurations)
defer framework.CloseSession(ssn)
for _, action := range pc.actions {
actionStartTime := time.Now()
action.Execute(ssn)
metrics.UpdateActionDuration(action.Name(), metrics.Duration(actionStartTime))
}
}
func (pc *Scheduler) loadSchedulerConf() {
var err error
// Load configuration of scheduler
schedConf := defaultSchedulerConf
if len(pc.schedulerConf) != 0 {
if schedConf, err = readSchedulerConf(pc.schedulerConf); err != nil {
klog.Errorf("Failed to read scheduler configuration '%s', using default configuration: %v",
pc.schedulerConf, err)
schedConf = defaultSchedulerConf
}
}
pc.actions, pc.plugins, pc.configurations, err = loadSchedulerConf(schedConf)
if err != nil {
panic(err)
}
}