mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 05:27:21 +00:00
Merge pull request #105924 from Huang-Wei/revert-105712
Revert "sched: ensure --leader-elect* CLI args are honored"
This commit is contained in:
commit
8bdf6af714
@ -67,26 +67,17 @@ type Options struct {
|
|||||||
WriteConfigTo string
|
WriteConfigTo string
|
||||||
|
|
||||||
Master string
|
Master string
|
||||||
|
|
||||||
// flags hold the parsed CLI flags.
|
|
||||||
flags *cliflag.NamedFlagSets
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewOptions returns default scheduler app options.
|
// NewOptions returns default scheduler app options.
|
||||||
func NewOptions() (*Options, error) {
|
func NewOptions() (*Options, error) {
|
||||||
cfg, err := latest.Default()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
o := &Options{
|
o := &Options{
|
||||||
ComponentConfig: *cfg,
|
SecureServing: apiserveroptions.NewSecureServingOptions().WithLoopback(),
|
||||||
SecureServing: apiserveroptions.NewSecureServingOptions().WithLoopback(),
|
Authentication: apiserveroptions.NewDelegatingAuthenticationOptions(),
|
||||||
Authentication: apiserveroptions.NewDelegatingAuthenticationOptions(),
|
Authorization: apiserveroptions.NewDelegatingAuthorizationOptions(),
|
||||||
Authorization: apiserveroptions.NewDelegatingAuthorizationOptions(),
|
Deprecated: &DeprecatedOptions{},
|
||||||
Deprecated: &DeprecatedOptions{},
|
Metrics: metrics.NewOptions(),
|
||||||
Metrics: metrics.NewOptions(),
|
Logs: logs.NewOptions(),
|
||||||
Logs: logs.NewOptions(),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
o.Authentication.TolerateInClusterLookupFailure = true
|
o.Authentication.TolerateInClusterLookupFailure = true
|
||||||
@ -101,11 +92,42 @@ func NewOptions() (*Options, error) {
|
|||||||
return o, nil
|
return o, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complete obtains the CLI args related with leaderelection, and override the values in `cfg`.
|
// Complete completes the remaining instantiation of the options obj.
|
||||||
// Then the `cfg` object is injected into the `options` object.
|
// In particular, it injects the latest internal versioned ComponentConfig.
|
||||||
func (o *Options) Complete(cfg *kubeschedulerconfig.KubeSchedulerConfiguration) {
|
func (o *Options) Complete(nfs *cliflag.NamedFlagSets) error {
|
||||||
// Obtain CLI args related with leaderelection. Set them to `cfg` if specified in command line.
|
cfg, err := latest.Default()
|
||||||
leaderelection := o.Flags().FlagSet("leader election")
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtain deprecated CLI args. Set them to cfg if specified in command line.
|
||||||
|
deprecated := nfs.FlagSet("deprecated")
|
||||||
|
if deprecated.Changed("profiling") {
|
||||||
|
cfg.EnableProfiling = o.ComponentConfig.EnableProfiling
|
||||||
|
}
|
||||||
|
if deprecated.Changed("contention-profiling") {
|
||||||
|
cfg.EnableContentionProfiling = o.ComponentConfig.EnableContentionProfiling
|
||||||
|
}
|
||||||
|
if deprecated.Changed("kubeconfig") {
|
||||||
|
cfg.ClientConnection.Kubeconfig = o.ComponentConfig.ClientConnection.Kubeconfig
|
||||||
|
}
|
||||||
|
if deprecated.Changed("kube-api-content-type") {
|
||||||
|
cfg.ClientConnection.ContentType = o.ComponentConfig.ClientConnection.ContentType
|
||||||
|
}
|
||||||
|
if deprecated.Changed("kube-api-qps") {
|
||||||
|
cfg.ClientConnection.QPS = o.ComponentConfig.ClientConnection.QPS
|
||||||
|
}
|
||||||
|
if deprecated.Changed("kube-api-burst") {
|
||||||
|
cfg.ClientConnection.Burst = o.ComponentConfig.ClientConnection.Burst
|
||||||
|
}
|
||||||
|
if deprecated.Changed("lock-object-namespace") {
|
||||||
|
cfg.LeaderElection.ResourceNamespace = o.ComponentConfig.LeaderElection.ResourceNamespace
|
||||||
|
}
|
||||||
|
if deprecated.Changed("lock-object-name") {
|
||||||
|
cfg.LeaderElection.ResourceName = o.ComponentConfig.LeaderElection.ResourceName
|
||||||
|
}
|
||||||
|
// Obtain CLI args related with leaderelection. Set them to cfg if specified in command line.
|
||||||
|
leaderelection := nfs.FlagSet("leader election")
|
||||||
if leaderelection.Changed("leader-elect") {
|
if leaderelection.Changed("leader-elect") {
|
||||||
cfg.LeaderElection.LeaderElect = o.ComponentConfig.LeaderElection.LeaderElect
|
cfg.LeaderElection.LeaderElect = o.ComponentConfig.LeaderElection.LeaderElect
|
||||||
}
|
}
|
||||||
@ -129,24 +151,17 @@ func (o *Options) Complete(cfg *kubeschedulerconfig.KubeSchedulerConfiguration)
|
|||||||
}
|
}
|
||||||
|
|
||||||
o.ComponentConfig = *cfg
|
o.ComponentConfig = *cfg
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flags returns flags for a specific scheduler by section name
|
// Flags returns flags for a specific scheduler by section name
|
||||||
func (o *Options) Flags() *cliflag.NamedFlagSets {
|
func (o *Options) Flags() (nfs cliflag.NamedFlagSets) {
|
||||||
if o.flags != nil {
|
|
||||||
return o.flags
|
|
||||||
}
|
|
||||||
|
|
||||||
nfs := cliflag.NamedFlagSets{}
|
|
||||||
fs := nfs.FlagSet("misc")
|
fs := nfs.FlagSet("misc")
|
||||||
fs.StringVar(&o.ConfigFile, "config", o.ConfigFile, "The path to the configuration file.")
|
fs.StringVar(&o.ConfigFile, "config", o.ConfigFile, "The path to the configuration file.")
|
||||||
fs.StringVar(&o.WriteConfigTo, "write-config-to", o.WriteConfigTo, "If set, write the configuration values to this file and exit.")
|
fs.StringVar(&o.WriteConfigTo, "write-config-to", o.WriteConfigTo, "If set, write the configuration values to this file and exit.")
|
||||||
fs.StringVar(&o.Master, "master", o.Master, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
|
fs.StringVar(&o.Master, "master", o.Master, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
|
||||||
|
|
||||||
// In some tests, o.SecureServing can be nil.
|
o.SecureServing.AddFlags(nfs.FlagSet("secure serving"))
|
||||||
if o.SecureServing != nil {
|
|
||||||
o.SecureServing.AddFlags(nfs.FlagSet("secure serving"))
|
|
||||||
}
|
|
||||||
o.Authentication.AddFlags(nfs.FlagSet("authentication"))
|
o.Authentication.AddFlags(nfs.FlagSet("authentication"))
|
||||||
o.Authorization.AddFlags(nfs.FlagSet("authorization"))
|
o.Authorization.AddFlags(nfs.FlagSet("authorization"))
|
||||||
o.Deprecated.AddFlags(nfs.FlagSet("deprecated"), &o.ComponentConfig)
|
o.Deprecated.AddFlags(nfs.FlagSet("deprecated"), &o.ComponentConfig)
|
||||||
@ -156,8 +171,7 @@ func (o *Options) Flags() *cliflag.NamedFlagSets {
|
|||||||
o.Metrics.AddFlags(nfs.FlagSet("metrics"))
|
o.Metrics.AddFlags(nfs.FlagSet("metrics"))
|
||||||
o.Logs.AddFlags(nfs.FlagSet("logs"))
|
o.Logs.AddFlags(nfs.FlagSet("logs"))
|
||||||
|
|
||||||
o.flags = &nfs
|
return nfs
|
||||||
return o.flags
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApplyTo applies the scheduler options to the given scheduler app configuration.
|
// ApplyTo applies the scheduler options to the given scheduler app configuration.
|
||||||
@ -169,9 +183,6 @@ func (o *Options) ApplyTo(c *schedulerappconfig.Config) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Honor the CLI args before assigning `cfg` to `c.ComponentConfig`.
|
|
||||||
o.Complete(cfg)
|
|
||||||
|
|
||||||
if err := validation.ValidateKubeSchedulerConfiguration(cfg); err != nil {
|
if err := validation.ValidateKubeSchedulerConfiguration(cfg); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -69,6 +69,7 @@ func NewSchedulerCommand(registryOptions ...Option) *cobra.Command {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namedFlagSets := opts.Flags()
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "kube-scheduler",
|
Use: "kube-scheduler",
|
||||||
Long: `The Kubernetes scheduler is a control plane process which assigns
|
Long: `The Kubernetes scheduler is a control plane process which assigns
|
||||||
@ -80,6 +81,9 @@ kube-scheduler is the reference implementation.
|
|||||||
See [scheduling](https://kubernetes.io/docs/concepts/scheduling-eviction/)
|
See [scheduling](https://kubernetes.io/docs/concepts/scheduling-eviction/)
|
||||||
for more information about scheduling and the kube-scheduler component.`,
|
for more information about scheduling and the kube-scheduler component.`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
if err := opts.Complete(&namedFlagSets); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return runCommand(cmd, opts, registryOptions...)
|
return runCommand(cmd, opts, registryOptions...)
|
||||||
},
|
},
|
||||||
Args: func(cmd *cobra.Command, args []string) error {
|
Args: func(cmd *cobra.Command, args []string) error {
|
||||||
@ -93,7 +97,6 @@ for more information about scheduling and the kube-scheduler component.`,
|
|||||||
}
|
}
|
||||||
|
|
||||||
fs := cmd.Flags()
|
fs := cmd.Flags()
|
||||||
namedFlagSets := opts.Flags()
|
|
||||||
verflag.AddFlags(namedFlagSets.FlagSet("global"))
|
verflag.AddFlags(namedFlagSets.FlagSet("global"))
|
||||||
globalflag.AddGlobalFlags(namedFlagSets.FlagSet("global"), cmd.Name())
|
globalflag.AddGlobalFlags(namedFlagSets.FlagSet("global"), cmd.Name())
|
||||||
for _, f := range namedFlagSets.FlagSets {
|
for _, f := range namedFlagSets.FlagSets {
|
||||||
@ -101,7 +104,7 @@ for more information about scheduling and the kube-scheduler component.`,
|
|||||||
}
|
}
|
||||||
|
|
||||||
cols, _, _ := term.TerminalSize(cmd.OutOrStdout())
|
cols, _, _ := term.TerminalSize(cmd.OutOrStdout())
|
||||||
cliflag.SetUsageAndHelpFunc(cmd, *namedFlagSets, cols)
|
cliflag.SetUsageAndHelpFunc(cmd, namedFlagSets, cols)
|
||||||
|
|
||||||
cmd.MarkFlagFilename("config", "yaml", "yml", "json")
|
cmd.MarkFlagFilename("config", "yaml", "yml", "json")
|
||||||
|
|
||||||
|
@ -26,13 +26,9 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
||||||
componentbaseconfig "k8s.io/component-base/config"
|
|
||||||
"k8s.io/kube-scheduler/config/v1beta3"
|
|
||||||
"k8s.io/kubernetes/cmd/kube-scheduler/app/options"
|
"k8s.io/kubernetes/cmd/kube-scheduler/app/options"
|
||||||
"k8s.io/kubernetes/pkg/scheduler/apis/config"
|
"k8s.io/kubernetes/pkg/scheduler/apis/config"
|
||||||
"k8s.io/kubernetes/pkg/scheduler/apis/config/testing/defaults"
|
"k8s.io/kubernetes/pkg/scheduler/apis/config/testing/defaults"
|
||||||
@ -143,35 +139,10 @@ profiles:
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// empty leader-election config
|
|
||||||
emptyLeaderElectionConfig := filepath.Join(tmpDir, "empty-leader-election-config.yaml")
|
|
||||||
if err := ioutil.WriteFile(emptyLeaderElectionConfig, []byte(fmt.Sprintf(`
|
|
||||||
apiVersion: kubescheduler.config.k8s.io/v1beta3
|
|
||||||
kind: KubeSchedulerConfiguration
|
|
||||||
clientConnection:
|
|
||||||
kubeconfig: "%s"
|
|
||||||
`, configKubeconfig)), os.FileMode(0600)); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// leader-election config
|
|
||||||
leaderElectionConfig := filepath.Join(tmpDir, "leader-election-config.yaml")
|
|
||||||
if err := ioutil.WriteFile(leaderElectionConfig, []byte(fmt.Sprintf(`
|
|
||||||
apiVersion: kubescheduler.config.k8s.io/v1beta3
|
|
||||||
kind: KubeSchedulerConfiguration
|
|
||||||
clientConnection:
|
|
||||||
kubeconfig: "%s"
|
|
||||||
leaderElection:
|
|
||||||
leaseDuration: 1h
|
|
||||||
`, configKubeconfig)), os.FileMode(0600)); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
testcases := []struct {
|
testcases := []struct {
|
||||||
name string
|
name string
|
||||||
flags []string
|
flags []string
|
||||||
wantPlugins map[string]*config.Plugins
|
wantPlugins map[string]*config.Plugins
|
||||||
wantLeaderElection *componentbaseconfig.LeaderElectionConfiguration
|
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "default config",
|
name: "default config",
|
||||||
@ -222,75 +193,6 @@ leaderElection:
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "leader election CLI args, along with --config arg",
|
|
||||||
flags: []string{
|
|
||||||
"--leader-elect=false",
|
|
||||||
"--leader-elect-lease-duration=2h", // CLI args are favored over the fields in ComponentConfig
|
|
||||||
"--lock-object-namespace=default", // deprecated CLI arg will be ignored if --config is specified
|
|
||||||
"--config", emptyLeaderElectionConfig,
|
|
||||||
},
|
|
||||||
wantLeaderElection: &componentbaseconfig.LeaderElectionConfiguration{
|
|
||||||
LeaderElect: false, // from CLI args
|
|
||||||
LeaseDuration: metav1.Duration{Duration: 2 * time.Hour}, // from CLI args
|
|
||||||
RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
|
|
||||||
RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
|
|
||||||
ResourceLock: "leases",
|
|
||||||
ResourceName: v1beta3.SchedulerDefaultLockObjectName,
|
|
||||||
ResourceNamespace: v1beta3.SchedulerDefaultLockObjectNamespace,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "leader election CLI args, without --config arg",
|
|
||||||
flags: []string{
|
|
||||||
"--leader-elect=false",
|
|
||||||
"--leader-elect-lease-duration=2h",
|
|
||||||
"--lock-object-namespace=default", // deprecated CLI arg is honored if --config is not specified
|
|
||||||
"--kubeconfig", configKubeconfig,
|
|
||||||
},
|
|
||||||
wantLeaderElection: &componentbaseconfig.LeaderElectionConfiguration{
|
|
||||||
LeaderElect: false, // from CLI args
|
|
||||||
LeaseDuration: metav1.Duration{Duration: 2 * time.Hour}, // from CLI args
|
|
||||||
RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
|
|
||||||
RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
|
|
||||||
ResourceLock: "leases",
|
|
||||||
ResourceName: v1beta3.SchedulerDefaultLockObjectName,
|
|
||||||
ResourceNamespace: "default", // from deprecated CLI args
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "leader election settings specified by ComponentConfig only",
|
|
||||||
flags: []string{
|
|
||||||
"--config", leaderElectionConfig,
|
|
||||||
},
|
|
||||||
wantLeaderElection: &componentbaseconfig.LeaderElectionConfiguration{
|
|
||||||
LeaderElect: true,
|
|
||||||
LeaseDuration: metav1.Duration{Duration: 1 * time.Hour}, // from CC
|
|
||||||
RenewDeadline: metav1.Duration{Duration: 10 * time.Second},
|
|
||||||
RetryPeriod: metav1.Duration{Duration: 2 * time.Second},
|
|
||||||
ResourceLock: "leases",
|
|
||||||
ResourceName: v1beta3.SchedulerDefaultLockObjectName,
|
|
||||||
ResourceNamespace: v1beta3.SchedulerDefaultLockObjectNamespace,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "leader election settings specified by CLI args and ComponentConfig",
|
|
||||||
flags: []string{
|
|
||||||
"--leader-elect=true",
|
|
||||||
"--leader-elect-renew-deadline=5s",
|
|
||||||
"--leader-elect-retry-period=1s",
|
|
||||||
"--config", leaderElectionConfig,
|
|
||||||
},
|
|
||||||
wantLeaderElection: &componentbaseconfig.LeaderElectionConfiguration{
|
|
||||||
LeaderElect: true,
|
|
||||||
LeaseDuration: metav1.Duration{Duration: 1 * time.Hour}, // from CC
|
|
||||||
RenewDeadline: metav1.Duration{Duration: 5 * time.Second}, // from CLI args
|
|
||||||
RetryPeriod: metav1.Duration{Duration: 1 * time.Second}, // from CLI args
|
|
||||||
ResourceLock: "leases",
|
|
||||||
ResourceName: v1beta3.SchedulerDefaultLockObjectName,
|
|
||||||
ResourceNamespace: v1beta3.SchedulerDefaultLockObjectNamespace,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
makeListener := func(t *testing.T) net.Listener {
|
makeListener := func(t *testing.T) net.Listener {
|
||||||
@ -309,9 +211,6 @@ leaderElection:
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
// use listeners instead of static ports so parallel test runs don't conflict
|
|
||||||
opts.SecureServing.Listener = makeListener(t)
|
|
||||||
defer opts.SecureServing.Listener.Close()
|
|
||||||
|
|
||||||
nfs := opts.Flags()
|
nfs := opts.Flags()
|
||||||
for _, f := range nfs.FlagSets {
|
for _, f := range nfs.FlagSets {
|
||||||
@ -321,6 +220,14 @@ leaderElection:
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := opts.Complete(&nfs); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// use listeners instead of static ports so parallel test runs don't conflict
|
||||||
|
opts.SecureServing.Listener = makeListener(t)
|
||||||
|
defer opts.SecureServing.Listener.Close()
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
_, sched, err := Setup(ctx, opts)
|
_, sched, err := Setup(ctx, opts)
|
||||||
@ -328,22 +235,13 @@ leaderElection:
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if tc.wantPlugins != nil {
|
gotPlugins := make(map[string]*config.Plugins)
|
||||||
gotPlugins := make(map[string]*config.Plugins)
|
for n, p := range sched.Profiles {
|
||||||
for n, p := range sched.Profiles {
|
gotPlugins[n] = p.ListPlugins()
|
||||||
gotPlugins[n] = p.ListPlugins()
|
|
||||||
}
|
|
||||||
|
|
||||||
if diff := cmp.Diff(tc.wantPlugins, gotPlugins); diff != "" {
|
|
||||||
t.Errorf("Unexpected plugins diff (-want, +got): %s", diff)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if tc.wantLeaderElection != nil {
|
if diff := cmp.Diff(tc.wantPlugins, gotPlugins); diff != "" {
|
||||||
gotLeaderElection := opts.ComponentConfig.LeaderElection
|
t.Errorf("unexpected plugins diff (-want, +got): %s", diff)
|
||||||
if diff := cmp.Diff(*tc.wantLeaderElection, gotLeaderElection); diff != "" {
|
|
||||||
t.Errorf("Unexpected leaderElection diff (-want, +got): %s", diff)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -93,6 +93,10 @@ func StartTestServer(t Logger, customFlags []string) (result TestServer, err err
|
|||||||
}
|
}
|
||||||
fs.Parse(customFlags)
|
fs.Parse(customFlags)
|
||||||
|
|
||||||
|
if err := opts.Complete(&namedFlagSets); err != nil {
|
||||||
|
return TestServer{}, err
|
||||||
|
}
|
||||||
|
|
||||||
if opts.SecureServing.BindPort != 0 {
|
if opts.SecureServing.BindPort != 0 {
|
||||||
opts.SecureServing.Listener, opts.SecureServing.BindPort, err = createListenerOnFreePort()
|
opts.SecureServing.Listener, opts.SecureServing.BindPort, err = createListenerOnFreePort()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user