Skip to content

Commit 2bb611c

Browse files
authored
bug: Remove duplicate results in preflights (#1626)
Change to stop re-analysing preflight results when uploadResultsTo is present leading to duplicate results Signed-off-by: Evans Mungai <[email protected]>
1 parent 142015c commit 2bb611c

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

pkg/analyze/analyzer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func Analyze(
122122
return nil, errors.New("nil analyzer")
123123
}
124124

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

191-
func getAnalyzer(analyzer *troubleshootv1beta2.Analyze) Analyzer {
191+
func GetAnalyzer(analyzer *troubleshootv1beta2.Analyze) Analyzer {
192192
switch {
193193
case analyzer.ClusterVersion != nil:
194194
return &AnalyzeClusterVersion{analyzer: analyzer.ClusterVersion}

pkg/analyze/distribution.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/replicatedhq/troubleshoot/pkg/constants"
1111
corev1 "k8s.io/api/core/v1"
1212
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13+
"k8s.io/klog/v2"
1314
)
1415

1516
type providers struct {
@@ -75,7 +76,8 @@ func (a *AnalyzeDistribution) IsExcluded() (bool, error) {
7576
func (a *AnalyzeDistribution) Analyze(getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
7677
result, err := a.analyzeDistribution(a.analyzer, getFile)
7778
if err != nil {
78-
return nil, err
79+
klog.Errorf("failed to analyze distribution: %v", err)
80+
return nil, errors.Wrapf(err, "failed to analyze distribution")
7981
}
8082
result.Strict = a.analyzer.Strict.BoolOrDefaultFalse()
8183
return []*AnalyzeResult{result}, nil

pkg/preflight/analyze.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,17 @@ func doAnalyze(
103103
klog.Errorf("failed to determine if analyzer %v is strict: %s", analyzer, strictErr)
104104
}
105105

106+
title := "Analyzer Failed"
107+
analyzerInst := analyze.GetAnalyzer(analyzer)
108+
if analyzerInst != nil {
109+
title = analyzerInst.Title()
110+
}
111+
106112
analyzeResult = []*analyze.AnalyzeResult{
107113
{
108114
Strict: strict,
109115
IsFail: true,
110-
Title: "Analyzer Failed",
116+
Title: title,
111117
Message: err.Error(),
112118
},
113119
}

pkg/preflight/run.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import (
3131
"k8s.io/klog/v2"
3232
)
3333

34+
type empty struct{}
35+
3436
func RunPreflights(interactive bool, output string, format string, args []string) error {
3537
ctx, root := otel.Tracer(
3638
constants.LIB_TRACER_NAME).Start(context.Background(), constants.TROUBLESHOOT_ROOT_SPAN_NAME)
@@ -113,7 +115,7 @@ func RunPreflights(interactive bool, output string, format string, args []string
113115
progressCollection.Go(collectNonInteractiveProgess(ctx, progressCh))
114116
}
115117

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

132134
if spec.Spec.UploadResultsTo != "" {
133-
uploadResultsMap[spec.Spec.UploadResultsTo] = append(uploadResultsMap[spec.Spec.UploadResultsTo], *r)
135+
uploadResultsMap[spec.Spec.UploadResultsTo] = empty{}
134136
uploadCollectResults = append(collectResults, *r)
135137
} else {
136138
collectResults = append(collectResults, *r)
@@ -188,11 +190,8 @@ func RunPreflights(interactive bool, output string, format string, args []string
188190
}
189191

190192
uploadAnalyzeResultsMap := make(map[string][]*analyzer.AnalyzeResult)
191-
for location, results := range uploadResultsMap {
192-
for _, res := range results {
193-
uploadAnalyzeResultsMap[location] = append(uploadAnalyzeResultsMap[location], res.Analyze()...)
194-
analyzeResults = append(analyzeResults, uploadAnalyzeResultsMap[location]...)
195-
}
193+
for location := range uploadResultsMap {
194+
uploadAnalyzeResultsMap[location] = append(uploadAnalyzeResultsMap[location], analyzeResults...)
196195
}
197196

198197
for k, v := range uploadAnalyzeResultsMap {

0 commit comments

Comments
 (0)