-
Notifications
You must be signed in to change notification settings - Fork 45
/
scheduler.go
143 lines (127 loc) · 4.5 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2023 Ant Group Co., Ltd.
//
// 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 modules
import (
"context"
"path/filepath"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apiserveroptions "k8s.io/apiserver/pkg/server/options"
componentbaseconfig "k8s.io/component-base/config"
"k8s.io/component-base/logs"
"k8s.io/component-base/metrics"
"k8s.io/kubernetes/cmd/kube-scheduler/app"
"k8s.io/kubernetes/cmd/kube-scheduler/app/options"
kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
pkgcom "github.com/secretflow/kuscia/pkg/common"
"github.com/secretflow/kuscia/pkg/scheduler/kusciascheduling"
"github.com/secretflow/kuscia/pkg/scheduler/queuesort"
"github.com/secretflow/kuscia/pkg/utils/common"
"github.com/secretflow/kuscia/pkg/utils/nlog"
)
type schedulerModule struct {
rootDir string
Kubeconfig string
opts *options.Options
}
func NewScheduler(i *Dependencies) Module {
o := &options.Options{
SecureServing: apiserveroptions.NewSecureServingOptions().WithLoopback(),
Authentication: apiserveroptions.NewDelegatingAuthenticationOptions(),
Authorization: apiserveroptions.NewDelegatingAuthorizationOptions(),
Deprecated: &options.DeprecatedOptions{
PodMaxInUnschedulablePodsDuration: 5 * time.Minute,
},
LeaderElection: &componentbaseconfig.LeaderElectionConfiguration{
LeaderElect: true,
LeaseDuration: metav1.Duration{Duration: 15 * time.Second},
RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
ResourceLock: "leases",
ResourceName: "kube-scheduler",
ResourceNamespace: "kube-system",
},
Metrics: metrics.NewOptions(),
Logs: logs.NewOptions(),
}
o.Authentication.TolerateInClusterLookupFailure = true
o.Authentication.RemoteKubeConfigFileOptional = true
o.Authorization.RemoteKubeConfigFileOptional = true
o.Authorization.RemoteKubeConfigFile = i.KubeconfigFile
o.Authentication.RemoteKubeConfigFile = i.KubeconfigFile
// Set the PairName but leave certificate directory blank to generate in-memory by default
o.SecureServing.ServerCert.CertDirectory = ""
o.SecureServing.ServerCert.PairName = "kube-scheduler"
o.SecureServing.BindPort = kubeschedulerconfig.DefaultKubeSchedulerPort
o.Master = i.ApiserverEndpoint
return &schedulerModule{
rootDir: i.RootDir,
Kubeconfig: i.KubeconfigFile,
opts: o,
}
}
func (s *schedulerModule) Run(ctx context.Context) error {
configPathTmpl := filepath.Join(s.rootDir, pkgcom.ConfPrefix, "scheduler-config.yaml.tmpl")
configPath := filepath.Join(s.rootDir, pkgcom.ConfPrefix, "scheduler-config.yaml")
if err := common.RenderConfig(configPathTmpl, configPath, s); err != nil {
return err
}
s.opts.ConfigFile = configPath
cc, sched, err := app.Setup(
ctx,
s.opts,
app.WithPlugin(kusciascheduling.Name, kusciascheduling.New),
app.WithPlugin(queuesort.Name, queuesort.New))
if err != nil {
nlog.Error(err)
return err
}
return app.Run(ctx, cc, sched)
}
func (s *schedulerModule) WaitReady(ctx context.Context) error {
return ctx.Err()
}
func (s *schedulerModule) Name() string {
return "kusciascheduler"
}
func RunSchedulerWithDestroy(conf *Dependencies) {
runCtx, cancel := context.WithCancel(context.Background())
shutdownEntry := NewShutdownHookEntry(1 * time.Second)
conf.RegisterDestroyFunc(DestroyFunc{
Name: "kusciascheduler",
DestroyCh: runCtx.Done(),
DestroyFn: cancel,
ShutdownHookEntry: shutdownEntry,
})
RunScheduler(runCtx, cancel, conf, shutdownEntry)
}
func RunScheduler(ctx context.Context, cancel context.CancelFunc, conf *Dependencies, shutdownEntry *shutdownHookEntry) Module {
m := NewScheduler(conf)
go func() {
defer func() {
if shutdownEntry != nil {
shutdownEntry.RunShutdown()
}
}()
if err := m.Run(ctx); err != nil {
nlog.Error(err)
cancel()
}
}()
if err := m.WaitReady(ctx); err != nil {
nlog.Fatalf("Scheduler wait ready failed: %v", err)
}
nlog.Info("scheduler is ready")
return m
}