Skip to content

Commit 107e989

Browse files
committed
Use WithOption to implment scheduler register
1 parent e9793c8 commit 107e989

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

cmd/kube-scheduler/app/config/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ go_library(
77
visibility = ["//visibility:public"],
88
deps = [
99
"//pkg/scheduler/apis/config:go_default_library",
10-
"//pkg/scheduler/framework/v1alpha1:go_default_library",
1110
"//staging/src/k8s.io/apiserver/pkg/server:go_default_library",
1211
"//staging/src/k8s.io/client-go/informers:go_default_library",
1312
"//staging/src/k8s.io/client-go/informers/core/v1:go_default_library",

cmd/kube-scheduler/app/config/config.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"k8s.io/client-go/tools/leaderelection"
2727
"k8s.io/client-go/tools/record"
2828
kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
29-
framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
3029
)
3130

3231
// Config has all the context to run a Scheduler
@@ -52,9 +51,6 @@ type Config struct {
5251

5352
// LeaderElection is optional.
5453
LeaderElection *leaderelection.LeaderElectionConfig
55-
56-
// Registry is a collection of all available plugins.
57-
Registry framework.Registry
5854
}
5955

6056
type completedConfig struct {
@@ -77,9 +73,6 @@ func (c *Config) Complete() CompletedConfig {
7773
if c.InsecureMetricsServing != nil {
7874
c.InsecureMetricsServing.Name = "metrics"
7975
}
80-
if c.Registry == nil {
81-
c.Registry = framework.NewRegistry()
82-
}
8376

8477
apiserver.AuthorizeClientBearerToken(c.LoopbackClientConfig, &c.Authentication, &c.Authorization)
8578

cmd/kube-scheduler/app/server.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,11 @@ import (
5858
"k8s.io/klog"
5959
)
6060

61-
// NewSchedulerCommand creates a *cobra.Command object with default parameters
62-
func NewSchedulerCommand() *cobra.Command {
63-
return NewSchedulerCommandWithRegistry(nil)
64-
}
61+
// PluginOption is an function type used for dealing with scheduler registry.
62+
type PluginOption func(framework.Registry) error
6563

66-
// NewSchedulerCommandWithRegistry creates a *cobra.Command object with registry and default parameters
67-
func NewSchedulerCommandWithRegistry(registry framework.Registry) *cobra.Command {
64+
// NewSchedulerCommand creates a *cobra.Command object with default parameters and pluginOptions
65+
func NewSchedulerCommand(pluginOptions ...PluginOption) *cobra.Command {
6866
opts, err := options.NewOptions()
6967
if err != nil {
7068
klog.Fatalf("unable to initialize command options: %v", err)
@@ -80,7 +78,7 @@ constraints, affinity and anti-affinity specifications, data locality, inter-wor
8078
interference, deadlines, and so on. Workload-specific requirements will be exposed
8179
through the API as necessary.`,
8280
Run: func(cmd *cobra.Command, args []string) {
83-
if err := runCommand(cmd, args, opts, registry); err != nil {
81+
if err := runCommand(cmd, args, opts, pluginOptions...); err != nil {
8482
fmt.Fprintf(os.Stderr, "%v\n", err)
8583
os.Exit(1)
8684
}
@@ -111,7 +109,7 @@ through the API as necessary.`,
111109
}
112110

113111
// runCommand runs the scheduler.
114-
func runCommand(cmd *cobra.Command, args []string, opts *options.Options, registry framework.Registry) error {
112+
func runCommand(cmd *cobra.Command, args []string, opts *options.Options, pluginOptions ...PluginOption) error {
115113
verflag.PrintAndExitIfRequested()
116114
utilflag.PrintFlags(cmd.Flags())
117115

@@ -139,11 +137,6 @@ func runCommand(cmd *cobra.Command, args []string, opts *options.Options, regist
139137
}
140138

141139
stopCh := make(chan struct{})
142-
143-
if registry != nil {
144-
c.Registry = registry
145-
}
146-
147140
// Get the completed config
148141
cc := c.Complete()
149142

@@ -161,14 +154,21 @@ func runCommand(cmd *cobra.Command, args []string, opts *options.Options, regist
161154
return fmt.Errorf("unable to register configz: %s", err)
162155
}
163156

164-
return Run(cc, stopCh)
157+
return Run(cc, stopCh, pluginOptions...)
165158
}
166159

167160
// Run executes the scheduler based on the given configuration. It only return on error or when stopCh is closed.
168-
func Run(cc schedulerserverconfig.CompletedConfig, stopCh <-chan struct{}) error {
161+
func Run(cc schedulerserverconfig.CompletedConfig, stopCh <-chan struct{}, pluginOptions ...PluginOption) error {
169162
// To help debugging, immediately log version
170163
klog.V(1).Infof("Starting Kubernetes Scheduler version %+v", version.Get())
171164

165+
registry := framework.NewRegistry()
166+
for _, option := range pluginOptions {
167+
if err := option(registry); err != nil {
168+
return err
169+
}
170+
}
171+
172172
// Create the scheduler.
173173
sched, err := scheduler.New(cc.Client,
174174
cc.InformerFactory.Core().V1().Nodes(),
@@ -184,7 +184,7 @@ func Run(cc schedulerserverconfig.CompletedConfig, stopCh <-chan struct{}) error
184184
cc.Recorder,
185185
cc.ComponentConfig.AlgorithmSource,
186186
stopCh,
187-
cc.Registry,
187+
registry,
188188
cc.ComponentConfig.Plugins,
189189
cc.ComponentConfig.PluginConfig,
190190
scheduler.WithName(cc.ComponentConfig.SchedulerName),
@@ -335,3 +335,10 @@ func newHealthzHandler(config *kubeschedulerconfig.KubeSchedulerConfiguration, s
335335
}
336336
return pathRecorderMux
337337
}
338+
339+
// WithPlugin creates a PluginOption based on plugin name and factory.
340+
func WithPlugin(name string, factory framework.PluginFactory) PluginOption {
341+
return func(registry framework.Registry) error {
342+
return registry.Register(name, factory)
343+
}
344+
}

0 commit comments

Comments
 (0)