feat(analyzer): remove duplicated analyzers (#1056)

feat(analyzer): add DedupAnalyzers
This commit is contained in:
Dexter Yan
2023-03-23 04:45:41 +13:00
committed by GitHub
parent 79f8e6efab
commit 8fe5bffae4
3 changed files with 119 additions and 0 deletions

View File

@@ -2,6 +2,7 @@ package analyzer
import (
"context"
"encoding/json"
"fmt"
"reflect"
"strconv"
@@ -235,3 +236,26 @@ func getAnalyzer(analyzer *troubleshootv1beta2.Analyze) Analyzer {
return nil
}
}
// deduplicates a list of troubleshootv1beta2.Analyze objects
// marshals object to json and then uses its string value to check for uniqueness
// there is no sorting of the keys in the analyze object's spec so if the spec isn't an exact match line for line as written, no dedup will occur
func DedupAnalyzers(allAnalyzers []*troubleshootv1beta2.Analyze) []*troubleshootv1beta2.Analyze {
uniqueAnalyzers := make(map[string]bool)
finalAnalyzers := []*troubleshootv1beta2.Analyze{}
for _, analyzer := range allAnalyzers {
data, err := json.Marshal(analyzer)
if err != nil {
// return analyzer as is if for whatever reason it can't be marshalled into json
finalAnalyzers = append(finalAnalyzers, analyzer)
} else {
stringData := string(data)
if _, value := uniqueAnalyzers[stringData]; !value {
uniqueAnalyzers[stringData] = true
finalAnalyzers = append(finalAnalyzers, analyzer)
}
}
}
return finalAnalyzers
}

View File

@@ -72,3 +72,97 @@ func TestAnalyzeWithNilAnalyzer(t *testing.T) {
assert.Error(t, err)
assert.Nil(t, got)
}
func TestCollector_DedupCollectors(t *testing.T) {
tests := []struct {
name string
Analyzers []*troubleshootv1beta2.Analyze
want []*troubleshootv1beta2.Analyze
}{
{
name: "multiple ClusterVersion",
Analyzers: []*troubleshootv1beta2.Analyze{
{
ClusterVersion: &troubleshootv1beta2.ClusterVersion{},
},
{
ClusterVersion: &troubleshootv1beta2.ClusterVersion{},
},
},
want: []*troubleshootv1beta2.Analyze{
{
ClusterVersion: &troubleshootv1beta2.ClusterVersion{},
},
},
},
{
name: "multiple TextAnalyze",
Analyzers: []*troubleshootv1beta2.Analyze{
{
TextAnalyze: &troubleshootv1beta2.TextAnalyze{
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{
CheckName: "hi",
},
},
},
{
TextAnalyze: &troubleshootv1beta2.TextAnalyze{
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{
CheckName: "hi",
},
},
},
},
want: []*troubleshootv1beta2.Analyze{
{
TextAnalyze: &troubleshootv1beta2.TextAnalyze{
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{
CheckName: "hi",
},
},
},
},
},
{
name: "multiple TextAnalyze with different CheckName",
Analyzers: []*troubleshootv1beta2.Analyze{
{
TextAnalyze: &troubleshootv1beta2.TextAnalyze{
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{
CheckName: "hi",
},
},
},
{
TextAnalyze: &troubleshootv1beta2.TextAnalyze{
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{
CheckName: "test",
},
},
},
},
want: []*troubleshootv1beta2.Analyze{
{
TextAnalyze: &troubleshootv1beta2.TextAnalyze{
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{
CheckName: "hi",
},
},
},
{
TextAnalyze: &troubleshootv1beta2.TextAnalyze{
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{
CheckName: "test",
},
},
},
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := DedupAnalyzers(tc.Analyzers)
assert.Equal(t, tc.want, got)
})
}
}

View File

@@ -255,6 +255,7 @@ func AnalyzeSupportBundle(ctx context.Context, spec *troubleshootv1beta2.Support
if len(spec.Analyzers) == 0 && len(spec.HostAnalyzers) == 0 {
return nil, nil
}
spec.Analyzers = analyzer.DedupAnalyzers(spec.Analyzers)
analyzeResults, err := analyzer.AnalyzeLocal(ctx, tmpDir, spec.Analyzers, spec.HostAnalyzers)
if err != nil {
return nil, errors.Wrap(err, "failed to analyze support bundle")