Skip to content

Commit f68e0db

Browse files
authored
Merge pull request #174 from replicatedhq/laverya/allow-analyzing-all-files-in-dir
allow textAnalyze to run on all files in a dir or matching a prefix
2 parents d2bc571 + 0126fb7 commit f68e0db

File tree

7 files changed

+310
-171
lines changed

7 files changed

+310
-171
lines changed

go.mod

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,12 @@ require (
4646
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 // indirect
4747
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 // indirect
4848
golang.org/x/tools v0.0.0-20191010075000-0337d82405ff // indirect
49-
gonum.org/v1/netlib v0.0.0-20190331212654-76723241ea4e // indirect
5049
gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect
5150
gopkg.in/yaml.v2 v2.2.8
5251
k8s.io/api v0.18.3
5352
k8s.io/apiextensions-apiserver v0.18.2
5453
k8s.io/apimachinery v0.18.3
5554
k8s.io/cli-runtime v0.18.0
5655
k8s.io/client-go v0.18.2
57-
k8s.io/code-generator v0.18.3-beta.0 // indirect
5856
sigs.k8s.io/controller-runtime v0.5.1-0.20200402191424-df180accb901
59-
sigs.k8s.io/controller-tools v0.3.0 // indirect
60-
sigs.k8s.io/structured-merge-diff v1.0.1-0.20191108220359-b1b620dd3f06 // indirect
6157
)

go.sum

Lines changed: 0 additions & 82 deletions
Large diffs are not rendered by default.

pkg/analyze/analyzer.go

Lines changed: 77 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func isExcluded(excludeVal multitype.BoolOrString) (bool, error) {
4040
return parsed, nil
4141
}
4242

