Skip to content

Commit

Permalink
feat: enable remote host collector in KOTS (#4958)
Browse files Browse the repository at this point in the history
  • Loading branch information
nvanthao authored Oct 25, 2024
1 parent 35c549e commit 75bcc6d
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 10 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ require (
github.com/replicatedhq/embedded-cluster/kinds v1.15.0
github.com/replicatedhq/kotskinds v0.0.0-20240718194123-1018dd404e95
github.com/replicatedhq/kurlkinds v1.5.0
github.com/replicatedhq/troubleshoot v0.107.1
github.com/replicatedhq/troubleshoot v0.107.2
github.com/replicatedhq/yaml/v3 v3.0.0-beta5-replicatedhq
github.com/robfig/cron v1.2.0
github.com/robfig/cron/v3 v3.0.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1350,8 +1350,8 @@ github.com/replicatedhq/kurlkinds v1.5.0 h1:zZ0PKNeh4kXvSzVGkn62DKTo314GxhXg1TSB
github.com/replicatedhq/kurlkinds v1.5.0/go.mod h1:rUpBMdC81IhmJNCWMU/uRsMETv9P0xFoMvdSP/TAr5A=
github.com/replicatedhq/termui/v3 v3.1.1-0.20200811145416-f40076d26851 h1:eRlNDHxGfVkPCRXbA4BfQJvt5DHjFiTtWy3R/t4djyY=
github.com/replicatedhq/termui/v3 v3.1.1-0.20200811145416-f40076d26851/go.mod h1:JDxG6+uubnk9/BZ2yUsyAJJwlptjrnmB2MPF5d2Xe/8=
github.com/replicatedhq/troubleshoot v0.107.1 h1:Hx9VbVv1r3M5fiH2fPTeoZ8LNIxh5R/e6vpe2jBgPfc=
github.com/replicatedhq/troubleshoot v0.107.1/go.mod h1:6mZzcO/EWVBNXVnFdSHfPaoTnjcQdV3sq61NkBF60YE=
github.com/replicatedhq/troubleshoot v0.107.2 h1:KPMQR+inoNACvZE5AV/6teK4jcM+lFlH0vGZANmIU4g=
github.com/replicatedhq/troubleshoot v0.107.2/go.mod h1:yzIVQsTu6bK+aw34SdWWUfh1UBhVwkDm6q1pVyoN6do=
github.com/replicatedhq/yaml/v3 v3.0.0-beta5-replicatedhq h1:PwPggruelq2336c1Ayg5STFqgbn/QB1tWLQwrVlU7ZQ=
github.com/replicatedhq/yaml/v3 v3.0.0-beta5-replicatedhq/go.mod h1:Txa7LopbYCU8aRgmNe0n+y/EPMz50NbCPcVVJBquwag=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
Expand Down
2 changes: 2 additions & 0 deletions pkg/supportbundle/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,11 @@ func executeSupportBundleCollectRoutine(bundle *types.SupportBundle, progressCha
Namespace: "",
ProgressChan: progressChan,
Redact: true,
RunHostCollectorsInPod: true, // always run host collectors in pod from KOTS regardless of the spec value
}

logger.Infof("Executing Collection go routine for support bundle ID: %s", bundle.ID)
logger.Infof("Always run host collectors in pod: %t", opts.RunHostCollectorsInPod)

go func() {
defer close(progressChan)
Expand Down
43 changes: 37 additions & 6 deletions pkg/supportbundle/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,15 @@ func mergeSupportBundleSpecs(builtBundles map[string]*troubleshootv1beta2.Suppor
mergedBundle.Spec.Collectors = append(mergedBundle.Spec.Collectors, builtBundle.Spec.Collectors...)
mergedBundle.Spec.Analyzers = append(mergedBundle.Spec.Analyzers, builtBundle.Spec.Analyzers...)
mergedBundle.Spec.AfterCollection = append(mergedBundle.Spec.AfterCollection, builtBundle.Spec.AfterCollection...)
mergedBundle.Spec.HostCollectors = append(mergedBundle.Spec.HostCollectors, builtBundle.Spec.HostCollectors...)
mergedBundle.Spec.HostAnalyzers = append(mergedBundle.Spec.HostAnalyzers, builtBundle.Spec.HostAnalyzers...)
}

mergedBundle = deduplicatedCollectors(mergedBundle)
mergedBundle = deduplicatedAnalyzers(mergedBundle)
mergedBundle = deduplicatedAfterCollection(mergedBundle)
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 @@ -465,11 +469,15 @@ func addDiscoveredSpecs(

supportBundle.Spec.Collectors = append(supportBundle.Spec.Collectors, sbObject.Spec.Collectors...)
supportBundle.Spec.Analyzers = append(supportBundle.Spec.Analyzers, sbObject.Spec.Analyzers...)
supportBundle.Spec.HostCollectors = append(supportBundle.Spec.HostCollectors, sbObject.Spec.HostCollectors...)
supportBundle.Spec.HostAnalyzers = append(supportBundle.Spec.HostAnalyzers, sbObject.Spec.HostAnalyzers...)
}

// remove duplicated collectors and analyzers if there are multiple support bundle upstream spec
supportBundle = deduplicatedCollectors(supportBundle)
supportBundle = deduplicatedAnalyzers(supportBundle)
// remove duplicated specs if there are multiple support bundle upstream spec
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 @@ -1249,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
}
77 changes: 77 additions & 0 deletions pkg/supportbundle/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,3 +769,80 @@ func createNamespaces(t *testing.T, clientset kubernetes.Interface, namespaces .
require.NoError(t, err)
}
}

func Test_mergeSupportBundleSpecs(t *testing.T) {
testBundle := &troubleshootv1beta2.SupportBundle{
Spec: troubleshootv1beta2.SupportBundleSpec{
Collectors: []*troubleshootv1beta2.Collect{
{
ClusterResources: &troubleshootv1beta2.ClusterResources{
CollectorMeta: troubleshootv1beta2.CollectorMeta{CollectorName: "first"},
},
},
{
ClusterResources: &troubleshootv1beta2.ClusterResources{
CollectorMeta: troubleshootv1beta2.CollectorMeta{CollectorName: "first"},
},
},
{
ClusterResources: &troubleshootv1beta2.ClusterResources{},
},
},
Analyzers: []*troubleshootv1beta2.Analyze{
{
ClusterVersion: &troubleshootv1beta2.ClusterVersion{
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{CheckName: "first"},
},
},
{
ClusterVersion: &troubleshootv1beta2.ClusterVersion{
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{CheckName: "first"},
},
},
},
AfterCollection: []*troubleshootv1beta2.AfterCollection{},
HostCollectors: []*troubleshootv1beta2.HostCollect{
{
CPU: &troubleshootv1beta2.CPU{},
Memory: &troubleshootv1beta2.Memory{
HostCollectorMeta: troubleshootv1beta2.HostCollectorMeta{CollectorName: "first"},
},
},
{
CPU: &troubleshootv1beta2.CPU{},
Memory: &troubleshootv1beta2.Memory{
HostCollectorMeta: troubleshootv1beta2.HostCollectorMeta{CollectorName: "first"},
},
},
{
CPU: &troubleshootv1beta2.CPU{},
Memory: &troubleshootv1beta2.Memory{
HostCollectorMeta: troubleshootv1beta2.HostCollectorMeta{CollectorName: "second"},
},
},
},
HostAnalyzers: []*troubleshootv1beta2.HostAnalyze{
{
CPU: &troubleshootv1beta2.CPUAnalyze{
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{CheckName: "first"},
},
},
{
CPU: &troubleshootv1beta2.CPUAnalyze{
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{CheckName: "first"},
},
},
},
},
}

builtBundles := map[string]*troubleshootv1beta2.SupportBundle{
"first": testBundle,
}
merged := mergeSupportBundleSpecs(builtBundles)

assert.Equal(t, 2, len(merged.Spec.Collectors))
assert.Equal(t, 1, len(merged.Spec.Analyzers))
assert.Equal(t, 2, len(merged.Spec.HostCollectors))
assert.Equal(t, 1, len(merged.Spec.HostAnalyzers))
}
2 changes: 1 addition & 1 deletion pkg/supportbundle/supportbundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func CreateSupportBundleDependencies(app *apptypes.App, sequence int64, opts typ
URI: GetSpecURI(app.GetSlug()),
RedactURIs: redactURIs,
Progress: types.SupportBundleProgress{
CollectorCount: len(supportBundle.Spec.Collectors),
CollectorCount: len(supportBundle.Spec.Collectors) + len(supportBundle.Spec.HostCollectors),
},
}

Expand Down

0 comments on commit 75bcc6d

Please sign in to comment.