Skip to content

Commit b12cafe

Browse files
authored
Merge pull request #3986 from BenTheElder/refactor
refactor log collection
2 parents 283d3d3 + 9f2c810 commit b12cafe

File tree

5 files changed

+82
-104
lines changed

5 files changed

+82
-104
lines changed

pkg/cluster/internal/providers/common/logs.go

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,8 @@ package common
33
import (
44
"os"
55
"path/filepath"
6-
7-
"sigs.k8s.io/kind/pkg/cluster/nodes"
8-
"sigs.k8s.io/kind/pkg/errors"
9-
"sigs.k8s.io/kind/pkg/exec"
106
)
117

12-
// CollectLogs provides the common functionality
13-
// to get various debug info from the node
14-
func CollectLogs(n nodes.Node, dir string) error {
15-
execToPathFn := func(cmd exec.Cmd, path string) func() error {
16-
return func() error {
17-
f, err := FileOnHost(filepath.Join(dir, path))
18-
if err != nil {
19-
return err
20-
}
21-
defer f.Close()
22-
return cmd.SetStdout(f).SetStderr(f).Run()
23-
}
24-
}
25-
26-
return errors.AggregateConcurrent([]func() error{
27-
// record info about the node container
28-
execToPathFn(
29-
n.Command("cat", "/kind/version"),
30-
"kubernetes-version.txt",
31-
),
32-
execToPathFn(
33-
n.Command("journalctl", "--no-pager"),
34-
"journal.log",
35-
),
36-
execToPathFn(
37-
n.Command("journalctl", "--no-pager", "-u", "kubelet.service"),
38-
"kubelet.log",
39-
),
40-
execToPathFn(
41-
n.Command("journalctl", "--no-pager", "-u", "containerd.service"),
42-
"containerd.log",
43-
),
44-
execToPathFn(
45-
n.Command("crictl", "images"),
46-
"images.log",
47-
),
48-
})
49-
}
50-
518
// FileOnHost is a helper to create a file at path
529
// even if the parent directory doesn't exist
5310
// in which case it will be created with ModePerm

pkg/cluster/internal/providers/docker/provider.go

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -254,34 +254,20 @@ func (p *provider) CollectLogs(dir string, nodes []nodes.Node) error {
254254
filepath.Join(dir, "docker-info.txt"),
255255
),
256256
}
257-
258-
// collect /var/log for each node and plan collecting more logs
259-
var errs []error
257+
// inspect each node
260258
for _, n := range nodes {
261259
node := n // https://golang.org/doc/faq#closures_and_goroutines
262260
name := node.String()
263261
path := filepath.Join(dir, name)
264-
if err := internallogs.DumpDir(p.logger, node, "/var/log", path); err != nil {
265-
errs = append(errs, err)
266-
}
267-
262+
fns = append(fns, func() error {
263+
return internallogs.DumpDir(p.logger, node, "/var/log", path)
264+
})
268265
fns = append(fns,
269-
func() error { return common.CollectLogs(node, path) },
270266
execToPathFn(exec.Command("docker", "inspect", name), filepath.Join(path, "inspect.json")),
271-
func() error {
272-
f, err := common.FileOnHost(filepath.Join(path, "serial.log"))
273-
if err != nil {
274-
return err
275-
}
276-
defer f.Close()
277-
return node.SerialLogs(f)
278-
},
279267
)
280268
}
281-
282269
// run and collect up all errors
283-
errs = append(errs, errors.AggregateConcurrent(fns))
284-
return errors.NewAggregate(errs)
270+
return errors.AggregateConcurrent(fns)
285271
}
286272

287273
// Info returns the provider info.

pkg/cluster/internal/providers/nerdctl/provider.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"sigs.k8s.io/kind/pkg/exec"
3131
"sigs.k8s.io/kind/pkg/log"
3232

