forked from Azure/AgentBaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.go
208 lines (175 loc) · 5.8 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package e2e_test
import (
"bytes"
"context"
"fmt"
"log"
"strings"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/remotecommand"
)
const (
sshCommandTemplate = `echo '%s' > sshkey%[2]s && chmod 0600 sshkey%[2]s && ssh -i sshkey%[2]s -o PasswordAuthentication=no -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ConnectTimeout=5 azureuser@%s`
)
type podExecResult struct {
exitCode string
stderr, stdout *bytes.Buffer
}
func (r podExecResult) dumpAll() {
r.dumpStdout()
r.dumpStderr()
}
func (r podExecResult) dumpStdout() {
if r.stdout != nil {
stdoutContent := r.stdout.String()
if stdoutContent != "" && stdoutContent != "<nil>" {
log.Printf("%s\n%s\n%s\n%s",
"dumping stdout:",
"----------------------------------- begin stdout -----------------------------------",
stdoutContent,
"------------------------------------ end stdout ------------------------------------")
}
}
}
func (r podExecResult) dumpStderr() {
if r.stderr != nil {
stderrContent := r.stderr.String()
if stderrContent != "" && stderrContent != "<nil>" {
log.Printf("%s\n%s\n%s\n%s",
"dumping stderr:",
"----------------------------------- begin stderr -----------------------------------",
stderrContent,
"------------------------------------ end stderr ------------------------------------")
}
}
}
func extractLogsFromVM(ctx context.Context, vmssName, privateIP, sshPrivateKey string, opts *scenarioRunOpts) (map[string]string, error) {
commandList := map[string]string{
"/var/log/azure/cluster-provision.log": "cat /var/log/azure/cluster-provision.log",
"kubelet.log": "journalctl -u kubelet",
"/var/log/azure/cluster-provision-cse-output.log": "cat /var/log/azure/cluster-provision-cse-output.log",
"sysctl-out.log": "sysctl -a",
}
podName, err := getDebugPodName(opts.clusterConfig.kube)
if err != nil {
return nil, fmt.Errorf("unable to get debug pod name: %w", err)
}
var result = map[string]string{}
for file, sourceCmd := range commandList {
log.Printf("executing command on remote VM at %s of VMSS %s: %q", privateIP, vmssName, sourceCmd)
execResult, err := execOnVM(ctx, opts.clusterConfig.kube, privateIP, podName, sshPrivateKey, sourceCmd, false)
if execResult != nil {
execResult.dumpStderr()
}
if err != nil {
return nil, err
}
result[file] = execResult.stdout.String()
}
return result, nil
}
func extractClusterParameters(ctx context.Context, kube *kubeclient) (map[string]string, error) {
commandList := map[string]string{
"/etc/kubernetes/azure.json": "cat /etc/kubernetes/azure.json",
"/etc/kubernetes/certs/ca.crt": "cat /etc/kubernetes/certs/ca.crt",
"/var/lib/kubelet/bootstrap-kubeconfig": "cat /var/lib/kubelet/bootstrap-kubeconfig",
}
podName, err := getDebugPodName(kube)
if err != nil {
return nil, err
}
var result = map[string]string{}
for file, sourceCmd := range commandList {
log.Printf("executing privileged command on pod %s/%s: %q", defaultNamespace, podName, sourceCmd)
execResult, err := execOnPrivilegedPod(ctx, kube, defaultNamespace, podName, sourceCmd)
if execResult != nil {
execResult.dumpStderr()
}
if err != nil {
return nil, err
}
result[file] = execResult.stdout.String()
}
return result, nil
}
func execOnVM(ctx context.Context, kube *kubeclient, vmPrivateIP, jumpboxPodName, sshPrivateKey, command string, isShellBuiltIn bool) (*podExecResult, error) {
sshCommand := fmt.Sprintf(sshCommandTemplate, sshPrivateKey, strings.ReplaceAll(vmPrivateIP, ".", ""), vmPrivateIP)
if !isShellBuiltIn {
sshCommand = sshCommand + " sudo"
}
commandToExecute := fmt.Sprintf("%s %s", sshCommand, command)
execResult, err := execOnPrivilegedPod(ctx, kube, defaultNamespace, jumpboxPodName, commandToExecute)
if err != nil {
return nil, fmt.Errorf("error executing command on pod: %w", err)
}
return execResult, nil
}
func execOnPrivilegedPod(ctx context.Context, kube *kubeclient, namespace, podName string, command string) (*podExecResult, error) {
privilegedCommand := append(nsenterCommandArray(), command)
return execOnPod(ctx, kube, namespace, podName, privilegedCommand)
}
func execOnPod(ctx context.Context, kube *kubeclient, namespace, podName string, command []string) (*podExecResult, error) {
req := kube.typed.CoreV1().RESTClient().Post().Resource("pods").Name(podName).Namespace(namespace).SubResource("exec")
option := &corev1.PodExecOptions{
Command: command,
Stdout: true,
Stderr: true,
}
req.VersionedParams(
option,
scheme.ParameterCodec,
)
exec, err := remotecommand.NewSPDYExecutor(kube.rest, "POST", req.URL())
if err != nil {
return nil, fmt.Errorf("unable to create new SPDY executor for pod exec: %w", err)
}
var (
stdout, stderr bytes.Buffer
exitCode string = "0"
)
err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{
Stdout: &stdout,
Stderr: &stderr,
})
if err != nil {
if strings.Contains(err.Error(), "command terminated with exit code") {
code, err := extractExitCode(err.Error())
if err != nil {
return nil, fmt.Errorf("error extracing exit code from remote command execution error msg: %w", err)
}
exitCode = code
} else {
return nil, fmt.Errorf("encountered unexpected error when executing command on pod: %w", err)
}
}
return &podExecResult{
exitCode: exitCode,
stdout: &stdout,
stderr: &stderr,
}, nil
}
func getWasmCurlCommand(url string) string {
return fmt.Sprintf(`curl \
--connect-timeout 5 \
--max-time 10 \
--retry 10 \
--retry-max-time 100 \
%s`, url)
}
func bashCommandArray() []string {
return []string{
"/bin/bash",
"-c",
}
}
func nsenterCommandArray() []string {
return []string{
"nsenter",
"-t",
"1",
"-m",
"bash",
"-c",
}
}