Skip to content

Commit aa18517

Browse files
authored
Merge pull request kubernetes-sigs#35 from pwittrock/cache
Fixup pkg/cache with comments from kubernetes-sigs#29
2 parents dbdeb47 + 916b3aa commit aa18517

File tree

5 files changed

+77
-105
lines changed

5 files changed

+77
-105
lines changed

pkg/cache/cache.go

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ limitations under the License.
1717
package cache
1818

1919
import (
20-
"time"
21-
2220
"fmt"
21+
"time"
2322

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

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

38-
// Cache implements Reader by reading objects from a cache populated by InformersMap
37+
// Cache implements CacheReader by reading objects from a cache populated by InformersMap
3938
type Cache interface {
40-
// Cache implements the client Reader
39+
// Cache implements the client CacheReader
4140
client.Reader
4241

4342
// Cache implements InformersMap
@@ -82,13 +81,16 @@ type Options struct {
8281
Resync *time.Duration
8382
}
8483

85-
var _ Informers = &informerCache{}
86-
var _ client.Reader = &informerCache{}
87-
var _ Cache = &informerCache{}
84+
var defaultResyncTime = 10 * time.Hour
8885

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

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

110112
// Default the resync period to 10 hours if unset
111113
if opts.Resync == nil {
112-
r := 10 * time.Hour
113-
opts.Resync = &r
114+
opts.Resync = &defaultResyncTime
114115
}
115116
return opts, nil
116117
}
117-
118-
// New initializes and returns a new Cache
119-
func New(config *rest.Config, opts Options) (Cache, error) {
120-
opts, err := defaultOpts(config, opts)
121-
if err != nil {
122-
return nil, err
123-
}
124-
im := internal.NewInformersMap(config, opts.Scheme, opts.Mapper, *opts.Resync)
125-
return &informerCache{InformersMap: im}, nil
126-
}

pkg/cache/informers.go renamed to pkg/cache/informer_cache.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ limitations under the License.
1717
package cache
1818

1919
import (
20+
"context"
2021
"fmt"
22+
"reflect"
2123

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

33+
var _ Informers = &informerCache{}
34+
var _ client.Reader = &informerCache{}
35+
var _ Cache = &informerCache{}
36+
37+
// informerCache is a Kubernetes Object cache populated from InformersMap. cache wraps an InformersMap.
38+
type informerCache struct {
39+
*internal.InformersMap
40+
}
41+
42+
// Get implements Reader
43+
func (ip *informerCache) Get(ctx context.Context, key client.ObjectKey, out runtime.Object) error {
44+
gvk, err := apiutil.GVKForObject(out, ip.Scheme)
45+
if err != nil {
46+
return err
47+
}
48+
49+
cache, err := ip.InformersMap.Get(gvk, out)
50+
if err != nil {
51+
return err
52+
}
53+
return cache.Reader.Get(ctx, key, out)
54+
}
55+
56+
// List implements Reader
57+
func (ip *informerCache) List(ctx context.Context, opts *client.ListOptions, out runtime.Object) error {
58+
itemsPtr, err := apimeta.GetItemsPtr(out)
59+
if err != nil {
60+
return nil
61+
}
62+
63+
// http://knowyourmeme.com/memes/this-is-fine
64+
outType := reflect.Indirect(reflect.ValueOf(itemsPtr)).Type().Elem()
65+
cacheType, ok := outType.(runtime.Object)
66+
if !ok {
67+
return fmt.Errorf("cannot get cache for %T, its element is not a runtime.Object", out)
68+
}
69+
70+
gvk, err := apiutil.GVKForObject(out, ip.Scheme)
71+
if err != nil {
72+
return err
73+
}
74+
75+
cache, err := ip.InformersMap.Get(gvk, cacheType)
76+
if err != nil {
77+
return err
78+
}
79+
80+
return cache.Reader.List(ctx, opts, out)
81+
}
82+
3183
// GetInformerForKind returns the informer for the GroupVersionKind
3284
func (ip *informerCache) GetInformerForKind(gvk schema.GroupVersionKind) (cache.SharedIndexInformer, error) {
3385
// Map the gvk to an object

pkg/cache/internal/typed_reader.go renamed to pkg/cache/internal/cache_reader.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ import (
3232
"k8s.io/client-go/tools/cache"
3333
)
3434

35-
// Reader is a Reader
36-
var _ client.Reader = &Reader{}
35+
// CacheReader is a CacheReader
36+
var _ client.Reader = &CacheReader{}
3737

38-
// Reader wraps a cache.Index to implement the client.Reader interface for a single type
39-
type Reader struct {
38+
// CacheReader wraps a cache.Index to implement the client.CacheReader interface for a single type
39+
type CacheReader struct {
4040
// indexer is the underlying indexer wrapped by this cache.
4141
indexer cache.Indexer
4242

@@ -45,7 +45,7 @@ type Reader struct {
4545
}
4646

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

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

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

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

124-
func (c *Reader) getListItems(objs []interface{}, labelSel labels.Selector) ([]runtime.Object, error) {
124+
func (c *CacheReader) getListItems(objs []interface{}, labelSel labels.Selector) ([]runtime.Object, error) {
125125
outItems := make([]runtime.Object, 0, len(objs))
126126
for _, item := range objs {
127127
obj, isObj := item.(runtime.Object)

pkg/cache/internal/factory.go renamed to pkg/cache/internal/informers_map.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ type MapEntry struct {
5454
// Informer is the cached informer
5555
Informer cache.SharedIndexInformer
5656

57-
// Reader wraps Informer and implements the Reader interface for a single type
58-
Reader Reader
57+
// CacheReader wraps Informer and implements the CacheReader interface for a single type
58+
Reader CacheReader
5959
}
6060

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

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

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

167167
// Start the Informer if need by

pkg/cache/reader.go

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)