mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 17:30:00 +00:00
Merge pull request #9164 from brendandburns/fix
Fix a bug in kubectl exec handling.
This commit is contained in:
commit
785740a0f8
@ -80,27 +80,29 @@ type execParams struct {
|
|||||||
tty bool
|
tty bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractPodAndContainer(cmd *cobra.Command, args []string, p *execParams) (podName string, containerName string, err error) {
|
func extractPodAndContainer(cmd *cobra.Command, argsIn []string, p *execParams) (podName string, containerName string, args []string, err error) {
|
||||||
if len(p.podName) == 0 && len(args) == 0 {
|
if len(p.podName) == 0 && len(argsIn) == 0 {
|
||||||
return "", "", cmdutil.UsageError(cmd, "POD is required for exec")
|
return "", "", nil, cmdutil.UsageError(cmd, "POD is required for exec")
|
||||||
}
|
}
|
||||||
if len(p.podName) != 0 {
|
if len(p.podName) != 0 {
|
||||||
printDeprecationWarning("exec POD", "-p POD")
|
printDeprecationWarning("exec POD", "-p POD")
|
||||||
podName = p.podName
|
podName = p.podName
|
||||||
if len(args) < 1 {
|
if len(argsIn) < 1 {
|
||||||
return "", "", cmdutil.UsageError(cmd, "COMMAND is required for exec")
|
return "", "", nil, cmdutil.UsageError(cmd, "COMMAND is required for exec")
|
||||||
}
|
}
|
||||||
|
args = argsIn
|
||||||
} else {
|
} else {
|
||||||
podName = args[0]
|
podName = argsIn[0]
|
||||||
if len(args) < 2 {
|
args = argsIn[1:]
|
||||||
return "", "", cmdutil.UsageError(cmd, "COMMAND is required for exec")
|
if len(args) < 1 {
|
||||||
|
return "", "", nil, cmdutil.UsageError(cmd, "COMMAND is required for exec")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return podName, p.containerName, nil
|
return podName, p.containerName, args, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunExec(f *cmdutil.Factory, cmd *cobra.Command, cmdIn io.Reader, cmdOut, cmdErr io.Writer, p *execParams, args []string, re remoteExecutor) error {
|
func RunExec(f *cmdutil.Factory, cmd *cobra.Command, cmdIn io.Reader, cmdOut, cmdErr io.Writer, p *execParams, argsIn []string, re remoteExecutor) error {
|
||||||
podName, containerName, err := extractPodAndContainer(cmd, args, p)
|
podName, containerName, args, err := extractPodAndContainer(cmd, argsIn, p)
|
||||||
namespace, err := f.DefaultNamespace()
|
namespace, err := f.DefaultNamespace()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -43,58 +44,73 @@ func TestPodAndContainer(t *testing.T) {
|
|||||||
tests := []struct {
|
tests := []struct {
|
||||||
args []string
|
args []string
|
||||||
p *execParams
|
p *execParams
|
||||||
|
name string
|
||||||
expectError bool
|
expectError bool
|
||||||
expectedPod string
|
expectedPod string
|
||||||
expectedContainer string
|
expectedContainer string
|
||||||
|
expectedArgs []string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
p: &execParams{},
|
p: &execParams{},
|
||||||
expectError: true,
|
expectError: true,
|
||||||
|
name: "empty",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
p: &execParams{podName: "foo"},
|
p: &execParams{podName: "foo"},
|
||||||
expectError: true,
|
expectError: true,
|
||||||
|
name: "no cmd",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
p: &execParams{podName: "foo", containerName: "bar"},
|
p: &execParams{podName: "foo", containerName: "bar"},
|
||||||
expectError: true,
|
expectError: true,
|
||||||
|
name: "no cmd, w/ container",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
p: &execParams{podName: "foo"},
|
p: &execParams{podName: "foo"},
|
||||||
args: []string{"cmd"},
|
args: []string{"cmd"},
|
||||||
expectedPod: "foo",
|
expectedPod: "foo",
|
||||||
|
expectedArgs: []string{"cmd"},
|
||||||
|
name: "pod in flags",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
p: &execParams{},
|
p: &execParams{},
|
||||||
args: []string{"foo"},
|
args: []string{"foo"},
|
||||||
expectError: true,
|
expectError: true,
|
||||||
|
name: "no cmd, w/o flags",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
p: &execParams{},
|
p: &execParams{},
|
||||||
args: []string{"foo", "cmd"},
|
args: []string{"foo", "cmd"},
|
||||||
expectedPod: "foo",
|
expectedPod: "foo",
|
||||||
|
expectedArgs: []string{"cmd"},
|
||||||
|
name: "cmd, w/o flags",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
p: &execParams{containerName: "bar"},
|
p: &execParams{containerName: "bar"},
|
||||||
args: []string{"foo", "cmd"},
|
args: []string{"foo", "cmd"},
|
||||||
expectedPod: "foo",
|
expectedPod: "foo",
|
||||||
expectedContainer: "bar",
|
expectedContainer: "bar",
|
||||||
|
expectedArgs: []string{"cmd"},
|
||||||
|
name: "cmd, container in flag",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
cmd := &cobra.Command{}
|
cmd := &cobra.Command{}
|
||||||
podName, containerName, err := extractPodAndContainer(cmd, test.args, test.p)
|
podName, containerName, args, err := extractPodAndContainer(cmd, test.args, test.p)
|
||||||
if podName != test.expectedPod {
|
if podName != test.expectedPod {
|
||||||
t.Errorf("expected: %s, got: %s", test.expectedPod, podName)
|
t.Errorf("expected: %s, got: %s (%s)", test.expectedPod, podName, test.name)
|
||||||
}
|
}
|
||||||
if containerName != test.expectedContainer {
|
if containerName != test.expectedContainer {
|
||||||
t.Errorf("expected: %s, got: %s", test.expectedContainer, containerName)
|
t.Errorf("expected: %s, got: %s (%s)", test.expectedContainer, containerName, test.name)
|
||||||
}
|
}
|
||||||
if test.expectError && err == nil {
|
if test.expectError && err == nil {
|
||||||
t.Error("unexpected non-error")
|
t.Errorf("unexpected non-error (%s)", test.name)
|
||||||
}
|
}
|
||||||
if !test.expectError && err != nil {
|
if !test.expectError && err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v (%s)", err, test.name)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(test.expectedArgs, args) {
|
||||||
|
t.Errorf("expected: %v, got %v (%s)", test.expectedArgs, args, test.name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user