Skip to content

Commit

Permalink
allow textAnalyze to run on all files in a dir or matching a prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
laverya committed Apr 15, 2020
1 parent f06cd9e commit 222a931
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 83 deletions.
98 changes: 81 additions & 17 deletions pkg/analyze/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func isExcluded(excludeVal multitype.BoolOrString) (bool, error) {
return parsed, nil
}

func Analyze(analyzer *troubleshootv1beta1.Analyze, getFile getCollectedFileContents, findFiles getChildCollectedFileContents) (*AnalyzeResult, error) {
func Analyze(analyzer *troubleshootv1beta1.Analyze, getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
results := []*AnalyzeResult{}
if analyzer.ClusterVersion != nil {
isExcluded, err := isExcluded(analyzer.ClusterVersion.Exclude)
if err != nil {
Expand All @@ -45,7 +46,11 @@ func Analyze(analyzer *troubleshootv1beta1.Analyze, getFile getCollectedFileCont
if isExcluded {
return nil, nil
}
return analyzeClusterVersion(analyzer.ClusterVersion, getFile)
result, err := analyzeClusterVersion(analyzer.ClusterVersion, getFile)
if err != nil {
return nil, err
}
results = append(results, result)
}
if analyzer.StorageClass != nil {
isExcluded, err := isExcluded(analyzer.StorageClass.Exclude)
Expand All @@ -55,7 +60,11 @@ func Analyze(analyzer *troubleshootv1beta1.Analyze, getFile getCollectedFileCont
if isExcluded {
return nil, nil
}
return analyzeStorageClass(analyzer.StorageClass, getFile)
result, err := analyzeStorageClass(analyzer.StorageClass, getFile)
if err != nil {
return nil, err
}
results = append(results, result)
}
if analyzer.CustomResourceDefinition != nil {
isExcluded, err := isExcluded(analyzer.CustomResourceDefinition.Exclude)
Expand All @@ -65,7 +74,11 @@ func Analyze(analyzer *troubleshootv1beta1.Analyze, getFile getCollectedFileCont
if isExcluded {
return nil, nil
}
return analyzeCustomResourceDefinition(analyzer.CustomResourceDefinition, getFile)
result, err := analyzeCustomResourceDefinition(analyzer.CustomResourceDefinition, getFile)
if err != nil {
return nil, err
}
results = append(results, result)
}
if analyzer.Ingress != nil {
isExcluded, err := isExcluded(analyzer.Ingress.Exclude)
Expand All @@ -75,7 +88,11 @@ func Analyze(analyzer *troubleshootv1beta1.Analyze, getFile getCollectedFileCont
if isExcluded {
return nil, nil
}
return analyzeIngress(analyzer.Ingress, getFile)
result, err := analyzeIngress(analyzer.Ingress, getFile)
if err != nil {
return nil, err
}
results = append(results, result)
}
if analyzer.Secret != nil {
isExcluded, err := isExcluded(analyzer.Secret.Exclude)
Expand All @@ -85,7 +102,11 @@ func Analyze(analyzer *troubleshootv1beta1.Analyze, getFile getCollectedFileCont
if isExcluded {
return nil, nil
}
return analyzeSecret(analyzer.Secret, getFile)
result, err := analyzeSecret(analyzer.Secret, getFile)
if err != nil {
return nil, err
}
results = append(results, result)
}
if analyzer.ImagePullSecret != nil {
isExcluded, err := isExcluded(analyzer.ImagePullSecret.Exclude)
Expand All @@ -95,7 +116,11 @@ func Analyze(analyzer *troubleshootv1beta1.Analyze, getFile getCollectedFileCont
if isExcluded {
return nil, nil
}
return analyzeImagePullSecret(analyzer.ImagePullSecret, findFiles)
result, err := analyzeImagePullSecret(analyzer.ImagePullSecret, findFiles)
if err != nil {
return nil, err
}
results = append(results, result)
}
if analyzer.DeploymentStatus != nil {
isExcluded, err := isExcluded(analyzer.DeploymentStatus.Exclude)
Expand All @@ -105,7 +130,11 @@ func Analyze(analyzer *troubleshootv1beta1.Analyze, getFile getCollectedFileCont
if isExcluded {
return nil, nil
}
return analyzeDeploymentStatus(analyzer.DeploymentStatus, getFile)
result, err := analyzeDeploymentStatus(analyzer.DeploymentStatus, getFile)
if err != nil {
return nil, err
}
results = append(results, result)
}
if analyzer.StatefulsetStatus != nil {
isExcluded, err := isExcluded(analyzer.StatefulsetStatus.Exclude)
Expand All @@ -115,7 +144,11 @@ func Analyze(analyzer *troubleshootv1beta1.Analyze, getFile getCollectedFileCont
if isExcluded {
return nil, nil
}
return analyzeStatefulsetStatus(analyzer.StatefulsetStatus, getFile)
result, err := analyzeStatefulsetStatus(analyzer.StatefulsetStatus, getFile)
if err != nil {
return nil, err
}
results = append(results, result)
}
if analyzer.ContainerRuntime != nil {
isExcluded, err := isExcluded(analyzer.ContainerRuntime.Exclude)
Expand All @@ -125,7 +158,11 @@ func Analyze(analyzer *troubleshootv1beta1.Analyze, getFile getCollectedFileCont
if isExcluded {
return nil, nil
}
return analyzeContainerRuntime(analyzer.ContainerRuntime, getFile)
result, err := analyzeContainerRuntime(analyzer.ContainerRuntime, getFile)
if err != nil {
return nil, err
}
results = append(results, result)
}
if analyzer.Distribution != nil {
isExcluded, err := isExcluded(analyzer.Distribution.Exclude)
Expand All @@ -135,7 +172,11 @@ func Analyze(analyzer *troubleshootv1beta1.Analyze, getFile getCollectedFileCont
if isExcluded {
return nil, nil
}
return analyzeDistribution(analyzer.Distribution, getFile)
result, err := analyzeDistribution(analyzer.Distribution, getFile)
if err != nil {
return nil, err
}
results = append(results, result)
}
if analyzer.NodeResources != nil {
isExcluded, err := isExcluded(analyzer.NodeResources.Exclude)
Expand All @@ -145,7 +186,11 @@ func Analyze(analyzer *troubleshootv1beta1.Analyze, getFile getCollectedFileCont
if isExcluded {
return nil, nil
}
return analyzeNodeResources(analyzer.NodeResources, getFile)
result, err := analyzeNodeResources(analyzer.NodeResources, getFile)
if err != nil {
return nil, err
}
results = append(results, result)
}
if analyzer.TextAnalyze != nil {
isExcluded, err := isExcluded(analyzer.TextAnalyze.Exclude)
Expand All @@ -155,7 +200,11 @@ func Analyze(analyzer *troubleshootv1beta1.Analyze, getFile getCollectedFileCont
if isExcluded {
return nil, nil
}
return analyzeTextAnalyze(analyzer.TextAnalyze, getFile)
multiResult, err := analyzeTextAnalyze(analyzer.TextAnalyze, findFiles)
if err != nil {
return nil, err
}
results = append(results, multiResult...)
}
if analyzer.Postgres != nil {
isExcluded, err := isExcluded(analyzer.Postgres.Exclude)
Expand All @@ -165,7 +214,11 @@ func Analyze(analyzer *troubleshootv1beta1.Analyze, getFile getCollectedFileCont
if isExcluded {
return nil, nil
}
return analyzePostgres(analyzer.Postgres, getFile)
result, err := analyzePostgres(analyzer.Postgres, getFile)
if err != nil {
return nil, err
}
results = append(results, result)
}
if analyzer.Mysql != nil {
isExcluded, err := isExcluded(analyzer.Mysql.Exclude)
Expand All @@ -175,7 +228,11 @@ func Analyze(analyzer *troubleshootv1beta1.Analyze, getFile getCollectedFileCont
if isExcluded {
return nil, nil
}
return analyzeMysql(analyzer.Mysql, getFile)
result, err := analyzeMysql(analyzer.Mysql, getFile)
if err != nil {
return nil, err
}
results = append(results, result)
}
if analyzer.Redis != nil {
isExcluded, err := isExcluded(analyzer.Redis.Exclude)
Expand All @@ -185,8 +242,15 @@ func Analyze(analyzer *troubleshootv1beta1.Analyze, getFile getCollectedFileCont
if isExcluded {
return nil, nil
}
return analyzeRedis(analyzer.Redis, getFile)
result, err := analyzeRedis(analyzer.Redis, getFile)
if err != nil {
return nil, err
}
results = append(results, result)
}

return nil, errors.New("invalid analyzer")
if len(results) == 0 {
return nil, errors.New("invalid analyzer")
}
return results, nil
}
4 changes: 2 additions & 2 deletions pkg/analyze/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func DownloadAndAnalyze(bundleURL string, analyzersSpec string) ([]*AnalyzeResul
}

if analyzeResult != nil {
analyzeResults = append(analyzeResults, analyzeResult)
analyzeResults = append(analyzeResults, analyzeResult...)
}
}

Expand Down Expand Up @@ -192,6 +192,6 @@ func (f fileContentProvider) getFileContents(fileName string) ([]byte, error) {

func (f fileContentProvider) getChildFileContents(dirName string) (map[string][]byte, error) {
// TODO: walk sub-dirs
// return nil, errors.New("not implemnted")
// return nil, errors.New("not implemented")
return map[string][]byte{}, nil
}
42 changes: 33 additions & 9 deletions pkg/analyze/text_analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
)

func analyzeTextAnalyze(analyzer *troubleshootv1beta1.TextAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
func analyzeTextAnalyze(analyzer *troubleshootv1beta1.TextAnalyze, getCollectedFileContents func(string) (map[string][]byte, error)) ([]*AnalyzeResult, error) {
fullPath := filepath.Join(analyzer.CollectorName, analyzer.FileName)
collected, err := getCollectedFileContents(fullPath)
if err != nil {
Expand All @@ -22,20 +22,44 @@ func analyzeTextAnalyze(analyzer *troubleshootv1beta1.TextAnalyze, getCollectedF
checkName = analyzer.CollectorName
}

results := []*AnalyzeResult{}

if analyzer.RegexPattern != "" {
return analyzeRegexPattern(analyzer.RegexPattern, collected, analyzer.Outcomes, checkName)
for _, fileContents := range collected {
result, err := analyzeRegexPattern(analyzer.RegexPattern, fileContents, analyzer.Outcomes, checkName)
if err != nil {
return nil, err
}
if result != nil {
results = append(results, result)
}
}
}

if analyzer.RegexGroups != "" {
return analyzeRegexGroups(analyzer.RegexGroups, collected, analyzer.Outcomes, checkName)
for _, fileContents := range collected {
result, err := analyzeRegexGroups(analyzer.RegexGroups, fileContents, analyzer.Outcomes, checkName)
if err != nil {
return nil, err
}
if result != nil {
results = append(results, result)
}
}
}

return &AnalyzeResult{
Title: checkName,
IconKey: "kubernetes_text_analyze",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
IsFail: true,
Message: "Invalid analyzer",
if len(results) > 0 {
return results, nil
}

return []*AnalyzeResult{
{
Title: checkName,
IconKey: "kubernetes_text_analyze",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
IsFail: true,
Message: "Invalid analyzer",
},
}, nil
}

Expand Down

0 comments on commit 222a931

Please sign in to comment.