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

Use default container if not specified in KubeExec function #357

Merged
merged 2 commits into from
Oct 18, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ KubeExec is similar to running

`namespace`, Yes, `string`, namespace in which to execute
`pod`, Yes, `string`, name of the pod in which to execute
`container`, Yes, `string`, name of the container in which to execute
`container`, No , `string`, (required if pod contains more than 1 container) name of the container in which to execute
`command`, Yes, `[]string`, command list to execute

Example:
Expand Down
4 changes: 2 additions & 2 deletions pkg/function/kube_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (kef *kubeExecFunc) Exec(ctx context.Context, tp param.TemplateParams, args
if err = Arg(args, KubeExecPodNameArg, &pod); err != nil {
return nil, err
}
if err = Arg(args, KubeExecContainerNameArg, &container); err != nil {
if err = OptArg(args, KubeExecContainerNameArg, &container, ""); err != nil {
return nil, err
}
if err = Arg(args, KubeExecCommandArg, &cmd); err != nil {
Expand All @@ -105,5 +105,5 @@ func (kef *kubeExecFunc) Exec(ctx context.Context, tp param.TemplateParams, args
}

func (*kubeExecFunc) RequiredArgs() []string {
return []string{KubeExecNamespaceArg, KubeExecPodNameArg, KubeExecContainerNameArg, KubeExecCommandArg}
return []string{KubeExecNamespaceArg, KubeExecPodNameArg, KubeExecCommandArg}
}
8 changes: 6 additions & 2 deletions pkg/kube/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ func ExecWithOptions(kubeCli kubernetes.Interface, options ExecOptions) (string,
Resource("pods").
Name(options.PodName).
Namespace(options.Namespace).
SubResource("exec").
Param("container", options.ContainerName)
SubResource("exec")

// Add container name if passed
if len(options.ContainerName) != 0 {
req.Param("container", options.ContainerName)
}
for _, c := range options.Command {
req.Param("command", c)
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/kube/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (s *ExecSuite) TearDownSuite(c *C) {
c.Assert(err, IsNil)
}
}

func (s *ExecSuite) TestExecEcho(c *C) {
cmd := []string{"sh", "-c", "cat -"}
c.Assert(s.pod.Status.Phase, Equals, v1.PodRunning)
Expand All @@ -85,3 +86,13 @@ func (s *ExecSuite) TestExecEcho(c *C) {
c.Assert(stderr, Equals, "")
}
}

func (s *ExecSuite) TestExecEchoDefaultContainer(c *C) {
cmd := []string{"sh", "-c", "cat -"}
c.Assert(s.pod.Status.Phase, Equals, v1.PodRunning)
c.Assert(len(s.pod.Status.ContainerStatuses) > 0, Equals, true)
stdout, stderr, err := Exec(s.cli, s.pod.Namespace, s.pod.Name, "", cmd, bytes.NewBufferString("badabing"))
c.Assert(err, IsNil)
c.Assert(stdout, Equals, "badabing")
c.Assert(stderr, Equals, "")
}