-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexec.go
More file actions
85 lines (76 loc) · 2.03 KB
/
exec.go
File metadata and controls
85 lines (76 loc) · 2.03 KB
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
package kube
import (
"bytes"
"context"
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/remotecommand"
)
type ExecRequestMethod string
const (
ExecPostRequest ExecRequestMethod = "POST"
ExecGetRequest ExecRequestMethod = "GET"
)
// Exec is like kubectl exec.
func (c *Client) Exec(pod, container, namespace string, command ...string) (string, string, error) {
return c.exec(context.TODO(), pod, container, namespace, command...)
}
// ExecWithContext executes exec with context.
func (c *Client) ExecWithContext(ctx context.Context, pod, container, namespace string, command ...string) (string, string, error) {
return c.exec(ctx, pod, container, namespace, command...)
}
func (c *Client) exec(ctx context.Context, pod, container, namespace string, command ...string) (string, string, error) {
err := c.validateExecResource(ctx, pod, container, namespace)
if err != nil {
return "", "", err
}
req := c.RemoteExecRequest(ExecPostRequest, pod, namespace, &corev1.PodExecOptions{
TypeMeta: metav1.TypeMeta{},
Stdout: true,
Stderr: true,
Container: container,
Command: command,
})
var (
stderr bytes.Buffer
stdout bytes.Buffer
)
restcfg, err := c.RestConfig()
if err != nil {
return "", "", err
}
exec, err := remotecommand.NewSPDYExecutor(restcfg, "POST", req.URL())
if err != nil {
return "", "", err
}
err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{
Stdin: nil,
Stdout: &stdout,
Stderr: &stderr,
})
if err != nil {
return "", "", err
}
return strings.TrimSpace(stdout.String()), strings.TrimSpace(stderr.String()), err
}
func (c *Client) validateExecResource(ctx context.Context, pod, container, namespace string) error {
p, err := c.GetPod(ctx, namespace, pod)
if err != nil {
return err
}
if container != "" {
found := false
for _, co := range p.Spec.Containers {
if co.Name == container {
found = true
break
}
}
if !found {
return fmt.Errorf("%s not found", container)
}
}
return nil
}