Skip to content

Commit

Permalink
analyze cli cub-command
Browse files Browse the repository at this point in the history
  • Loading branch information
divolgin committed Jul 30, 2019
1 parent 31e60ab commit ea58127
Show file tree
Hide file tree
Showing 15 changed files with 395 additions and 19 deletions.
3 changes: 2 additions & 1 deletion cmd/preflight/cli/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"os"

"github.com/replicatedhq/troubleshoot/pkg/logger"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -56,7 +57,7 @@ func receivePreflightResults(preflightJobNamespace string, preflightJobName stri
return err
}

fmt.Printf("%s\n", body)
logger.Printf("%s\n", body)
receivedPreflights = append(receivedPreflights, readyPreflight)
}

Expand Down
7 changes: 4 additions & 3 deletions cmd/preflight/cli/run_nocrd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
analyzerunner "github.com/replicatedhq/troubleshoot/pkg/analyze"
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
collectrunner "github.com/replicatedhq/troubleshoot/pkg/collect"
"github.com/replicatedhq/troubleshoot/pkg/logger"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -96,7 +97,7 @@ func runPreflightsNoCRD(v *viper.Viper, arg string) error {
for _, analyzer := range preflight.Spec.Analyzers {
analyzeResult, err := analyzerunner.Analyze(analyzer, getCollectedFileContents, getChildCollectedFileContents)
if err != nil {
fmt.Printf("an analyzer failed to run: %v\n", err)
logger.Printf("an analyzer failed to run: %v\n", err)
continue
}

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

fmt.Printf("only interactive results are supported\n")
logger.Printf("only interactive results are supported\n")
return nil
}

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

collectedData, err := parseCollectorOutput(buf.String())
if err != nil {
fmt.Printf("parse collected data: %v\n", err)
logger.Printf("parse collected data: %v\n", err)
return
}
for k, v := range collectedData {
Expand Down
72 changes: 72 additions & 0 deletions cmd/troubleshoot/cli/analyze.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cli

import (
"context"
"encoding/json"
"fmt"

"gopkg.in/yaml.v2"
analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze"
"github.com/replicatedhq/troubleshoot/pkg/convert"
"github.com/replicatedhq/troubleshoot/pkg/logger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func Analyze() *cobra.Command {
cmd := &cobra.Command{
Use: "analyze",
Short: "analyze a support bundle",
Long: `...`,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlag("url", cmd.Flags().Lookup("url"))
viper.BindPFlag("output", cmd.Flags().Lookup("output"))
viper.BindPFlag("quiet", cmd.Flags().Lookup("quiet"))
},
RunE: func(cmd *cobra.Command, args []string) error {
v := viper.GetViper()

logger.SetQuiet(v.GetBool("quiet"))

result, err := analyzer.DownloadAndAnalyze(context.TODO(), v.GetString("url"))
if err != nil {
return err
}

var data interface{}
switch v.GetString("compatibility") {
case "support-bundle":
data = convert.FromAnalyzerResult(result)
default:
data = result
}

var formatted []byte
switch v.GetString("output") {
case "json":
formatted, err = json.MarshalIndent(data, "", " ")
case "", "yaml":
formatted, err = yaml.Marshal(data)
default:
return fmt.Errorf("unsupported output format: %q", v.GetString("output"))
}

if err != nil {
return err
}

fmt.Printf("%s", formatted)
return nil
},
}

cmd.Flags().String("url", "", "URL of the support bundle to analyze")
cmd.Flags().String("output", "", "output format: json, yaml")
cmd.Flags().String("compatibility", "", "output compatibility mode: support-bundle")
cmd.Flags().Bool("quiet", false, "enable/disable error messaging and only show parseable output")
cmd.MarkFlagRequired("url")

viper.BindPFlags(cmd.Flags())

return cmd
}
5 changes: 3 additions & 2 deletions cmd/troubleshoot/cli/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"

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

decoded, err := base64.StdEncoding.DecodeString(string(body))
if err != nil {
fmt.Printf("failed to output for collector %s\n", readyCollector)
logger.Printf("failed to output for collector %s\n", readyCollector)
continue
}

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

for filename, maybeContents := range files {
Expand Down
6 changes: 3 additions & 3 deletions cmd/troubleshoot/cli/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package cli

import (
"errors"
"fmt"
"path/filepath"

troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
"github.com/replicatedhq/troubleshoot/pkg/logger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -47,11 +47,11 @@ func Retrieve() *cobra.Command {
}

if collectorJob == nil {
fmt.Printf("unable to find collector job\n")
logger.Printf("unable to find collector job\n")
return errors.New("no collectors")
}

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

stopChan, err := k8sutil.PortForward(v.GetString("kubecontext"), 8000, 8000, collectorJob.Status.ServerPodNamespace, collectorJob.Status.ServerPodName)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions cmd/troubleshoot/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

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

logger.SetQuiet(v.GetBool("quiet"))

if len(args) == 0 {
return runTroubleshootCRD(v)
}
Expand All @@ -33,6 +36,8 @@ from a server that can be used to assist when troubleshooting a server.`,

cobra.OnInitialize(initConfig)

cmd.AddCommand(Analyze())

cmd.Flags().String("collectors", "", "name of the collectors to use")
cmd.Flags().String("namespace", "default", "namespace the collectors can be found in")

Expand Down
7 changes: 4 additions & 3 deletions cmd/troubleshoot/cli/run_nocrd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/mholt/archiver"
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
collectrunner "github.com/replicatedhq/troubleshoot/pkg/collect"
"github.com/replicatedhq/troubleshoot/pkg/logger"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -207,13 +208,13 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector) (str

collectorDir, err := parseAndSaveCollectorOutput(buf.String(), bundlePath)
if err != nil {
fmt.Printf("parse collected data: %v\n", err)
logger.Printf("parse collected data: %v\n", err)
return
}

// empty dir name will make tar fail
if collectorDir == "" {
fmt.Printf("pod %s did not return any files\n", newPod.Name)
logger.Printf("pod %s did not return any files\n", newPod.Name)
return
}

Expand All @@ -234,7 +235,7 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector) (str
for _, collect := range desiredCollectors {
_, pod, err := collectrunner.CreateCollector(client, s, &owner, collector.Name, v.GetString("namespace"), serviceAccountName, "troubleshoot", collect, v.GetString("image"), v.GetString("pullpolicy"))
if err != nil {
fmt.Printf("A collector pod cannot be created: %v\n", err)
logger.Printf("A collector pod cannot be created: %v\n", err)
continue
}
podsCreated = append(podsCreated, pod)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/gizak/termui/v3 v3.1.0
github.com/golang/snappy v0.0.1 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/hashicorp/go-getter v1.3.0
github.com/hashicorp/go-multierror v1.0.0
github.com/huandu/xstrings v1.2.0 // indirect
github.com/manifoldco/promptui v0.3.2 // indirect
Expand All @@ -27,7 +28,6 @@ require (
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/pierrec/lz4 v2.0.5+incompatible // indirect
github.com/pkg/errors v0.8.1
github.com/sergi/go-diff v1.0.0 // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.3.0
Expand Down
Loading

0 comments on commit ea58127

Please sign in to comment.