Skip to content

Commit

Permalink
Merge pull request #174 from replicatedhq/laverya/allow-analyzing-all…
Browse files Browse the repository at this point in the history
…-files-in-dir

allow textAnalyze to run on all files in a dir or matching a prefix
  • Loading branch information
laverya committed Sep 8, 2020
2 parents d2bc571 + 0126fb7 commit f68e0db
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 171 deletions.
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,12 @@ require (
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 // indirect
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 // indirect
golang.org/x/tools v0.0.0-20191010075000-0337d82405ff // indirect
gonum.org/v1/netlib v0.0.0-20190331212654-76723241ea4e // indirect
gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect
gopkg.in/yaml.v2 v2.2.8
k8s.io/api v0.18.3
k8s.io/apiextensions-apiserver v0.18.2
k8s.io/apimachinery v0.18.3
k8s.io/cli-runtime v0.18.0
k8s.io/client-go v0.18.2
k8s.io/code-generator v0.18.3-beta.0 // indirect
sigs.k8s.io/controller-runtime v0.5.1-0.20200402191424-df180accb901
sigs.k8s.io/controller-tools v0.3.0 // indirect
sigs.k8s.io/structured-merge-diff v1.0.1-0.20191108220359-b1b620dd3f06 // indirect
)
82 changes: 0 additions & 82 deletions go.sum

Large diffs are not rendered by default.

94 changes: 77 additions & 17 deletions pkg/analyze/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func isExcluded(excludeVal multitype.BoolOrString) (bool, error) {
return parsed, nil
}

func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileContents, findFiles getChildCollectedFileContents) (*AnalyzeResult, error) {
func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
if analyzer.ClusterVersion != nil {
isExcluded, err := isExcluded(analyzer.ClusterVersion.Exclude)
if err != nil {
Expand All @@ -49,7 +49,11 @@ func Analyze(analyzer *troubleshootv1beta2.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
}
return []*AnalyzeResult{result}, nil
}
if analyzer.StorageClass != nil {
isExcluded, err := isExcluded(analyzer.StorageClass.Exclude)
Expand All @@ -59,7 +63,11 @@ func Analyze(analyzer *troubleshootv1beta2.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
}
return []*AnalyzeResult{result}, nil
}
if analyzer.CustomResourceDefinition != nil {
isExcluded, err := isExcluded(analyzer.CustomResourceDefinition.Exclude)
Expand All @@ -69,7 +77,11 @@ func Analyze(analyzer *troubleshootv1beta2.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
}
return []*AnalyzeResult{result}, nil
}
if analyzer.Ingress != nil {
isExcluded, err := isExcluded(analyzer.Ingress.Exclude)
Expand All @@ -79,7 +91,11 @@ func Analyze(analyzer *troubleshootv1beta2.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
}
return []*AnalyzeResult{result}, nil
}
if analyzer.Secret != nil {
isExcluded, err := isExcluded(analyzer.Secret.Exclude)
Expand All @@ -89,7 +105,11 @@ func Analyze(analyzer *troubleshootv1beta2.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
}
return []*AnalyzeResult{result}, nil
}
if analyzer.ImagePullSecret != nil {
isExcluded, err := isExcluded(analyzer.ImagePullSecret.Exclude)
Expand All @@ -99,7 +119,11 @@ func Analyze(analyzer *troubleshootv1beta2.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
}
return []*AnalyzeResult{result}, nil
}
if analyzer.DeploymentStatus != nil {
isExcluded, err := isExcluded(analyzer.DeploymentStatus.Exclude)
Expand All @@ -109,7 +133,11 @@ func Analyze(analyzer *troubleshootv1beta2.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
}
return []*AnalyzeResult{result}, nil
}
if analyzer.StatefulsetStatus != nil {
isExcluded, err := isExcluded(analyzer.StatefulsetStatus.Exclude)
Expand All @@ -119,7 +147,11 @@ func Analyze(analyzer *troubleshootv1beta2.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
}
return []*AnalyzeResult{result}, nil
}
if analyzer.ContainerRuntime != nil {
isExcluded, err := isExcluded(analyzer.ContainerRuntime.Exclude)
Expand All @@ -129,7 +161,11 @@ func Analyze(analyzer *troubleshootv1beta2.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
}
return []*AnalyzeResult{result}, nil
}
if analyzer.Distribution != nil {
isExcluded, err := isExcluded(analyzer.Distribution.Exclude)
Expand All @@ -139,7 +175,11 @@ func Analyze(analyzer *troubleshootv1beta2.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
}
return []*AnalyzeResult{result}, nil
}
if analyzer.NodeResources != nil {
isExcluded, err := isExcluded(analyzer.NodeResources.Exclude)
Expand All @@ -149,7 +189,11 @@ func Analyze(analyzer *troubleshootv1beta2.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
}
return []*AnalyzeResult{result}, nil
}
if analyzer.TextAnalyze != nil {
isExcluded, err := isExcluded(analyzer.TextAnalyze.Exclude)
Expand All @@ -159,7 +203,11 @@ func Analyze(analyzer *troubleshootv1beta2.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
}
return multiResult, nil
}
if analyzer.Postgres != nil {
isExcluded, err := isExcluded(analyzer.Postgres.Exclude)
Expand All @@ -169,7 +217,11 @@ func Analyze(analyzer *troubleshootv1beta2.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
}
return []*AnalyzeResult{result}, nil
}
if analyzer.Mysql != nil {
isExcluded, err := isExcluded(analyzer.Mysql.Exclude)
Expand All @@ -179,7 +231,11 @@ func Analyze(analyzer *troubleshootv1beta2.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
}
return []*AnalyzeResult{result}, nil
}
if analyzer.Redis != nil {
isExcluded, err := isExcluded(analyzer.Redis.Exclude)
Expand All @@ -189,8 +245,12 @@ func Analyze(analyzer *troubleshootv1beta2.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
}
return []*AnalyzeResult{result}, nil
}

return nil, errors.New("invalid analyzer")

}
18 changes: 14 additions & 4 deletions pkg/analyze/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func AnalyzeLocal(localBundlePath string, analyzers []*troubleshootv1beta2.Analy
}

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

Expand Down Expand Up @@ -202,7 +202,17 @@ 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 map[string][]byte{}, nil
files, err := filepath.Glob(filepath.Join(f.rootDir, dirName))
if err != nil {
return nil, errors.Wrapf(err, "invalid glob %q", dirName)
}
fileArr := map[string][]byte{}
for _, filePath := range files {
bytes, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, errors.Wrapf(err, "read %q", filePath)
}
fileArr[filePath] = bytes
}
return fileArr, nil
}
54 changes: 45 additions & 9 deletions pkg/analyze/text_analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
)

func analyzeTextAnalyze(analyzer *troubleshootv1beta2.TextAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
func analyzeTextAnalyze(analyzer *troubleshootv1beta2.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 @@ -23,20 +23,56 @@ func analyzeTextAnalyze(analyzer *troubleshootv1beta2.TextAnalyze, getCollectedF
checkName = analyzer.CollectorName
}

if len(collected) == 0 {
return []*AnalyzeResult{
{
Title: checkName,
IconKey: "kubernetes_text_analyze",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
IsFail: false,
Message: "No matching files",
},
}, nil
}

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 f68e0db

Please sign in to comment.