Skip to content

Commit 3f7d90d

Browse files
committed
Merge cache and informers into a single type.
1 parent 835071a commit 3f7d90d

File tree

7 files changed

+413
-472
lines changed

7 files changed

+413
-472
lines changed

pkg/cache/cache.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ import (
3333

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

36-
// Cache implements ReadInterface by reading objects from a cache populated by Informers
36+
// Cache implements ReadInterface by reading objects from a cache populated by InformersMap
3737
type Cache interface {
3838
// Cache implements the client ReadInterface
3939
client.ReadInterface
4040

41-
// Cache implements Informers
41+
// Cache implements InformersMap
4242
Informers
4343
}
4444

45-
// Informers knows how to create or fetch informers for different group-version-kinds.
45+
// InformersMap knows how to create or fetch informers for different group-version-kinds.
4646
// It's safe to call GetInformer from multiple threads.
4747
type Informers interface {
4848
// GetInformer fetches or constructs an informer for the given object that corresponds to a single
@@ -64,7 +64,7 @@ type Informers interface {
6464
IndexField(obj runtime.Object, field string, extractValue client.IndexerFunc) error
6565
}
6666

67-
// Options are the optional arguments for creating a new Informers object
67+
// Options are the optional arguments for creating a new InformersMap object
6868
type Options struct {
6969
// Scheme is the scheme to use for mapping objects to GroupVersionKinds
7070
Scheme *runtime.Scheme
@@ -76,14 +76,13 @@ type Options struct {
7676
Resync *time.Duration
7777
}
7878

79-
var _ Informers = &cache{}
80-
var _ client.ReadInterface = &cache{}
81-
var _ Cache = &cache{}
79+
var _ Informers = &informerCache{}
80+
var _ client.ReadInterface = &informerCache{}
81+
var _ Cache = &informerCache{}
8282

83-
// cache is a Kubernetes Object cache populated from Informers. cache wraps a CacheProvider and InformerProvider.
84-
type cache struct {
85-
*internal.CacheProvider
86-
*internal.InformerProvider
83+
// cache is a Kubernetes Object cache populated from InformersMap. cache wraps a CacheProvider and InformersMap.
84+
type informerCache struct {
85+
*internal.InformersMap
8786
}
8887

8988
// New initializes and returns a new Cache
@@ -109,9 +108,6 @@ func New(config *rest.Config, opts Options) (Cache, error) {
109108
opts.Resync = &r
110109
}
111110

112-
cp := internal.NewCacheProvider()
113-
ip := internal.NewInformerProvider(config, opts.Scheme, opts.Mapper, *opts.Resync, cp)
114-
cp.SetInformerProvider(ip)
115-
c := &cache{InformerProvider: ip, CacheProvider: cp}
116-
return c, nil
111+
im := internal.NewInformers(config, opts.Scheme, opts.Mapper, *opts.Resync)
112+
return &informerCache{InformersMap: im}, nil
117113
}

pkg/cache/informers.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cache
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/kubernetes-sigs/controller-runtime/pkg/cache/internal"
23+
"github.com/kubernetes-sigs/controller-runtime/pkg/client"
24+
"github.com/kubernetes-sigs/controller-runtime/pkg/client/apiutil"
25+
apimeta "k8s.io/apimachinery/pkg/api/meta"
26+
"k8s.io/apimachinery/pkg/runtime"
27+
"k8s.io/apimachinery/pkg/runtime/schema"
28+
"k8s.io/client-go/tools/cache"
29+
)
30+
31+
// GetInformerForKind returns the informer for the GroupVersionKind
32+
func (ip *informerCache) GetInformerForKind(gvk schema.GroupVersionKind) (cache.SharedIndexInformer, error) {
33+
// Map the gvk to an object
34+
obj, err := ip.Scheme.New(gvk)
35+
if err != nil {
36+
return nil, err
37+
}
38+
i, err := ip.InformersMap.Get(gvk, obj)
39+
if err != nil {
40+
return nil, err
41+
}
42+
return i.Informer, err
43+
}
44+
45+
// GetInformer returns the informer for the obj
46+
func (ip *informerCache) GetInformer(obj runtime.Object) (cache.SharedIndexInformer, error) {
47+
gvk, err := apiutil.GVKForObject(obj, ip.Scheme)
48+
if err != nil {
49+
return nil, err
50+
}
51+
i, err := ip.InformersMap.Get(gvk, obj)
52+
if err != nil {
53+
return nil, err
54+
}
55+
return i.Informer, err
56+
}
57+
58+
// IndexField adds an indexer to the underlying cache, using extraction function to get
59+
// value(s) from the given field. This index can then be used by passing a field selector
60+
// to List. For one-to-one compatibility with "normal" field selectors, only return one value.
61+
// The values may be anything. They will automatically be prefixed with the namespace of the
62+
// given object, if present. The objects passed are guaranteed to be objects of the correct type.
63+
func (ip *informerCache) IndexField(obj runtime.Object, field string, extractValue client.IndexerFunc) error {
64+
informer, err := ip.GetInformer(obj)
65+
if err != nil {
66+
return err
67+
}
68+
return indexByField(informer.GetIndexer(), field, extractValue)
69+
}
70+
71+
func indexByField(indexer cache.Indexer, field string, extractor client.IndexerFunc) error {
72+
indexFunc := func(objRaw interface{}) ([]string, error) {
73+
// TODO(directxman12): check if this is the correct type?
74+
obj, isObj := objRaw.(runtime.Object)
75+
if !isObj {
76+
return nil, fmt.Errorf("object of type %T is not an Object", objRaw)
77+
}
78+
meta, err := apimeta.Accessor(obj)
79+
if err != nil {
80+
return nil, err
81+
}
82+
ns := meta.GetNamespace()
83+
84+
rawVals := extractor(obj)
85+
var vals []string
86+
if ns == "" {
87+
// if we're not doubling the keys for the namespaced case, just re-use what was returned to us
88+
vals = rawVals
89+
} else {
90+
// if we need to add non-namespaced versions too, double the length
91+
vals = make([]string, len(rawVals)*2)
92+
}
93+
for i, rawVal := range rawVals {
94+
// save a namespaced variant, so that we can ask
95+
// "what are all the object matching a given index *in a given namespace*"
96+
vals[i] = internal.KeyToNamespacedKey(ns, rawVal)
97+
if ns != "" {
98+
// if we have a namespace, also inject a special index key for listing
99+
// regardless of the object namespace
100+
vals[i+len(rawVals)] = internal.KeyToNamespacedKey("", rawVal)
101+
}
102+
}
103+
104+
return vals, nil
105+
}
106+
107+
return indexer.AddIndexers(cache.Indexers{internal.FieldIndexName(field): indexFunc})
108+
}

pkg/cache/internal/cache_provider.go

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

0 commit comments

Comments
 (0)