Cleanup counts

This commit is contained in:
Darren Shepherd 2019-08-16 11:40:42 -07:00
parent b059dcca00
commit 5142b5c386
4 changed files with 64 additions and 5 deletions

View File

@ -133,3 +133,25 @@ func SetColumns(s *types.Schema, columns interface{}) {
func Columns(s *types.Schema) interface{} { func Columns(s *types.Schema) interface{} {
return s.Attributes["columns"] 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
}

View File

@ -6,6 +6,8 @@ import (
"sync" "sync"
"time" "time"
"github.com/rancher/naok/pkg/attributes"
"github.com/rancher/naok/pkg/accesscontrol" "github.com/rancher/naok/pkg/accesscontrol"
"github.com/rancher/norman/pkg/store/empty" "github.com/rancher/norman/pkg/store/empty"
"github.com/rancher/norman/pkg/types" "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) child := make(chan Count)
for name, countItem := range c.Counts { for name, countItem := range c.Counts {
wg.Add(1) wg.Add(1)
name := name
countItem := countItem
go func() { go func() {
s.watchItem(apiOp.WithContext(ctx), name, countItem, cancel, child) s.watchItem(apiOp.WithContext(ctx), name, countItem, cancel, child)
wg.Done() 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 { if schema == nil || schema.Store == nil || apiOp.AccessControl.CanWatch(apiOp, schema) != nil {
return return
} }
defer cancel() 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}) w, err := schema.Store.Watch(apiOp, schema, types.WatchRequest{Revision: start.Revision})
if err != nil { if err != nil {
logrus.Errorf("failed to watch %s for counts: %v", schema.ID, err) 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 continue
} }
if attributes.PreferredVersion(schema) != "" {
continue
}
if attributes.PreferredGroup(schema) != "" {
continue
}
if ignoreSlow && slow[schema.ID] { if ignoreSlow && slow[schema.ID] {
continue continue
} }

View File

@ -67,5 +67,7 @@ func forVersion(group, version, resource string, schemas map[string]*types.Schem
if schema == nil { if schema == nil {
return return
} }
if len(columns) > 0 {
attributes.SetColumns(schema, columns) attributes.SetColumns(schema, columns)
} }
}

View File

@ -12,14 +12,22 @@ import (
"k8s.io/client-go/discovery" "k8s.io/client-go/discovery"
) )
var (
preferredGroups = map[string]string{
"extensions": "apps",
}
)
func AddDiscovery(client discovery.DiscoveryInterface, schemas map[string]*types.Schema) error { func AddDiscovery(client discovery.DiscoveryInterface, schemas map[string]*types.Schema) error {
logrus.Info("Refreshing all schemas") logrus.Info("Refreshing all schemas")
_, resourceLists, err := client.ServerGroupsAndResources() groups, resourceLists, err := client.ServerGroupsAndResources()
if err != nil { if err != nil {
return err return err
} }
versions := indexVersions(groups)
var errs []error var errs []error
for _, resourceList := range resourceLists { for _, resourceList := range resourceLists {
gv, err := schema.ParseGroupVersion(resourceList.GroupVersion) gv, err := schema.ParseGroupVersion(resourceList.GroupVersion)
@ -27,7 +35,7 @@ func AddDiscovery(client discovery.DiscoveryInterface, schemas map[string]*types
errs = append(errs, err) 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) errs = append(errs, err)
} }
} }
@ -35,7 +43,15 @@ func AddDiscovery(client discovery.DiscoveryInterface, schemas map[string]*types
return merr.NewErrors(errs...) 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 { for _, resource := range resources.APIResources {
if strings.Contains(resource.Name, "/") { if strings.Contains(resource.Name, "/") {
continue continue
@ -62,6 +78,12 @@ func refresh(gv schema.GroupVersion, resources *metav1.APIResourceList, schemas
schema.PluralName = resource.Name schema.PluralName = resource.Name
attributes.SetAPIResource(schema, resource) 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 // switch ID to be GVR, not GVK
if schema.ID != "" { if schema.ID != "" {