Skip to content

Commit

Permalink
feat: Add ExecWithContext (#10)
Browse files Browse the repository at this point in the history
* exec parameters

* exec test

* exec with context
  • Loading branch information
shipengqi authored Feb 9, 2023
1 parent 58ff48e commit add2c22
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 17 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,9 @@ You can find the docs at [go docs](https://pkg.go.dev/github.com/shipengqi/kube)
```bash
go test -v . -kubeconfig <kubeconfig file>
```

## Test Client.Exec

```bash
go test -v -coverprofile=coverage.out -kubeconfig <kubeconfig file> -container <container name> -pod <pod name> -namespace <namespace> .
```
3 changes: 3 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ type testcm struct {

func TestMain(m *testing.M) {
flag.StringVar(&kubeconfig, "kubeconfig", "", "kubeconfig file")
flag.StringVar(&podname, "pod", "", "pod name for exec")
flag.StringVar(&containername, "container", "", "container name for exec")
flag.StringVar(&namespace, "namespace", "", "namespace for exec")
flag.Parse()

flags := genericclioptions.NewConfigFlags(false)
Expand Down
13 changes: 11 additions & 2 deletions exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ import (

// Exec is like kubectl exec.
func (c *Client) Exec(pod, container, namespace string, command ...string) (string, string, error) {
p, err := c.GetPod(context.TODO(), namespace, pod)
return c.exec(context.TODO(), pod, container, namespace, command...)
}

// ExecWithContext executes exec with context.
func (c *Client) ExecWithContext(ctx context.Context, pod, container, namespace string, command ...string) (string, string, error) {
return c.exec(ctx, pod, container, namespace, command...)
}

func (c *Client) exec(ctx context.Context, pod, container, namespace string, command ...string) (string, string, error) {
p, err := c.GetPod(ctx, namespace, pod)
if err != nil {
return "", "", err
}
Expand Down Expand Up @@ -43,7 +52,7 @@ func (c *Client) Exec(pod, container, namespace string, command ...string) (stri
if err != nil {
return "", "", err
}
err = exec.Stream(remotecommand.StreamOptions{
err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{
Stdin: nil,
Stdout: &stdout,
Stderr: &stderr,
Expand Down
41 changes: 26 additions & 15 deletions exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,37 @@ const (
)

func TestClientExec(t *testing.T) {
list, err := mockcli.GetPods(context.TODO(), _defaultTestNamespace)
require.NoError(t, err)
require.NotEqual(t, len(list.Items), 0)

var (
podName string
containerName string
)
// exec coredns
for _, v := range list.Items {
if strings.Contains(strings.ToLower(v.Name), _defaultTestPod) {
podName = v.Name
containerName = v.Spec.Containers[0].Name
break

var containerName string

podNamespace := _defaultTestNamespace
podName := _defaultTestPod

if namespace != "" {
podNamespace = namespace
}

if podname != "" && containername != "" {
podName = podname
containerName = containername
} else {
list, err := mockcli.GetPods(context.TODO(), podNamespace)
require.NoError(t, err)
require.NotEqual(t, len(list.Items), 0)

// exec coredns
for _, v := range list.Items {
if strings.Contains(strings.ToLower(v.Name), podName) {
podName = v.Name
containerName = v.Spec.Containers[0].Name
break
}
}
}

// Todo, create a pod, and test Exec(), currently just skip validating the error
t.Log("exec:", podName, containerName)
stdout, _, err := mockcli.Exec(podName, containerName, _defaultTestNamespace, "/bin/sh", "-c", "/bin/ls /")
stdout, _, err := mockcli.Exec(podName, containerName, podNamespace, "/bin/sh", "-c", "/bin/ls /")
require.Error(t, err)
t.Log(stdout)
}

0 comments on commit add2c22

Please sign in to comment.