33-
internallogs "sigs.k8s.io/kind/pkg/cluster/internal/logs"
3433
"sigs.k8s.io/kind/pkg/cluster/internal/providers"
3534
"sigs.k8s.io/kind/pkg/cluster/internal/providers/common"
3635
"sigs.k8s.io/kind/pkg/cluster/nodeutils"
@@ -302,34 +301,17 @@ func (p *provider) CollectLogs(dir string, nodes []nodes.Node) error {
302301
filepath.Join(dir, "docker-info.txt"),
303302
),
304303
}
305-
306-
// collect /var/log for each node and plan collecting more logs
307-
var errs []error
304+
// inspect each node
308305
for _, n := range nodes {
309306
node := n // https://golang.org/doc/faq#closures_and_goroutines
310307
name := node.String()
311308
path := filepath.Join(dir, name)
312-
if err := internallogs.DumpDir(p.logger, node, "/var/log", path); err != nil {
313-
errs = append(errs, err)
314-
}
315-
316309
fns = append(fns,
317-
func() error { return common.CollectLogs(node, path) },
318310
execToPathFn(exec.Command(p.Binary(), "inspect", name), filepath.Join(path, "inspect.json")),
319-
func() error {
320-
f, err := common.FileOnHost(filepath.Join(path, "serial.log"))
321-
if err != nil {
322-
return err
323-
}
324-
defer f.Close()
325-
return node.SerialLogs(f)
326-
},
327311
)
328312
}
329-
330313
// run and collect up all errors
331-
errs = append(errs, errors.AggregateConcurrent(fns))
332-
return errors.NewAggregate(errs)
314+
return errors.AggregateConcurrent(fns)
333315
}
334316

335317
// Info returns the provider info.

pkg/cluster/internal/providers/podman/provider.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"sigs.k8s.io/kind/pkg/exec"
3232
"sigs.k8s.io/kind/pkg/log"
3333

34-
internallogs "sigs.k8s.io/kind/pkg/cluster/internal/logs"
3534
"sigs.k8s.io/kind/pkg/cluster/internal/providers"
3635
"sigs.k8s.io/kind/pkg/cluster/internal/providers/common"
3736
"sigs.k8s.io/kind/pkg/internal/apis/config"
@@ -336,33 +335,17 @@ func (p *provider) CollectLogs(dir string, nodes []nodes.Node) error {
336335
filepath.Join(dir, "podman-info.txt"),
337336
),
338337
}
339-
340-
// collect /var/log for each node and plan collecting more logs
341-
var errs []error
338+
// inspect each node
342339
for _, n := range nodes {
343340
node := n // https://golang.org/doc/faq#closures_and_goroutines
344341
name := node.String()
345342
path := filepath.Join(dir, name)
346-
if err := internallogs.DumpDir(p.logger, node, "/var/log", path); err != nil {
347-
errs = append(errs, err)
348-
}
349-
350343
fns = append(fns,
351-
func() error { return common.CollectLogs(node, path) },
352344
execToPathFn(exec.Command("podman", "inspect", name), filepath.Join(path, "inspect.json")),
353-
func() error {
354-
f, err := common.FileOnHost(filepath.Join(path, "serial.log"))
355-
if err != nil {
356-
return err
357-
}
358-
return node.SerialLogs(f)
359-
},
360345
)
361346
}
362-
363347
// run and collect up all errors
364-
errs = append(errs, errors.AggregateConcurrent(fns))
365-
return errors.NewAggregate(errs)
348+
return errors.AggregateConcurrent(fns)
366349
}
367350

368351
// Info returns the provider info.

