Skip to content

Commit a22e2e2

Browse files
committed
Analyze from the CLI
1 parent 8d991e1 commit a22e2e2

File tree

4 files changed

+99
-40
lines changed

4 files changed

+99
-40
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ run-preflight: preflight
128128

129129
.PHONY: run-troubleshoot
130130
run-troubleshoot: support-bundle
131-
./bin/support-bundle ./examples/troubleshoot/sample-troubleshoot.yaml
131+
./bin/support-bundle ./examples/troubleshoot/sample-supportbundle.yaml
132132

133133
.PHONY: run-analyze
134134
run-analyze: analyze

cmd/troubleshoot/cli/run.go

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ import (
2121
"github.com/mholt/archiver"
2222
"github.com/pkg/errors"
2323
"github.com/replicatedhq/troubleshoot/cmd/util"
24+
analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze"
2425
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
2526
"github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme"
2627
troubleshootclientsetscheme "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme"
2728
"github.com/replicatedhq/troubleshoot/pkg/collect"
29+
"github.com/replicatedhq/troubleshoot/pkg/convert"
2830
"github.com/replicatedhq/troubleshoot/pkg/redact"
2931
"github.com/spf13/viper"
3032
spin "github.com/tj/go-spin"
@@ -132,7 +134,63 @@ func runTroubleshoot(v *viper.Viper, arg string) error {
132134

133135
fmt.Printf("\r%s\r", cursor.ClearEntireLine())
134136

135-
if len(supportBundleSpec.Spec.AfterCollection) == 0 {
137+
// upload if needed
138+
fileUploaded := false
139+
if len(supportBundleSpec.Spec.AfterCollection) > 0 {
140+
for _, ac := range supportBundleSpec.Spec.AfterCollection {
141+
if ac.UploadResultsTo != nil {
142+
if err := uploadSupportBundle(ac.UploadResultsTo, archivePath); err != nil {
143+
c := color.New(color.FgHiRed)
144+
c.Printf("%s\r * Failed to upload support bundle: %v\n", cursor.ClearEntireLine(), err)
145+
} else {
146+
fileUploaded = true
147+
}
148+
} else if ac.Callback != nil {
149+
if err := callbackSupportBundleAPI(ac.Callback, archivePath); err != nil {
150+
c := color.New(color.FgHiRed)
151+
c.Printf("%s\r * Failed to notify API that support bundle has been uploaded: %v\n", cursor.ClearEntireLine(), err)
152+
}
153+
}
154+
}
155+
156+
}
157+
158+
// perform analysis, if possible
159+
if len(supportBundleSpec.Spec.Analyzers) > 0 {
160+
tmpDir, err := ioutil.TempDir("", "troubleshoot")
161+
if err != nil {
162+
c := color.New(color.FgHiRed)
163+
c.Printf("%s\r * Failed to make directory for analysis: %v\n", cursor.ClearEntireLine(), err)
164+
}
165+
166+
f, err := os.Open(archivePath)
167+
if err != nil {
168+
c := color.New(color.FgHiRed)
169+
c.Printf("%s\r * Failed to open support bundle for analysis: %v\n", cursor.ClearEntireLine(), err)
170+
171+
}
172+
if err := analyzer.ExtractTroubleshootBundle(f, tmpDir); err != nil {
173+
c := color.New(color.FgHiRed)
174+
c.Printf("%s\r * Failed to extract support bundle for analysis: %v\n", cursor.ClearEntireLine(), err)
175+
}
176+
177+
analyzeResults, err := analyzer.AnalyzeLocal(tmpDir, supportBundleSpec.Spec.Analyzers)
178+
if err != nil {
179+
c := color.New(color.FgHiRed)
180+
c.Printf("%s\r * Failed to analyze support bundle: %v\n", cursor.ClearEntireLine(), err)
181+
}
182+
183+
data := convert.FromAnalyzerResult(analyzeResults)
184+
formatted, err := json.MarshalIndent(data, "", " ")
185+
if err != nil {
186+
c := color.New(color.FgHiRed)
187+
c.Printf("%s\r * Failed to format analysis: %v\n", cursor.ClearEntireLine(), err)
188+
}
189+
190+
fmt.Printf("%s", formatted)
191+
}
192+
193+
if !fileUploaded {
136194
msg := archivePath
137195
if appName := supportBundleSpec.Labels["applicationName"]; appName != "" {
138196
f := `A support bundle for %s has been created in this directory
@@ -146,23 +204,6 @@ the %s Admin Console to begin analysis.`
146204
return nil
147205
}
148206

149-
fileUploaded := false
150-
for _, ac := range supportBundleSpec.Spec.AfterCollection {
151-
if ac.UploadResultsTo != nil {
152-
if err := uploadSupportBundle(ac.UploadResultsTo, archivePath); err != nil {
153-
c := color.New(color.FgHiRed)
154-
c.Printf("%s\r * Failed to upload support bundle: %v\n", cursor.ClearEntireLine(), err)
155-
} else {
156-
fileUploaded = true
157-
}
158-
} else if ac.Callback != nil {
159-
if err := callbackSupportBundleAPI(ac.Callback, archivePath); err != nil {
160-
c := color.New(color.FgHiRed)
161-
c.Printf("%s\r * Failed to notify API that support bundle has been uploaded: %v\n", cursor.ClearEntireLine(), err)
162-
}
163-
}
164-
}
165-
166207
fmt.Printf("\r%s\r", cursor.ClearEntireLine())
167208
if fileUploaded {
168209
fmt.Printf("A support bundle has been created and uploaded to your cluster for analysis. Please visit the Troubleshoot page to continue.\n")

examples/troubleshoot/sample-supportbundle.yaml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,17 @@ metadata:
44
name: supportbundle-sample
55
spec:
66
collectors: []
7-
analyzers: []
7+
analyzers:
8+
- clusterVersion:
9+
outcomes:
10+
- fail:
11+
when: "< 1.13.0"
12+
message: The application requires at Kubernetes 1.13.0 or later, and recommends 1.15.0.
13+
uri: https://www.kubernetes.io
14+
- warn:
15+
when: "< 1.15.0"
16+
message: Your cluster meets the minimum version of Kubernetes, but we recommend you update to 1.15.0 or later.
17+
uri: https://kubernetes.io
18+
- pass:
19+
when: ">= 1.15.0"
20+
message: Your cluster meets the recommended and required versions of Kubernetes.

pkg/analyze/download.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,26 @@ type fileContentProvider struct {
2020
rootDir string
2121
}
2222

23+
// Analyze local will analyze a locally available (already downloaded) bundle
24+
func AnalyzeLocal(localBundlePath string, analyzers []*troubleshootv1beta1.Analyze) ([]*AnalyzeResult, error) {
25+
fcp := fileContentProvider{rootDir: localBundlePath}
26+
27+
analyzeResults := []*AnalyzeResult{}
28+
for _, analyzer := range analyzers {
29+
analyzeResult, err := Analyze(analyzer, fcp.getFileContents, fcp.getChildFileContents)
30+
if err != nil {
31+
logger.Printf("an analyzer failed to run: %v\n", err)
32+
continue
33+
}
34+
35+
if analyzeResult != nil {
36+
analyzeResults = append(analyzeResults, analyzeResult)
37+
}
38+
}
39+
40+
return analyzeResults, nil
41+
}
42+
2343
func DownloadAndAnalyze(bundleURL string, analyzersSpec string) ([]*AnalyzeResult, error) {
2444
tmpDir, err := ioutil.TempDir("", "troubleshoot-k8s")
2545
if err != nil {
@@ -52,22 +72,7 @@ func DownloadAndAnalyze(bundleURL string, analyzersSpec string) ([]*AnalyzeResul
5272
analyzers = parsedAnalyzers
5373
}
5474

55-
fcp := fileContentProvider{rootDir: tmpDir}
56-
57-
analyzeResults := []*AnalyzeResult{}
58-
for _, analyzer := range analyzers {
59-
analyzeResult, err := Analyze(analyzer, fcp.getFileContents, fcp.getChildFileContents)
60-
if err != nil {
61-
logger.Printf("an analyzer failed to run: %v\n", err)
62-
continue
63-
}
64-
65-
if analyzeResult != nil {
66-
analyzeResults = append(analyzeResults, analyzeResult)
67-
}
68-
}
69-
70-
return analyzeResults, nil
75+
return AnalyzeLocal(tmpDir, analyzers)
7176
}
7277

7378
func downloadTroubleshootBundle(bundleURL string, destDir string) error {
@@ -77,15 +82,15 @@ func downloadTroubleshootBundle(bundleURL string, destDir string) error {
7782
return errors.Wrap(err, "failed to open support bundle")
7883
}
7984
defer f.Close()
80-
return extractTroubleshootBundle(f, destDir)
85+
return ExtractTroubleshootBundle(f, destDir)
8186
}
8287

8388
pwd, err := os.Getwd()
8489
if err != nil {
8590
return errors.Wrap(err, "failed to get workdir")
8691
}
8792

88-
tmpDir, err := ioutil.TempDir("", "getter")
93+
tmpDir, err := ioutil.TempDir("", "troubleshoot")
8994
if err != nil {
9095
return errors.Wrap(err, "failed to create tmp dir")
9196
}
@@ -107,10 +112,10 @@ func downloadTroubleshootBundle(bundleURL string, destDir string) error {
107112
}
108113
defer f.Close()
109114

110-
return extractTroubleshootBundle(f, destDir)
115+
return ExtractTroubleshootBundle(f, destDir)
111116
}
112117

113-
func extractTroubleshootBundle(reader io.Reader, destDir string) error {
118+
func ExtractTroubleshootBundle(reader io.Reader, destDir string) error {
114119
gzReader, err := gzip.NewReader(reader)
115120
if err != nil {
116121
return errors.Wrap(err, "failed to create gzip reader")

0 commit comments

Comments
 (0)