Files
troubleshoot/pkg/preflight/analyze.go
Dexter Yan 7e3a59cfc0 feat(analyze): add ExcludeFiles field to textAnazlye (#867)
* feat(analyze): add ExcludeFiles field to textAnazlye

* feat(analyze): fix test for getFiles

* feat(analyze): change function name to  excludeFilePaths

* feat(analyze): fix preflight test fail

* feat(analyze): add tests for excludeFiles

* feat(schemas): run make schemas

* feat(analyze): use getChildCollectedFileContents function prototype

* feat(analyze): reduce time complexity

* feat(longhorn): add getFileContents as getCollectedFileContents
2022-11-28 10:45:10 +13:00

127 lines
3.6 KiB
Go

package preflight
import (
"encoding/json"
"fmt"
"path/filepath"
"strings"
analyze "github.com/replicatedhq/troubleshoot/pkg/analyze"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
"github.com/replicatedhq/troubleshoot/pkg/logger"
)
// Analyze runs the analyze phase of preflight checks
func (c ClusterCollectResult) Analyze() []*analyze.AnalyzeResult {
return doAnalyze(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.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(byteResult, nil, c.Spec.Spec.Analyzers, nodeName)...)
}
return results
}
func doAnalyze(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(analyzer, getCollectedFileContents, getChildCollectedFileContents)
if err != nil {
strict, strictErr := HasStrictAnalyzer(analyzer)
if strictErr != nil {
logger.Printf("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(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
}