Skip to content

Commit 4fddd6a

Browse files
committed
Fix inconsistent validation of -f or -k flag in kubectl create command
1 parent 74d5784 commit 4fddd6a

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/create/create.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,6 @@ func NewCmdCreate(f cmdutil.Factory, ioStreams genericiooptions.IOStreams) *cobr
109109
Long: createLong,
110110
Example: createExample,
111111
Run: func(cmd *cobra.Command, args []string) {
112-
if cmdutil.IsFilenameSliceEmpty(o.FilenameOptions.Filenames, o.FilenameOptions.Kustomize) {
113-
ioStreams.ErrOut.Write([]byte("Error: must specify one of -f and -k\n\n"))
114-
defaultRunFunc := cmdutil.DefaultSubCommandRun(ioStreams.ErrOut)
115-
defaultRunFunc(cmd, args)
116-
return
117-
}
118112
cmdutil.CheckErr(o.Complete(f, cmd, args))
119113
cmdutil.CheckErr(o.Validate())
120114
cmdutil.CheckErr(o.RunCreate(f, cmd))
@@ -159,8 +153,12 @@ func NewCmdCreate(f cmdutil.Factory, ioStreams genericiooptions.IOStreams) *cobr
159153
return cmd
160154
}
161155

162-
// Validate makes sure there is no discrepency in command options
156+
// Validate makes sure there is no discrepancy in command options
163157
func (o *CreateOptions) Validate() error {
158+
if err := o.FilenameOptions.RequireFilenameOrKustomize(); err != nil {
159+
return err
160+
}
161+
164162
if len(o.Raw) > 0 {
165163
if o.EditBeforeCreate {
166164
return fmt.Errorf("--raw and --edit are mutually exclusive")

staging/src/k8s.io/kubectl/pkg/cmd/create/create_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"k8s.io/cli-runtime/pkg/resource"
2626
"k8s.io/client-go/rest/fake"
2727
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
28+
cmdutil "k8s.io/kubectl/pkg/cmd/util"
2829
"k8s.io/kubectl/pkg/scheme"
2930
)
3031

@@ -150,3 +151,35 @@ func TestCreateDirectory(t *testing.T) {
150151
t.Errorf("unexpected output: %s", buf.String())
151152
}
152153
}
154+
155+
func TestMissingFilenameError(t *testing.T) {
156+
var errStr string
157+
var exitCode int
158+
cmdutil.BehaviorOnFatal(func(str string, code int) {
159+
if errStr == "" {
160+
errStr = str
161+
exitCode = code
162+
}
163+
})
164+
165+
tf := cmdtesting.NewTestFactory().WithNamespace("test")
166+
defer tf.Cleanup()
167+
168+
ioStreams, _, buf, _ := genericiooptions.NewTestIOStreams()
169+
cmd := NewCmdCreate(tf, ioStreams)
170+
cmd.Run(cmd, []string{})
171+
172+
if buf.Len() > 0 {
173+
t.Errorf("unexpected output: %s", buf.String())
174+
}
175+
176+
if len(errStr) == 0 {
177+
t.Errorf("unexpected non-error")
178+
} else if errStr != "error: must specify one of -f and -k" {
179+
t.Errorf("unexpected error: %s", errStr)
180+
}
181+
182+
if exitCode != 1 {
183+
t.Errorf("unexpected exit code: %d", exitCode)
184+
}
185+
}

0 commit comments

Comments
 (0)