43-
func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileContents, findFiles getChildCollectedFileContents) (*AnalyzeResult, error) {
43+
func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
4444
if analyzer.ClusterVersion != nil {
4545
isExcluded, err := isExcluded(analyzer.ClusterVersion.Exclude)
4646
if err != nil {
@@ -49,7 +49,11 @@ func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileCont
4949
if isExcluded {
5050
return nil, nil
5151
}
52-
return analyzeClusterVersion(analyzer.ClusterVersion, getFile)
52+
result, err := analyzeClusterVersion(analyzer.ClusterVersion, getFile)
53+
if err != nil {
54+
return nil, err
55+
}
56+
return []*AnalyzeResult{result}, nil
5357
}
5458
if analyzer.StorageClass != nil {
5559
isExcluded, err := isExcluded(analyzer.StorageClass.Exclude)
@@ -59,7 +63,11 @@ func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileCont
5963
if isExcluded {
6064
return nil, nil
6165
}
62-
return analyzeStorageClass(analyzer.StorageClass, getFile)
66+
result, err := analyzeStorageClass(analyzer.StorageClass, getFile)
67+
if err != nil {
68+
return nil, err
69+
}
70+
return []*AnalyzeResult{result}, nil
6371
}
6472
if analyzer.CustomResourceDefinition != nil {
6573
isExcluded, err := isExcluded(analyzer.CustomResourceDefinition.Exclude)
@@ -69,7 +77,11 @@ func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileCont
6977
if isExcluded {
7078
return nil, nil
7179
}
72-
return analyzeCustomResourceDefinition(analyzer.CustomResourceDefinition, getFile)
80+
result, err := analyzeCustomResourceDefinition(analyzer.CustomResourceDefinition, getFile)
81+
if err != nil {
82+
return nil, err
83+
}
84+
return []*AnalyzeResult{result}, nil
7385
}
7486
if analyzer.Ingress != nil {
7587
isExcluded, err := isExcluded(analyzer.Ingress.Exclude)
@@ -79,7 +91,11 @@ func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileCont
7991
if isExcluded {
8092
return nil, nil
8193
}
82-
return analyzeIngress(analyzer.Ingress, getFile)
94+
result, err := analyzeIngress(analyzer.Ingress, getFile)
95+
if err != nil {
96+
return nil, err
97+
}
98+
return []*AnalyzeResult{result}, nil
8399
}
84100
if analyzer.Secret != nil {
85101
isExcluded, err := isExcluded(analyzer.Secret.Exclude)
@@ -89,7 +105,11 @@ func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileCont
89105
if isExcluded {
90106
return nil, nil
91107
}
92-
return analyzeSecret(analyzer.Secret, getFile)
108+
result, err := analyzeSecret(analyzer.Secret, getFile)
109+
if err != nil {
110+
return nil, err
111+
}
112+
return []*AnalyzeResult{result}, nil
93113
}
94114
if analyzer.ImagePullSecret != nil {
95115
isExcluded, err := isExcluded(analyzer.ImagePullSecret.Exclude)
@@ -99,7 +119,11 @@ func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileCont
99119
if isExcluded {
100120
return nil, nil
101121
}
102-
return analyzeImagePullSecret(analyzer.ImagePullSecret, findFiles)
122+
result, err := analyzeImagePullSecret(analyzer.ImagePullSecret, findFiles)
123+
if err != nil {
124+
return nil, err
125+
}
126+
return []*AnalyzeResult{result}, nil
103127
}
104128
if analyzer.DeploymentStatus != nil {
105129
isExcluded, err := isExcluded(analyzer.DeploymentStatus.Exclude)
@@ -109,7 +133,11 @@ func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileCont
109133
if isExcluded {
110134
return nil, nil
111135
}
112-
return analyzeDeploymentStatus(analyzer.DeploymentStatus, getFile)
136+
result, err := analyzeDeploymentStatus(analyzer.DeploymentStatus, getFile)
137+
if err != nil {
138+
return nil, err
139+
}
140+
return []*AnalyzeResult{result}, nil
113141
}
114142
if analyzer.StatefulsetStatus != nil {
115143
isExcluded, err := isExcluded(analyzer.StatefulsetStatus.Exclude)
@@ -119,7 +147,11 @@ func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileCont
119147
if isExcluded {
120148
return nil, nil
121149
}
122-
return analyzeStatefulsetStatus(analyzer.StatefulsetStatus, getFile)
150+
result, err := analyzeStatefulsetStatus(analyzer.StatefulsetStatus, getFile)
151+
if err != nil {
152+
return nil, err
153+
}
154+
return []*AnalyzeResult{result}, nil
123155
}
124156
if analyzer.ContainerRuntime != nil {
125157
isExcluded, err := isExcluded(analyzer.ContainerRuntime.Exclude)
@@ -129,7 +161,11 @@ func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileCont
129161
if isExcluded {
130162
return nil, nil
131163
}
132-
return analyzeContainerRuntime(analyzer.ContainerRuntime, getFile)
164+
result, err := analyzeContainerRuntime(analyzer.ContainerRuntime, getFile)
165+
if err != nil {
166+
return nil, err
167+
}
168+
return []*AnalyzeResult{result}, nil
133169
}
134170
if analyzer.Distribution != nil {
135171
isExcluded, err := isExcluded(analyzer.Distribution.Exclude)
@@ -139,7 +175,11 @@ func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileCont
139175
if isExcluded {
140176
return nil, nil
141177
}
142-
return analyzeDistribution(analyzer.Distribution, getFile)
178+
result, err := analyzeDistribution(analyzer.Distribution, getFile)
179+
if err != nil {
180+
return nil, err
181+
}
182+
return []*AnalyzeResult{result}, nil
143183
}
144184
if analyzer.NodeResources != nil {
145185
isExcluded, err := isExcluded(analyzer.NodeResources.Exclude)
@@ -149,7 +189,11 @@ func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileCont
149189
if isExcluded {
150190
return nil, nil
151191
}
152-
return analyzeNodeResources(analyzer.NodeResources, getFile)
192+
result, err := analyzeNodeResources(analyzer.NodeResources, getFile)
193+
if err != nil {
194+
return nil, err
195+
}
196+
return []*AnalyzeResult{result}, nil
153197
}
154198
if analyzer.TextAnalyze != nil {
155199
isExcluded, err := isExcluded(analyzer.TextAnalyze.Exclude)
@@ -159,7 +203,11 @@ func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileCont
159203
if isExcluded {
160204
return nil, nil
161205
}
162-
return analyzeTextAnalyze(analyzer.TextAnalyze, getFile)
206+
multiResult, err := analyzeTextAnalyze(analyzer.TextAnalyze, findFiles)
207+
if err != nil {
208+
return nil, err
209+
}
210+
return multiResult, nil
163211
}
164212
if analyzer.Postgres != nil {
165213
isExcluded, err := isExcluded(analyzer.Postgres.Exclude)
@@ -169,7 +217,11 @@ func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileCont
169217
if isExcluded {
170218
return nil, nil
171219
}
172-
return analyzePostgres(analyzer.Postgres, getFile)
220+
result, err := analyzePostgres(analyzer.Postgres, getFile)
221+
if err != nil {
222+
return nil, err
223+
}
224+
return []*AnalyzeResult{result}, nil
173225
}
174226
if analyzer.Mysql != nil {
175227
isExcluded, err := isExcluded(analyzer.Mysql.Exclude)
@@ -179,7 +231,11 @@ func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileCont
179231
if isExcluded {
180232
return nil, nil
181233
}
182-
return analyzeMysql(analyzer.Mysql, getFile)
234+
result, err := analyzeMysql(analyzer.Mysql, getFile)
235+
if err != nil {
236+
return nil, err
237+
}
238+
return []*AnalyzeResult{result}, nil
183239
}
184240
if analyzer.Redis != nil {
185241
isExcluded, err := isExcluded(analyzer.Redis.Exclude)
@@ -189,8 +245,12 @@ func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileCont
189245
if isExcluded {
190246
return nil, nil
191247
}
192-
return analyzeRedis(analyzer.Redis, getFile)
248+
result, err := analyzeRedis(analyzer.Redis, getFile)
249+
if err != nil {
250+
return nil, err
251+
}
252+
return []*AnalyzeResult{result}, nil
193253
}
194-
195254
return nil, errors.New("invalid analyzer")
255+
196256
}

pkg/analyze/download.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func AnalyzeLocal(localBundlePath string, analyzers []*troubleshootv1beta2.Analy
3434
}
3535

3636
if analyzeResult != nil {
37-
analyzeResults = append(analyzeResults, analyzeResult)
37+
analyzeResults = append(analyzeResults, analyzeResult...)
3838
}
3939
}
4040

