From c55b1eed181c98c6f3985d306608ffcbbbc5898b Mon Sep 17 00:00:00 2001 From: rmweir Date: Thu, 28 Mar 2019 11:02:24 -0700 Subject: [PATCH] Track namespaces in options property Now, identifies and assigns namespaces. Prior, the backend listed items belongng to all namespaces in all projects no matter what because they were not being tracked. This led to longer request times for projects that contained a fraction of total resources. Now, rancher can filter project resource requests; times are improved and largely dependent on resource amount for the given project. --- store/proxy/proxy_store.go | 39 ++++++++++++++++++++++++++++++++++---- types/server_types.go | 2 ++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/store/proxy/proxy_store.go b/store/proxy/proxy_store.go index 3fb722ec..13927dca 100644 --- a/store/proxy/proxy_store.go +++ b/store/proxy/proxy_store.go @@ -17,6 +17,7 @@ import ( "github.com/rancher/norman/types/convert/merge" "github.com/rancher/norman/types/values" "github.com/sirupsen/logrus" + "golang.org/x/sync/errgroup" "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -192,11 +193,41 @@ func (s *Store) Context() types.StorageContext { } func (s *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) { - namespace := getNamespace(apiContext, opt) + var resultList unstructured.UnstructuredList - resultList, err := s.retryList(namespace, apiContext) - if err != nil { - return nil, err + // if there are no namespaces field in options, a single request is made + if opt == nil || opt.Namespaces == nil { + ns := getNamespace(apiContext, opt) + list, err := s.retryList(ns, apiContext) + if err != nil { + return nil, err + } + resultList = *list + } else { + var ( + errGroup errgroup.Group + mux sync.Mutex + ) + + allNS := opt.Namespaces + for _, ns := range allNS { + nsCopy := ns + errGroup.Go(func() error { + list, err := s.retryList(nsCopy, apiContext) + if err != nil { + return err + } + + mux.Lock() + resultList.Items = append(resultList.Items, list.Items...) + mux.Unlock() + + return nil + }) + } + if err := errGroup.Wait(); err != nil { + return nil, err + } } var result []map[string]interface{} diff --git a/types/server_types.go b/types/server_types.go index 7fede86f..7e36aa22 100644 --- a/types/server_types.go +++ b/types/server_types.go @@ -184,6 +184,8 @@ type QueryOptions struct { Pagination *Pagination Conditions []*QueryCondition Options map[string]string + // Set namespaces to an empty array will result in an empty response + Namespaces []string } type ReferenceValidator interface {