-
Notifications
You must be signed in to change notification settings - Fork 7
/
execute.go
139 lines (128 loc) · 3.79 KB
/
execute.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package kommons
import (
"bytes"
"context"
"fmt"
"time"
utils "github.com/flanksource/commons/utils"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/remotecommand"
)
// ExecutePodf runs the specified shell command inside a container of the specified pod
func (c *Client) ExecutePodf(namespace, pod, container string, command ...string) (string, string, error) {
client, err := c.GetClientset()
if err != nil {
return "", "", fmt.Errorf("executePodf: Failed to get clientset: %v", err)
}
c.Debugf("[%s/%s/%s] %s", namespace, pod, container, command)
const tty = false
req := client.CoreV1().RESTClient().Post().
Resource("pods").
Name(pod).
Namespace(namespace).
SubResource("exec").
Param("container", container)
req.VersionedParams(&v1.PodExecOptions{
Container: container,
Command: command,
Stdin: false,
Stdout: true,
Stderr: true,
TTY: tty,
}, scheme.ParameterCodec)
rc, err := c.GetRESTConfig()
if err != nil {
return "", "", fmt.Errorf("ExecutePodf: Failed to get REST config: %v", err)
}
exec, err := remotecommand.NewSPDYExecutor(rc, "POST", req.URL())
if err != nil {
return "", "", fmt.Errorf("ExecutePodf: Failed to get SPDY Executor: %v", err)
}
var stdout, stderr bytes.Buffer
err = exec.Stream(remotecommand.StreamOptions{
Stdin: nil,
Stdout: &stdout,
Stderr: &stderr,
Tty: tty,
})
_stdout := safeString(&stdout)
_stderr := safeString(&stderr)
if err != nil {
return _stdout, _stderr, fmt.Errorf("exec returned an error: %+v", err)
}
c.Tracef("[%s/%s/%s] %s => %s %s ", namespace, pod, container, command, _stdout, _stderr)
return _stdout, _stderr, nil
}
// Executef runs the specified shell command on a node by creating
// a pre-scheduled pod that runs in the host namespace
func (c *Client) Executef(node string, timeout time.Duration, command string, args ...interface{}) (string, error) {
client, err := c.GetClientset()
if err != nil {
return "", fmt.Errorf("executef: Failed to get clientset: %v", err)
}
pods := client.CoreV1().Pods("kube-system")
command = fmt.Sprintf(command, args...)
pod, err := pods.Create(context.TODO(), &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("command-%s-%s", node, utils.ShortTimestamp()),
},
Spec: NewCommandJob(node, command),
}, metav1.CreateOptions{})
c.Tracef("[%s] executing '%s' in pod %s", node, command, pod.Name)
if err != nil {
return "", fmt.Errorf("executef: Failed to create pod: %v", err)
}
defer pods.Delete(context.TODO(), pod.ObjectMeta.Name, metav1.DeleteOptions{}) // nolint: errcheck
logs := pods.GetLogs(pod.Name, &v1.PodLogOptions{
Container: pod.Spec.Containers[0].Name,
})
err = c.WaitForPod("kube-system", pod.ObjectMeta.Name, timeout, v1.PodSucceeded)
logString := read(logs)
if err != nil {
return logString, fmt.Errorf("failed to execute command, pod did not complete: %v", err)
}
c.Tracef("[%s] stdout: %s", node, logString)
return logString, nil
}
func NewCommandJob(node, command string) v1.PodSpec {
yes := true
return v1.PodSpec{
RestartPolicy: v1.RestartPolicyNever,
NodeName: node,
Volumes: []v1.Volume{{
Name: "root",
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: "/",
},
},
}},
Containers: []v1.Container{{
Name: "shell",
Image: "docker.io/ubuntu:18.04",
Command: []string{
"sh",
"-c",
"chroot /chroot bash -c \"" + command + "\"",
},
VolumeMounts: []v1.VolumeMount{{
Name: "root",
MountPath: "/chroot",
}},
SecurityContext: &v1.SecurityContext{
Privileged: &yes,
},
}},
Tolerations: []v1.Toleration{
{
// tolerate all values
Operator: "Exists",
},
},
HostNetwork: true,
HostPID: true,
HostIPC: true,
}
}