Skip to content

Commit

Permalink
Upload results
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed Jul 19, 2019
1 parent 997a220 commit 3e76554
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cmd/preflight/cli/run_nocrd.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ func runPreflightsNoCRD(v *viper.Viper, arg string) error {
analyzeResults = append(analyzeResults, analyzeResult)
}

if preflight.Spec.UploadResultsTo != "" {
tryUploadResults(preflight.Spec.UploadResultsTo, preflight.Name, analyzeResults)
}
if v.GetBool("interactive") {
return showInteractiveResults(preflight.Name, analyzeResults)
}
Expand Down
61 changes: 61 additions & 0 deletions cmd/preflight/cli/upload_results.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cli

import (
"bytes"
"encoding/json"
"net/http"

analyzerunner "github.com/replicatedhq/troubleshoot/pkg/analyze"
)

type UploadPreflightResult struct {
IsFail bool `json:"isFail,omitempty"`
IsWarn bool `json:"isWarn,omitempty"`
IsPass bool `json:"isPass,omitempty"`

Title string `json:"title"`
Message string `json:"message"`
URI string `json:"uri,omitempty"`
}

type UploadPreflightResults struct {
Results []*UploadPreflightResult `json:"results"`
}

func tryUploadResults(uri string, preflightName string, analyzeResults []*analyzerunner.AnalyzeResult) error {
uploadPreflightResults := UploadPreflightResults{
Results: []*UploadPreflightResult{},
}
for _, analyzeResult := range analyzeResults {
uploadPreflightResult := &UploadPreflightResult{
IsFail: analyzeResult.IsFail,
IsWarn: analyzeResult.IsWarn,
IsPass: analyzeResult.IsPass,
Title: analyzeResult.Title,
Message: analyzeResult.Message,
URI: analyzeResult.URI,
}

uploadPreflightResults.Results = append(uploadPreflightResults.Results, uploadPreflightResult)
}

b, err := json.Marshal(uploadPreflightResults)
if err != nil {
return err
}

req, err := http.NewRequest("POST", uri, bytes.NewBuffer(b))
if err != nil {
return err
}

req.Header.Set("Content-Type", "application/json")

client := http.DefaultClient
_, err = client.Do(req)
if err != nil {
return err
}

return nil
}
2 changes: 2 additions & 0 deletions config/crds/troubleshoot.replicated.com_preflights.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,8 @@ spec:
type: object
type: object
type: array
uploadResultsTo:
type: string
type: object
status:
type: object
Expand Down
1 change: 1 addition & 0 deletions config/samples/troubleshoot_v1beta1_preflight.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ kind: Preflight
metadata:
name: shiny-new-ai
spec:
uploadResultsTo: https://hookb.in/Z26mz8R9VpC7q7eYrWob
analyzers:
- clusterVersion:
outcomes:
Expand Down
5 changes: 3 additions & 2 deletions pkg/apis/troubleshoot/v1beta1/preflight_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import (

// PreflightSpec defines the desired state of Preflight
type PreflightSpec struct {
Collectors []*Collect `json:"collectors,omitempty" yaml:"collectors,omitempty"`
Analyzers []*Analyze `json:"analyzers,omitempty" yaml:"analyzers,omitempty"`
UploadResultsTo string `json:"uploadResultsTo,omitempty" yaml:"uploadResultsTo,omitempty"`
Collectors []*Collect `json:"collectors,omitempty" yaml:"collectors,omitempty"`
Analyzers []*Analyze `json:"analyzers,omitempty" yaml:"analyzers,omitempty"`
}

// PreflightStatus defines the observed state of Preflight
Expand Down

0 comments on commit 3e76554

Please sign in to comment.