forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.go
487 lines (410 loc) · 13.1 KB
/
dump.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bytes"
"context"
"fmt"
"io"
"log"
"net"
"os"
"os/exec"
"path/filepath"
"strings"
"golang.org/x/crypto/ssh"
)
// logDumper gets all the nodes from a kubernetes cluster and dumps a well-known set of logs
type logDumper struct {
sshClientFactory sshClientFactory
artifactsDir string
services []string
files []string
// DumpSysctls will record sysctl values from each node
DumpSysctls bool
}
// newLogDumper is the constructor for a logDumper
func newLogDumper(sshClientFactory sshClientFactory, artifactsDir string) (*logDumper, error) {
d := &logDumper{
sshClientFactory: sshClientFactory,
artifactsDir: artifactsDir,
}
d.services = []string{
"node-problem-detector",
"kubelet",
"containerd",
"docker",
"kops-configuration",
"protokube",
}
d.files = []string{
"kube-apiserver",
"kube-scheduler",
"rescheduler",
"kube-controller-manager",
"kops-controller",
"etcd",
"etcd-events",
"glbc",
"cluster-autoscaler",
"kube-addon-manager",
"fluentd",
"kube-proxy",
"node-problem-detector",
"cloud-init-output",
"startupscript",
"kern",
"docker",
}
return d, nil
}
// DumpAllNodes connects to every node from kubectl get nodes and dumps the logs.
// additionalIPs holds IP addresses of instances found by the deployment tool;
// if the IPs are not found from kubectl get nodes, then these will be dumped also.
// This allows for dumping log on nodes even if they don't register as a kubernetes
// node, or if a node fails to register, or if the whole cluster fails to start.
func (d *logDumper) DumpAllNodes(ctx context.Context, additionalIPs []string) error {
var dumped []*node
nodes, err := kubectlGetNodes("")
if err != nil {
log.Printf("Failed to get nodes for dumping via kubectl: %v", err)
} else {
for i := range nodes.Items {
if ctx.Err() != nil {
log.Printf("stopping dumping nodes: %v", ctx.Err())
return ctx.Err()
}
node := &nodes.Items[i]
ip := ""
for _, address := range node.Status.Addresses {
if address.Type == "ExternalIP" {
ip = address.Address
break
}
}
err := d.dumpNode(ctx, node.Metadata.Name, ip)
if err != nil {
log.Printf("could not dump node %s (%s): %v", node.Metadata.Name, ip, err)
} else {
dumped = append(dumped, node)
}
}
}
notDumped := findInstancesNotDumped(additionalIPs, dumped)
for _, ip := range notDumped {
if ctx.Err() != nil {
log.Printf("stopping dumping nodes: %v", ctx.Err())
return ctx.Err()
}
log.Printf("dumping node not registered in kubernetes: %s", ip)
err := d.dumpNode(ctx, ip, ip)
if err != nil {
log.Printf("error dumping node %s: %v", ip, err)
}
}
return nil
}
// findInstancesNotDumped returns ips from the slice that do not appear as any address of the nodes
func findInstancesNotDumped(ips []string, dumped []*node) []string {
var notDumped []string
dumpedAddresses := make(map[string]bool)
for _, node := range dumped {
for _, address := range node.Status.Addresses {
dumpedAddresses[address.Address] = true
}
}
for _, ip := range ips {
if !dumpedAddresses[ip] {
notDumped = append(notDumped, ip)
}
}
return notDumped
}
// DumpNode connects to a node and dumps the logs.
func (d *logDumper) dumpNode(ctx context.Context, name string, ip string) error {
if ip == "" {
return fmt.Errorf("could not find address for %v, ", name)
}
log.Printf("Dumping node %s", name)
n, err := d.connectToNode(ctx, name, ip)
if err != nil {
return fmt.Errorf("could not connect: %w", err)
}
// As long as we connect to the node we will not return an error;
// a failure to collect a log (or even any logs at all) is not
// considered an error in dumping the node.
// TODO(justinsb): clean up / rationalize
errors := n.dump(ctx)
for _, e := range errors {
log.Printf("error dumping node %s: %v", name, e)
}
if err := n.Close(); err != nil {
log.Printf("error closing connection: %v", err)
}
return nil
}
func (d *logDumper) dumpPods(ctx context.Context, namespace string, labelSelector []string) error {
pods, err := kubectlGetPods(ctx, namespace, labelSelector)
if err != nil {
return err
}
for _, pod := range pods.Items {
err = d.dumpPodLogs(ctx, pod)
if err != nil {
log.Printf("error dumping pod logs %s: %v", pod.Metadata.Name, err)
}
}
return nil
}
func (d *logDumper) dumpPodLogs(ctx context.Context, pod pod) error {
logfileName := filepath.Join(d.artifactsDir, pod.Spec.NodeName, fmt.Sprintf("%v.log", pod.Metadata.Name))
if err := os.MkdirAll(filepath.Dir(logfileName), 0755); err != nil {
log.Printf("unable to mkdir on %q: %v", filepath.Dir(logfileName), err)
return err
}
logfile, err := os.Create(logfileName)
if err != nil {
log.Printf("unable to create file %q: %v", logfileName, err)
return err
}
defer logfile.Close()
args := []string{"-n", pod.Metadata.Namespace, "logs", "--all-containers", pod.Metadata.Name}
cmd := exec.CommandContext(ctx, "kubectl", args...)
cmd.Stdout = logfile
cmd.Stderr = logfile
if err := control.FinishRunning(cmd); err != nil {
return err
}
return nil
}
// sshClient is an interface abstracting *ssh.Client, which allows us to test it
type sshClient interface {
io.Closer
// ExecPiped runs the command, piping stdout & stderr
ExecPiped(ctx context.Context, command string, stdout io.Writer, stderr io.Writer) error
}
// sshClientFactory is an interface abstracting to a node over SSH
type sshClientFactory interface {
Dial(ctx context.Context, host string) (sshClient, error)
}
// logDumperNode holds state for a particular node we are dumping
type logDumperNode struct {
client sshClient
dumper *logDumper
dir string
// DumpSysctls will record sysctl values from the node
DumpSysctls bool
}
// connectToNode makes an SSH connection to the node and returns a logDumperNode
func (d *logDumper) connectToNode(ctx context.Context, nodeName string, host string) (*logDumperNode, error) {
client, err := d.sshClientFactory.Dial(ctx, host)
if err != nil {
return nil, fmt.Errorf("unable to SSH to %q: %w", host, err)
}
return &logDumperNode{
client: client,
dir: filepath.Join(d.artifactsDir, nodeName),
dumper: d,
DumpSysctls: d.DumpSysctls,
}, nil
}
// logDumperNode cleans up any state in the logDumperNode
func (n *logDumperNode) Close() error {
return n.client.Close()
}
// dump captures the well-known set of logs
func (n *logDumperNode) dump(ctx context.Context) []error {
if ctx.Err() != nil {
return []error{ctx.Err()}
}
var errors []error
// Capture kernel log
if err := n.shellToFile(ctx, "sudo journalctl --output=short-precise -k", filepath.Join(n.dir, "kern.log")); err != nil {
errors = append(errors, err)
}
// Capture full journal - needed so we can see e.g. disk mounts
// This does duplicate the other files, but ensures we have all output
if err := n.shellToFile(ctx, "sudo journalctl --output=short-precise", filepath.Join(n.dir, "journal.log")); err != nil {
errors = append(errors, err)
}
if n.DumpSysctls {
// Capture sysctls if asked
if err := n.shellToFile(ctx, "sudo sysctl --all", filepath.Join(n.dir, "sysctl.conf")); err != nil {
errors = append(errors, err)
}
}
// Capture logs from any systemd services in our list, that are registered
services, err := n.listSystemdUnits(ctx)
if err != nil {
errors = append(errors, fmt.Errorf("error listing systemd services: %w", err))
}
for _, s := range n.dumper.services {
name := s + ".service"
for _, service := range services {
if service == name {
if err := n.shellToFile(ctx, "sudo journalctl --output=cat -u "+name, filepath.Join(n.dir, s+".log")); err != nil {
errors = append(errors, err)
}
}
}
}
// Capture any file logs where the files exist
fileList, err := n.findFiles(ctx, "/var/log")
if err != nil {
errors = append(errors, fmt.Errorf("error reading /var/log: %w", err))
}
for _, name := range n.dumper.files {
prefix := "/var/log/" + name + ".log"
for _, f := range fileList {
if !strings.HasPrefix(f, prefix) {
continue
}
if err := n.shellToFile(ctx, "sudo cat "+f, filepath.Join(n.dir, filepath.Base(f))); err != nil {
errors = append(errors, err)
}
}
}
return errors
}
// findFiles lists files under the specified directory (recursively)
func (n *logDumperNode) findFiles(ctx context.Context, dir string) ([]string, error) {
var stdout bytes.Buffer
var stderr bytes.Buffer
err := n.client.ExecPiped(ctx, "sudo find "+dir+" -print0", &stdout, &stderr)
if err != nil {
return nil, fmt.Errorf("error listing %q: %w", dir, err)
}
paths := []string{}
for _, b := range bytes.Split(stdout.Bytes(), []byte{0}) {
if len(b) == 0 {
// Likely the last value
continue
}
paths = append(paths, string(b))
}
return paths, nil
}
// listSystemdUnits returns the list of systemd units on the node
func (n *logDumperNode) listSystemdUnits(ctx context.Context) ([]string, error) {
var stdout bytes.Buffer
var stderr bytes.Buffer
err := n.client.ExecPiped(ctx, "sudo systemctl list-units -t service --no-pager --no-legend --all", &stdout, &stderr)
if err != nil {
return nil, fmt.Errorf("error listing systemd units: %w", err)
}
var services []string
for _, line := range strings.Split(stdout.String(), "\n") {
tokens := strings.Fields(line)
if len(tokens) == 0 || tokens[0] == "" {
continue
}
services = append(services, tokens[0])
}
return services, nil
}
// shellToFile executes a command and copies the output to a file
func (n *logDumperNode) shellToFile(ctx context.Context, command string, destPath string) error {
if err := os.MkdirAll(filepath.Dir(destPath), 0755); err != nil {
log.Printf("unable to mkdir on %q: %v", filepath.Dir(destPath), err)
}
f, err := os.Create(destPath)
if err != nil {
return fmt.Errorf("error creating file %q: %w", destPath, err)
}
defer f.Close()
if err := n.client.ExecPiped(ctx, command, f, f); err != nil {
return fmt.Errorf("error executing command %q: %w", command, err)
}
return nil
}
// sshClientImplementation is the default implementation of sshClient, binding to a *ssh.Client
type sshClientImplementation struct {
client *ssh.Client
}
var _ sshClient = &sshClientImplementation{}
// ExecPiped implements sshClientImplementation::ExecPiped
func (s *sshClientImplementation) ExecPiped(ctx context.Context, cmd string, stdout io.Writer, stderr io.Writer) error {
if ctx.Err() != nil {
return ctx.Err()
}
finished := make(chan error)
go func() {
session, err := s.client.NewSession()
if err != nil {
finished <- fmt.Errorf("error creating ssh session: %w", err)
return
}
defer session.Close()
if verbose {
log.Printf("Running SSH command: %v", cmd)
}
session.Stdout = stdout
session.Stderr = stderr
finished <- session.Run(cmd)
}()
select {
case <-ctx.Done():
log.Print("closing SSH tcp connection due to context completion")
// terminate the TCP connection to force a disconnect - we assume everyone is using the same context.
// We could make this better by sending a signal on the session, waiting and then closing the session,
// and only if we still haven't succeeded then closing the TCP connection. This is sufficient for our
// current usage though - and hopefully that logic will be implemented in the SSH package itself.
s.Close()
<-finished // Wait for cancellation
return ctx.Err()
case err := <-finished:
return err
}
}
// Close implements sshClientImplementation::Close
func (s *sshClientImplementation) Close() error {
return s.client.Close()
}
// sshClientFactoryImplementation is the default implementation of sshClientFactory
type sshClientFactoryImplementation struct {
sshConfig *ssh.ClientConfig
}
var _ sshClientFactory = &sshClientFactoryImplementation{}
// Dial implements sshClientFactory::Dial
func (f *sshClientFactoryImplementation) Dial(ctx context.Context, host string) (sshClient, error) {
addr := host + ":22"
d := net.Dialer{}
conn, err := d.DialContext(ctx, "tcp", addr)
if err != nil {
return nil, err
}
// We have a TCP connection; we will force-close it to support context cancellation
var client *ssh.Client
finished := make(chan error)
go func() {
c, chans, reqs, err := ssh.NewClientConn(conn, addr, f.sshConfig)
if err == nil {
client = ssh.NewClient(c, chans, reqs)
}
finished <- err
}()
select {
case <-ctx.Done():
log.Print("cancelling SSH tcp connection due to context completion")
conn.Close() // Close the TCP connection to force cancellation
<-finished // Wait for cancellation
return nil, ctx.Err()
case err := <-finished:
if err != nil {
return nil, err
}
return &sshClientImplementation{
client: client,
}, nil
}
}