mirror of
https://github.com/kubernetes-sigs/kind.git
synced 2025-11-30 23:16:04 +07:00
refactor and conslidate per-node common log collection
This commit is contained in:
@@ -3,51 +3,8 @@ package common
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"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",
|
|
||||||
),
|
|
||||||
execToPathFn(
|
|
||||||
n.Command("crictl", "images"),
|
|
||||||
"images.log",
|
|
||||||
),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// FileOnHost is a helper to create a file at path
|
// FileOnHost is a helper to create a file at path
|
||||||
// even if the parent directory doesn't exist
|
// even if the parent directory doesn't exist
|
||||||
// in which case it will be created with ModePerm
|
// in which case it will be created with ModePerm
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import (
|
|||||||
"sigs.k8s.io/kind/pkg/cluster/nodes"
|
"sigs.k8s.io/kind/pkg/cluster/nodes"
|
||||||
"sigs.k8s.io/kind/pkg/cluster/nodeutils"
|
"sigs.k8s.io/kind/pkg/cluster/nodeutils"
|
||||||
"sigs.k8s.io/kind/pkg/errors"
|
"sigs.k8s.io/kind/pkg/errors"
|
||||||
|
"sigs.k8s.io/kind/pkg/exec"
|
||||||
"sigs.k8s.io/kind/pkg/log"
|
"sigs.k8s.io/kind/pkg/log"
|
||||||
|
|
||||||
internalcreate "sigs.k8s.io/kind/pkg/cluster/internal/create"
|
internalcreate "sigs.k8s.io/kind/pkg/cluster/internal/create"
|
||||||
@@ -264,19 +265,8 @@ func (p *Provider) CollectLogs(name, dir string) error {
|
|||||||
node := n // https://golang.org/doc/faq#closures_and_goroutines
|
node := n // https://golang.org/doc/faq#closures_and_goroutines
|
||||||
name := node.String()
|
name := node.String()
|
||||||
path := filepath.Join(dir, name)
|
path := filepath.Join(dir, name)
|
||||||
fns = append(fns, func() error {
|
|
||||||
return internallogs.DumpDir(p.logger, node, "/var/log", path)
|
|
||||||
})
|
|
||||||
fns = append(fns,
|
fns = append(fns,
|
||||||
func() error { return common.CollectLogs(node, path) },
|
func() error { return collectNodeLogs(p.logger, node, path) },
|
||||||
func() error {
|
|
||||||
f, err := common.FileOnHost(filepath.Join(path, "serial.log"))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
return node.SerialLogs(f)
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
errs = append(errs, errors.AggregateConcurrent(fns))
|
errs = append(errs, errors.AggregateConcurrent(fns))
|
||||||
@@ -287,3 +277,51 @@ func (p *Provider) CollectLogs(name, dir string) error {
|
|||||||
// flatten
|
// flatten
|
||||||
return errors.NewAggregate(errs)
|
return errors.NewAggregate(errs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func collectNodeLogs(logger log.Logger, n nodes.Node, dir string) error {
|
||||||
|
execToPathFn := func(cmd exec.Cmd, path string) func() error {
|
||||||
|
return func() error {
|
||||||
|
f, err := common.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{
|
||||||
|
func() error {
|
||||||
|
f, err := common.FileOnHost(filepath.Join(dir, "serial.log"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
return n.SerialLogs(f)
|
||||||
|
},
|
||||||
|
func() error {
|
||||||
|
return internallogs.DumpDir(logger, n, "/var/log", dir)
|
||||||
|
},
|
||||||
|
// 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",
|
||||||
|
),
|
||||||
|
execToPathFn(
|
||||||
|
n.Command("crictl", "images"),
|
||||||
|
"images.log",
|
||||||
|
),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user