Skip to content

🌱 Refactor internal cache/informers map impl #2103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,31 @@ func New(config *rest.Config, opts Options) (Cache, error) {
if err != nil {
return nil, err
}
transformByGVK, err := convertToByGVK(opts.TransformByObject, opts.DefaultTransform, opts.Scheme)
transformers, err := convertToByGVK(opts.TransformByObject, opts.DefaultTransform, opts.Scheme)
if err != nil {
return nil, err
}
transformByObj := internal.TransformFuncByObjectFromMap(transformByGVK)
transformByGVK := internal.TransformFuncByGVKFromMap(transformers)

internalSelectorsByGVK := internal.SelectorsByGVK{}
for gvk, selector := range selectorsByGVK {
internalSelectorsByGVK[gvk] = internal.Selector(selector)
}

im := internal.NewInformersMap(config, opts.Scheme, opts.Mapper, *opts.Resync, opts.Namespace, internalSelectorsByGVK, disableDeepCopyByGVK, transformByObj)
return &informerCache{InformersMap: im}, nil
return &informerCache{
scheme: opts.Scheme,
InformersMap: internal.NewInformersMap(config, &internal.InformersMapOptions{
Scheme: opts.Scheme,
Mapper: opts.Mapper,
ResyncPeriod: *opts.Resync,
Namespace: opts.Namespace,
ByGVK: internal.InformersMapOptionsByGVK{
Selectors: internalSelectorsByGVK,
DisableDeepCopy: disableDeepCopyByGVK,
Transformers: transformByGVK,
},
}),
}, nil
}

// BuilderWithOptions returns a Cache constructor that will build a cache
Expand Down
9 changes: 5 additions & 4 deletions pkg/cache/informer_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ func (*ErrCacheNotStarted) Error() string {

// informerCache is a Kubernetes Object cache populated from InformersMap. informerCache wraps an InformersMap.
type informerCache struct {
scheme *runtime.Scheme
*internal.InformersMap
}

// Get implements Reader.
func (ip *informerCache) Get(ctx context.Context, key client.ObjectKey, out client.Object, opts ...client.GetOption) error {
gvk, err := apiutil.GVKForObject(out, ip.Scheme)
gvk, err := apiutil.GVKForObject(out, ip.scheme)
if err != nil {
return err
}
Expand Down Expand Up @@ -91,7 +92,7 @@ func (ip *informerCache) List(ctx context.Context, out client.ObjectList, opts .
// for a single object corresponding to the passed-in list type. We need them
// because they are used as cache map key.
func (ip *informerCache) objectTypeForListObject(list client.ObjectList) (*schema.GroupVersionKind, runtime.Object, error) {
gvk, err := apiutil.GVKForObject(list, ip.Scheme)
gvk, err := apiutil.GVKForObject(list, ip.scheme)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -132,7 +133,7 @@ func (ip *informerCache) objectTypeForListObject(list client.ObjectList) (*schem
// GetInformerForKind returns the informer for the GroupVersionKind.
func (ip *informerCache) GetInformerForKind(ctx context.Context, gvk schema.GroupVersionKind) (Informer, error) {
// Map the gvk to an object
obj, err := ip.Scheme.New(gvk)
obj, err := ip.scheme.New(gvk)
if err != nil {
return nil, err
}
Expand All @@ -146,7 +147,7 @@ func (ip *informerCache) GetInformerForKind(ctx context.Context, gvk schema.Grou

// GetInformer returns the informer for the obj.
func (ip *informerCache) GetInformer(ctx context.Context, obj client.Object) (Informer, error) {
gvk, err := apiutil.GVKForObject(obj, ip.Scheme)
gvk, err := apiutil.GVKForObject(obj, ip.scheme)
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/cache/informer_cache_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ const (

var _ = Describe("ip.objectTypeForListObject", func() {
ip := &informerCache{
InformersMap: &internal.InformersMap{Scheme: scheme.Scheme},
scheme: scheme.Scheme,
InformersMap: &internal.InformersMap{},
}

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

It("should find the object type of a list with a slice of pointers items field", func() {
By("registering the type", func() {
ip.Scheme = runtime.NewScheme()
ip.scheme = runtime.NewScheme()
err := (&crscheme.Builder{
GroupVersion: schema.GroupVersion{Group: itemPointerSliceTypeGroupName, Version: itemPointerSliceTypeVersion},
}).
Register(
&controllertest.UnconventionalListType{},
&controllertest.UnconventionalListTypeList{},
).AddToScheme(ip.Scheme)
).AddToScheme(ip.scheme)
Expect(err).To(BeNil())
})

Expand Down
126 changes: 0 additions & 126 deletions pkg/cache/internal/deleg_map.go

This file was deleted.

Loading