From f6c6ca839c22ea0f3717c1c93826e478fbb2d2f9 Mon Sep 17 00:00:00 2001 From: Michael Bolot Date: Fri, 18 Oct 2024 12:15:42 -0500 Subject: [PATCH] Re-adding formatter when SQL cache is enabled (#300) Previously, the formatter for state/relationships was disabled when the sql cache was enabled, since a transform function was adding those values before they were added to the cache. However, the get/watch calls currently don't use the cache, causing the state/relationships to be missing. --- pkg/resources/common/formatter.go | 36 +++++++++++++++---------------- pkg/resources/schema.go | 4 ++-- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/pkg/resources/common/formatter.go b/pkg/resources/common/formatter.go index a876f89c..b28eb05f 100644 --- a/pkg/resources/common/formatter.go +++ b/pkg/resources/common/formatter.go @@ -23,19 +23,18 @@ import ( func DefaultTemplate(clientGetter proxy.ClientGetter, summaryCache *summarycache.SummaryCache, asl accesscontrol.AccessSetLookup, - namespaceCache corecontrollers.NamespaceCache, - sqlCache bool) schema.Template { + namespaceCache corecontrollers.NamespaceCache) schema.Template { return schema.Template{ Store: metricsStore.NewMetricsStore(proxy.NewProxyStore(clientGetter, summaryCache, asl, namespaceCache)), - Formatter: formatter(summaryCache, sqlCache), + Formatter: formatter(summaryCache), } } // DefaultTemplateForStore provides a default schema template which uses a provided, pre-initialized store. Primarily used when creating a Template that uses a Lasso SQL store internally. -func DefaultTemplateForStore(store types.Store, summaryCache *summarycache.SummaryCache, sqlCache bool) schema.Template { +func DefaultTemplateForStore(store types.Store, summaryCache *summarycache.SummaryCache) schema.Template { return schema.Template{ Store: store, - Formatter: formatter(summaryCache, sqlCache), + Formatter: formatter(summaryCache), } } @@ -72,7 +71,7 @@ func selfLink(gvr schema2.GroupVersionResource, meta metav1.Object) (prefix stri return buf.String() } -func formatter(summarycache *summarycache.SummaryCache, sqlCache bool) types.Formatter { +func formatter(summarycache *summarycache.SummaryCache) types.Formatter { return func(request *types.APIRequest, resource *types.RawResource) { if resource.Schema == nil { return @@ -105,20 +104,19 @@ func formatter(summarycache *summarycache.SummaryCache, sqlCache bool) types.For } if unstr, ok := resource.APIObject.Object.(*unstructured.Unstructured); ok { - if !sqlCache { - // with the sql cache, these were already added by the indexer - s, rel := summarycache.SummaryAndRelationship(unstr) - data.PutValue(unstr.Object, map[string]interface{}{ - "name": s.State, - "error": s.Error, - "transitioning": s.Transitioning, - "message": strings.Join(s.Message, ":"), - }, "metadata", "state") - data.PutValue(unstr.Object, rel, "metadata", "relationships") - - summary.NormalizeConditions(unstr) + // with the sql cache, these were already added by the indexer. However, the sql cache + // is only used for lists, so we need to re-add here for get/watch + s, rel := summarycache.SummaryAndRelationship(unstr) + data.PutValue(unstr.Object, map[string]interface{}{ + "name": s.State, + "error": s.Error, + "transitioning": s.Transitioning, + "message": strings.Join(s.Message, ":"), + }, "metadata", "state") + data.PutValue(unstr.Object, rel, "metadata", "relationships") + + summary.NormalizeConditions(unstr) - } includeFields(request, unstr) excludeFields(request, unstr) excludeValues(request, unstr) diff --git a/pkg/resources/schema.go b/pkg/resources/schema.go index 298807c2..a96e45f7 100644 --- a/pkg/resources/schema.go +++ b/pkg/resources/schema.go @@ -49,7 +49,7 @@ func DefaultSchemaTemplates(cf *client.Factory, discovery discovery.DiscoveryInterface, namespaceCache corecontrollers.NamespaceCache) []schema.Template { return []schema.Template{ - common.DefaultTemplate(cf, summaryCache, lookup, namespaceCache, false), + common.DefaultTemplate(cf, summaryCache, lookup, namespaceCache), apigroups.Template(discovery), { ID: "configmap", @@ -79,7 +79,7 @@ func DefaultSchemaTemplatesForStore(store types.Store, discovery discovery.DiscoveryInterface) []schema.Template { return []schema.Template{ - common.DefaultTemplateForStore(store, summaryCache, true), + common.DefaultTemplateForStore(store, summaryCache), apigroups.Template(discovery), { ID: "configmap",