|
| 1 | +package preflight |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + |
| 6 | + "github.com/pkg/errors" |
| 7 | + troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" |
| 8 | +) |
| 9 | + |
| 10 | +// HasStrictAnalyzers - checks and returns true if a preflight's analyzer has strict:true, else false |
| 11 | +func HasStrictAnalyzers(preflight *troubleshootv1beta2.Preflight) (bool, error) { |
| 12 | + if preflight == nil { |
| 13 | + return false, nil |
| 14 | + } |
| 15 | + |
| 16 | + marshalledAnalyzers, err := json.Marshal(preflight.Spec.Analyzers) // marshall and remove nil Analyzers eg result: "[{\"clusterVersion\":{\"exclude\":\"\",\"strict\":\"false\",\"outcomes\":null}}]" |
| 17 | + if err != nil { |
| 18 | + return false, errors.Wrap(err, "failed to marshal analyzers") |
| 19 | + } |
| 20 | + |
| 21 | + analyzersMap := []map[string]interface{}{} |
| 22 | + err = json.Unmarshal(marshalledAnalyzers, &analyzersMap) // Unmarshall again so we can loop over non nil analyzers |
| 23 | + if err != nil { |
| 24 | + return false, errors.Wrap(err, "failed to unmarshal analyzers") |
| 25 | + } |
| 26 | + |
| 27 | + // analyzerMap will ignore empty Analyzers and loop around Analyzer with data |
| 28 | + for _, analyzers := range analyzersMap { // for each analyzer: map["clusterVersion": map[string]interface{} ["exclude": "", "strict": "true", "outcomes": nil] |
| 29 | + return hasStrictAnalyzer(analyzers) |
| 30 | + } |
| 31 | + return false, nil |
| 32 | +} |
| 33 | + |
| 34 | +// HasStrictAnalyzers - checks and returns true if a preflight's analyzer has strict:true, else false |
| 35 | +func HasStrictAnalyzer(analyzer *troubleshootv1beta2.Analyze) (bool, error) { |
| 36 | + marshalledAnalyzer, err := json.Marshal(analyzer) |
| 37 | + if err != nil { |
| 38 | + return false, errors.Wrap(err, "error while marshalling analyzer") |
| 39 | + } |
| 40 | + |
| 41 | + analyzerMap := make(map[string]interface{}) |
| 42 | + err = json.Unmarshal(marshalledAnalyzer, &analyzerMap) // Unmarshall again so we can loop over non nil analyzers |
| 43 | + if err != nil { |
| 44 | + return false, errors.Wrap(err, "failed to unmarshal analyzers") |
| 45 | + } |
| 46 | + return hasStrictAnalyzer(analyzerMap) |
| 47 | +} |
| 48 | + |
| 49 | +func hasStrictAnalyzer(analyzerMap map[string]interface{}) (bool, error) { |
| 50 | + for _, analyzer := range analyzerMap { // for each analyzerMeta: map[string]interface{} ["exclude": "", "strict": "true", "outcomes": nil] |
| 51 | + marshalledAnalyzer, err := json.Marshal(analyzer) |
| 52 | + if err != nil { |
| 53 | + return false, errors.Wrap(err, "error while marshalling analyzer") |
| 54 | + } |
| 55 | + // return Analyzer.Strict which can be extracted from AnalyzeMeta |
| 56 | + analyzeMeta := troubleshootv1beta2.AnalyzeMeta{} |
| 57 | + err = json.Unmarshal(marshalledAnalyzer, &analyzeMeta) |
| 58 | + if err != nil { |
| 59 | + return false, errors.Wrap(err, "error while un-marshalling marshalledAnalyzers") |
| 60 | + } |
| 61 | + return analyzeMeta.Strict.BoolOrDefaultFalse(), nil |
| 62 | + } |
| 63 | + return false, nil |
| 64 | +} |
| 65 | + |
| 66 | +// HasStrictAnalyzersFailed - checks if preflight analyzer's result is strict:true and isFail:true, then returns true else false |
| 67 | +func HasStrictAnalyzersFailed(preflightResult *UploadPreflightResults) bool { |
| 68 | + hasStrictAnalyzersFailed := false |
| 69 | + // if results are empty, treat as failure |
| 70 | + if preflightResult == nil || len(preflightResult.Results) == 0 { |
| 71 | + hasStrictAnalyzersFailed = true |
| 72 | + } else { |
| 73 | + for _, result := range preflightResult.Results { |
| 74 | + if result.IsFail && result.Strict { |
| 75 | + hasStrictAnalyzersFailed = true |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + return hasStrictAnalyzersFailed |
| 80 | +} |
0 commit comments