Skip to content

Commit 6acedd9

Browse files
BON4eslavyansky
authored andcommitted
PT-2421 - Adding further data collection to pt-k8s-debug-collector
This PR introduces additional files in to the final dump archive for pgv2. Tool logs: - patronictl list - pgbackrest info Log files from folders: - $PGDATA/log - pgdata/pgbackrest/log
1 parent 9a8e662 commit 6acedd9

4 files changed

Lines changed: 106 additions & 2 deletions

File tree

src/go/pt-k8s-debug-collector/dumper/dumper.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,19 @@ type Dumper struct {
6565
restConfig *rest.Config
6666
}
6767

68-
// individualFile struct is used to dump the necessary files from the containers
68+
type toolLog struct {
69+
filename string
70+
args []string
71+
}
72+
73+
// individualFile struct is used to dump the necessary files from the containers,
74+
// or files produced by executing a tool command inside a container
6975
type individualFile struct {
7076
resourceName string
7177
containerName string
7278
filepaths []string
7379
dirpaths map[string][]string // map[tarFolder][]dirPaths
80+
toolCmds map[string][]toolLog
7481
}
7582

7683
// resourceMap struct is used to dump the resources from namespace scope or cluster scope

src/go/pt-k8s-debug-collector/dumper/individual_files.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,33 @@ func (d *Dumper) getIndividualFiles(ctx context.Context, job exportJob, crType s
6868
}
6969
}
7070
}
71+
72+
// Process tool command outputs (e.g. patronictl list, pgbackrest info)
73+
for tarFolder, cmds := range indf.toolCmds {
74+
for _, cmd := range cmds {
75+
if err := d.processToolOutput(ctx, job, indf.containerName, tarFolder, cmd); err != nil {
76+
log.Warnf("Skipping tool cmd %v: %v", cmd.args, err)
77+
}
78+
}
79+
}
7180
}
7281
}
7382

83+
func (d *Dumper) processToolOutput(
84+
ctx context.Context,
85+
job exportJob,
86+
container, tarFolder string, cmd toolLog,
87+
) error {
88+
out, stderr, err := d.executeInPod(ctx, cmd.args, job.Pod, container, nil)
89+
if err != nil {
90+
return fmt.Errorf("exec %s: %w (stderr: %s)", cmd, err, stderr.String())
91+
}
92+
93+
dst := d.PodIndividualFilesPath(job.Pod.Namespace, job.Pod.Name, path.Join(tarFolder, cmd.filename))
94+
95+
return d.archive.WriteVirtualFile(dst, out.Bytes())
96+
}
97+
7498
func (d *Dumper) processSingleFile(
7599
ctx context.Context,
76100
job exportJob,

src/go/pt-k8s-debug-collector/dumper/resources.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,28 @@ func (d *Dumper) addPg1() error {
2626

2727
func (d *Dumper) addPg2() error {
2828
dirpaths := map[string][]string{
29-
"pg_log": {"$PGDATA/log"},
29+
"pg_log": {"$PGDATA/log"},
30+
"pgbackrest_log": {"pgdata/pgbackrest/log"},
31+
}
32+
33+
tools := map[string][]toolLog{
34+
"": {
35+
{
36+
filename: "patronictl-list.log",
37+
args: []string{"patronictl", "list"},
38+
},
39+
{
40+
filename: "pgbackrest-info.log",
41+
args: []string{"pgbackrest", "info"},
42+
},
43+
},
3044
}
3145

3246
d.individualFiles = append(d.individualFiles, individualFile{
3347
resourceName: "pgv2",
3448
containerName: "database",
3549
dirpaths: dirpaths,
50+
toolCmds: tools,
3651
})
3752
return nil
3853
}

src/go/pt-k8s-debug-collector/main_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,64 @@ func (s *CollectorSuite) TestIndividualFiles() {
379379
return strings.Join(result, "")
380380
},
381381
},
382+
{
383+
namespace: "pgv2",
384+
// If the tool collects pgBackRest execution log files for pgv2
385+
name: "pgv2_pgbackrest_log_list",
386+
// tar -tf cluster-dump.tar.gz --wildcards 'cluster-dump/*/*/pgbackrest_log/*'
387+
cmd: []string{"tar", "-tf", "cluster-dump.tar.gz", "--wildcards", "cluster-dump/*/*/pgbackrest_log/*"},
388+
want: []string{"db-archive-push-async.log", "db-stanza-create.log"},
389+
preprocessor: func(in string) string {
390+
required := map[string]struct{}{
391+
"db-archive-push-async.log": {},
392+
"db-stanza-create.log": {},
393+
}
394+
395+
files := strings.Split(in, "\n")
396+
var result []string
397+
for _, f := range files {
398+
b := path.Base(f)
399+
if _, ok := required[b]; !ok {
400+
continue
401+
}
402+
403+
if !slices.Contains(result, b) {
404+
result = append(result, b)
405+
}
406+
}
407+
slices.Sort(result)
408+
return strings.Join(result, "\n")
409+
},
410+
},
411+
{
412+
namespace: "pgv2",
413+
// If the tool collects tool command outputs (patronictl list, pgbackrest info) for pgv2
414+
name: "pgv2_tools_log_list",
415+
// tar -tf cluster-dump.tar.gz --wildcards 'cluster-dump/*/*/*'
416+
cmd: []string{"tar", "-tf", "cluster-dump.tar.gz", "--wildcards", "cluster-dump/*/*/*"},
417+
want: []string{"patronictl-list.log", "pgbackrest-info.log"},
418+
preprocessor: func(in string) string {
419+
required := map[string]struct{}{
420+
"patronictl-list.log": {},
421+
"pgbackrest-info.log": {},
422+
}
423+
424+
files := strings.Split(in, "\n")
425+
var result []string
426+
for _, f := range files {
427+
b := path.Base(f)
428+
if _, ok := required[b]; !ok {
429+
continue
430+
}
431+
432+
if !slices.Contains(result, b) {
433+
result = append(result, b)
434+
}
435+
}
436+
slices.Sort(result)
437+
return strings.Join(result, "\n")
438+
},
439+
},
382440
{
383441
namespace: "pxc",
384442
// If pod logs are exported as one file per container

0 commit comments

Comments
 (0)