Skip to content

Commit

Permalink
feat: ensure Copy collector run last (#1688)
Browse files Browse the repository at this point in the history
* ensure Copy collector run last

* * add unit test
* reorder in Preflight as well
  • Loading branch information
nvanthao authored Nov 14, 2024
1 parent be2c212 commit 7bb88e6
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
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
}

// 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

0 comments on commit 7bb88e6

Please sign in to comment.