/
logs.go
54 lines (50 loc) · 1.31 KB
/
logs.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
package common
import (
"os"
"path/filepath"
"sigs.k8s.io/kind/pkg/cluster/nodes"
"sigs.k8s.io/kind/pkg/errors"
"sigs.k8s.io/kind/pkg/exec"
)
// CollectLogs provides the common functionality
// to get various debug info from the node
func CollectLogs(n nodes.Node, dir string) error {
execToPathFn := func(cmd exec.Cmd, path string) func() error {
return func() error {
f, err := FileOnHost(filepath.Join(dir, path))
if err != nil {
return err
}
defer f.Close()
return cmd.SetStdout(f).SetStderr(f).Run()
}
}
return errors.AggregateConcurrent([]func() error{
// record info about the node container
execToPathFn(
n.Command("cat", "/kind/version"),
"kubernetes-version.txt",
),
execToPathFn(
n.Command("journalctl", "--no-pager"),
"journal.log",
),
execToPathFn(
n.Command("journalctl", "--no-pager", "-u", "kubelet.service"),
"kubelet.log",
),
execToPathFn(
n.Command("journalctl", "--no-pager", "-u", "containerd.service"),
"containerd.log",
),
})
}
// FileOnHost is a helper to create a file at path
// even if the parent directory doesn't exist
// in which case it will be created with ModePerm
func FileOnHost(path string) (*os.File, error) {
if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
return nil, err
}
return os.Create(path)
}