Skip to content

Commit

Permalink
Merge pull request #2130 from vincepri/cleanup-internal-informers
Browse files Browse the repository at this point in the history
🌱 Further cleanup internal/informers
  • Loading branch information
k8s-ci-robot committed Jan 17, 2023
2 parents 319c422 + 0f07001 commit e1fd8e7
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 95 deletions.
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

0 comments on commit e1fd8e7

Please sign in to comment.