From 9f5d8027080fb6ce78a0da58841ec4cfd6212be7 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Mon, 9 Aug 2021 16:47:09 -0700 Subject: [PATCH] Cleanup schema change reporting --- pkg/accesscontrol/access_store.go | 8 +++++++- pkg/resources/schemas/template.go | 17 +++++++++++++++-- pkg/stores/proxy/watch_refresh.go | 2 +- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/pkg/accesscontrol/access_store.go b/pkg/accesscontrol/access_store.go index c14ee6e..9c9968b 100644 --- a/pkg/accesscontrol/access_store.go +++ b/pkg/accesscontrol/access_store.go @@ -64,6 +64,10 @@ func (l *AccessStore) AccessFor(user user.Info) *AccessSet { } func (l *AccessStore) CacheKey(user user.Info) string { + cacheKey, ok := l.cache.Get(user.GetName()) + if ok { + return cacheKey.(string) + } d := sha256.New() l.users.addRolesToHash(d, user.GetName()) @@ -77,5 +81,7 @@ func (l *AccessStore) CacheKey(user user.Info) string { l.groups.addRolesToHash(d, group) } - return hex.EncodeToString(d.Sum(nil)) + cacheKey = hex.EncodeToString(d.Sum(nil)) + l.cache.Add(user.GetName(), cacheKey, 2*time.Second) + return cacheKey.(string) } diff --git a/pkg/resources/schemas/template.go b/pkg/resources/schemas/template.go index b683aad..50b7bb8 100644 --- a/pkg/resources/schemas/template.go +++ b/pkg/resources/schemas/template.go @@ -6,6 +6,7 @@ import ( "time" "github.com/rancher/apiserver/pkg/builtin" + "k8s.io/apimachinery/pkg/api/equality" schemastore "github.com/rancher/apiserver/pkg/store/schema" "github.com/rancher/apiserver/pkg/types" @@ -96,12 +97,24 @@ func (s *Store) sendSchemas(result chan types.APIEvent, apiOp *types.APIRequest, inNewSchemas := map[string]bool{} for _, apiObject := range schemastore.FilterSchemas(apiOp, schemas.Schemas).Objects { + inNewSchemas[apiObject.ID] = true + eventName := types.ChangeAPIEvent + if oldSchema := oldSchemas.LookupSchema(apiObject.ID); oldSchema == nil { + eventName = types.CreateAPIEvent + } else { + newSchemaCopy := apiObject.Object.(*types.APISchema).Schema.DeepCopy() + oldSchemaCopy := oldSchema.Schema.DeepCopy() + newSchemaCopy.Mapper = nil + oldSchemaCopy.Mapper = nil + if equality.Semantic.DeepEqual(newSchemaCopy, oldSchemaCopy) { + continue + } + } result <- types.APIEvent{ - Name: types.ChangeAPIEvent, + Name: eventName, ResourceType: "schema", Object: apiObject, } - inNewSchemas[apiObject.ID] = true } for _, oldSchema := range schemastore.FilterSchemas(apiOp, oldSchemas.Schemas).Objects { diff --git a/pkg/stores/proxy/watch_refresh.go b/pkg/stores/proxy/watch_refresh.go index 87ecda0..7674a16 100644 --- a/pkg/stores/proxy/watch_refresh.go +++ b/pkg/stores/proxy/watch_refresh.go @@ -29,7 +29,7 @@ func (w *WatchRefresh) Watch(apiOp *types.APIRequest, schema *types.APISchema, w select { case <-ctx.Done(): return - case <-time.After(30 * time.Second): + case <-time.After(2 * time.Second): } newAs := w.asl.AccessFor(user)