-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.go
64 lines (59 loc) · 1.33 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
// Copyright 2022 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package kubernetes
import (
"bytes"
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/remotecommand"
"k8s.io/kubectl/pkg/scheme"
)
func ExecuteInPod(
ctx context.Context,
c kubernetes.Interface, restCfg *rest.Config,
podNamespace, podName, container string,
command ...string,
) (stdout, stderr string, err error) {
buf := &bytes.Buffer{}
errBuf := &bytes.Buffer{}
request := c.CoreV1().RESTClient().
Post().
Namespace(podNamespace).
Resource("pods").
Name(podName).
SubResource("exec").
VersionedParams(&corev1.PodExecOptions{
Command: command,
Stdin: false,
Stdout: true,
Stderr: true,
TTY: false,
}, scheme.ParameterCodec)
exec, err := remotecommand.NewSPDYExecutor(restCfg, "POST", request.URL())
if err != nil {
return "", "", fmt.Errorf(
"%w Failed executing command %s on %v/%v",
err,
command,
podNamespace,
podName,
)
}
err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{
Stdout: buf,
Stderr: errBuf,
})
if err != nil {
return "", "", fmt.Errorf(
"%w Failed executing command %s on %v/%v",
err,
command,
podNamespace,
podName,
)
}
return buf.String(), errBuf.String(), nil
}