Skip to content

Commit 75bcc6d

Browse files
authored
feat: enable remote host collector in KOTS (#4958)
1 parent 35c549e commit 75bcc6d

File tree

6 files changed

+120
-10
lines changed

6 files changed

+120
-10
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ require (
5252
github.com/replicatedhq/embedded-cluster/kinds v1.15.0
5353
github.com/replicatedhq/kotskinds v0.0.0-20240718194123-1018dd404e95
5454
github.com/replicatedhq/kurlkinds v1.5.0
55-
github.com/replicatedhq/troubleshoot v0.107.1
55+
github.com/replicatedhq/troubleshoot v0.107.2
5656
github.com/replicatedhq/yaml/v3 v3.0.0-beta5-replicatedhq
5757
github.com/robfig/cron v1.2.0
5858
github.com/robfig/cron/v3 v3.0.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,8 +1350,8 @@ github.com/replicatedhq/kurlkinds v1.5.0 h1:zZ0PKNeh4kXvSzVGkn62DKTo314GxhXg1TSB
13501350
github.com/replicatedhq/kurlkinds v1.5.0/go.mod h1:rUpBMdC81IhmJNCWMU/uRsMETv9P0xFoMvdSP/TAr5A=
13511351
github.com/replicatedhq/termui/v3 v3.1.1-0.20200811145416-f40076d26851 h1:eRlNDHxGfVkPCRXbA4BfQJvt5DHjFiTtWy3R/t4djyY=
13521352
github.com/replicatedhq/termui/v3 v3.1.1-0.20200811145416-f40076d26851/go.mod h1:JDxG6+uubnk9/BZ2yUsyAJJwlptjrnmB2MPF5d2Xe/8=
1353-
github.com/replicatedhq/troubleshoot v0.107.1 h1:Hx9VbVv1r3M5fiH2fPTeoZ8LNIxh5R/e6vpe2jBgPfc=
1354-
github.com/replicatedhq/troubleshoot v0.107.1/go.mod h1:6mZzcO/EWVBNXVnFdSHfPaoTnjcQdV3sq61NkBF60YE=
1353+
github.com/replicatedhq/troubleshoot v0.107.2 h1:KPMQR+inoNACvZE5AV/6teK4jcM+lFlH0vGZANmIU4g=
1354+
github.com/replicatedhq/troubleshoot v0.107.2/go.mod h1:yzIVQsTu6bK+aw34SdWWUfh1UBhVwkDm6q1pVyoN6do=
13551355
github.com/replicatedhq/yaml/v3 v3.0.0-beta5-replicatedhq h1:PwPggruelq2336c1Ayg5STFqgbn/QB1tWLQwrVlU7ZQ=
13561356
github.com/replicatedhq/yaml/v3 v3.0.0-beta5-replicatedhq/go.mod h1:Txa7LopbYCU8aRgmNe0n+y/EPMz50NbCPcVVJBquwag=
13571357
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=

pkg/supportbundle/execute.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,11 @@ func executeSupportBundleCollectRoutine(bundle *types.SupportBundle, progressCha
171171
Namespace: "",
172172
ProgressChan: progressChan,
173173
Redact: true,
174+
RunHostCollectorsInPod: true, // always run host collectors in pod from KOTS regardless of the spec value
174175
}
175176

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

178180
go func() {
179181
defer close(progressChan)

pkg/supportbundle/spec.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,15 @@ func mergeSupportBundleSpecs(builtBundles map[string]*troubleshootv1beta2.Suppor
163163
mergedBundle.Spec.Collectors = append(mergedBundle.Spec.Collectors, builtBundle.Spec.Collectors...)
164164
mergedBundle.Spec.Analyzers = append(mergedBundle.Spec.Analyzers, builtBundle.Spec.Analyzers...)
165165
mergedBundle.Spec.AfterCollection = append(mergedBundle.Spec.AfterCollection, builtBundle.Spec.AfterCollection...)
166+
mergedBundle.Spec.HostCollectors = append(mergedBundle.Spec.HostCollectors, builtBundle.Spec.HostCollectors...)
167+
mergedBundle.Spec.HostAnalyzers = append(mergedBundle.Spec.HostAnalyzers, builtBundle.Spec.HostAnalyzers...)
166168
}
167169

168-
mergedBundle = deduplicatedCollectors(mergedBundle)
169-
mergedBundle = deduplicatedAnalyzers(mergedBundle)
170-
mergedBundle = deduplicatedAfterCollection(mergedBundle)
170+
mergedBundle.Spec.Collectors = Dedup(mergedBundle.Spec.Collectors)
171+
mergedBundle.Spec.Analyzers = Dedup(mergedBundle.Spec.Analyzers)
172+
mergedBundle.Spec.AfterCollection = Dedup(mergedBundle.Spec.AfterCollection)
173+
mergedBundle.Spec.HostCollectors = Dedup(mergedBundle.Spec.HostCollectors)
174+
mergedBundle.Spec.HostAnalyzers = Dedup(mergedBundle.Spec.HostAnalyzers)
171175

172176
return mergedBundle
173177
}
@@ -465,11 +469,15 @@ func addDiscoveredSpecs(
465469

466470
supportBundle.Spec.Collectors = append(supportBundle.Spec.Collectors, sbObject.Spec.Collectors...)
467471
supportBundle.Spec.Analyzers = append(supportBundle.Spec.Analyzers, sbObject.Spec.Analyzers...)
472+
supportBundle.Spec.HostCollectors = append(supportBundle.Spec.HostCollectors, sbObject.Spec.HostCollectors...)
473+
supportBundle.Spec.HostAnalyzers = append(supportBundle.Spec.HostAnalyzers, sbObject.Spec.HostAnalyzers...)
468474
}
469475

470-
// remove duplicated collectors and analyzers if there are multiple support bundle upstream spec
471-
supportBundle = deduplicatedCollectors(supportBundle)
472-
supportBundle = deduplicatedAnalyzers(supportBundle)
476+
// remove duplicated specs if there are multiple support bundle upstream spec
477+
supportBundle.Spec.Collectors = Dedup(supportBundle.Spec.Collectors)
478+
supportBundle.Spec.Analyzers = Dedup(supportBundle.Spec.Analyzers)
479+
supportBundle.Spec.HostCollectors = Dedup(supportBundle.Spec.HostCollectors)
480+
supportBundle.Spec.HostAnalyzers = Dedup(supportBundle.Spec.HostAnalyzers)
473481

474482
return supportBundle
475483
}
@@ -1249,3 +1257,26 @@ func removeKurlAnalyzers(analyzers []*troubleshootv1beta2.Analyze) []*troublesho
12491257

12501258
return analyze
12511259
}
1260+
1261+
func Dedup[T any](objs []T) []T {
1262+
seen := make(map[string]bool)
1263+
out := []T{}
1264+
1265+
if len(objs) == 0 {
1266+
return objs
1267+
}
1268+
1269+
for _, o := range objs {
1270+
data, err := json.Marshal(o)
1271+
if err != nil {
1272+
out = append(out, o)
1273+
continue
1274+
}
1275+
key := string(data)
1276+
if _, ok := seen[key]; !ok {
1277+
out = append(out, o)
1278+
seen[key] = true
1279+
}
1280+
}
1281+
return out
1282+
}

