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..bca88e0d3 --- /dev/null +++ b/cmd/preflight/cli/upload_results.go @@ -0,0 +1,65 @@ +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 + resp, err := client.Do(req) + if err != nil { + return err + } + + if resp.StatusCode > 290 { + return err + } + + return nil +} diff --git a/config/crds/troubleshoot.replicated.com_preflights.yaml b/config/crds/troubleshoot.replicated.com_preflights.yaml index 91c31321f..39cb34bb4 100644 --- a/config/crds/troubleshoot.replicated.com_preflights.yaml +++ b/config/crds/troubleshoot.replicated.com_preflights.yaml @@ -744,6 +744,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