Skip to content

Commit a49522b

Browse files
committed
Calculate the fields needed to create a db table only when needed.
Wrap the field-calculator in a closure, and pass it to the informer, to be invoked only when it needs to create db tables.
1 parent 191990a commit a49522b

File tree

7 files changed

+313
-198
lines changed

7 files changed

+313
-198
lines changed

pkg/sqlcache/Readme.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ intended to be used as a way of enforcing RBAC.
7878
fields := [][]string{{"metadata", "name"}, {"metadata", "namespace"}}
7979
opts := &informer.ListOptions{}
8080
// gvk should be of type k8s.io/apimachinery/pkg/runtime/schema.GroupVersionKind
81-
c, err := cacheFactory.CacheFor(fields, client, gvk)
81+
c, err := cacheFactory.CacheFor(context.Context,
82+
getFieldsForGVKFunc, // See pkg/stores/sqlproxy/proxy_store:fieldsForGVK for an example
83+
tableClient,
84+
gvk,
85+
schema,
86+
controllerschema.IsListWatchable(schema))
8287
if err != nil {
8388
panic(err)
8489
}

pkg/sqlcache/informer/factory/informer_factory.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ type guardedInformer struct {
6363
wg wait.Group
6464
}
6565

66+
// This function is used by factory.CacheFor() to pull in table info when the function needs to create new DB tables.
67+
type GetFieldsFuncType func() (fields [][]string, typeGuidance map[string]string, externalUpdateInfo *sqltypes.ExternalGVKUpdates, selfUpdateInfo *sqltypes.ExternalGVKUpdates, isNamespaced bool, transform cache.TransformFunc)
68+
6669
type newInformer func(ctx context.Context, client dynamic.ResourceInterface, fields [][]string, externalUpdateInfo *sqltypes.ExternalGVKUpdates, selfUpdateInfo *sqltypes.ExternalGVKUpdates, transform cache.TransformFunc, gvk schema.GroupVersionKind, db db.Client, shouldEncrypt bool, typeGuidance map[string]string, namespace bool, watchable bool, gcInterval time.Duration, gcKeepCount int) (*informer.Informer, error)
6770

6871
type Cache struct {
@@ -144,7 +147,8 @@ func NewCacheFactoryWithContext(ctx context.Context, opts CacheFactoryOptions) (
144147
// a context for a single cache to be able to stop that cache (eg: on schema refresh) without impacting the other caches.
145148
//
146149
// Don't forget to call DoneWithCache with the given informer once done with it.
147-
func (f *CacheFactory) CacheFor(ctx context.Context, fields [][]string, externalUpdateInfo *sqltypes.ExternalGVKUpdates, selfUpdateInfo *sqltypes.ExternalGVKUpdates, transform cache.TransformFunc, client dynamic.ResourceInterface, gvk schema.GroupVersionKind, typeGuidance map[string]string, namespaced bool, watchable bool) (*Cache, error) {
150+
151+
func (f *CacheFactory) CacheFor(ctx context.Context, getFieldsFunc GetFieldsFuncType, client dynamic.ResourceInterface, gvk schema.GroupVersionKind, watchable bool) (*Cache, error) {
148152
// Second, check if the informer and its accompanying informer-specific mutex exist already in the informers cache
149153
// If not, start by creating such informer-specific mutex. That is used later to ensure no two goroutines create
150154
// informers for the same GVK at the same type
@@ -168,15 +172,15 @@ func (f *CacheFactory) CacheFor(ctx context.Context, fields [][]string, external
168172
// Prevent Stop() to be called for that GVK
169173
gi.stopMutex.RLock()
170174

171-
gvkCache, err := f.cacheForLocked(ctx, gi, fields, externalUpdateInfo, selfUpdateInfo, transform, client, gvk, typeGuidance, namespaced, watchable)
175+
gvkCache, err := f.cacheForLocked(ctx, gi, getFieldsFunc, client, gvk, watchable)
172176
if err != nil {
173177
gi.stopMutex.RUnlock()
174178
return nil, err
175179
}
176180
return gvkCache, nil
177181
}
178182

179-
func (f *CacheFactory) cacheForLocked(ctx context.Context, gi *guardedInformer, fields [][]string, externalUpdateInfo *sqltypes.ExternalGVKUpdates, selfUpdateInfo *sqltypes.ExternalGVKUpdates, transform cache.TransformFunc, client dynamic.ResourceInterface, gvk schema.GroupVersionKind, typeGuidance map[string]string, namespaced bool, watchable bool) (*Cache, error) {
183+
func (f *CacheFactory) cacheForLocked(ctx context.Context, gi *guardedInformer, getFieldsFunc GetFieldsFuncType, client dynamic.ResourceInterface, gvk schema.GroupVersionKind, watchable bool) (*Cache, error) {
180184
// At this point an informer-specific mutex (gi.mutex) is guaranteed to exist. Lock it
181185
gi.informerMutex.Lock()
182186

@@ -191,9 +195,10 @@ func (f *CacheFactory) cacheForLocked(ctx context.Context, gi *guardedInformer,
191195

192196
_, encryptResourceAlways := defaultEncryptedResourceTypes[gvk]
193197
shouldEncrypt := f.encryptAll || encryptResourceAlways
198+
fields, typeGuidance, externalUpdateInfo, selfUpdateInfo, isNamespaced, transformFunc := getFieldsFunc()
194199
// In non-test code this invokes pkg/sqlcache/informer/informer.go: NewInformer()
195200
// search for "func NewInformer(ctx"
196-
i, err := f.newInformer(gi.ctx, client, fields, externalUpdateInfo, selfUpdateInfo, transform, gvk, f.dbClient, shouldEncrypt, typeGuidance, namespaced, watchable, f.gcInterval, f.gcKeepCount)
201+
i, err := f.newInformer(gi.ctx, client, fields, externalUpdateInfo, selfUpdateInfo, transformFunc, gvk, f.dbClient, shouldEncrypt, typeGuidance, isNamespaced, watchable, f.gcInterval, f.gcKeepCount)
197202
if err != nil {
198203
gi.informerMutex.Unlock()
199204
return nil, err

0 commit comments

Comments
 (0)