Skip to content

Commit fb0f81d

Browse files
authored
bug: [sc-103753] Host analysers are not deduplicating during multiple spec merges #1485 (#1542)
* dedupe host collector and analyzer * dedup analyzer * add unit test * use generic
1 parent 78bbea1 commit fb0f81d

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

cmd/troubleshoot/cli/run.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,12 @@ func loadSpecs(ctx context.Context, args []string, client kubernetes.Interface)
362362
additionalRedactors.Spec.Redactors = util.Append(additionalRedactors.Spec.Redactors, r.Spec.Redactors)
363363
}
364364

365+
// dedupe specs
366+
mainBundle.Spec.Collectors = util.Dedup(mainBundle.Spec.Collectors)
367+
mainBundle.Spec.Analyzers = util.Dedup(mainBundle.Spec.Analyzers)
368+
mainBundle.Spec.HostCollectors = util.Dedup(mainBundle.Spec.HostCollectors)
369+
mainBundle.Spec.HostAnalyzers = util.Dedup(mainBundle.Spec.HostAnalyzers)
370+
365371
return mainBundle, additionalRedactors, nil
366372
}
367373

cmd/troubleshoot/cli/run_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,31 @@ spec:
343343
})
344344
}
345345
}
346+
347+
func Test_loadDuplicatedBundleSpecs(t *testing.T) {
348+
spec := testutils.ServeFromFilePath(t, `
349+
apiVersion: troubleshoot.sh/v1beta2
350+
kind: SupportBundle
351+
metadata:
352+
name: sb
353+
spec:
354+
collectors:
355+
- helm: {}
356+
analyzers:
357+
- clusterVersion: {}
358+
hostCollectors:
359+
- cpu: {}
360+
hostAnalyzers:
361+
- cpu: {}
362+
`)
363+
args := []string{spec, spec}
364+
365+
ctx := context.Background()
366+
client := testclient.NewSimpleClientset()
367+
sb, _, err := loadSpecs(ctx, args, client)
368+
require.NoError(t, err)
369+
assert.Len(t, sb.Spec.Collectors, 1+2) // default clusterInfo + clusterResources
370+
assert.Len(t, sb.Spec.Analyzers, 1)
371+
assert.Len(t, sb.Spec.HostCollectors, 1)
372+
assert.Len(t, sb.Spec.HostAnalyzers, 1)
373+
}

internal/util/util.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package util
33
import (
44
"bufio"
55
"bytes"
6+
"encoding/json"
67
"fmt"
78
"net/url"
89
"os"
@@ -145,3 +146,26 @@ func PromptYesNo(question string) bool {
145146
}
146147
}
147148
}
149+
150+
func Dedup[T any](objs []T) []T {
151+
seen := make(map[string]bool)
152+
out := []T{}
153+
154+
if len(objs) == 0 {
155+
return objs
156+
}
157+
158+
for _, o := range objs {
159+
data, err := json.Marshal(o)
160+
if err != nil {
161+
out = append(out, o)
162+
continue
163+
}
164+
key := string(data)
165+
if _, ok := seen[key]; !ok {
166+
out = append(out, o)
167+
seen[key] = true
168+
}
169+
}
170+
return out
171+
}

0 commit comments

Comments
 (0)