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

WIP: Try to parallelize fetching all aps #211

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
46 changes: 35 additions & 11 deletions api/list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package api

import (
"time"
"sync"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -67,10 +69,12 @@ func (opts *ListOpts) namedResourceNotFound(project string, foundInProjects ...s
return errors.New(errorMessage)
}


// ListObjects lists objects in the current client project with some
// ux-improvements like hinting when a resource has been found in a different
// project of the same organization.
func (c *Client) ListObjects(ctx context.Context, list runtimeclient.ObjectList, options ...ListOpt) error {
fmt.Println("api.list.ListObjects")
opts := &ListOpts{}
for _, opt := range options {
opt(opts)
Expand Down Expand Up @@ -125,19 +129,39 @@ func (c *Client) ListObjects(ctx context.Context, list runtimeclient.ObjectList,
return fmt.Errorf("error when searching for projects: %w", err)
}

projectsSize := len(projects)
var wg sync.WaitGroup
wg.Add(projectsSize)
ch := make(chan reflect.Value, projectsSize)


fmt.Printf("%T\n", projects)
start := time.Now()
for _, proj := range projects {
tempOpts := slices.Clone(opts.clientListOptions)
// we ensured the list is a pointer type and that is has an
// 'Items' field which is a slice above, so we don't need to do
// this again here and instead use the reflect functions directly.
tempList := reflect.New(reflect.TypeOf(list).Elem()).Interface().(runtimeclient.ObjectList)
tempList.GetObjectKind().SetGroupVersionKind(list.GetObjectKind().GroupVersionKind())
if err := c.List(ctx, tempList, append(tempOpts, runtimeclient.InNamespace(proj.Name))...); err != nil {
return fmt.Errorf("error when searching in project %s: %w", proj.Name, err)
}
tempListItems := reflect.ValueOf(tempList).Elem().FieldByName("Items")
for i := 0; i < tempListItems.Len(); i++ {
items.Set(reflect.Append(items, tempListItems.Index(i)))
go func() {
fmt.Printf(" %s Start (%s)\n", proj.Name, time.Now().Sub(start))
defer wg.Done()
// we ensured the list is a pointer type and that is has an
// 'Items' field which is a slice above, so we don't need to do
// this again here and instead use the reflect functions directly.
tempList := reflect.New(reflect.TypeOf(list).Elem()).Interface().(runtimeclient.ObjectList)
tempList.GetObjectKind().SetGroupVersionKind(list.GetObjectKind().GroupVersionKind())
if err := c.List(ctx, tempList, append(tempOpts, runtimeclient.InNamespace(proj.Name))...); err != nil {
return //fmt.Errorf("error when searching in project %s: %w", proj.Name, err)
}
tempListItems := reflect.ValueOf(tempList).Elem().FieldByName("Items")
ch <- tempListItems
fmt.Printf(" %s Done (%s)\n", proj.Name, time.Now().Sub(start))
}()
}

wg.Wait()
close(ch)

for listItems := range(ch) {
for i := 0; i < listItems.Len(); i++ {
items.Set(reflect.Append(items, listItems.Index(i)))
}
}

Expand Down