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:
Monis Khan
2020-04-28 13:51:29 -04:00
committed by Lubomir I. Ivanov
parent 60559bc919
commit fc4f91f10b
16 changed files with 78 additions and 18 deletions

View File

@@ -294,10 +294,7 @@ func (o *Options) processHostnameOverrideFlag() error {
}
// Validate validates all the required options.
func (o *Options) Validate(args []string) error {
if len(args) != 0 {
return errors.New("no arguments are supported")
}
func (o *Options) Validate() error {
if errs := validation.Validate(o.config); len(errs) != 0 {
return errs.ToAggregate()
}
@@ -490,7 +487,7 @@ with the apiserver API to configure the proxy.`,
if err := opts.Complete(); err != nil {
klog.Fatalf("failed complete: %v", err)
}
if err := opts.Validate(args); err != nil {
if err := opts.Validate(); err != nil {
klog.Fatalf("failed validate: %v", err)
}
@@ -498,6 +495,14 @@ with the apiserver API to configure the proxy.`,
klog.Exit(err)
}
},
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
},
}
var err error