Skip to content

Commit 2b95900

Browse files
committed
kube-scheduler: allow deprecated options to be set with configfile
1 parent cb37c08 commit 2b95900

File tree

3 files changed

+60
-22
lines changed

3 files changed

+60
-22
lines changed

cmd/kube-scheduler/app/options/deprecated.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ func (o *DeprecatedOptions) AddFlags(fs *pflag.FlagSet, cfg *kubeschedulerconfig
4747
return
4848
}
4949

50-
fs.StringVar(&o.AlgorithmProvider, "algorithm-provider", o.AlgorithmProvider, "DEPRECATED: the scheduling algorithm provider to use, one of: "+algorithmprovider.ListAlgorithmProviders())
51-
fs.StringVar(&o.PolicyConfigFile, "policy-config-file", o.PolicyConfigFile, "DEPRECATED: file with scheduler policy configuration. This file is used if policy ConfigMap is not provided or --use-legacy-policy-config=true")
52-
usage := fmt.Sprintf("DEPRECATED: name of the ConfigMap object that contains scheduler's policy configuration. It must exist in the system namespace before scheduler initialization if --use-legacy-policy-config=false. The config must be provided as the value of an element in 'Data' map with the key='%v'", kubeschedulerconfig.SchedulerPolicyConfigMapKey)
50+
fs.StringVar(&o.AlgorithmProvider, "algorithm-provider", o.AlgorithmProvider, "DEPRECATED: the scheduling algorithm provider to use, this sets the default plugins for component config profiles. Choose one of: "+algorithmprovider.ListAlgorithmProviders())
51+
fs.StringVar(&o.PolicyConfigFile, "policy-config-file", o.PolicyConfigFile, "DEPRECATED: file with scheduler policy configuration. This file is used if policy ConfigMap is not provided or --use-legacy-policy-config=true. Note: The scheduler will fail if this is combined with Plugin configs")
52+
usage := fmt.Sprintf("DEPRECATED: name of the ConfigMap object that contains scheduler's policy configuration. It must exist in the system namespace before scheduler initialization if --use-legacy-policy-config=false. The config must be provided as the value of an element in 'Data' map with the key='%v'. Note: The scheduler will fail if this is combined with Plugin configs", kubeschedulerconfig.SchedulerPolicyConfigMapKey)
5353
fs.StringVar(&o.PolicyConfigMapName, "policy-configmap", o.PolicyConfigMapName, usage)
54-
fs.StringVar(&o.PolicyConfigMapNamespace, "policy-configmap-namespace", o.PolicyConfigMapNamespace, "DEPRECATED: the namespace where policy ConfigMap is located. The kube-system namespace will be used if this is not provided or is empty.")
55-
fs.BoolVar(&o.UseLegacyPolicyConfig, "use-legacy-policy-config", o.UseLegacyPolicyConfig, "DEPRECATED: when set to true, scheduler will ignore policy ConfigMap and uses policy config file")
54+
fs.StringVar(&o.PolicyConfigMapNamespace, "policy-configmap-namespace", o.PolicyConfigMapNamespace, "DEPRECATED: the namespace where policy ConfigMap is located. The kube-system namespace will be used if this is not provided or is empty. Note: The scheduler will fail if this is combined with Plugin configs")
55+
fs.BoolVar(&o.UseLegacyPolicyConfig, "use-legacy-policy-config", o.UseLegacyPolicyConfig, "DEPRECATED: when set to true, scheduler will ignore policy ConfigMap and uses policy config file. Note: The scheduler will fail if this is combined with Plugin configs")
5656

5757
fs.BoolVar(&cfg.EnableProfiling, "profiling", cfg.EnableProfiling, "DEPRECATED: enable profiling via web interface host:port/debug/pprof/")
5858
fs.BoolVar(&cfg.EnableContentionProfiling, "contention-profiling", cfg.EnableContentionProfiling, "DEPRECATED: enable lock contention profiling, if profiling is enabled")
@@ -87,16 +87,14 @@ func (o *DeprecatedOptions) Validate() []error {
8787
return errs
8888
}
8989

