Skip to content

Commit 4a1bb11

Browse files
committed
🌱 Cleanup internal cache/informers map impl
Signed-off-by: Vince Prignano <[email protected]>
1 parent 69f0938 commit 4a1bb11

File tree

5 files changed

+79
-71
lines changed

5 files changed

+79
-71
lines changed

pkg/cache/cache.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,20 @@ func New(config *rest.Config, opts Options) (Cache, error) {
181181
internalSelectorsByGVK[gvk] = internal.Selector(selector)
182182
}
183183

184-
im := internal.NewInformersMap(config, opts.Scheme, opts.Mapper, *opts.Resync, opts.Namespace, internalSelectorsByGVK, disableDeepCopyByGVK, transformByObj)
185-
return &informerCache{InformersMap: im}, nil
184+
return &informerCache{
185+
scheme: opts.Scheme,
186+
InformersMap: internal.NewInformersMap(config, &internal.InformersMapOptions{
187+
Scheme: opts.Scheme,
188+
Mapper: opts.Mapper,
189+
ResyncPeriod: *opts.Resync,
190+
Namespace: opts.Namespace,
191+
ByGVK: internal.InformersMapOptionsByGVK{
192+
Selectors: internalSelectorsByGVK,
193+
DisableDeepCopy: disableDeepCopyByGVK,
194+
Transformers: transformByObj,
195+
},
196+
}),
197+
}, nil
186198
}
187199

188200
// BuilderWithOptions returns a Cache constructor that will build a cache

