Skip to content

Commit b764050

Browse files
authored
Merge pull request #16 from replicatedhq/upload
Upload results
2 parents 0527490 + 6407df6 commit b764050

File tree

5 files changed

+74
-2
lines changed

5 files changed

+74
-2
lines changed

cmd/preflight/cli/run_nocrd.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ func runPreflightsNoCRD(v *viper.Viper, arg string) error {
8787
analyzeResults = append(analyzeResults, analyzeResult)
8888
}
8989

90+
if preflight.Spec.UploadResultsTo != "" {
91+
tryUploadResults(preflight.Spec.UploadResultsTo, preflight.Name, analyzeResults)
92+
}
9093
if v.GetBool("interactive") {
9194
return showInteractiveResults(preflight.Name, analyzeResults)
9295
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package cli
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"net/http"
7+
8+
analyzerunner "github.com/replicatedhq/troubleshoot/pkg/analyze"
9+
)
10+
11+
type UploadPreflightResult struct {
12+
IsFail bool `json:"isFail,omitempty"`
13+
IsWarn bool `json:"isWarn,omitempty"`
14+
IsPass bool `json:"isPass,omitempty"`
15+
16+
Title string `json:"title"`
17+
Message string `json:"message"`
18+
URI string `json:"uri,omitempty"`
19+
}
20+
21+
type UploadPreflightResults struct {
22+
Results []*UploadPreflightResult `json:"results"`
23+
}
24+
25+
func tryUploadResults(uri string, preflightName string, analyzeResults []*analyzerunner.AnalyzeResult) error {
26+
uploadPreflightResults := UploadPreflightResults{
27+
Results: []*UploadPreflightResult{},
28+
}
29+
for _, analyzeResult := range analyzeResults {
30+
uploadPreflightResult := &UploadPreflightResult{
31+
IsFail: analyzeResult.IsFail,
32+
IsWarn: analyzeResult.IsWarn,
33+
IsPass: analyzeResult.IsPass,
34+
Title: analyzeResult.Title,
35+
Message: analyzeResult.Message,
36+
URI: analyzeResult.URI,
37+
}
38+
39+
uploadPreflightResults.Results = append(uploadPreflightResults.Results, uploadPreflightResult)
40+
}
41+
42+
b, err := json.Marshal(uploadPreflightResults)
43+
if err != nil {
44+
return err
45+
}
46+
47+
req, err := http.NewRequest("POST", uri, bytes.NewBuffer(b))
48+
if err != nil {
49+
return err
50+
}
51+
52+
req.Header.Set("Content-Type", "application/json")
53+
54+
client := http.DefaultClient
55+
resp, err := client.Do(req)
56+
if err != nil {
57+
return err
58+
}
59+
60+
if resp.StatusCode > 290 {
61+
return err
62+
}
63+
64+
return nil
65+
}

config/crds/troubleshoot.replicated.com_preflights.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,8 @@ spec:
744744
type: object
745745
type: object
746746
type: array
747+
uploadResultsTo:
748+
type: string
747749
type: object
748750
status:
749751
type: object

config/samples/troubleshoot_v1beta1_preflight.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ kind: Preflight
33
metadata:
44
name: shiny-new-ai
55
spec:
6+
uploadResultsTo: https://hookb.in/Z26mz8R9VpC7q7eYrWob
67
analyzers:
78
- clusterVersion:
89
outcomes:

pkg/apis/troubleshoot/v1beta1/preflight_types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ import (
2222

2323
// PreflightSpec defines the desired state of Preflight
2424
type PreflightSpec struct {
25-
Collectors []*Collect `json:"collectors,omitempty" yaml:"collectors,omitempty"`
26-
Analyzers []*Analyze `json:"analyzers,omitempty" yaml:"analyzers,omitempty"`
25+
UploadResultsTo string `json:"uploadResultsTo,omitempty" yaml:"uploadResultsTo,omitempty"`
26+
Collectors []*Collect `json:"collectors,omitempty" yaml:"collectors,omitempty"`
27+
Analyzers []*Analyze `json:"analyzers,omitempty" yaml:"analyzers,omitempty"`
2728
}
2829

2930
// PreflightStatus defines the observed state of Preflight

0 commit comments

Comments
 (0)