Skip to content

Commit

Permalink
errors are masked when collector fails
Browse files Browse the repository at this point in the history
  • Loading branch information
divolgin committed Aug 20, 2019
1 parent beb0514 commit b6ca58d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
19 changes: 13 additions & 6 deletions cmd/troubleshoot/cli/run_nocrd.go
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/ahmetalpbalkan/go-cursor"
"github.com/fatih/color"
"github.com/mholt/archiver"
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
"github.com/replicatedhq/troubleshoot/pkg/collect"
Expand Down Expand Up @@ -62,13 +63,19 @@ func runTroubleshootNoCRD(v *viper.Viper, arg string) error {

s := spin.New()
finishedCh := make(chan bool, 1)
progressChan := make(chan string, 1)
progressChan := make(chan interface{}, 1)
go func() {
currentDir := ""
for {
select {
case dir := <-progressChan:
currentDir = filepath.Base(dir)
case msg := <-progressChan:
switch msg := msg.(type) {
case error:
c := color.New(color.FgHiRed)
c.Println(fmt.Sprintf("%s * %v", cursor.ClearEntireLine(), msg))
case string:
currentDir = filepath.Base(msg)
}
case <-finishedCh:
fmt.Printf("\r")
return
Expand Down Expand Up @@ -105,7 +112,7 @@ the %s Admin Console to begin analysis.`
return nil
}

func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, progressChan chan string) (string, error) {
func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, progressChan chan interface{}) (string, error) {
bundlePath, err := ioutil.TempDir("", "troubleshoot")
if err != nil {
return "", err
Expand Down Expand Up @@ -135,13 +142,13 @@ func runCollectors(v *viper.Viper, collector troubleshootv1beta1.Collector, prog

result, err := collector.RunCollectorSync()
if err != nil {
progressChan <- fmt.Sprintf("failed to run collector %v", collector)
progressChan <- fmt.Errorf("failed to run collector %q: %v", collector.GetDisplayName(), err)
continue
}

collectorDir, err := parseAndSaveCollectorOutput(string(result), bundlePath)
if err != nil {
progressChan <- fmt.Sprintf("failed to parse collector spec: %v", collector)
progressChan <- fmt.Errorf("failed to parse collector spec %q: %v", collector.GetDisplayName(), err)
continue
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -15,6 +15,7 @@ require (
github.com/docker/go-units v0.4.0 // indirect
github.com/docker/spdystream v0.0.0-20181023171402-6480d4af844c // indirect
github.com/dsnet/compress v0.0.1 // indirect
github.com/fatih/color v1.7.0
github.com/gin-gonic/gin v1.4.0
github.com/gizak/termui/v3 v3.1.0
github.com/golang/snappy v0.0.1 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Expand Up @@ -94,6 +94,7 @@ github.com/evanphx/json-patch v4.2.0+incompatible h1:fUDGZCv/7iAN7u0puUVhvKCcsR6
github.com/evanphx/json-patch v4.2.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch v4.5.0+incompatible h1:ouOWdg56aJriqS0huScTkVXPC5IcNrDCXZ6OoTAWu7M=
github.com/evanphx/json-patch v4.5.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
Expand Down
50 changes: 50 additions & 0 deletions pkg/collect/collector.go
Expand Up @@ -2,6 +2,8 @@ package collect

import (
"errors"
"fmt"
"strings"

troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -41,6 +43,54 @@ func (c *Collector) RunCollectorSync() ([]byte, error) {
return nil, errors.New("no spec found to run")
}

func (c *Collector) GetDisplayName() string {
var collector, name, selector string
if c.Collect.ClusterInfo != nil {
collector = "cluster-info"
}
if c.Collect.ClusterResources != nil {
collector = "cluster-resources"
}
if c.Collect.Secret != nil {
collector = "secret"
name = c.Collect.Secret.CollectorName
}
if c.Collect.Logs != nil {
collector = "logs"
name = c.Collect.Logs.CollectorName
selector = strings.Join(c.Collect.Logs.Selector, ",")
}
if c.Collect.Run != nil {
collector = "run"
name = c.Collect.Run.CollectorName
}
if c.Collect.Exec != nil {
collector = "exec"
name = c.Collect.Exec.CollectorName
selector = strings.Join(c.Collect.Exec.Selector, ",")
}
if c.Collect.Copy != nil {
collector = "copy"
name = c.Collect.Copy.CollectorName
selector = strings.Join(c.Collect.Copy.Selector, ",")
}
if c.Collect.HTTP != nil {
collector = "http"
name = c.Collect.HTTP.CollectorName
}

if collector == "" {
return "<none>"
}
if name != "" {
return fmt.Sprintf("%s/%s", collector, name)
}
if selector != "" {
return fmt.Sprintf("%s/%s", collector, selector)
}
return collector
}

func ParseSpec(specContents string) (*troubleshootv1beta1.Collect, error) {
collect := troubleshootv1beta1.Collect{}

Expand Down

0 comments on commit b6ca58d

Please sign in to comment.