From 4fddd6a3cda9c95c8ae5b4a36d3c3fb06aa1dce1 Mon Sep 17 00:00:00 2001 From: Brian Pursley Date: Wed, 22 May 2024 15:36:28 -0400 Subject: [PATCH] Fix inconsistent validation of -f or -k flag in kubectl create command --- .../k8s.io/kubectl/pkg/cmd/create/create.go | 12 +++---- .../kubectl/pkg/cmd/create/create_test.go | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/create/create.go b/staging/src/k8s.io/kubectl/pkg/cmd/create/create.go index 17b21c4e08d..fa7876d8676 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/create/create.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/create/create.go @@ -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") diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_test.go index 5351810f9dc..9d248eda62d 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_test.go @@ -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) + } +}