-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.go
108 lines (90 loc) · 2.35 KB
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package k8t
import (
"bytes"
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/remotecommand"
)
type ExecOpts struct {
// here you can pass your context. It it's not set, the default
// context.Background() will be used
Context context.Context
// namespace where is pod for exec. If it's not set, the cluster's
// test namespace will be used.
Namespace string
}
type ExecResult struct {
Stdout bytes.Buffer
Stderr bytes.Buffer
Err error
}
func (e ExecResult) String() string {
if e.Err != nil {
return fmt.Sprintf("%s \n %s", e.Stdout.String(), e.Err.Error())
} else {
return e.Stdout.String()
}
}
func errorResult(err error) ExecResult {
return ExecResult{
Err: err,
}
}
// Less verbose version of ExecWithOpts, which will format and executes given
// command with args with '/bin/sh' shell in given pod.
func (c *Cluster) Execf(pod, container, command string, args ...any) ExecResult {
formatted := fmt.Sprintf(command, args...)
cmd := []string{
"/bin/sh",
"-c",
formatted,
}
return c.ExecWithOpts(pod, container, cmd, ExecOpts{})
}
// function executes command as array (without shell) in given pod
// and container. It's very general function.
func (c *Cluster) ExecWithOpts(pod string, container string, command []string, opts ExecOpts) ExecResult {
var err error
ctx := opts.Context
if ctx == nil {
ctx = context.Background()
}
namespace := opts.Namespace
if namespace == "" {
namespace = c.testNamespace
}
scheme := runtime.NewScheme()
if err = corev1.AddToScheme(scheme); err != nil {
return errorResult(err)
}
parameterCodec := runtime.NewParameterCodec(scheme)
execOpts := &corev1.PodExecOptions{
Command: command,
Container: container,
Stdin: false,
Stdout: true,
Stderr: true,
TTY: true,
}
req := c.k8sClient.CoreV1().RESTClient().Post().Resource("pods").Name(pod).Namespace(namespace).SubResource("exec")
req.VersionedParams(execOpts, parameterCodec)
exec, err := remotecommand.NewSPDYExecutor(c.restConfig, "POST", req.URL())
if err != nil {
return errorResult(err)
}
var stdout bytes.Buffer
var stderr bytes.Buffer
err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{
Stdin: nil,
Stdout: &stdout,
Stderr: &stderr,
Tty: true,
})
return ExecResult{
Stdout: stdout,
Stderr: stderr,
Err: err,
}
}