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"], visibility = ["//visibility:public"],
deps = [ deps = [
"//pkg/scheduler/apis/config:go_default_library", "//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/apiserver/pkg/server:go_default_library",
"//staging/src/k8s.io/client-go/informers: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", "//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/leaderelection"
"k8s.io/client-go/tools/record" "k8s.io/client-go/tools/record"
kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config" 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 // Config has all the context to run a Scheduler
@ -52,9 +51,6 @@ type Config struct {
// LeaderElection is optional. // LeaderElection is optional.
LeaderElection *leaderelection.LeaderElectionConfig LeaderElection *leaderelection.LeaderElectionConfig
// Registry is a collection of all available plugins.
Registry framework.Registry
} }
type completedConfig struct { type completedConfig struct {
@ -77,9 +73,6 @@ func (c *Config) Complete() CompletedConfig {
if c.InsecureMetricsServing != nil { if c.InsecureMetricsServing != nil {
c.InsecureMetricsServing.Name = "metrics" c.InsecureMetricsServing.Name = "metrics"
} }
if c.Registry == nil {
c.Registry = framework.NewRegistry()
}
apiserver.AuthorizeClientBearerToken(c.LoopbackClientConfig, &c.Authentication, &c.Authorization) apiserver.AuthorizeClientBearerToken(c.LoopbackClientConfig, &c.Authentication, &c.Authorization)

View File

@ -58,13 +58,11 @@ import (
"k8s.io/klog" "k8s.io/klog"
) )
// NewSchedulerCommand creates a *cobra.Command object with default parameters // PluginOption is an function type used for dealing with scheduler registry.
func NewSchedulerCommand() *cobra.Command { type PluginOption func(framework.Registry) error
return NewSchedulerCommandWithRegistry(nil)
}
// NewSchedulerCommandWithRegistry creates a *cobra.Command object with registry and default parameters // NewSchedulerCommand creates a *cobra.Command object with default parameters and pluginOptions
func NewSchedulerCommandWithRegistry(registry framework.Registry) *cobra.Command { func NewSchedulerCommand(pluginOptions ...PluginOption) *cobra.Command {
opts, err := options.NewOptions() opts, err := options.NewOptions()
if err != nil { if err != nil {
klog.Fatalf("unable to initialize command options: %v", err) 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 interference, deadlines, and so on. Workload-specific requirements will be exposed
through the API as necessary.`, through the API as necessary.`,
Run: func(cmd *cobra.Command, args []string) { 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) fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1) os.Exit(1)
} }
@ -111,7 +109,7 @@ through the API as necessary.`,
} }
// runCommand runs the scheduler. // 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() verflag.PrintAndExitIfRequested()
utilflag.PrintFlags(cmd.Flags()) utilflag.PrintFlags(cmd.Flags())
@ -139,11 +137,6 @@ func runCommand(cmd *cobra.Command, args []string, opts *options.Options, regist
} }
stopCh := make(chan struct{}) stopCh := make(chan struct{})
if registry != nil {
c.Registry = registry
}
// Get the completed config // Get the completed config
cc := c.Complete() 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 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. // 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 // To help debugging, immediately log version
klog.V(1).Infof("Starting Kubernetes Scheduler version %+v", version.Get()) 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. // Create the scheduler.
sched, err := scheduler.New(cc.Client, sched, err := scheduler.New(cc.Client,
cc.InformerFactory.Core().V1().Nodes(), cc.InformerFactory.Core().V1().Nodes(),
@ -184,7 +184,7 @@ func Run(cc schedulerserverconfig.CompletedConfig, stopCh <-chan struct{}) error
cc.Recorder, cc.Recorder,
cc.ComponentConfig.AlgorithmSource, cc.ComponentConfig.AlgorithmSource,
stopCh, stopCh,
cc.Registry, registry,
cc.ComponentConfig.Plugins, cc.ComponentConfig.Plugins,
cc.ComponentConfig.PluginConfig, cc.ComponentConfig.PluginConfig,
scheduler.WithName(cc.ComponentConfig.SchedulerName), scheduler.WithName(cc.ComponentConfig.SchedulerName),
@ -335,3 +335,10 @@ func newHealthzHandler(config *kubeschedulerconfig.KubeSchedulerConfiguration, s
} }
return pathRecorderMux 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)
}
}