Skip to content

Commit

Permalink
Use default container if not specified in KubeExec function (#357)
Browse files Browse the repository at this point in the history
Add unit test

Signed-off-by: Prasad Ghangal <prasad.ghangal@gmail.com>
  • Loading branch information
PrasadG193 authored and mergify[bot] committed Oct 18, 2019
1 parent 007759a commit 3a6e5b1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
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, "")
}

0 comments on commit 3a6e5b1

Please sign in to comment.