Skip to content
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

🌱 Further cleanup internal/informers #2130

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/cache/cache.go
Expand Up @@ -183,12 +183,12 @@ func New(config *rest.Config, opts Options) (Cache, error) {

return &informerCache{
scheme: opts.Scheme,
InformersMap: internal.NewInformersMap(config, &internal.InformersMapOptions{
Informers: internal.NewInformers(config, &internal.InformersOpts{
Scheme: opts.Scheme,
Mapper: opts.Mapper,
ResyncPeriod: *opts.Resync,
Namespace: opts.Namespace,
ByGVK: internal.InformersMapOptionsByGVK{
ByGVK: internal.InformersOptsByGVK{
Selectors: internalSelectorsByGVK,
DisableDeepCopy: disableDeepCopyByGVK,
Transformers: transformByGVK,
Expand Down
41 changes: 21 additions & 20 deletions pkg/cache/informer_cache.go
Expand Up @@ -45,20 +45,21 @@ func (*ErrCacheNotStarted) Error() string {
return "the cache is not started, can not read objects"
}

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

// 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)
func (ic *informerCache) Get(ctx context.Context, key client.ObjectKey, out client.Object, opts ...client.GetOption) error {
gvk, err := apiutil.GVKForObject(out, ic.scheme)
if err != nil {
return err
}

started, cache, err := ip.InformersMap.Get(ctx, gvk, out)
started, cache, err := ic.Informers.Get(ctx, gvk, out)
if err != nil {
return err
}
Expand All @@ -70,13 +71,13 @@ func (ip *informerCache) Get(ctx context.Context, key client.ObjectKey, out clie
}

// List implements Reader.
func (ip *informerCache) List(ctx context.Context, out client.ObjectList, opts ...client.ListOption) error {
gvk, cacheTypeObj, err := ip.objectTypeForListObject(out)
func (ic *informerCache) List(ctx context.Context, out client.ObjectList, opts ...client.ListOption) error {
gvk, cacheTypeObj, err := ic.objectTypeForListObject(out)
if err != nil {
return err
}

started, cache, err := ip.InformersMap.Get(ctx, *gvk, cacheTypeObj)
started, cache, err := ic.Informers.Get(ctx, *gvk, cacheTypeObj)
if err != nil {
return err
}
Expand All @@ -91,8 +92,8 @@ func (ip *informerCache) List(ctx context.Context, out client.ObjectList, opts .
// objectTypeForListObject tries to find the runtime.Object and associated GVK
// 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)
func (ic *informerCache) objectTypeForListObject(list client.ObjectList) (*schema.GroupVersionKind, runtime.Object, error) {
gvk, err := apiutil.GVKForObject(list, ic.scheme)
if err != nil {
return nil, nil, err
}
Expand All @@ -115,36 +116,36 @@ func (ip *informerCache) objectTypeForListObject(list client.ObjectList) (*schem

// Any other list type should have a corresponding non-list type registered
// in the scheme. Use that to create a new instance of the non-list type.
cacheTypeObj, err := ip.scheme.New(gvk)
cacheTypeObj, err := ic.scheme.New(gvk)
if err != nil {
return nil, nil, err
}
return &gvk, cacheTypeObj, nil
}

// GetInformerForKind returns the informer for the GroupVersionKind.
func (ip *informerCache) GetInformerForKind(ctx context.Context, gvk schema.GroupVersionKind) (Informer, error) {
func (ic *informerCache) GetInformerForKind(ctx context.Context, gvk schema.GroupVersionKind) (Informer, error) {
// Map the gvk to an object
obj, err := ip.scheme.New(gvk)
obj, err := ic.scheme.New(gvk)
if err != nil {
return nil, err
}

_, i, err := ip.InformersMap.Get(ctx, gvk, obj)
_, i, err := ic.Informers.Get(ctx, gvk, obj)
if err != nil {
return nil, err
}
return i.Informer, err
}

// 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)
func (ic *informerCache) GetInformer(ctx context.Context, obj client.Object) (Informer, error) {
gvk, err := apiutil.GVKForObject(obj, ic.scheme)
if err != nil {
return nil, err
}

_, i, err := ip.InformersMap.Get(ctx, gvk, obj)
_, i, err := ic.Informers.Get(ctx, gvk, obj)
if err != nil {
return nil, err
}
Expand All @@ -153,7 +154,7 @@ func (ip *informerCache) GetInformer(ctx context.Context, obj client.Object) (In

// NeedLeaderElection implements the LeaderElectionRunnable interface
// to indicate that this can be started without requiring the leader lock.
func (ip *informerCache) NeedLeaderElection() bool {
func (ic *informerCache) NeedLeaderElection() bool {
return false
}

Expand All @@ -162,8 +163,8 @@ func (ip *informerCache) NeedLeaderElection() bool {
// to List. For one-to-one compatibility with "normal" field selectors, only return one value.
// The values may be anything. They will automatically be prefixed with the namespace of the
// given object, if present. The objects passed are guaranteed to be objects of the correct type.
func (ip *informerCache) IndexField(ctx context.Context, obj client.Object, field string, extractValue client.IndexerFunc) error {
informer, err := ip.GetInformer(ctx, obj)
func (ic *informerCache) IndexField(ctx context.Context, obj client.Object, field string, extractValue client.IndexerFunc) error {
informer, err := ic.GetInformer(ctx, obj)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cache/informer_cache_unit_test.go
Expand Up @@ -39,8 +39,8 @@ const (

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

It("should find the object type for unstructured lists", func() {
Expand Down