/
exec.go
50 lines (43 loc) · 1.05 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
package ran
import (
"net/url"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/remotecommand"
)
type executor struct {
config *rest.Config
client rest.Interface
impl func(config *rest.Config, method string, url *url.URL) (remotecommand.Executor, error)
}
func newExecutor(config *rest.Config, client rest.Interface) executor {
var e executor
e.config = config
e.client = client
e.impl = remotecommand.NewSPDYExecutor
return e
}
func (e executor) execute(pod, namespace string, command []string, opts remotecommand.StreamOptions) error {
req := e.client.Post().
Resource("pods").
Name(pod).
Namespace(namespace).
SubResource("exec").
Param("container", ContainerName)
if opts.Stdin != nil {
req = req.Param("stdin", "true")
}
if opts.Stdout != nil {
req = req.Param("stdout", "true")
}
if opts.Stderr != nil {
req = req.Param("stderr", "true")
}
for _, cmd := range command {
req = req.Param("command", cmd)
}
executor, err := e.impl(e.config, "POST", req.URL())
if err != nil {
return err
}
return executor.Stream(opts)
}