pkg/cluster/provider.go

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ import (
2727
"sigs.k8s.io/kind/pkg/cluster/nodes"
2828
"sigs.k8s.io/kind/pkg/cluster/nodeutils"
2929
"sigs.k8s.io/kind/pkg/errors"
30+
"sigs.k8s.io/kind/pkg/exec"
3031
"sigs.k8s.io/kind/pkg/log"
3132

3233
internalcreate "sigs.k8s.io/kind/pkg/cluster/internal/create"
3334
internaldelete "sigs.k8s.io/kind/pkg/cluster/internal/delete"
3435
"sigs.k8s.io/kind/pkg/cluster/internal/kubeconfig"
36+
internallogs "sigs.k8s.io/kind/pkg/cluster/internal/logs"
3537
internalproviders "sigs.k8s.io/kind/pkg/cluster/internal/providers"
38+
"sigs.k8s.io/kind/pkg/cluster/internal/providers/common"
3639
"sigs.k8s.io/kind/pkg/cluster/internal/providers/docker"
3740
"sigs.k8s.io/kind/pkg/cluster/internal/providers/nerdctl"
3841
"sigs.k8s.io/kind/pkg/cluster/internal/providers/podman"
@@ -236,14 +239,16 @@ func (p *Provider) ListInternalNodes(name string) ([]nodes.Node, error) {
236239
func (p *Provider) CollectLogs(name, dir string) error {
237240
// TODO: should use ListNodes and Collect should handle nodes differently
238241
// based on role ...
239-
n, err := p.ListInternalNodes(name)
242+
internalNodes, err := p.ListInternalNodes(name)
240243
if err != nil {
241244
return err
242245
}
246+
243247
// ensure directory
244248
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
245249
return errors.Wrap(err, "failed to create logs directory")
246250
}
251+
247252
// write kind version
248253
if err := os.WriteFile(
249254
filepath.Join(dir, "kind-version.txt"),
@@ -252,6 +257,71 @@ func (p *Provider) CollectLogs(name, dir string) error {
252257
); err != nil {
253258
return errors.Wrap(err, "failed to write kind-version.txt")
254259
}
255-
// collect and write cluster logs
256-
return p.provider.CollectLogs(dir, n)
260+
261+
// common portable log collection for the nodes
262+
fns := []func() error{}
263+
var errs []error
264+
for _, n := range internalNodes {
265+
node := n // https://golang.org/doc/faq#closures_and_goroutines
266+
name := node.String()
267+
path := filepath.Join(dir, name)
268+
fns = append(fns,
269+
func() error { return collectNodeLogs(p.logger, node, path) },
270+
)
271+
}
272+
errs = append(errs, errors.AggregateConcurrent(fns))
273+
274+
// additional, provider specific log collection
275+
errs = append(errs, p.provider.CollectLogs(dir, internalNodes))
276+
277+
// flatten
278+
return errors.NewAggregate(errs)
279+
}
280+
281+
func collectNodeLogs(logger log.Logger, n nodes.Node, dir string) error {
282+
execToPathFn := func(cmd exec.Cmd, path string) func() error {
283+
return func() error {
284+
f, err := common.FileOnHost(filepath.Join(dir, path))
285+
if err != nil {
286+
return err
287+
}
288+
defer f.Close()
289+
return cmd.SetStdout(f).SetStderr(f).Run()
290+
}
291+
}
292+
293+
return errors.AggregateConcurrent([]func() error{
294+
func() error {
295+
f, err := common.FileOnHost(filepath.Join(dir, "serial.log"))
296+
if err != nil {
297+
return err
298+
}
299+
defer f.Close()
300+
return n.SerialLogs(f)
301+
},
302+
func() error {
303+
return internallogs.DumpDir(logger, n, "/var/log", dir)
304+
},
305+
// record info about the node container
306+
execToPathFn(
307+
n.Command("cat", "/kind/version"),
308+
"kubernetes-version.txt",
309+
),
310+
execToPathFn(
311+
n.Command("journalctl", "--no-pager"),
312+
"journal.log",
313+
),
314+
execToPathFn(
315+
n.Command("journalctl", "--no-pager", "-u", "kubelet.service"),
316+
"kubelet.log",
317+
),
318+
execToPathFn(
319+
n.Command("journalctl", "--no-pager", "-u", "containerd.service"),
320+
"containerd.log",
321+
),
322+
execToPathFn(
323+
n.Command("crictl", "images"),
324+
"images.log",
325+
),
326+
})
257327
}

0 commit comments

Comments
 (0)