Merge pull request #91349 from neolit123/1.19-fail-on-unrecognized-args

cmd/*: fail on unrecognized flags/arguments for component CLI
This commit is contained in:
Kubernetes Prow Robot
2020-05-30 00:27:53 -07:00
committed by GitHub
16 changed files with 78 additions and 18 deletions

View File

@@ -27,7 +27,7 @@ import (
"github.com/spf13/cobra"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
eventsv1beta1 "k8s.io/api/events/v1beta1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apiserver/pkg/authentication/authenticator"
@@ -83,11 +83,19 @@ kube-scheduler is the reference implementation.
See [scheduling](https://kubernetes.io/docs/concepts/scheduling-eviction/)
for more information about scheduling and the kube-scheduler component.`,
Run: func(cmd *cobra.Command, args []string) {
if err := runCommand(cmd, args, opts, registryOptions...); err != nil {
if err := runCommand(cmd, opts, registryOptions...); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
},
Args: func(cmd *cobra.Command, args []string) error {
for _, arg := range args {
if len(arg) > 0 {
return fmt.Errorf("%q does not take any arguments, got %q", cmd.CommandPath(), args)
}
}
return nil
},
}
fs := cmd.Flags()
namedFlagSets := opts.Flags()
@@ -114,14 +122,14 @@ for more information about scheduling and the kube-scheduler component.`,
}
// runCommand runs the scheduler.
func runCommand(cmd *cobra.Command, args []string, opts *options.Options, registryOptions ...Option) error {
func runCommand(cmd *cobra.Command, opts *options.Options, registryOptions ...Option) error {
verflag.PrintAndExitIfRequested()
cliflag.PrintFlags(cmd.Flags())
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cc, sched, err := Setup(ctx, args, opts, registryOptions...)
cc, sched, err := Setup(ctx, opts, registryOptions...)
if err != nil {
return err
}
@@ -298,11 +306,7 @@ func WithPlugin(name string, factory framework.PluginFactory) Option {
}
// Setup creates a completed config and a scheduler based on the command args and options
func Setup(ctx context.Context, args []string, opts *options.Options, outOfTreeRegistryOptions ...Option) (*schedulerserverconfig.CompletedConfig, *scheduler.Scheduler, error) {
if len(args) != 0 {
fmt.Fprint(os.Stderr, "arguments are not supported\n")
}
func Setup(ctx context.Context, opts *options.Options, outOfTreeRegistryOptions ...Option) (*schedulerserverconfig.CompletedConfig, *scheduler.Scheduler, error) {
if errs := opts.Validate(); len(errs) > 0 {
return nil, nil, utilerrors.NewAggregate(errs)
}

View File

@@ -373,10 +373,9 @@ profiles:
t.Fatal(err)
}
var args []string
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cc, sched, err := Setup(ctx, args, opts)
cc, sched, err := Setup(ctx, opts)
if err != nil {
t.Fatal(err)
}

View File

@@ -114,7 +114,7 @@ func StartTestServer(t Logger, customFlags []string) (result TestServer, err err
t.Logf("kube-scheduler will listen insecurely on port %d...", opts.CombinedInsecureServing.BindPort)
}
cc, sched, err := app.Setup(ctx, []string{}, opts)
cc, sched, err := app.Setup(ctx, opts)
if err != nil {
return result, fmt.Errorf("failed to create config from options: %v", err)
}