pkg/supportbundle/spec_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,3 +769,80 @@ func createNamespaces(t *testing.T, clientset kubernetes.Interface, namespaces .
769769
require.NoError(t, err)
770770
}
771771
}
772+
773+
func Test_mergeSupportBundleSpecs(t *testing.T) {
774+
testBundle := &troubleshootv1beta2.SupportBundle{
775+
Spec: troubleshootv1beta2.SupportBundleSpec{
776+
Collectors: []*troubleshootv1beta2.Collect{
777+
{
778+
ClusterResources: &troubleshootv1beta2.ClusterResources{
779+
CollectorMeta: troubleshootv1beta2.CollectorMeta{CollectorName: "first"},
780+
},
781+
},
782+
{
783+
ClusterResources: &troubleshootv1beta2.ClusterResources{
784+
CollectorMeta: troubleshootv1beta2.CollectorMeta{CollectorName: "first"},
785+
},
786+
},
787+
{
788+
ClusterResources: &troubleshootv1beta2.ClusterResources{},
789+
},
790+
},
791+
Analyzers: []*troubleshootv1beta2.Analyze{
792+
{
793+
ClusterVersion: &troubleshootv1beta2.ClusterVersion{
794+
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{CheckName: "first"},
795+
},
796+
},
797+
{
798+
ClusterVersion: &troubleshootv1beta2.ClusterVersion{
799+
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{CheckName: "first"},
800+
},
801+
},
802+
},
803+
AfterCollection: []*troubleshootv1beta2.AfterCollection{},
804+
HostCollectors: []*troubleshootv1beta2.HostCollect{
805+
{
806+
CPU: &troubleshootv1beta2.CPU{},
807+
Memory: &troubleshootv1beta2.Memory{
808+
HostCollectorMeta: troubleshootv1beta2.HostCollectorMeta{CollectorName: "first"},
809+
},
810+
},
811+
{
812+
CPU: &troubleshootv1beta2.CPU{},
813+
Memory: &troubleshootv1beta2.Memory{
814+
HostCollectorMeta: troubleshootv1beta2.HostCollectorMeta{CollectorName: "first"},
815+
},
816+
},
817+
{
818+
CPU: &troubleshootv1beta2.CPU{},
819+
Memory: &troubleshootv1beta2.Memory{
820+
HostCollectorMeta: troubleshootv1beta2.HostCollectorMeta{CollectorName: "second"},
821+
},
822+
},
823+
},
824+
HostAnalyzers: []*troubleshootv1beta2.HostAnalyze{
825+
{
826+
CPU: &troubleshootv1beta2.CPUAnalyze{
827+
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{CheckName: "first"},
828+
},
829+
},
830+
{
831+
CPU: &troubleshootv1beta2.CPUAnalyze{
832+
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{CheckName: "first"},
833+
},
834+
},
835+
},
836+
},
837+
}
838+
839+
builtBundles := map[string]*troubleshootv1beta2.SupportBundle{
840+
"first": testBundle,
841+
}
842+
merged := mergeSupportBundleSpecs(builtBundles)
843+
844+
assert.Equal(t, 2, len(merged.Spec.Collectors))
845+
assert.Equal(t, 1, len(merged.Spec.Analyzers))
846+
assert.Equal(t, 2, len(merged.Spec.HostCollectors))
847+
assert.Equal(t, 1, len(merged.Spec.HostAnalyzers))
848+
}

pkg/supportbundle/supportbundle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func CreateSupportBundleDependencies(app *apptypes.App, sequence int64, opts typ
155155
URI: GetSpecURI(app.GetSlug()),
156156
RedactURIs: redactURIs,
157157
Progress: types.SupportBundleProgress{
158-
CollectorCount: len(supportBundle.Spec.Collectors),
158+
CollectorCount: len(supportBundle.Spec.Collectors) + len(supportBundle.Spec.HostCollectors),
159159
},
160160
}
161161

0 commit comments

Comments
 (0)