From 3e765543584c2a4ec08f46e7219d5424d5652256 Mon Sep 17 00:00:00 2001 From: Marc Campbell Date: Fri, 19 Jul 2019 22:37:53 +0000 Subject: [PATCH 1/2] Upload results --- cmd/preflight/cli/run_nocrd.go | 3 + cmd/preflight/cli/upload_results.go | 61 +++++++++++++++++++ ...roubleshoot.replicated.com_preflights.yaml | 2 + .../troubleshoot_v1beta1_preflight.yaml | 1 + .../troubleshoot/v1beta1/preflight_types.go | 5 +- 5 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 cmd/preflight/cli/upload_results.go diff --git a/cmd/preflight/cli/run_nocrd.go b/cmd/preflight/cli/run_nocrd.go index 75b0e39c0..b2f1e306d 100644 --- a/cmd/preflight/cli/run_nocrd.go +++ b/cmd/preflight/cli/run_nocrd.go @@ -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) } diff --git a/cmd/preflight/cli/upload_results.go b/cmd/preflight/cli/upload_results.go new file mode 100644 index 000000000..10ab3d5e1 --- /dev/null +++ b/cmd/preflight/cli/upload_results.go @@ -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 +} diff --git a/config/crds/troubleshoot.replicated.com_preflights.yaml b/config/crds/troubleshoot.replicated.com_preflights.yaml index 029ba2d07..31270b1fb 100644 --- a/config/crds/troubleshoot.replicated.com_preflights.yaml +++ b/config/crds/troubleshoot.replicated.com_preflights.yaml @@ -694,6 +694,8 @@ spec: type: object type: object type: array + uploadResultsTo: + type: string type: object status: type: object diff --git a/config/samples/troubleshoot_v1beta1_preflight.yaml b/config/samples/troubleshoot_v1beta1_preflight.yaml index f391fb353..969c119f1 100644 --- a/config/samples/troubleshoot_v1beta1_preflight.yaml +++ b/config/samples/troubleshoot_v1beta1_preflight.yaml @@ -3,6 +3,7 @@ kind: Preflight metadata: name: shiny-new-ai spec: + uploadResultsTo: https://hookb.in/Z26mz8R9VpC7q7eYrWob analyzers: - clusterVersion: outcomes: diff --git a/pkg/apis/troubleshoot/v1beta1/preflight_types.go b/pkg/apis/troubleshoot/v1beta1/preflight_types.go index c6ca56286..399fbddae 100644 --- a/pkg/apis/troubleshoot/v1beta1/preflight_types.go +++ b/pkg/apis/troubleshoot/v1beta1/preflight_types.go @@ -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 From 6407df6a16a7099dce43e6fe6f8e9abb8045459b Mon Sep 17 00:00:00 2001 From: Marc Campbell Date: Fri, 19 Jul 2019 22:43:34 +0000 Subject: [PATCH 2/2] Upload error --- cmd/preflight/cli/upload_results.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cmd/preflight/cli/upload_results.go b/cmd/preflight/cli/upload_results.go index 10ab3d5e1..bca88e0d3 100644 --- a/cmd/preflight/cli/upload_results.go +++ b/cmd/preflight/cli/upload_results.go @@ -52,10 +52,14 @@ func tryUploadResults(uri string, preflightName string, analyzeResults []*analyz req.Header.Set("Content-Type", "application/json") client := http.DefaultClient - _, err = client.Do(req) + resp, err := client.Do(req) if err != nil { return err } + if resp.StatusCode > 290 { + return err + } + return nil }