90-
// ApplyTo sets cfg.AlgorithmSource from flags passed on the command line in the following precedence order:
90+
// ApplyAlgorithmSourceTo sets cfg.AlgorithmSource from flags passed on the command line in the following precedence order:
9191
//
9292
// 1. --use-legacy-policy-config to use a policy file.
9393
// 2. --policy-configmap to use a policy config map value.
9494
// 3. --algorithm-provider to use a named algorithm provider.
95-
//
96-
// This function is only called when no config file is provided.
97-
func (o *DeprecatedOptions) ApplyTo(cfg *kubeschedulerconfig.KubeSchedulerConfiguration) error {
95+
func (o *DeprecatedOptions) ApplyAlgorithmSourceTo(cfg *kubeschedulerconfig.KubeSchedulerConfiguration) {
9896
if o == nil {
99-
return nil
97+
return
10098
}
10199

102100
switch {
@@ -122,10 +120,19 @@ func (o *DeprecatedOptions) ApplyTo(cfg *kubeschedulerconfig.KubeSchedulerConfig
122120
Provider: &o.AlgorithmProvider,
123121
}
124122
}
123+
}
125124

126-
// Deprecated flags have an effect iff no config file was provided, in which
127-
// case this function expects a default KubeSchedulerConfiguration instance,
128-
// which has a single profile.
125+
// ApplyTo sets a default profile plugin config if no config file is specified
126+
// It also calls ApplyAlgorithmSourceTo to set Policy settings in AlgorithmSource, if applicable.
127+
// Deprecated flags have an effect iff no config file was provided, in which
128+
// case this function expects a default KubeSchedulerConfiguration instance,
129+
// which has a single profile.
130+
func (o *DeprecatedOptions) ApplyTo(cfg *kubeschedulerconfig.KubeSchedulerConfiguration) {
131+
if o == nil {
132+
return
133+
}
134+
// The following deprecated options affect the only existing profile that is
135+
// added by default.
129136
profile := &cfg.Profiles[0]
130137
if len(o.SchedulerName) > 0 {
131138
profile.SchedulerName = o.SchedulerName
@@ -136,6 +143,7 @@ func (o *DeprecatedOptions) ApplyTo(cfg *kubeschedulerconfig.KubeSchedulerConfig
136143
HardPodAffinityWeight: o.HardPodAffinitySymmetricWeight,
137144
},
138145
}
146+
139147
profile.PluginConfig = append(profile.PluginConfig, plCfg)
140-
return nil
148+
o.ApplyAlgorithmSourceTo(cfg)
141149
}

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,13 @@ func newDefaultComponentConfig() (*kubeschedulerconfig.KubeSchedulerConfiguratio
148148
// Flags returns flags for a specific scheduler by section name
149149
func (o *Options) Flags() (nfs cliflag.NamedFlagSets) {
150150
fs := nfs.FlagSet("misc")
151-
fs.StringVar(&o.ConfigFile, "config", o.ConfigFile, "The path to the configuration file. Flags override values in this file.")
151+
fs.StringVar(&o.ConfigFile, "config", o.ConfigFile, `The path to the configuration file. The following flags can overwrite fields in this file:
152+
--address
153+
--port
154+
--use-legacy-policy-config
155+
--policy-configmap
156+
--policy-config-file
157+
--algorithm-provider`)
152158
fs.StringVar(&o.WriteConfigTo, "write-config-to", o.WriteConfigTo, "If set, write the configuration values to this file and exit.")
153159
fs.StringVar(&o.Master, "master", o.Master, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
154160

@@ -170,10 +176,8 @@ func (o *Options) ApplyTo(c *schedulerappconfig.Config) error {
170176
if len(o.ConfigFile) == 0 {
171177
c.ComponentConfig = o.ComponentConfig
172178

173-
// only apply deprecated flags if no config file is loaded (this is the old behaviour).
174-
if err := o.Deprecated.ApplyTo(&c.ComponentConfig); err != nil {
175-
return err
176-
}
179+
// apply deprecated flags if no config file is loaded (this is the old behaviour).
180+
o.Deprecated.ApplyTo(&c.ComponentConfig)
177181
if err := o.CombinedInsecureServing.ApplyTo(c, &c.ComponentConfig); err != nil {
178182
return err
179183
}
@@ -186,11 +190,18 @@ func (o *Options) ApplyTo(c *schedulerappconfig.Config) error {
186190
return err
187191
}
188192

189-
// use the loaded config file only, with the exception of --address and --port. This means that
190-
// none of the deprecated flags in o.Deprecated are taken into consideration. This is the old
191-
// behaviour of the flags we have to keep.
192193
c.ComponentConfig = *cfg
193194

195+
// apply any deprecated Policy flags, if applicable
196+
o.Deprecated.ApplyAlgorithmSourceTo(&c.ComponentConfig)
197+
198+
// if the user has set CC profiles and is trying to use a Policy config, error out
199+
// these configs are no longer merged and they should not be used simultaneously
200+
if !emptySchedulerProfileConfig(c.ComponentConfig.Profiles) && c.ComponentConfig.AlgorithmSource.Policy != nil {
201+
return fmt.Errorf("cannot set a Plugin config and Policy config")
202+
}
203+
204+
// use the loaded config file only, with the exception of --address and --port.
194205
if err := o.CombinedInsecureServing.ApplyToFromLoadedConfig(c, &c.ComponentConfig); err != nil {
195206
return err
196207
}
@@ -211,6 +222,15 @@ func (o *Options) ApplyTo(c *schedulerappconfig.Config) error {
211222
return nil
212223
}
213224

225+
// emptySchedulerProfileConfig returns true if the list of profiles passed to it contains only
226+
// the "default-scheduler" profile with no plugins or pluginconfigs registered
227+
// (this is the default empty profile initialized by defaults.go)
228+
func emptySchedulerProfileConfig(profiles []kubeschedulerconfig.KubeSchedulerProfile) bool {
229+
return len(profiles) == 1 &&
230+
len(profiles[0].PluginConfig) == 0 &&
231+
profiles[0].Plugins == nil
232+
}
233+
214234
// Validate validates all the required options.
215235
func (o *Options) Validate() []error {
216236
var errs []error

cmd/kube-scheduler/app/options/options_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,16 @@ profiles:
721721
},
722722
},
723723
},
724+
{
725+
name: "Attempting to set Component Config Profiles and Policy config",
726+
options: &Options{
727+
ConfigFile: pluginConfigFile,
728+
Deprecated: &DeprecatedOptions{
729+
PolicyConfigMapName: "bar",
730+
},
731+
},
732+
expectedError: "cannot set a Plugin config and Policy config",
733+
},
724734
{
725735
name: "unknown field",
726736
options: &Options{

0 commit comments

Comments
 (0)