From 5142b5c386bd9fa53222bb45f451650caa7ac49c Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Fri, 16 Aug 2019 11:40:42 -0700 Subject: [PATCH] Cleanup counts --- pkg/attributes/attributes.go | 22 +++++++++++++++ pkg/resources/counts/{types.go => counts.go} | 15 ++++++++++- pkg/resources/schema/converter/crd.go | 4 ++- pkg/resources/schema/converter/discovery.go | 28 +++++++++++++++++--- 4 files changed, 64 insertions(+), 5 deletions(-) rename pkg/resources/counts/{types.go => counts.go} (94%) diff --git a/pkg/attributes/attributes.go b/pkg/attributes/attributes.go index 428dedc..9c6a905 100644 --- a/pkg/attributes/attributes.go +++ b/pkg/attributes/attributes.go @@ -133,3 +133,25 @@ func SetColumns(s *types.Schema, columns interface{}) { func Columns(s *types.Schema) interface{} { return s.Attributes["columns"] } + +func PreferredVersion(s *types.Schema) string { + return convert.ToString(s.Attributes["preferredVersion"]) +} + +func SetPreferredVersion(s *types.Schema, ver string) { + if s.Attributes == nil { + s.Attributes = map[string]interface{}{} + } + s.Attributes["preferredVersion"] = ver +} + +func PreferredGroup(s *types.Schema) string { + return convert.ToString(s.Attributes["preferredGroup"]) +} + +func SetPreferredGroup(s *types.Schema, ver string) { + if s.Attributes == nil { + s.Attributes = map[string]interface{}{} + } + s.Attributes["preferredGroup"] = ver +} diff --git a/pkg/resources/counts/types.go b/pkg/resources/counts/counts.go similarity index 94% rename from pkg/resources/counts/types.go rename to pkg/resources/counts/counts.go index b143082..0e8a735 100644 --- a/pkg/resources/counts/types.go +++ b/pkg/resources/counts/counts.go @@ -6,6 +6,8 @@ import ( "sync" "time" + "github.com/rancher/naok/pkg/attributes" + "github.com/rancher/naok/pkg/accesscontrol" "github.com/rancher/norman/pkg/store/empty" "github.com/rancher/norman/pkg/types" @@ -79,6 +81,8 @@ func (s *Store) Watch(apiOp *types.APIRequest, schema *types.Schema, w types.Wat child := make(chan Count) for name, countItem := range c.Counts { wg.Add(1) + name := name + countItem := countItem go func() { s.watchItem(apiOp.WithContext(ctx), name, countItem, cancel, child) wg.Done() @@ -121,9 +125,10 @@ func (s *Store) watchItem(apiOp *types.APIRequest, schemaID string, start ItemCo if schema == nil || schema.Store == nil || apiOp.AccessControl.CanWatch(apiOp, schema) != nil { return } - defer cancel() + logrus.Debugf("watching %s for count", schemaID) + defer logrus.Debugf("close watching %s for count", schemaID) w, err := schema.Store.Watch(apiOp, schema, types.WatchRequest{Revision: start.Revision}) if err != nil { logrus.Errorf("failed to watch %s for counts: %v", schema.ID, err) @@ -171,6 +176,14 @@ func (s *Store) getCount(apiOp *types.APIRequest, timeout time.Duration, ignoreS continue } + if attributes.PreferredVersion(schema) != "" { + continue + } + + if attributes.PreferredGroup(schema) != "" { + continue + } + if ignoreSlow && slow[schema.ID] { continue } diff --git a/pkg/resources/schema/converter/crd.go b/pkg/resources/schema/converter/crd.go index bbec861..f095f97 100644 --- a/pkg/resources/schema/converter/crd.go +++ b/pkg/resources/schema/converter/crd.go @@ -67,5 +67,7 @@ func forVersion(group, version, resource string, schemas map[string]*types.Schem if schema == nil { return } - attributes.SetColumns(schema, columns) + if len(columns) > 0 { + attributes.SetColumns(schema, columns) + } } diff --git a/pkg/resources/schema/converter/discovery.go b/pkg/resources/schema/converter/discovery.go index 71d6faa..3068b30 100644 --- a/pkg/resources/schema/converter/discovery.go +++ b/pkg/resources/schema/converter/discovery.go @@ -12,14 +12,22 @@ import ( "k8s.io/client-go/discovery" ) +var ( + preferredGroups = map[string]string{ + "extensions": "apps", + } +) + func AddDiscovery(client discovery.DiscoveryInterface, schemas map[string]*types.Schema) error { logrus.Info("Refreshing all schemas") - _, resourceLists, err := client.ServerGroupsAndResources() + groups, resourceLists, err := client.ServerGroupsAndResources() if err != nil { return err } + versions := indexVersions(groups) + var errs []error for _, resourceList := range resourceLists { gv, err := schema.ParseGroupVersion(resourceList.GroupVersion) @@ -27,7 +35,7 @@ func AddDiscovery(client discovery.DiscoveryInterface, schemas map[string]*types errs = append(errs, err) } - if err := refresh(gv, resourceList, schemas); err != nil { + if err := refresh(gv, versions, resourceList, schemas); err != nil { errs = append(errs, err) } } @@ -35,7 +43,15 @@ func AddDiscovery(client discovery.DiscoveryInterface, schemas map[string]*types return merr.NewErrors(errs...) } -func refresh(gv schema.GroupVersion, resources *metav1.APIResourceList, schemas map[string]*types.Schema) error { +func indexVersions(groups []*metav1.APIGroup) map[string]string { + result := map[string]string{} + for _, group := range groups { + result[group.Name] = group.PreferredVersion.Version + } + return result +} + +func refresh(gv schema.GroupVersion, groupToPreferredVersion map[string]string, resources *metav1.APIResourceList, schemas map[string]*types.Schema) error { for _, resource := range resources.APIResources { if strings.Contains(resource.Name, "/") { continue @@ -62,6 +78,12 @@ func refresh(gv schema.GroupVersion, resources *metav1.APIResourceList, schemas schema.PluralName = resource.Name attributes.SetAPIResource(schema, resource) + if preferredVersion := groupToPreferredVersion[gv.Group]; preferredVersion != "" && preferredVersion != gv.Version { + attributes.SetPreferredVersion(schema, preferredVersion) + } + if group := preferredGroups[gv.Group]; group != "" { + attributes.SetPreferredGroup(schema, group) + } // switch ID to be GVR, not GVK if schema.ID != "" {