Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ensure Copy collector run last #1688

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pkg/collect/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,20 @@ func DedupCollectors(allCollectors []*troubleshootv1beta2.Collect) []*troublesho
}
return finalCollectors
}

nvanthao marked this conversation as resolved.
Show resolved Hide resolved
// Ensure Copy collectors are last in the list
// This is because copy collectors are expected to copy files from other collectors such as Exec, RunPod, RunDaemonSet
func EnsureCopyLast(allCollectors []Collector) []Collector {
otherCollectors := make([]Collector, 0)
copyCollectors := make([]Collector, 0)

for _, collector := range allCollectors {
if _, ok := collector.(*CollectCopy); ok {
copyCollectors = append(copyCollectors, collector)
} else {
otherCollectors = append(otherCollectors, collector)
}
}

return append(otherCollectors, copyCollectors...)
}
81 changes: 81 additions & 0 deletions pkg/collect/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,84 @@ func TestCollector_DedupCollectors(t *testing.T) {
})
}
}
func TestEnsureCopyLast(t *testing.T) {
tests := []struct {
name string
allCollectors []Collector
want []Collector
}{
{
name: "no copy collectors",
allCollectors: []Collector{
&CollectClusterInfo{},
&CollectClusterResources{},
},
want: []Collector{
&CollectClusterInfo{},
&CollectClusterResources{},
},
},
{
name: "only copy collectors",
allCollectors: []Collector{
&CollectCopy{},
&CollectCopy{},
},
want: []Collector{
&CollectCopy{},
&CollectCopy{},
},
},
{
name: "mixed collectors",
allCollectors: []Collector{
&CollectClusterInfo{},
&CollectCopy{},
&CollectClusterResources{},
&CollectCopy{},
},
want: []Collector{
&CollectClusterInfo{},
&CollectClusterResources{},
&CollectCopy{},
&CollectCopy{},
},
},
{
name: "copy collectors at the beginning",
allCollectors: []Collector{
&CollectCopy{},
&CollectCopy{},
&CollectClusterInfo{},
&CollectClusterResources{},
},
want: []Collector{
&CollectClusterInfo{},
&CollectClusterResources{},
&CollectCopy{},
&CollectCopy{},
},
},
{
name: "copy collectors at the end",
allCollectors: []Collector{
&CollectClusterInfo{},
&CollectClusterResources{},
&CollectCopy{},
&CollectCopy{},
},
want: []Collector{
&CollectClusterInfo{},
&CollectClusterResources{},
&CollectCopy{},
&CollectCopy{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := EnsureCopyLast(tt.allCollectors)
assert.Equal(t, tt.want, got)
})
}
}
3 changes: 3 additions & 0 deletions pkg/preflight/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ func CollectWithContext(ctx context.Context, opts CollectOpts, p *troubleshootv1
return collectResult, collect.ErrInsufficientPermissionsToRun
}

// move Copy Collectors if any to the end of the execution list
allCollectors = collect.EnsureCopyLast(allCollectors)

for i, collector := range allCollectors {
_, span := otel.Tracer(constants.LIB_TRACER_NAME).Start(ctx, collector.Title())
span.SetAttributes(attribute.String("type", reflect.TypeOf(collector).String()))
Expand Down
3 changes: 3 additions & 0 deletions pkg/supportbundle/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ func runCollectors(ctx context.Context, collectors []*troubleshootv1beta2.Collec
return nil, collect.ErrInsufficientPermissionsToRun
}

// move Copy Collectors if any to the end of the execution list
allCollectors = collect.EnsureCopyLast(allCollectors)

for _, collector := range allCollectors {
_, span := otel.Tracer(constants.LIB_TRACER_NAME).Start(ctx, collector.Title())
span.SetAttributes(attribute.String("type", reflect.TypeOf(collector).String()))
Expand Down
Loading