Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let kubectl exec follow dash rule #14161

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions pkg/kubectl/cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ func NewCmdExec(f *cmdutil.Factory, cmdIn io.Reader, cmdOut, cmdErr io.Writer) *
Long: "Execute a command in a container.",
Example: exec_example,
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(options.Complete(f, cmd, args))
argsLenAtDash := cmd.ArgsLenAtDash()
cmdutil.CheckErr(options.Complete(f, cmd, args, argsLenAtDash))
cmdutil.CheckErr(options.Validate())
cmdutil.CheckErr(options.Run())
},
Expand Down Expand Up @@ -103,8 +104,9 @@ type ExecOptions struct {
}

// Complete verifies command line arguments and loads data from the command environment
func (p *ExecOptions) Complete(f *cmdutil.Factory, cmd *cobra.Command, argsIn []string) error {
if len(p.PodName) == 0 && len(argsIn) == 0 {
func (p *ExecOptions) Complete(f *cmdutil.Factory, cmd *cobra.Command, argsIn []string, argsLenAtDash int) error {
// Let kubectl exec follow rules for `--`, see #13004 issue
if len(p.PodName) == 0 && (len(argsIn) == 0 || argsLenAtDash == 0) {
return cmdutil.UsageError(cmd, "POD is required for exec")
}
if len(p.PodName) != 0 {
Expand Down
74 changes: 49 additions & 25 deletions pkg/kubectl/cmd/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (f *fakeRemoteExecutor) Execute(req *client.Request, config *client.Config,
func TestPodAndContainer(t *testing.T) {
tests := []struct {
args []string
argsLenAtDash int
p *ExecOptions
name string
expectError bool
Expand All @@ -53,43 +54,65 @@ func TestPodAndContainer(t *testing.T) {
expectedArgs []string
}{
{
p: &ExecOptions{},
expectError: true,
name: "empty",
p: &ExecOptions{},
argsLenAtDash: -1,
expectError: true,
name: "empty",
},
{
p: &ExecOptions{PodName: "foo"},
expectError: true,
name: "no cmd",
p: &ExecOptions{PodName: "foo"},
argsLenAtDash: -1,
expectError: true,
name: "no cmd",
},
{
p: &ExecOptions{PodName: "foo", ContainerName: "bar"},
expectError: true,
name: "no cmd, w/ container",
p: &ExecOptions{PodName: "foo", ContainerName: "bar"},
argsLenAtDash: -1,
expectError: true,
name: "no cmd, w/ container",
},
{
p: &ExecOptions{PodName: "foo"},
args: []string{"cmd"},
expectedPod: "foo",
expectedArgs: []string{"cmd"},
name: "pod in flags",
p: &ExecOptions{PodName: "foo"},
args: []string{"cmd"},
argsLenAtDash: -1,
expectedPod: "foo",
expectedArgs: []string{"cmd"},
name: "pod in flags",
},
{
p: &ExecOptions{},
args: []string{"foo"},
expectError: true,
name: "no cmd, w/o flags",
p: &ExecOptions{},
args: []string{"foo", "cmd"},
argsLenAtDash: 0,
expectError: true,
name: "no pod, pod name is behind dash",
},
{
p: &ExecOptions{},
args: []string{"foo", "cmd"},
expectedPod: "foo",
expectedArgs: []string{"cmd"},
name: "cmd, w/o flags",
p: &ExecOptions{},
args: []string{"foo"},
argsLenAtDash: -1,
expectError: true,
name: "no cmd, w/o flags",
},
{
p: &ExecOptions{},
args: []string{"foo", "cmd"},
argsLenAtDash: -1,
expectedPod: "foo",
expectedArgs: []string{"cmd"},
name: "cmd, w/o flags",
},
{
p: &ExecOptions{},
args: []string{"foo", "cmd"},
argsLenAtDash: 1,
expectedPod: "foo",
expectedArgs: []string{"cmd"},
name: "cmd, cmd is behind dash",
},
{
p: &ExecOptions{ContainerName: "bar"},
args: []string{"foo", "cmd"},
argsLenAtDash: -1,
expectedPod: "foo",
expectedContainer: "bar",
expectedArgs: []string{"cmd"},
Expand All @@ -107,7 +130,7 @@ func TestPodAndContainer(t *testing.T) {

cmd := &cobra.Command{}
options := test.p
err := options.Complete(f, cmd, test.args)
err := options.Complete(f, cmd, test.args, test.argsLenAtDash)
if test.expectError && err == nil {
t.Errorf("unexpected non-error (%s)", test.name)
}
Expand Down Expand Up @@ -186,7 +209,8 @@ func TestExec(t *testing.T) {
Executor: ex,
}
cmd := &cobra.Command{}
if err := params.Complete(f, cmd, []string{"test", "command"}); err != nil {
args := []string{"test", "command"}
if err := params.Complete(f, cmd, args, -1); err != nil {
t.Fatal(err)
}
err := params.Run()
Expand Down