Use WithOption to implment scheduler register

This commit is contained in:
Jun Gong 2019-06-25 16:09:54 +08:00
parent e9793c8bdb
commit 107e989454
3 changed files with 23 additions and 24 deletions

View File

@ -7,7 +7,6 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//pkg/scheduler/apis/config:go_default_library",
"//pkg/scheduler/framework/v1alpha1:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/server:go_default_library",
"//staging/src/k8s.io/client-go/informers:go_default_library",
"//staging/src/k8s.io/client-go/informers/core/v1:go_default_library",

View File

@ -26,7 +26,6 @@ import (
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/record"
kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
)
// Config has all the context to run a Scheduler
@ -52,9 +51,6 @@ type Config struct {
// LeaderElection is optional.
LeaderElection *leaderelection.LeaderElectionConfig
// Registry is a collection of all available plugins.
Registry framework.Registry
}
type completedConfig struct {
@ -77,9 +73,6 @@ func (c *Config) Complete() CompletedConfig {
if c.InsecureMetricsServing != nil {
c.InsecureMetricsServing.Name = "metrics"
}
if c.Registry == nil {
c.Registry = framework.NewRegistry()
}
apiserver.AuthorizeClientBearerToken(c.LoopbackClientConfig, &c.Authentication, &c.Authorization)

View File

@ -58,13 +58,11 @@ import (
"k8s.io/klog"
)
// NewSchedulerCommand creates a *cobra.Command object with default parameters
func NewSchedulerCommand() *cobra.Command {
return NewSchedulerCommandWithRegistry(nil)
}
// PluginOption is an function type used for dealing with scheduler registry.
type PluginOption func(framework.Registry) error
// NewSchedulerCommandWithRegistry creates a *cobra.Command object with registry and default parameters
func NewSchedulerCommandWithRegistry(registry framework.Registry) *cobra.Command {
// NewSchedulerCommand creates a *cobra.Command object with default parameters and pluginOptions
func NewSchedulerCommand(pluginOptions ...PluginOption) *cobra.Command {
opts, err := options.NewOptions()
if err != nil {
klog.Fatalf("unable to initialize command options: %v", err)
@ -80,7 +78,7 @@ constraints, affinity and anti-affinity specifications, data locality, inter-wor
interference, deadlines, and so on. Workload-specific requirements will be exposed
through the API as necessary.`,
Run: func(cmd *cobra.Command, args []string) {
if err := runCommand(cmd, args, opts, registry); err != nil {
if err := runCommand(cmd, args, opts, pluginOptions...); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
@ -111,7 +109,7 @@ through the API as necessary.`,
}
// runCommand runs the scheduler.
func runCommand(cmd *cobra.Command, args []string, opts *options.Options, registry framework.Registry) error {
func runCommand(cmd *cobra.Command, args []string, opts *options.Options, pluginOptions ...PluginOption) error {
verflag.PrintAndExitIfRequested()
utilflag.PrintFlags(cmd.Flags())
@ -139,11 +137,6 @@ func runCommand(cmd *cobra.Command, args []string, opts *options.Options, regist
}
stopCh := make(chan struct{})
if registry != nil {
c.Registry = registry
}
// Get the completed config
cc := c.Complete()
@ -161,14 +154,21 @@ func runCommand(cmd *cobra.Command, args []string, opts *options.Options, regist
return fmt.Errorf("unable to register configz: %s", err)
}
return Run(cc, stopCh)
return Run(cc, stopCh, pluginOptions...)
}
// Run executes the scheduler based on the given configuration. It only return on error or when stopCh is closed.
func Run(cc schedulerserverconfig.CompletedConfig, stopCh <-chan struct{}) error {
func Run(cc schedulerserverconfig.CompletedConfig, stopCh <-chan struct{}, pluginOptions ...PluginOption) error {
// To help debugging, immediately log version
klog.V(1).Infof("Starting Kubernetes Scheduler version %+v", version.Get())
registry := framework.NewRegistry()
for _, option := range pluginOptions {
if err := option(registry); err != nil {
return err
}
}
// Create the scheduler.
sched, err := scheduler.New(cc.Client,
cc.InformerFactory.Core().V1().Nodes(),
@ -184,7 +184,7 @@ func Run(cc schedulerserverconfig.CompletedConfig, stopCh <-chan struct{}) error
cc.Recorder,
cc.ComponentConfig.AlgorithmSource,
stopCh,
cc.Registry,
registry,
cc.ComponentConfig.Plugins,
cc.ComponentConfig.PluginConfig,
scheduler.WithName(cc.ComponentConfig.SchedulerName),
@ -335,3 +335,10 @@ func newHealthzHandler(config *kubeschedulerconfig.KubeSchedulerConfiguration, s
}
return pathRecorderMux
}
// WithPlugin creates a PluginOption based on plugin name and factory.
func WithPlugin(name string, factory framework.PluginFactory) PluginOption {
return func(registry framework.Registry) error {
return registry.Register(name, factory)
}
}