Merge pull request #107967 from jlsong01/check_kubectl_version_extra_arguments

kubectl version should fail when given extra arguments

Kubernetes-commit: a3301946010ed6618b3f61f384623b1168260a4f
This commit is contained in:
Kubernetes Publisher 2022-03-03 22:26:52 -08:00
commit 4dc12633c1
4 changed files with 18 additions and 11 deletions

8
go.mod
View File

@ -33,8 +33,8 @@ require (
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.0.0-20220226220324-b8c40e080bc5
k8s.io/apimachinery v0.0.0-20220226220127-2936d3f03931
k8s.io/cli-runtime v0.0.0-20220221182607-1d7a7893878d
k8s.io/client-go v0.0.0-20220301020630-8e46da3fd12b
k8s.io/cli-runtime v0.0.0-20220304062546-5a23dd4f767a
k8s.io/client-go v0.0.0-20220303122547-eb103e0abf62
k8s.io/component-base v0.0.0-20220301021127-30d23418100a
k8s.io/component-helpers v0.0.0-20220221181225-89b3c2ca20da
k8s.io/klog/v2 v2.40.1
@ -50,8 +50,8 @@ require (
replace (
k8s.io/api => k8s.io/api v0.0.0-20220226220324-b8c40e080bc5
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20220226220127-2936d3f03931
k8s.io/cli-runtime => k8s.io/cli-runtime v0.0.0-20220221182607-1d7a7893878d
k8s.io/client-go => k8s.io/client-go v0.0.0-20220301020630-8e46da3fd12b
k8s.io/cli-runtime => k8s.io/cli-runtime v0.0.0-20220304062546-5a23dd4f767a
k8s.io/client-go => k8s.io/client-go v0.0.0-20220303122547-eb103e0abf62
k8s.io/code-generator => k8s.io/code-generator v0.0.0-20220226191723-8d7923dd2451
k8s.io/component-base => k8s.io/component-base v0.0.0-20220301021127-30d23418100a
k8s.io/component-helpers => k8s.io/component-helpers v0.0.0-20220221181225-89b3c2ca20da

8
go.sum
View File

@ -923,10 +923,10 @@ k8s.io/api v0.0.0-20220226220324-b8c40e080bc5 h1:z4oqfOInb6p7EwsJbKUe2IcKaeSBWmf
k8s.io/api v0.0.0-20220226220324-b8c40e080bc5/go.mod h1:xmVR3mDgBB2FAJoueQFwuWn03L5odGCiOKivsptcgRU=
k8s.io/apimachinery v0.0.0-20220226220127-2936d3f03931 h1:pb7vtSnIF7dReuA6s+WsakFL6vLZ4xA71kJMln9j4Pc=
k8s.io/apimachinery v0.0.0-20220226220127-2936d3f03931/go.mod h1:6HjHJr7AD3yHuu+gOdE3O1dqE21lBVCDBk5W7wry/WI=
k8s.io/cli-runtime v0.0.0-20220221182607-1d7a7893878d h1:kVNVC7PpJTDg8Q3FZqZ0l8a46jambiYt3cx9SQRtvhQ=
k8s.io/cli-runtime v0.0.0-20220221182607-1d7a7893878d/go.mod h1:C/IVuWJzuw8sMRBUUqFdI2VPQQ/drnZ4FWItz2FMfYc=
k8s.io/client-go v0.0.0-20220301020630-8e46da3fd12b h1:2OxK6XlDGvXIwEp+5zeiTfrQi/orFdNgsISyxZzhUvY=
k8s.io/client-go v0.0.0-20220301020630-8e46da3fd12b/go.mod h1:X+9ONiZCJ4K5521TlOgbcMrYFvkjacBd90IPckfzTac=
k8s.io/cli-runtime v0.0.0-20220304062546-5a23dd4f767a h1:GLtjCZTc3Qx7ZuYJb+gKDGT1XTBqENco3v64fSYJjPk=
k8s.io/cli-runtime v0.0.0-20220304062546-5a23dd4f767a/go.mod h1:R3r66bEzcK8AGKIBf7tz+CW0cOyn7CfOtPfblRlGT6o=
k8s.io/client-go v0.0.0-20220303122547-eb103e0abf62 h1:kpsNQiWWMoxhrGbeNBVlojVEIkOUUXs+prjf7026YDE=
k8s.io/client-go v0.0.0-20220303122547-eb103e0abf62/go.mod h1:X+9ONiZCJ4K5521TlOgbcMrYFvkjacBd90IPckfzTac=
k8s.io/code-generator v0.0.0-20220226191723-8d7923dd2451/go.mod h1:BunYIiSvEAW736r5Q7j7shOSPkb80INs0kWYJvW2o6I=
k8s.io/component-base v0.0.0-20220301021127-30d23418100a h1:ZEEAhoBrshp9Dskp4Cu01GWusuSj7rxqT9gpGHPT87M=
k8s.io/component-base v0.0.0-20220301021127-30d23418100a/go.mod h1:GxjPIEe+u2ujTZ5FAWeI72IXLuFY8otpmXIeWIP0G7M=

View File

@ -75,7 +75,7 @@ func NewCmdVersion(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *co
Example: versionExample,
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(o.Complete(f, cmd))
cmdutil.CheckErr(o.Validate())
cmdutil.CheckErr(o.Validate(args))
cmdutil.CheckErr(o.Run())
},
}
@ -101,7 +101,11 @@ func (o *Options) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
}
// Validate validates the provided options
func (o *Options) Validate() error {
func (o *Options) Validate(args []string) error {
if len(args) != 0 {
return errors.New(fmt.Sprintf("extra arguments: %v", args))
}
if o.Output != "" && o.Output != "yaml" && o.Output != "json" {
return errors.New(`--output must be 'yaml' or 'json'`)
}

View File

@ -34,9 +34,12 @@ func TestNewCmdVersionClientVersion(t *testing.T) {
if err := o.Complete(tf, &cobra.Command{}); err != nil {
t.Errorf("Unexpected error: %v", err)
}
if err := o.Validate(); err != nil {
if err := o.Validate(nil); err != nil {
t.Errorf("Unexpected error: %v", err)
}
if err := o.Validate([]string{"extraParameter0"}); !strings.Contains(err.Error(), "extra arguments") {
t.Errorf("Unexpected error: should fail to validate the args length greater than 0")
}
if err := o.Run(); err != nil {
t.Errorf("Cannot execute version command: %v", err)
}