-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathexec.go
66 lines (58 loc) · 1.48 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
package exec
import (
"bytes"
"context"
"fmt"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/remotecommand"
)
func InPod(
scheme *runtime.Scheme,
config *rest.Config,
namespace, name, container string,
cmd []string,
) (string, string, error) {
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return "", "", errors.Wrapf(err, "failed to create kubernetes clientset")
}
req := clientset.CoreV1().RESTClient().Post().
Resource("pods").
Name(name).
Namespace(namespace).
SubResource("exec")
parameterCodec := runtime.NewParameterCodec(scheme)
req.VersionedParams(&corev1.PodExecOptions{
Command: cmd,
Container: container,
Stdin: false,
Stdout: true,
Stderr: true,
TTY: false,
}, parameterCodec)
exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
if err != nil {
return "", "", errors.Wrapf(err, "failed to initialize SPDY executor")
}
var stdout, stderr bytes.Buffer
err = exec.StreamWithContext(
context.TODO(),
remotecommand.StreamOptions{
Stdin: nil,
Stdout: &stdout,
Stderr: &stderr,
Tty: false,
},
)
if err != nil {
return stdout.String(), stderr.String(), errors.Wrapf(
err,
fmt.Sprintf("failed to stream execution results back, stdout:\n\"%s\"stderr:\n\"%s\"", stdout.String(), stderr.String()),
)
}
return stdout.String(), stderr.String(), nil
}