Skip to content

Commit c30f024

Browse files
authored
Merge pull request kubernetes#78162 from hex108/registry
Add support for writing out of tree custom scheduler plugins
2 parents 7163dcb + 5828223 commit c30f024

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

cmd/kube-scheduler/app/server.go

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

61-
// NewSchedulerCommand creates a *cobra.Command object with default parameters
62-
func NewSchedulerCommand() *cobra.Command {
61+
// Option configures a framework.Registry.
62+
type Option func(framework.Registry) error
63+
64+
// NewSchedulerCommand creates a *cobra.Command object with default parameters and registryOptions
65+
func NewSchedulerCommand(registryOptions ...Option) *cobra.Command {
6366
opts, err := options.NewOptions()
6467
if err != nil {
6568
klog.Fatalf("unable to initialize command options: %v", err)
@@ -75,7 +78,7 @@ constraints, affinity and anti-affinity specifications, data locality, inter-wor
7578
interference, deadlines, and so on. Workload-specific requirements will be exposed
7679
through the API as necessary.`,
7780
Run: func(cmd *cobra.Command, args []string) {
78-
if err := runCommand(cmd, args, opts); err != nil {
81+
if err := runCommand(cmd, args, opts, registryOptions...); err != nil {
7982
fmt.Fprintf(os.Stderr, "%v\n", err)
8083
os.Exit(1)
8184
}
@@ -106,7 +109,7 @@ through the API as necessary.`,
106109
}
107110

108111
// runCommand runs the scheduler.
109-
func runCommand(cmd *cobra.Command, args []string, opts *options.Options) error {
112+
func runCommand(cmd *cobra.Command, args []string, opts *options.Options, registryOptions ...Option) error {
110113
verflag.PrintAndExitIfRequested()
111114
utilflag.PrintFlags(cmd.Flags())
112115

@@ -134,7 +137,6 @@ func runCommand(cmd *cobra.Command, args []string, opts *options.Options) error
134137
}
135138

136139
stopCh := make(chan struct{})
137-
138140
// Get the completed config
139141
cc := c.Complete()
140142

@@ -152,14 +154,21 @@ func runCommand(cmd *cobra.Command, args []string, opts *options.Options) error
152154
return fmt.Errorf("unable to register configz: %s", err)
153155
}
154156

155-
return Run(cc, stopCh)
157+
return Run(cc, stopCh, registryOptions...)
156158
}
157159

158160
// Run executes the scheduler based on the given configuration. It only return on error or when stopCh is closed.
159-
func Run(cc schedulerserverconfig.CompletedConfig, stopCh <-chan struct{}) error {
161+
func Run(cc schedulerserverconfig.CompletedConfig, stopCh <-chan struct{}, registryOptions ...Option) error {
160162
// To help debugging, immediately log version
161163
klog.V(1).Infof("Starting Kubernetes Scheduler version %+v", version.Get())
162164

165+
registry := framework.NewRegistry()
166+
for _, option := range registryOptions {
167+
if err := option(registry); err != nil {
168+
return err
169+
}
170+
}
171+
163172
// Create the scheduler.
164173
sched, err := scheduler.New(cc.Client,
165174
cc.InformerFactory.Core().V1().Nodes(),
@@ -176,7 +185,7 @@ func Run(cc schedulerserverconfig.CompletedConfig, stopCh <-chan struct{}) error
176185
cc.Recorder,
177186
cc.ComponentConfig.AlgorithmSource,
178187
stopCh,
179-
framework.NewRegistry(),
188+
registry,
180189
cc.ComponentConfig.Plugins,
181190
cc.ComponentConfig.PluginConfig,
182191
scheduler.WithName(cc.ComponentConfig.SchedulerName),
@@ -328,3 +337,10 @@ func newHealthzHandler(config *kubeschedulerconfig.KubeSchedulerConfiguration, s
328337
}
329338
return pathRecorderMux
330339
}
340+
341+
// WithPlugin creates an Option based on plugin name and factory.
342+
func WithPlugin(name string, factory framework.PluginFactory) Option {
343+
return func(registry framework.Registry) error {
344+
return registry.Register(name, factory)
345+
}
346+
}

0 commit comments

Comments
 (0)