-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_strategy.go
52 lines (45 loc) · 1.27 KB
/
run_strategy.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
package executor
import (
"bytes"
"github.com/dhenkel92/pod-helper/pkg/config"
"github.com/dhenkel92/pod-helper/pkg/types"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/remotecommand"
)
func RunStrategy(c chan bool, podExecutor *PodExecutor, conf *config.Config, result *types.Result) {
req := podExecutor.Clientset.
CoreV1().
RESTClient().
Post().
Resource("pods").
Name(result.Pod.Name).
Namespace(result.Pod.Namespace).
SubResource("exec")
option := &v1.PodExecOptions{
Container: result.Container.Name,
Command: append(conf.RunConfig.Entrypoint, conf.RunConfig.Command),
Stdin: false,
Stdout: true,
Stderr: true,
}
req.VersionedParams(option, scheme.ParameterCodec)
exec, err := remotecommand.NewSPDYExecutor(podExecutor.Config, "POST", req.URL())
if err != nil {
result.ExecResult = types.ExecResult{Error: err}
c <- true
return
}
var stdout, stderr bytes.Buffer
err = exec.Stream(remotecommand.StreamOptions{
Stdout: &stdout,
Stderr: &stderr,
})
if err != nil {
result.ExecResult = types.ExecResult{Error: err, StdErr: stderr.String(), StdOut: stdout.String()}
c <- true
return
}
result.ExecResult = types.ExecResult{StdOut: stdout.String(), StdErr: stderr.String()}
c <- true
}