Skip to content

Commit

Permalink
relocated Dedup to support bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
DexterYan committed Oct 24, 2024
1 parent 0922b7d commit 542e402
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
23 changes: 0 additions & 23 deletions pkg/kotsutil/troubleshoot.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,3 @@ func LoadTSKindsFromPath(dir string) (*troubleshootloader.TroubleshootKinds, err
}
return tsKinds, nil
}

func Dedup[T any](objs []T) []T {
seen := make(map[string]bool)
out := []T{}

if len(objs) == 0 {
return objs
}

for _, o := range objs {
data, err := json.Marshal(o)
if err != nil {
out = append(out, o)
continue
}
key := string(data)
if _, ok := seen[key]; !ok {
out = append(out, o)
seen[key] = true
}
}
return out
}
41 changes: 32 additions & 9 deletions pkg/supportbundle/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ func mergeSupportBundleSpecs(builtBundles map[string]*troubleshootv1beta2.Suppor
mergedBundle.Spec.HostAnalyzers = append(mergedBundle.Spec.HostAnalyzers, builtBundle.Spec.HostAnalyzers...)
}

mergedBundle.Spec.Collectors = kotsutil.Dedup(mergedBundle.Spec.Collectors)
mergedBundle.Spec.Analyzers = kotsutil.Dedup(mergedBundle.Spec.Analyzers)
mergedBundle.Spec.AfterCollection = kotsutil.Dedup(mergedBundle.Spec.AfterCollection)
mergedBundle.Spec.HostCollectors = kotsutil.Dedup(mergedBundle.Spec.HostCollectors)
mergedBundle.Spec.HostAnalyzers = kotsutil.Dedup(mergedBundle.Spec.HostAnalyzers)
mergedBundle.Spec.Collectors = Dedup(mergedBundle.Spec.Collectors)
mergedBundle.Spec.Analyzers = Dedup(mergedBundle.Spec.Analyzers)
mergedBundle.Spec.AfterCollection = Dedup(mergedBundle.Spec.AfterCollection)
mergedBundle.Spec.HostCollectors = Dedup(mergedBundle.Spec.HostCollectors)
mergedBundle.Spec.HostAnalyzers = Dedup(mergedBundle.Spec.HostAnalyzers)

return mergedBundle
}
Expand Down Expand Up @@ -474,10 +474,10 @@ func addDiscoveredSpecs(
}

// remove duplicated specs if there are multiple support bundle upstream spec
supportBundle.Spec.Collectors = kotsutil.Dedup(supportBundle.Spec.Collectors)
supportBundle.Spec.Analyzers = kotsutil.Dedup(supportBundle.Spec.Analyzers)
supportBundle.Spec.HostCollectors = kotsutil.Dedup(supportBundle.Spec.HostCollectors)
supportBundle.Spec.HostAnalyzers = kotsutil.Dedup(supportBundle.Spec.HostAnalyzers)
supportBundle.Spec.Collectors = Dedup(supportBundle.Spec.Collectors)
supportBundle.Spec.Analyzers = Dedup(supportBundle.Spec.Analyzers)
supportBundle.Spec.HostCollectors = Dedup(supportBundle.Spec.HostCollectors)
supportBundle.Spec.HostAnalyzers = Dedup(supportBundle.Spec.HostAnalyzers)

return supportBundle
}
Expand Down Expand Up @@ -1257,3 +1257,26 @@ func removeKurlAnalyzers(analyzers []*troubleshootv1beta2.Analyze) []*troublesho

return analyze
}

func Dedup[T any](objs []T) []T {
seen := make(map[string]bool)
out := []T{}

if len(objs) == 0 {
return objs
}

for _, o := range objs {
data, err := json.Marshal(o)
if err != nil {
out = append(out, o)
continue
}
key := string(data)
if _, ok := seen[key]; !ok {
out = append(out, o)
seen[key] = true
}
}
return out
}

0 comments on commit 542e402

Please sign in to comment.