Merge pull request #125072 from brianpursley/create-fix-validation
Fix validation of -f or -k flag in kubectl create command to be consistent with other commands Kubernetes-commit: c9a1a0a3b8acb70cba30d6f4ea59505b4564b4d9
This commit is contained in:
commit
0bd9881b18
4
go.mod
4
go.mod
|
|
@ -30,8 +30,8 @@ require (
|
|||
golang.org/x/sys v0.18.0
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
k8s.io/api v0.0.0-20240516203440-664bdd58a30d
|
||||
k8s.io/apimachinery v0.0.0-20240510224318-1da46c3f5a5b
|
||||
k8s.io/cli-runtime v0.0.0-20240509010650-cee732fd4ad8
|
||||
k8s.io/apimachinery v0.0.0-20240523043213-5c8637dbd9df
|
||||
k8s.io/cli-runtime v0.0.0-20240523010300-0b28b30a28ba
|
||||
k8s.io/client-go v0.0.0-20240521090535-c7396197f39e
|
||||
k8s.io/component-base v0.0.0-20240509004100-482591e4108c
|
||||
k8s.io/component-helpers v0.0.0-20240511203730-30c6ab0cd73e
|
||||
|
|
|
|||
8
go.sum
8
go.sum
|
|
@ -271,10 +271,10 @@ honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWh
|
|||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
k8s.io/api v0.0.0-20240516203440-664bdd58a30d h1:0j0GmUGa2xvgJJiTzGQhNRKa2ByzpqDlBhggVRVmrfY=
|
||||
k8s.io/api v0.0.0-20240516203440-664bdd58a30d/go.mod h1:GIl44YEE/I5fp0xgOEQrSjkLfe5trkF4SgIeRHHviOk=
|
||||
k8s.io/apimachinery v0.0.0-20240510224318-1da46c3f5a5b h1:UTQPecSyfYRHmREs/0iVer4dQClGGZuYKTyHqOAScYQ=
|
||||
k8s.io/apimachinery v0.0.0-20240510224318-1da46c3f5a5b/go.mod h1:+hpAhBheGa7Ub4X6JfKqjEeACgGYZqZv+ILGzigzVGU=
|
||||
k8s.io/cli-runtime v0.0.0-20240509010650-cee732fd4ad8 h1:2CkqqqVdTCyNf/z2DF5qKs42HHsIU0bkkWb7nNgw0LE=
|
||||
k8s.io/cli-runtime v0.0.0-20240509010650-cee732fd4ad8/go.mod h1:fM1OoEh4Wil6xcm53hPjs5c2Bu37hxWoaEOmXui1Fh4=
|
||||
k8s.io/apimachinery v0.0.0-20240523043213-5c8637dbd9df h1:8gUIgtpvsB04mpZKdHNcI0AVWYExv0LJNV1Rfrpp4Qs=
|
||||
k8s.io/apimachinery v0.0.0-20240523043213-5c8637dbd9df/go.mod h1:+hpAhBheGa7Ub4X6JfKqjEeACgGYZqZv+ILGzigzVGU=
|
||||
k8s.io/cli-runtime v0.0.0-20240523010300-0b28b30a28ba h1:vH9EA1tMw5B9c25JyUs3lGq4hRRkjElPgHTKINrneQc=
|
||||
k8s.io/cli-runtime v0.0.0-20240523010300-0b28b30a28ba/go.mod h1:QHDEBrxOos23lQqwel/Ak/yDb4rDJSc62X4l7RF8mgU=
|
||||
k8s.io/client-go v0.0.0-20240521090535-c7396197f39e h1:ELb5A6Mh1VC9KKJYaFUf/6m16G5HEO7q6wRnDSsWNS4=
|
||||
k8s.io/client-go v0.0.0-20240521090535-c7396197f39e/go.mod h1:mF3HjWextzMO13L26Q3GHi+ttX57kjYOBB0XT26k9/o=
|
||||
k8s.io/component-base v0.0.0-20240509004100-482591e4108c h1:dsvBpyLyEc10p5ARPS+9ZZgYIu8W89k2T9oNWvgxEmQ=
|
||||
|
|
|
|||
|
|
@ -109,12 +109,6 @@ func NewCmdCreate(f cmdutil.Factory, ioStreams genericiooptions.IOStreams) *cobr
|
|||
Long: createLong,
|
||||
Example: createExample,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if cmdutil.IsFilenameSliceEmpty(o.FilenameOptions.Filenames, o.FilenameOptions.Kustomize) {
|
||||
ioStreams.ErrOut.Write([]byte("Error: must specify one of -f and -k\n\n"))
|
||||
defaultRunFunc := cmdutil.DefaultSubCommandRun(ioStreams.ErrOut)
|
||||
defaultRunFunc(cmd, args)
|
||||
return
|
||||
}
|
||||
cmdutil.CheckErr(o.Complete(f, cmd, args))
|
||||
cmdutil.CheckErr(o.Validate())
|
||||
cmdutil.CheckErr(o.RunCreate(f, cmd))
|
||||
|
|
@ -159,8 +153,12 @@ func NewCmdCreate(f cmdutil.Factory, ioStreams genericiooptions.IOStreams) *cobr
|
|||
return cmd
|
||||
}
|
||||
|
||||
// Validate makes sure there is no discrepency in command options
|
||||
// Validate makes sure there is no discrepancy in command options
|
||||
func (o *CreateOptions) Validate() error {
|
||||
if err := o.FilenameOptions.RequireFilenameOrKustomize(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(o.Raw) > 0 {
|
||||
if o.EditBeforeCreate {
|
||||
return fmt.Errorf("--raw and --edit are mutually exclusive")
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ import (
|
|||
"k8s.io/cli-runtime/pkg/resource"
|
||||
"k8s.io/client-go/rest/fake"
|
||||
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
|
||||
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
||||
"k8s.io/kubectl/pkg/scheme"
|
||||
)
|
||||
|
||||
|
|
@ -150,3 +151,35 @@ func TestCreateDirectory(t *testing.T) {
|
|||
t.Errorf("unexpected output: %s", buf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestMissingFilenameError(t *testing.T) {
|
||||
var errStr string
|
||||
var exitCode int
|
||||
cmdutil.BehaviorOnFatal(func(str string, code int) {
|
||||
if errStr == "" {
|
||||
errStr = str
|
||||
exitCode = code
|
||||
}
|
||||
})
|
||||
|
||||
tf := cmdtesting.NewTestFactory().WithNamespace("test")
|
||||
defer tf.Cleanup()
|
||||
|
||||
ioStreams, _, buf, _ := genericiooptions.NewTestIOStreams()
|
||||
cmd := NewCmdCreate(tf, ioStreams)
|
||||
cmd.Run(cmd, []string{})
|
||||
|
||||
if buf.Len() > 0 {
|
||||
t.Errorf("unexpected output: %s", buf.String())
|
||||
}
|
||||
|
||||
if len(errStr) == 0 {
|
||||
t.Errorf("unexpected non-error")
|
||||
} else if errStr != "error: must specify one of -f and -k" {
|
||||
t.Errorf("unexpected error: %s", errStr)
|
||||
}
|
||||
|
||||
if exitCode != 1 {
|
||||
t.Errorf("unexpected exit code: %d", exitCode)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue