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{} {
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"
"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
}

View File

@ -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)
}
}

View File

@ -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 != "" {