Skip to content

Commit fa26a5e

Browse files
authored
Merge pull request #33 from replicatedhq/analyze
analyze cli cub-command
2 parents 31e60ab + 7e541de commit fa26a5e

File tree

17 files changed

+396
-21
lines changed

17 files changed

+396
-21
lines changed

cmd/preflight/cli/receive.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"os"
88

9+
"github.com/replicatedhq/troubleshoot/pkg/logger"
910
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
1011
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1112
)
@@ -56,7 +57,7 @@ func receivePreflightResults(preflightJobNamespace string, preflightJobName stri
5657
return err
5758
}
5859

59-
fmt.Printf("%s\n", body)
60+
logger.Printf("%s\n", body)
6061
receivedPreflights = append(receivedPreflights, readyPreflight)
6162
}
6263

cmd/preflight/cli/run_nocrd.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
analyzerunner "github.com/replicatedhq/troubleshoot/pkg/analyze"
1818
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
1919
collectrunner "github.com/replicatedhq/troubleshoot/pkg/collect"
20+
"github.com/replicatedhq/troubleshoot/pkg/logger"
2021
"github.com/spf13/viper"
2122
"gopkg.in/yaml.v2"
2223
corev1 "k8s.io/api/core/v1"
@@ -96,7 +97,7 @@ func runPreflightsNoCRD(v *viper.Viper, arg string) error {
9697
for _, analyzer := range preflight.Spec.Analyzers {
9798
analyzeResult, err := analyzerunner.Analyze(analyzer, getCollectedFileContents, getChildCollectedFileContents)
9899
if err != nil {
99-
fmt.Printf("an analyzer failed to run: %v\n", err)
100+
logger.Printf("an analyzer failed to run: %v\n", err)
100101
continue
101102
}
102103

@@ -110,7 +111,7 @@ func runPreflightsNoCRD(v *viper.Viper, arg string) error {
110111
return showInteractiveResults(preflight.Name, analyzeResults)
111112
}
112113

113-
fmt.Printf("only interactive results are supported\n")
114+
logger.Printf("only interactive results are supported\n")
114115
return nil
115116
}
116117

@@ -230,7 +231,7 @@ func runCollectors(v *viper.Viper, preflight troubleshootv1beta1.Preflight) (map
230231

231232
collectedData, err := parseCollectorOutput(buf.String())
232233
if err != nil {
233-
fmt.Printf("parse collected data: %v\n", err)
234+
logger.Printf("parse collected data: %v\n", err)
234235
return
235236
}
236237
for k, v := range collectedData {

cmd/troubleshoot/cli/analyze.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package cli
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
8+
analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze"
9+
"github.com/replicatedhq/troubleshoot/pkg/convert"
10+
"github.com/replicatedhq/troubleshoot/pkg/logger"
11+
"github.com/spf13/cobra"
12+
"github.com/spf13/viper"
13+
"gopkg.in/yaml.v2"
14+
)
15+
16+
func Analyze() *cobra.Command {
17+
cmd := &cobra.Command{
18+
Use: "analyze",
19+
Short: "analyze a support bundle",
20+
Long: `...`,
21+
PreRun: func(cmd *cobra.Command, args []string) {
22+
viper.BindPFlag("url", cmd.Flags().Lookup("url"))
23+
viper.BindPFlag("output", cmd.Flags().Lookup("output"))
24+
viper.BindPFlag("quiet", cmd.Flags().Lookup("quiet"))
25+
},
26+
RunE: func(cmd *cobra.Command, args []string) error {
27+
v := viper.GetViper()
28+
29+
logger.SetQuiet(v.GetBool("quiet"))
30+
31+
result, err := analyzer.DownloadAndAnalyze(context.TODO(), v.GetString("url"))
32+
if err != nil {
33+
return err
34+
}
35+
36+
var data interface{}
37+
switch v.GetString("compatibility") {
38+
case "support-bundle":
39+
data = convert.FromAnalyzerResult(result)
40+
default:
41+
data = result
42+
}
43+
44+
var formatted []byte
45+
switch v.GetString("output") {
46+
case "json":
47+
formatted, err = json.MarshalIndent(data, "", " ")
48+
case "", "yaml":
49+
formatted, err = yaml.Marshal(data)
50+
default:
51+
return fmt.Errorf("unsupported output format: %q", v.GetString("output"))
52+
}
53+
54+
if err != nil {
55+
return err
56+
}
57+
58+
fmt.Printf("%s", formatted)
59+
return nil
60+
},
61+
}
62+
63+
cmd.Flags().String("url", "", "URL of the support bundle to analyze")
64+
cmd.MarkFlagRequired("url")
65+
cmd.Flags().String("output", "", "output format: json, yaml")
66+
cmd.Flags().String("compatibility", "", "output compatibility mode: support-bundle")
67+
cmd.Flags().MarkHidden("compatibility")
68+
cmd.Flags().Bool("quiet", false, "enable/disable error messaging and only show parseable output")
69+
70+
viper.BindPFlags(cmd.Flags())
71+
72+
return cmd
73+
}

cmd/troubleshoot/cli/receive.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"path/filepath"
1111

1212
"github.com/mholt/archiver"
13+
"github.com/replicatedhq/troubleshoot/pkg/logger"
1314
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
1415
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1516
)
@@ -67,13 +68,13 @@ func receiveSupportBundle(collectorJobNamespace string, collectorJobName string)
6768

6869
decoded, err := base64.StdEncoding.DecodeString(string(body))
6970
if err != nil {
70-
fmt.Printf("failed to output for collector %s\n", readyCollector)
71+
logger.Printf("failed to output for collector %s\n", readyCollector)
7172
continue
7273
}
7374

7475
files := make(map[string]interface{})
7576
if err := json.Unmarshal(decoded, &files); err != nil {
76-
fmt.Printf("failed to unmarshal output for collector %s\n", readyCollector)
77+
logger.Printf("failed to unmarshal output for collector %s\n", readyCollector)
7778
}
7879

7980
for filename, maybeContents := range files {

cmd/troubleshoot/cli/retrieve.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package cli
22

33
import (
44
"errors"
5-
"fmt"
65
"path/filepath"
76

87
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
98
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
9+
"github.com/replicatedhq/troubleshoot/pkg/logger"
1010
"github.com/spf13/cobra"
1111
"github.com/spf13/viper"
1212
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -47,11 +47,11 @@ func Retrieve() *cobra.Command {
4747
}
4848

4949
if collectorJob == nil {
50-
fmt.Printf("unable to find collector job\n")
50+
logger.Printf("unable to find collector job\n")
5151
return errors.New("no collectors")
5252
}
5353

54-
fmt.Printf("connecting to collector job %s\n", collectorJob.Name)
54+
logger.Printf("connecting to collector job %s\n", collectorJob.Name)
5555

5656
stopChan, err := k8sutil.PortForward(v.GetString("kubecontext"), 8000, 8000, collectorJob.Status.ServerPodNamespace, collectorJob.Status.ServerPodName)
5757
if err != nil {

cmd/troubleshoot/cli/root.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
10+
"github.com/replicatedhq/troubleshoot/pkg/logger"
1011
"github.com/spf13/cobra"
1112
"github.com/spf13/viper"
1213
)
@@ -23,6 +24,8 @@ from a server that can be used to assist when troubleshooting a server.`,
2324
RunE: func(cmd *cobra.Command, args []string) error {
2425
v := viper.GetViper()
2526

27+
logger.SetQuiet(v.GetBool("quiet"))
28+
2629
if len(args) == 0 {
2730
return runTroubleshootCRD(v)
2831
}
@@ -33,6 +36,8 @@ from a server that can be used to assist when troubleshooting a server.`,
3336

3437
cobra.OnInitialize(initConfig)
3538

39+
cmd.AddCommand(Analyze())
40+
3641
cmd.Flags().String("collectors", "", "name of the collectors to use")
3742
cmd.Flags().String("namespace", "default", "namespace the collectors can be found in")
3843

cmd/troubleshoot/cli/run_nocrd.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/mholt/archiver"
1717
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
1818
collectrunner "github.com/replicatedhq/troubleshoot/pkg/collect"
19+
"github.com/replicatedhq/troubleshoot/pkg/logger"
1920
"github.com/spf13/viper"
2021
"gopkg.in/yaml.v2"
2122
corev1 "k8s.io/api/core/v1"
@@ -207,13 +208,13 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector) (str
207208

208209
collectorDir, err := parseAndSaveCollectorOutput(buf.String(), bundlePath)
209210
if err != nil {
210-
fmt.Printf("parse collected data: %v\n", err)
211+
logger.Printf("parse collected data: %v\n", err)
211212
return
212213
}
213214

214215
// empty dir name will make tar fail
215216
if collectorDir == "" {
216-
fmt.Printf("pod %s did not return any files\n", newPod.Name)
217+
logger.Printf("pod %s did not return any files\n", newPod.Name)
217218
return
218219
}
219220

@@ -234,7 +235,7 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector) (str
234235
for _, collect := range desiredCollectors {
235236
_, pod, err := collectrunner.CreateCollector(client, s, &owner, collector.Name, v.GetString("namespace"), serviceAccountName, "troubleshoot", collect, v.GetString("image"), v.GetString("pullpolicy"))
236237
if err != nil {
237-
fmt.Printf("A collector pod cannot be created: %v\n", err)
238+
logger.Printf("A collector pod cannot be created: %v\n", err)
238239
continue
239240
}
240241
podsCreated = append(podsCreated, pod)

cmd/troubleshoot/cli/version.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ func writeVersionFile(path string) (string, error) {
1111
version := troubleshootv1beta1.SupportBundleVersion{
1212
ApiVersion: "troubleshoot.replicated.com/v1beta1",
1313
Kind: "SupportBundle",
14-
LayoutVersion: "0.0.1",
1514
}
1615
b, err := yaml.Marshal(version)
1716
if err != nil {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ require (
1818
github.com/gizak/termui/v3 v3.1.0
1919
github.com/golang/snappy v0.0.1 // indirect
2020
github.com/google/uuid v1.1.1 // indirect
21+
github.com/hashicorp/go-getter v1.3.0
2122
github.com/hashicorp/go-multierror v1.0.0
2223
github.com/huandu/xstrings v1.2.0 // indirect
2324
github.com/manifoldco/promptui v0.3.2 // indirect
@@ -27,7 +28,6 @@ require (
2728
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
2829
github.com/pierrec/lz4 v2.0.5+incompatible // indirect
2930
github.com/pkg/errors v0.8.1
30-
github.com/sergi/go-diff v1.0.0 // indirect
3131
github.com/spf13/cobra v0.0.3
3232
github.com/spf13/viper v1.4.0
3333
github.com/stretchr/testify v1.3.0

0 commit comments

Comments
 (0)