-
Notifications
You must be signed in to change notification settings - Fork 92
/
analyze.go
133 lines (116 loc) · 3.61 KB
/
analyze.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package preflight
import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"strings"
analyze "github.com/replicatedhq/troubleshoot/pkg/analyze"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
"k8s.io/klog/v2"
)
// Analyze runs the analyze phase of preflight checks
func (c ClusterCollectResult) Analyze() []*analyze.AnalyzeResult {
return doAnalyze(c.Context, c.AllCollectedData, c.Spec.Spec.Analyzers, nil, "")
}
// Analyze runs the analyze phase of host preflight checks
func (c HostCollectResult) Analyze() []*analyze.AnalyzeResult {
return doAnalyze(c.Context, c.AllCollectedData, nil, c.Spec.Spec.Analyzers, "")
}
// Analyze runs the analyze phase of host preflight checks.
//
// Runs the analysis for each node and aggregates the results.
func (c RemoteCollectResult) Analyze() []*analyze.AnalyzeResult {
var results []*analyze.AnalyzeResult
for nodeName, nodeResult := range c.AllCollectedData {
var strResult = make(map[string]string)
if err := json.Unmarshal(nodeResult, &strResult); err != nil {
analyzeResult := &analyze.AnalyzeResult{
IsFail: true,
Title: "Remote Result Parser Failed",
Message: err.Error(),
}
results = append(results, analyzeResult)
continue
}
var byteResult = make(map[string][]byte)
for k, v := range strResult {
byteResult[k] = []byte(v)
}
results = append(results, doAnalyze(c.Context, byteResult, nil, c.Spec.Spec.Analyzers, nodeName)...)
}
return results
}
func doAnalyze(
ctx context.Context,
allCollectedData map[string][]byte,
analyzers []*troubleshootv1beta2.Analyze,
hostAnalyzers []*troubleshootv1beta2.HostAnalyze,
nodeName string,
) []*analyze.AnalyzeResult {
getCollectedFileContents := func(fileName string) ([]byte, error) {
contents, ok := allCollectedData[fileName]
if !ok {
return nil, fmt.Errorf("file %s was not collected", fileName)
}
return contents, nil
}
getChildCollectedFileContents := func(prefix string, excludeFiles []string) (map[string][]byte, error) {
matching := make(map[string][]byte)
for k, v := range allCollectedData {
if strings.HasPrefix(k, prefix) {
matching[k] = v
}
}
for k, v := range allCollectedData {
if ok, _ := filepath.Match(prefix, k); ok {
matching[k] = v
}
}
if len(excludeFiles) > 0 {
for k := range matching {
for _, ex := range excludeFiles {
if ok, _ := filepath.Match(ex, k); ok {
delete(matching, k)
}
}
}
}
if len(matching) == 0 {
return nil, fmt.Errorf("File not found: %s", prefix)
}
return matching, nil
}
analyzeResults := []*analyze.AnalyzeResult{}
for _, analyzer := range analyzers {
analyzeResult, err := analyze.Analyze(ctx, analyzer, getCollectedFileContents, getChildCollectedFileContents)
if err != nil {
strict, strictErr := HasStrictAnalyzer(analyzer)
if strictErr != nil {
klog.Errorf("failed to determine if analyzer %v is strict: %s", analyzer, strictErr)
}
analyzeResult = []*analyze.AnalyzeResult{
{
Strict: strict,
IsFail: true,
Title: "Analyzer Failed",
Message: err.Error(),
},
}
}
if analyzeResult != nil {
analyzeResults = append(analyzeResults, analyzeResult...)
}
}
for _, hostAnalyzer := range hostAnalyzers {
analyzeResult := analyze.HostAnalyze(ctx, hostAnalyzer, getCollectedFileContents, getChildCollectedFileContents)
analyzeResults = append(analyzeResults, analyzeResult...)
}
// Add the nodename to the result title if provided.
if nodeName != "" {
for _, result := range analyzeResults {
result.Title = fmt.Sprintf("%s (%s)", result.Title, nodeName)
}
}
return analyzeResults
}