-
Notifications
You must be signed in to change notification settings - Fork 273
/
troubleshoot.go
77 lines (68 loc) 路 2.34 KB
/
troubleshoot.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
package executables
import (
"context"
"encoding/json"
"fmt"
"regexp"
"time"
)
const (
troubleshootPath = "support-bundle"
supportBundleArchiveRegex = `support-bundle-([0-9]+(-[0-9]+)+)T([0-9]+(_[0-9]+)+)\.tar\.gz`
)
type Troubleshoot struct {
Executable
}
func NewTroubleshoot(executable Executable) *Troubleshoot {
return &Troubleshoot{
Executable: executable,
}
}
func (t *Troubleshoot) Collect(ctx context.Context, bundlePath string, sinceTime *time.Time, kubeconfig string) (archivePath string, err error) {
marshalledTime, err := sinceTime.MarshalText()
if err != nil {
return "", fmt.Errorf("could not marshal sinceTime for Collect parameters: %v", err)
}
params := []string{bundlePath, "--kubeconfig", kubeconfig, "--interactive=false", "--since-time", string(marshalledTime)}
output, err := t.Execute(ctx, params...)
if err != nil {
return "", fmt.Errorf("executing support-bundle: %v", err)
}
archivePath, err = parseArchivePathFromCollectOutput(output.String())
if err != nil {
return "", fmt.Errorf("parsing support-bundle output: %v", err)
}
return archivePath, nil
}
func (t *Troubleshoot) Analyze(ctx context.Context, bundleSpecPath string, archivePath string) ([]*SupportBundleAnalysis, error) {
params := []string{"analyze", bundleSpecPath, "--bundle", archivePath, "--output", "json"}
output, err := t.Execute(ctx, params...)
if err != nil {
return nil, fmt.Errorf("analyzing support bundle %s with analyzers %s: %v", archivePath, bundleSpecPath, err)
}
var analysisOutput []*SupportBundleAnalysis
err = json.Unmarshal(output.Bytes(), &analysisOutput)
if err != nil {
return nil, fmt.Errorf("unmarshalling support-bundle analyze output: %v", err)
}
return analysisOutput, err
}
func parseArchivePathFromCollectOutput(tsLogs string) (archivePath string, err error) {
r, err := regexp.Compile(supportBundleArchiveRegex)
if err != nil {
return "", fmt.Errorf("parsing support-bundle output: %v", err)
}
archivePath = r.FindString(tsLogs)
if archivePath == "" {
return "", fmt.Errorf("parsing support-bundle output: could not find archive path in output")
}
return archivePath, nil
}
type SupportBundleAnalysis struct {
Title string `json:"title"`
IsPass bool `json:"isPass"`
IsFail bool `json:"isFail"`
IsWarn bool `json:"isWarn"`
Message string `json:"message"`
Uri string `json:"URI"`
}