mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-07 20:21:20 +00:00
cmd/*: fail on unrecognized flags/arguments for component CLI
In case a malformed flag is passed to k8s components such as "–foo", where "–" is not an ASCII dash character, the components currently silently ignore the flag and treat it as a positional argument. Make k8s components/commands exit with an error if a positional argument that is not empty is found. Include a custom error message for all components except kubeadm, as cobra.NoArgs is used in a lot of places already (can be fixed in a followup). The kubelet already handles this properly - e.g.: 'unknown command: "–foo"' This change affects: - cloud-controller-manager - kube-apiserver - kube-controller-manager - kube-proxy - kubeadm {alpha|config|token|version} - kubemark Signed-off-by: Monis Khan <mok@vmware.com> Signed-off-by: Lubomir I. Ivanov <lubomirivanov@vmware.com>
This commit is contained in:
committed by
Lubomir I. Ivanov
parent
60559bc919
commit
fc4f91f10b
@@ -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/)
|
||||
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)
|
||||
}
|
||||
|
@@ -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)
|
||||
}
|
||||
|
@@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user