pkg/cache/informer_cache.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ func (*ErrCacheNotStarted) Error() string {
4747

4848
// informerCache is a Kubernetes Object cache populated from InformersMap. informerCache wraps an InformersMap.
4949
type informerCache struct {
50+
scheme *runtime.Scheme
5051
*internal.InformersMap
5152
}
5253

5354
// Get implements Reader.
5455
func (ip *informerCache) Get(ctx context.Context, key client.ObjectKey, out client.Object, opts ...client.GetOption) error {
55-
gvk, err := apiutil.GVKForObject(out, ip.Scheme)
56+
gvk, err := apiutil.GVKForObject(out, ip.scheme)
5657
if err != nil {
5758
return err
5859
}
@@ -91,7 +92,7 @@ func (ip *informerCache) List(ctx context.Context, out client.ObjectList, opts .
9192
// for a single object corresponding to the passed-in list type. We need them
9293
// because they are used as cache map key.
9394
func (ip *informerCache) objectTypeForListObject(list client.ObjectList) (*schema.GroupVersionKind, runtime.Object, error) {
94-
gvk, err := apiutil.GVKForObject(list, ip.Scheme)
95+
gvk, err := apiutil.GVKForObject(list, ip.scheme)
9596
if err != nil {
9697
return nil, nil, err
9798
}
@@ -132,7 +133,7 @@ func (ip *informerCache) objectTypeForListObject(list client.ObjectList) (*schem
132133
// GetInformerForKind returns the informer for the GroupVersionKind.
133134
func (ip *informerCache) GetInformerForKind(ctx context.Context, gvk schema.GroupVersionKind) (Informer, error) {
134135
// Map the gvk to an object
135-
obj, err := ip.Scheme.New(gvk)
136+
obj, err := ip.scheme.New(gvk)
136137
if err != nil {
137138
return nil, err
138139
}
@@ -146,7 +147,7 @@ func (ip *informerCache) GetInformerForKind(ctx context.Context, gvk schema.Grou
146147

147148
// GetInformer returns the informer for the obj.
148149
func (ip *informerCache) GetInformer(ctx context.Context, obj client.Object) (Informer, error) {
149-
gvk, err := apiutil.GVKForObject(obj, ip.Scheme)
150+
gvk, err := apiutil.GVKForObject(obj, ip.scheme)
150151
if err != nil {
151152
return nil, err
152153
}

pkg/cache/informer_cache_unit_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ const (
3838

3939
var _ = Describe("ip.objectTypeForListObject", func() {
4040
ip := &informerCache{
41-
InformersMap: &internal.InformersMap{Scheme: scheme.Scheme},
41+
scheme: scheme.Scheme,
42+
InformersMap: &internal.InformersMap{},
4243
}
4344

4445
It("should find the object type for unstructured lists", func() {
@@ -70,14 +71,14 @@ var _ = Describe("ip.objectTypeForListObject", func() {
7071

7172
It("should find the object type of a list with a slice of pointers items field", func() {
7273
By("registering the type", func() {
73-
ip.Scheme = runtime.NewScheme()
74+
ip.scheme = runtime.NewScheme()
7475
err := (&crscheme.Builder{
7576
GroupVersion: schema.GroupVersion{Group: itemPointerSliceTypeGroupName, Version: itemPointerSliceTypeVersion},
7677
}).
7778
Register(
7879
&controllertest.UnconventionalListType{},
7980
&controllertest.UnconventionalListTypeList{},
80-
).AddToScheme(ip.Scheme)
81+
).AddToScheme(ip.scheme)
8182
Expect(err).To(BeNil())
8283
})
8384

pkg/cache/internal/deleg_map.go

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,32 +34,48 @@ import (
3434
type InformersMap struct {
3535
// we abstract over the details of structured/unstructured/metadata with the specificInformerMaps
3636
// TODO(directxman12): genericize this over different projections now that we have 3 different maps
37+
// TODO(vincepri): A different structure of the specific informer map is possible, although it requires
38+
// a large refactoring that takes into account that there may be different kind of informers, in this case
39+
// 3 of those: structured, unstructured, and metadata.
3740

3841
structured *specificInformersMap
3942
unstructured *specificInformersMap
4043
metadata *specificInformersMap
44+
}
45+
46+
// InformersMapOptions configures an InformerMap.
47+
type InformersMapOptions struct {
48+
Scheme *runtime.Scheme
49+
Mapper meta.RESTMapper
50+
ResyncPeriod time.Duration
51+
Namespace string
52+
ByGVK InformersMapOptionsByGVK
53+
}
4154

42-
// Scheme maps runtime.Objects to GroupVersionKinds
43-
Scheme *runtime.Scheme
55+
// InformersMapOptionsByGVK configured additional by group version kind (or object)
56+
// in an InformerMap.
57+
type InformersMapOptionsByGVK struct {
58+
Selectors SelectorsByGVK
59+
Transformers TransformFuncByObject // TODO(vincepri): Why is this by object and not GVK?
60+
DisableDeepCopy DisableDeepCopyByGVK
4461
}
4562

4663
// NewInformersMap creates a new InformersMap that can create informers for
4764
// both structured and unstructured objects.
48-
func NewInformersMap(config *rest.Config,
49-
scheme *runtime.Scheme,
50-
mapper meta.RESTMapper,
51-
resync time.Duration,
52-
namespace string,
53-
selectors SelectorsByGVK,
54-
disableDeepCopy DisableDeepCopyByGVK,
55-
transformers TransformFuncByObject,
56-
) *InformersMap {
65+
func NewInformersMap(config *rest.Config, options *InformersMapOptions) *InformersMap {
5766
return &InformersMap{
58-
structured: newStructuredInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, transformers),
59-
unstructured: newUnstructuredInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, transformers),
60-
metadata: newMetadataInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, transformers),
61-
62-
Scheme: scheme,
67+
structured: newSpecificInformersMap(config, &specificInformerMapOptions{
68+
InformersMapOptions: options,
69+
ListWatcherFunc: structuredListWatch,
70+
}),
71+
unstructured: newSpecificInformersMap(config, &specificInformerMapOptions{
72+
InformersMapOptions: options,
73+
ListWatcherFunc: unstructuredListWatch,
74+
}),
75+
metadata: newSpecificInformersMap(config, &specificInformerMapOptions{
76+
InformersMapOptions: options,
77+
ListWatcherFunc: metadataListWatch,
78+
}),
6379
}
6480
}
6581

@@ -106,21 +122,3 @@ func (m *InformersMap) Get(ctx context.Context, gvk schema.GroupVersionKind, obj
106122
return m.structured.Get(ctx, gvk, obj)
107123
}
108124
}
109-
110-
// newStructuredInformersMap creates a new InformersMap for structured objects.
111-
func newStructuredInformersMap(config *rest.Config, scheme *runtime.Scheme, mapper meta.RESTMapper, resync time.Duration,
112-
namespace string, selectors SelectorsByGVK, disableDeepCopy DisableDeepCopyByGVK, transformers TransformFuncByObject) *specificInformersMap {
113-
return newSpecificInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, transformers, createStructuredListWatch)
114-
}
115-
116-
// newUnstructuredInformersMap creates a new InformersMap for unstructured objects.
117-
func newUnstructuredInformersMap(config *rest.Config, scheme *runtime.Scheme, mapper meta.RESTMapper, resync time.Duration,
118-
namespace string, selectors SelectorsByGVK, disableDeepCopy DisableDeepCopyByGVK, transformers TransformFuncByObject) *specificInformersMap {
119-
return newSpecificInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, transformers, createUnstructuredListWatch)
120-
}
121-
122-
// newMetadataInformersMap creates a new InformersMap for metadata-only objects.
123-
func newMetadataInformersMap(config *rest.Config, scheme *runtime.Scheme, mapper meta.RESTMapper, resync time.Duration,
124-
namespace string, selectors SelectorsByGVK, disableDeepCopy DisableDeepCopyByGVK, transformers TransformFuncByObject) *specificInformersMap {
125-
return newSpecificInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, transformers, createMetadataListWatch)
126-
}

pkg/cache/internal/informers_map.go

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,34 +45,30 @@ func init() {
4545
// clientListWatcherFunc knows how to create a ListWatcher.
4646
type createListWatcherFunc func(gvk schema.GroupVersionKind, ip *specificInformersMap) (*cache.ListWatch, error)
4747

48+
// specificInformerMapOptions contains options to create a new specific informer map.
49+
type specificInformerMapOptions struct {
50+
*InformersMapOptions
51+
ListWatcherFunc createListWatcherFunc
52+
}
53+
4854
// newSpecificInformersMap returns a new specificInformersMap (like
4955
// the generical InformersMap, except that it doesn't implement WaitForCacheSync).
50-
func newSpecificInformersMap(config *rest.Config,
51-
scheme *runtime.Scheme,
52-
mapper meta.RESTMapper,
53-
resync time.Duration,
54-
namespace string,
55-
selectors SelectorsByGVK,
56-
disableDeepCopy DisableDeepCopyByGVK,
57-
transformers TransformFuncByObject,
58-
createListWatcher createListWatcherFunc,
59-
) *specificInformersMap {
60-
ip := &specificInformersMap{
56+
func newSpecificInformersMap(config *rest.Config, options *specificInformerMapOptions) *specificInformersMap {
57+
return &specificInformersMap{
6158
config: config,
62-
Scheme: scheme,
63-
mapper: mapper,
59+
scheme: options.Scheme,
60+
mapper: options.Mapper,
6461
informersByGVK: make(map[schema.GroupVersionKind]*MapEntry),
65-
codecs: serializer.NewCodecFactory(scheme),
66-
paramCodec: runtime.NewParameterCodec(scheme),
67-
resync: resync,
62+
codecs: serializer.NewCodecFactory(options.Scheme),
63+
paramCodec: runtime.NewParameterCodec(options.Scheme),
64+
resync: options.ResyncPeriod,
6865
startWait: make(chan struct{}),
69-
createListWatcher: createListWatcher,
70-
namespace: namespace,
71-
selectors: selectors.forGVK,
72-
disableDeepCopy: disableDeepCopy,
73-
transformers: transformers,
66+
createListWatcher: options.ListWatcherFunc,
67+
namespace: options.Namespace,
68+
selectors: options.ByGVK.Selectors.forGVK,
69+
disableDeepCopy: options.ByGVK.DisableDeepCopy,
70+
transformers: options.ByGVK.Transformers,
7471
}
75-
return ip
7672
}
7773

7874
// MapEntry contains the cached data for an Informer.
@@ -87,8 +83,8 @@ type MapEntry struct {
8783
// specificInformersMap create and caches Informers for (runtime.Object, schema.GroupVersionKind) pairs.
8884
// It uses a standard parameter codec constructed based on the given generated Scheme.
8985
type specificInformersMap struct {
90-
// Scheme maps runtime.Objects to GroupVersionKinds
91-
Scheme *runtime.Scheme
86+
// scheme maps runtime.Objects to GroupVersionKinds
87+
scheme *runtime.Scheme
9288

9389
// config is used to talk to the apiserver
9490
config *rest.Config
@@ -265,7 +261,7 @@ func (ip *specificInformersMap) addInformerToMap(gvk schema.GroupVersionKind, ob
265261
}
266262

267263
// newListWatch returns a new ListWatch object that can be used to create a SharedIndexInformer.
268-
func createStructuredListWatch(gvk schema.GroupVersionKind, ip *specificInformersMap) (*cache.ListWatch, error) {
264+
func structuredListWatch(gvk schema.GroupVersionKind, ip *specificInformersMap) (*cache.ListWatch, error) {
269265
// Kubernetes APIs work against Resources, not GroupVersionKinds. Map the
270266
// groupVersionKind to the Resource API we will use.
271267
mapping, err := ip.mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
@@ -278,7 +274,7 @@ func createStructuredListWatch(gvk schema.GroupVersionKind, ip *specificInformer
278274
return nil, err
279275
}
280276
listGVK := gvk.GroupVersion().WithKind(gvk.Kind + "List")
281-
listObj, err := ip.Scheme.New(listGVK)
277+
listObj, err := ip.scheme.New(listGVK)
282278
if err != nil {
283279
return nil, err
284280
}
@@ -308,7 +304,7 @@ func createStructuredListWatch(gvk schema.GroupVersionKind, ip *specificInformer
308304
}, nil
309305
}
310306

311-
func createUnstructuredListWatch(gvk schema.GroupVersionKind, ip *specificInformersMap) (*cache.ListWatch, error) {
307+
func unstructuredListWatch(gvk schema.GroupVersionKind, ip *specificInformersMap) (*cache.ListWatch, error) {
312308
// Kubernetes APIs work against Resources, not GroupVersionKinds. Map the
313309
// groupVersionKind to the Resource API we will use.
314310
mapping, err := ip.mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
@@ -352,7 +348,7 @@ func createUnstructuredListWatch(gvk schema.GroupVersionKind, ip *specificInform
352348
}, nil
353349
}
354350

355-
func createMetadataListWatch(gvk schema.GroupVersionKind, ip *specificInformersMap) (*cache.ListWatch, error) {
351+
func metadataListWatch(gvk schema.GroupVersionKind, ip *specificInformersMap) (*cache.ListWatch, error) {
356352
// Kubernetes APIs work against Resources, not GroupVersionKinds. Map the
357353
// groupVersionKind to the Resource API we will use.
358354
mapping, err := ip.mapper.RESTMapping(gvk.GroupKind(), gvk.Version)

0 commit comments

Comments
 (0)