Pass registry parameter to scheduler instead of global singleton

This commit is contained in:
Jun Gong 2019-06-24 11:09:25 +08:00
parent ea8e1e01c5
commit e9793c8bdb
3 changed files with 20 additions and 11 deletions

View File

@ -7,6 +7,7 @@ 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,6 +26,7 @@ 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
@ -51,6 +52,9 @@ type Config struct {
// LeaderElection is optional.
LeaderElection *leaderelection.LeaderElectionConfig
// Registry is a collection of all available plugins.
Registry framework.Registry
}
type completedConfig struct {
@ -73,6 +77,9 @@ 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,10 +58,13 @@ import (
"k8s.io/klog"
)
var frameworkRegistry = framework.NewRegistry()
// NewSchedulerCommand creates a *cobra.Command object with default parameters
func NewSchedulerCommand() *cobra.Command {
return NewSchedulerCommandWithRegistry(nil)
}
// NewSchedulerCommandWithRegistry creates a *cobra.Command object with registry and default parameters
func NewSchedulerCommandWithRegistry(registry framework.Registry) *cobra.Command {
opts, err := options.NewOptions()
if err != nil {
klog.Fatalf("unable to initialize command options: %v", err)
@ -77,7 +80,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); err != nil {
if err := runCommand(cmd, args, opts, registry); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
@ -108,7 +111,7 @@ through the API as necessary.`,
}
// runCommand runs the scheduler.
func runCommand(cmd *cobra.Command, args []string, opts *options.Options) error {
func runCommand(cmd *cobra.Command, args []string, opts *options.Options, registry framework.Registry) error {
verflag.PrintAndExitIfRequested()
utilflag.PrintFlags(cmd.Flags())
@ -137,6 +140,10 @@ func runCommand(cmd *cobra.Command, args []string, opts *options.Options) error
stopCh := make(chan struct{})
if registry != nil {
c.Registry = registry
}
// Get the completed config
cc := c.Complete()
@ -177,7 +184,7 @@ func Run(cc schedulerserverconfig.CompletedConfig, stopCh <-chan struct{}) error
cc.Recorder,
cc.ComponentConfig.AlgorithmSource,
stopCh,
frameworkRegistry,
cc.Registry,
cc.ComponentConfig.Plugins,
cc.ComponentConfig.PluginConfig,
scheduler.WithName(cc.ComponentConfig.SchedulerName),
@ -328,9 +335,3 @@ func newHealthzHandler(config *kubeschedulerconfig.KubeSchedulerConfiguration, s
}
return pathRecorderMux
}
// RegisterFrameworkPlugin adds a new plugin to the registry. If a plugin with the same name
// exists, it returns an error.
func RegisterFrameworkPlugin(name string, factory framework.PluginFactory) error {
return frameworkRegistry.Register(name, factory)
}