Skip to content

Commit

Permalink
🌱 Further cleanup internal/informers
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <[email protected]>
  • Loading branch information
vincepri committed Jan 16, 2023
1 parent 5028a59 commit 0503ad8
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 94 deletions.
4 changes: 2 additions & 2 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
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
39 changes: 20 additions & 19 deletions pkg/cache/informer_cache.go
Original file line number Diff line number Diff line change
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 Down Expand Up @@ -131,28 +132,28 @@ 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) {
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 @@ -161,7 +162,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 @@ -170,8 +171,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
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,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
Loading

0 comments on commit 0503ad8

Please sign in to comment.