mirror of
https://github.com/replicatedhq/troubleshoot.git
synced 2026-02-14 18:29:53 +00:00
feat(analyzer): remove duplicated analyzers (#1056)
feat(analyzer): add DedupAnalyzers
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user