Skip to content

Commit

Permalink
bug: Remove duplicate results in preflights (#1626)
Browse files Browse the repository at this point in the history
Change to stop re-analysing preflight results when uploadResultsTo is present leading to duplicate results

Signed-off-by: Evans Mungai <evans@replicated.com>
  • Loading branch information
banjoh authored Sep 26, 2024
1 parent 142015c commit 2bb611c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pkg/analyze/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func Analyze(
return nil, errors.New("nil analyzer")
}

analyzerInst := getAnalyzer(analyzer)
analyzerInst := GetAnalyzer(analyzer)
if analyzerInst == nil {
klog.Info("Non-existent analyzer found in the spec. Please double-check the spelling and indentation of the analyzers in the spec.")
return nil, nil
Expand Down Expand Up @@ -188,7 +188,7 @@ type Analyzer interface {
Analyze(getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error)
}

func getAnalyzer(analyzer *troubleshootv1beta2.Analyze) Analyzer {
func GetAnalyzer(analyzer *troubleshootv1beta2.Analyze) Analyzer {
switch {
case analyzer.ClusterVersion != nil:
return &AnalyzeClusterVersion{analyzer: analyzer.ClusterVersion}
Expand Down
4 changes: 3 additions & 1 deletion pkg/analyze/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/replicatedhq/troubleshoot/pkg/constants"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
)

type providers struct {
Expand Down Expand Up @@ -75,7 +76,8 @@ func (a *AnalyzeDistribution) IsExcluded() (bool, error) {
func (a *AnalyzeDistribution) Analyze(getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
result, err := a.analyzeDistribution(a.analyzer, getFile)
if err != nil {
return nil, err
klog.Errorf("failed to analyze distribution: %v", err)
return nil, errors.Wrapf(err, "failed to analyze distribution")
}
result.Strict = a.analyzer.Strict.BoolOrDefaultFalse()
return []*AnalyzeResult{result}, nil
Expand Down
8 changes: 7 additions & 1 deletion pkg/preflight/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,17 @@ func doAnalyze(
klog.Errorf("failed to determine if analyzer %v is strict: %s", analyzer, strictErr)
}

title := "Analyzer Failed"
analyzerInst := analyze.GetAnalyzer(analyzer)
if analyzerInst != nil {
title = analyzerInst.Title()
}

analyzeResult = []*analyze.AnalyzeResult{
{
Strict: strict,
IsFail: true,
Title: "Analyzer Failed",
Title: title,
Message: err.Error(),
},
}
Expand Down
13 changes: 6 additions & 7 deletions pkg/preflight/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"k8s.io/klog/v2"
)

type empty struct{}

func RunPreflights(interactive bool, output string, format string, args []string) error {
ctx, root := otel.Tracer(
constants.LIB_TRACER_NAME).Start(context.Background(), constants.TROUBLESHOOT_ROOT_SPAN_NAME)
Expand Down Expand Up @@ -113,7 +115,7 @@ func RunPreflights(interactive bool, output string, format string, args []string
progressCollection.Go(collectNonInteractiveProgess(ctx, progressCh))
}

uploadResultsMap := make(map[string][]CollectResult)
uploadResultsMap := make(map[string]empty)
collectorResults := collect.NewResult()
analyzers := []*troubleshootv1beta2.Analyze{}
hostAnalyzers := []*troubleshootv1beta2.HostAnalyze{}
Expand All @@ -130,7 +132,7 @@ func RunPreflights(interactive bool, output string, format string, args []string
collectorResults.AddResult(collect.CollectorResult(collectorResult.AllCollectedData))

if spec.Spec.UploadResultsTo != "" {
uploadResultsMap[spec.Spec.UploadResultsTo] = append(uploadResultsMap[spec.Spec.UploadResultsTo], *r)
uploadResultsMap[spec.Spec.UploadResultsTo] = empty{}
uploadCollectResults = append(collectResults, *r)
} else {
collectResults = append(collectResults, *r)
Expand Down Expand Up @@ -188,11 +190,8 @@ func RunPreflights(interactive bool, output string, format string, args []string
}

uploadAnalyzeResultsMap := make(map[string][]*analyzer.AnalyzeResult)
for location, results := range uploadResultsMap {
for _, res := range results {
uploadAnalyzeResultsMap[location] = append(uploadAnalyzeResultsMap[location], res.Analyze()...)
analyzeResults = append(analyzeResults, uploadAnalyzeResultsMap[location]...)
}
for location := range uploadResultsMap {
uploadAnalyzeResultsMap[location] = append(uploadAnalyzeResultsMap[location], analyzeResults...)
}

for k, v := range uploadAnalyzeResultsMap {
Expand Down

0 comments on commit 2bb611c

Please sign in to comment.