@@ -202,7 +202,17 @@ func (f fileContentProvider) getFileContents(fileName string) ([]byte, error) {
202202
}
203203

204204
func (f fileContentProvider) getChildFileContents(dirName string) (map[string][]byte, error) {
205-
// TODO: walk sub-dirs
206-
// return nil, errors.New("not implemnted")
207-
return map[string][]byte{}, nil
205+
files, err := filepath.Glob(filepath.Join(f.rootDir, dirName))
206+
if err != nil {
207+
return nil, errors.Wrapf(err, "invalid glob %q", dirName)
208+
}
209+
fileArr := map[string][]byte{}
210+
for _, filePath := range files {
211+
bytes, err := ioutil.ReadFile(filePath)
212+
if err != nil {
213+
return nil, errors.Wrapf(err, "read %q", filePath)
214+
}
215+
fileArr[filePath] = bytes
216+
}
217+
return fileArr, nil
208218
}

pkg/analyze/text_analyze.go

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
1212
)
1313

14-
func analyzeTextAnalyze(analyzer *troubleshootv1beta2.TextAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
14+
func analyzeTextAnalyze(analyzer *troubleshootv1beta2.TextAnalyze, getCollectedFileContents func(string) (map[string][]byte, error)) ([]*AnalyzeResult, error) {
1515
fullPath := filepath.Join(analyzer.CollectorName, analyzer.FileName)
1616
collected, err := getCollectedFileContents(fullPath)
1717
if err != nil {
@@ -23,20 +23,56 @@ func analyzeTextAnalyze(analyzer *troubleshootv1beta2.TextAnalyze, getCollectedF
2323
checkName = analyzer.CollectorName
2424
}
2525

26+
if len(collected) == 0 {
27+
return []*AnalyzeResult{
28+
{
29+
Title: checkName,
30+
IconKey: "kubernetes_text_analyze",
31+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
32+
IsFail: false,
33+
Message: "No matching files",
34+
},
35+
}, nil
36+
}
37+
38+
results := []*AnalyzeResult{}
39+
2640
if analyzer.RegexPattern != "" {
27-
return analyzeRegexPattern(analyzer.RegexPattern, collected, analyzer.Outcomes, checkName)
41+
for _, fileContents := range collected {
42+
result, err := analyzeRegexPattern(analyzer.RegexPattern, fileContents, analyzer.Outcomes, checkName)
43+
if err != nil {
44+
return nil, err
45+
}
46+
if result != nil {
47+
results = append(results, result)
48+
}
49+
}
2850
}
2951

3052
if analyzer.RegexGroups != "" {
31-
return analyzeRegexGroups(analyzer.RegexGroups, collected, analyzer.Outcomes, checkName)
53+
for _, fileContents := range collected {
54+
result, err := analyzeRegexGroups(analyzer.RegexGroups, fileContents, analyzer.Outcomes, checkName)
55+
if err != nil {
56+
return nil, err
57+
}
58+
if result != nil {
59+
results = append(results, result)
60+
}
61+
}
3262
}
3363

34-
return &AnalyzeResult{
35-
Title: checkName,
36-
IconKey: "kubernetes_text_analyze",
37-
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
38-
IsFail: true,
39-
Message: "Invalid analyzer",
64+
if len(results) > 0 {
65+
return results, nil
66+
}
67+
68+
return []*AnalyzeResult{
69+
{
70+
Title: checkName,
71+
IconKey: "kubernetes_text_analyze",
72+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
73+
IsFail: true,
74+
Message: "Invalid analyzer",
75+
},
4076
}, nil
4177
}
4278

0 commit comments

Comments
 (0)