Skip to content

Fixup pkg/cache with comments from #29 #35

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 1 commit into from
Jun 14, 2018
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
35 changes: 13 additions & 22 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ limitations under the License.
package cache

import (
"time"

"fmt"
"time"

"github.com/kubernetes-sigs/controller-runtime/pkg/cache/internal"
"github.com/kubernetes-sigs/controller-runtime/pkg/client"
Expand All @@ -35,9 +34,9 @@ import (

var log = logf.KBLog.WithName("object-cache")

// Cache implements Reader by reading objects from a cache populated by InformersMap
// Cache implements CacheReader by reading objects from a cache populated by InformersMap
type Cache interface {
// Cache implements the client Reader
// Cache implements the client CacheReader
client.Reader

// Cache implements InformersMap
Expand Down Expand Up @@ -82,13 +81,16 @@ type Options struct {
Resync *time.Duration
}

var _ Informers = &informerCache{}
var _ client.Reader = &informerCache{}
var _ Cache = &informerCache{}
var defaultResyncTime = 10 * time.Hour

// cache is a Kubernetes Object cache populated from InformersMap. cache wraps a CacheProvider and InformersMap.
type informerCache struct {
*internal.InformersMap
// New initializes and returns a new Cache
func New(config *rest.Config, opts Options) (Cache, error) {
opts, err := defaultOpts(config, opts)
if err != nil {
return nil, err
}
im := internal.NewInformersMap(config, opts.Scheme, opts.Mapper, *opts.Resync)
return &informerCache{InformersMap: im}, nil
}

func defaultOpts(config *rest.Config, opts Options) (Options, error) {
Expand All @@ -109,18 +111,7 @@ func defaultOpts(config *rest.Config, opts Options) (Options, error) {

// Default the resync period to 10 hours if unset
if opts.Resync == nil {
r := 10 * time.Hour
opts.Resync = &r
opts.Resync = &defaultResyncTime
}
return opts, nil
}

// New initializes and returns a new Cache
func New(config *rest.Config, opts Options) (Cache, error) {
opts, err := defaultOpts(config, opts)
if err != nil {
return nil, err
}
im := internal.NewInformersMap(config, opts.Scheme, opts.Mapper, *opts.Resync)
return &informerCache{InformersMap: im}, nil
}
52 changes: 52 additions & 0 deletions pkg/cache/informers.go → pkg/cache/informer_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package cache

import (
"context"
"fmt"
"reflect"

"github.com/kubernetes-sigs/controller-runtime/pkg/cache/internal"
"github.com/kubernetes-sigs/controller-runtime/pkg/client"
Expand All @@ -28,6 +30,56 @@ import (
"k8s.io/client-go/tools/cache"
)

var _ Informers = &informerCache{}
var _ client.Reader = &informerCache{}
var _ Cache = &informerCache{}

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

// Get implements Reader
func (ip *informerCache) Get(ctx context.Context, key client.ObjectKey, out runtime.Object) error {
gvk, err := apiutil.GVKForObject(out, ip.Scheme)
if err != nil {
return err
}

cache, err := ip.InformersMap.Get(gvk, out)
if err != nil {
return err
}
return cache.Reader.Get(ctx, key, out)
}

// List implements Reader
func (ip *informerCache) List(ctx context.Context, opts *client.ListOptions, out runtime.Object) error {
itemsPtr, err := apimeta.GetItemsPtr(out)
if err != nil {
return nil
}

// http://knowyourmeme.com/memes/this-is-fine
outType := reflect.Indirect(reflect.ValueOf(itemsPtr)).Type().Elem()
cacheType, ok := outType.(runtime.Object)
if !ok {
return fmt.Errorf("cannot get cache for %T, its element is not a runtime.Object", out)
}

gvk, err := apiutil.GVKForObject(out, ip.Scheme)
if err != nil {
return err
}

cache, err := ip.InformersMap.Get(gvk, cacheType)
if err != nil {
return err
}

return cache.Reader.List(ctx, opts, out)
}

// GetInformerForKind returns the informer for the GroupVersionKind
func (ip *informerCache) GetInformerForKind(gvk schema.GroupVersionKind) (cache.SharedIndexInformer, error) {
// Map the gvk to an object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ import (
"k8s.io/client-go/tools/cache"
)

// Reader is a Reader
var _ client.Reader = &Reader{}
// CacheReader is a CacheReader
var _ client.Reader = &CacheReader{}

// Reader wraps a cache.Index to implement the client.Reader interface for a single type
type Reader struct {
// CacheReader wraps a cache.Index to implement the client.CacheReader interface for a single type
type CacheReader struct {
// indexer is the underlying indexer wrapped by this cache.
indexer cache.Indexer

Expand All @@ -45,7 +45,7 @@ type Reader struct {
}

// Get checks the indexer for the object and writes a copy of it if found
func (c *Reader) Get(_ context.Context, key client.ObjectKey, out runtime.Object) error {
func (c *CacheReader) Get(_ context.Context, key client.ObjectKey, out runtime.Object) error {
storeKey := objectKeyToStoreKey(key)

// Lookup the object from the indexer cache
Expand Down Expand Up @@ -86,7 +86,7 @@ func (c *Reader) Get(_ context.Context, key client.ObjectKey, out runtime.Object
}

// List lists items out of the indexer and writes them to out
func (c *Reader) List(ctx context.Context, opts *client.ListOptions, out runtime.Object) error {
func (c *CacheReader) List(ctx context.Context, opts *client.ListOptions, out runtime.Object) error {
var objs []interface{}
var err error

Expand Down Expand Up @@ -121,7 +121,7 @@ func (c *Reader) List(ctx context.Context, opts *client.ListOptions, out runtime
return apimeta.SetList(out, outItems)
}

func (c *Reader) getListItems(objs []interface{}, labelSel labels.Selector) ([]runtime.Object, error) {
func (c *CacheReader) getListItems(objs []interface{}, labelSel labels.Selector) ([]runtime.Object, error) {
outItems := make([]runtime.Object, 0, len(objs))
for _, item := range objs {
obj, isObj := item.(runtime.Object)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ type MapEntry struct {
// Informer is the cached informer
Informer cache.SharedIndexInformer

// Reader wraps Informer and implements the Reader interface for a single type
Reader Reader
// CacheReader wraps Informer and implements the CacheReader interface for a single type
Reader CacheReader
}

// InformersMap create and caches Informers for (runtime.Object, schema.GroupVersionKind) pairs.
Expand Down Expand Up @@ -122,7 +122,7 @@ func (ip *InformersMap) WaitForCacheSync(stop <-chan struct{}) bool {
return cache.WaitForCacheSync(stop, syncedFuncs...)
}

// Get will create a new Informer and added it to the map of InformersMap if none exists. Returns
// Get will create a new Informer and add it to the map of InformersMap if none exists. Returns
// the Informer from the map.
func (ip *InformersMap) Get(gvk schema.GroupVersionKind, obj runtime.Object) (*MapEntry, error) {
// Return the informer if it is found
Expand All @@ -137,7 +137,7 @@ func (ip *InformersMap) Get(gvk schema.GroupVersionKind, obj runtime.Object) (*M
}

// Do the mutex part in its own function so we can use defer without blocking pieces that don't
// to be locked
// need to be locked
var sync bool
i, err := func() (*MapEntry, error) {
ip.mu.Lock()
Expand All @@ -161,7 +161,7 @@ func (ip *InformersMap) Get(gvk schema.GroupVersionKind, obj runtime.Object) (*M
ni := cache.NewSharedIndexInformer(lw, obj, ip.resync, cache.Indexers{})
i = &MapEntry{
Informer: ni,
Reader: Reader{indexer: ni.GetIndexer(), groupVersionKind: gvk}}
Reader: CacheReader{indexer: ni.GetIndexer(), groupVersionKind: gvk}}
ip.informersByGVK[gvk] = i

// Start the Informer if need by
Expand Down
71 changes: 0 additions & 71 deletions pkg/cache/reader.go

This file was deleted.