Skip to content

Commit b0d3f0b

Browse files
DirectXMan12Shawn Hurley
authored andcommitted
Refactor structure details out of informer cache.
1 parent 0e19995 commit b0d3f0b

File tree

5 files changed

+245
-158
lines changed

5 files changed

+245
-158
lines changed

pkg/cache/internal/deleg_map.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 internal
18+
19+
import (
20+
"time"
21+
22+
"k8s.io/apimachinery/pkg/api/meta"
23+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
24+
"k8s.io/apimachinery/pkg/runtime"
25+
"k8s.io/apimachinery/pkg/runtime/schema"
26+
"k8s.io/client-go/rest"
27+
"k8s.io/client-go/tools/cache"
28+
)
29+
30+
// InformersMap create and caches Informers for (runtime.Object, schema.GroupVersionKind) pairs.
31+
// It uses a standard parameter codec constructed based on the given generated Scheme.
32+
type InformersMap struct {
33+
// we abstract over the details of structured vs unstructured with the specificInformerMaps
34+
35+
structured *specificInformersMap
36+
unstructured *specificInformersMap
37+
38+
// Scheme maps runtime.Objects to GroupVersionKinds
39+
Scheme *runtime.Scheme
40+
}
41+
42+
// NewInformersMap creates a new InformersMap that can create informers for
43+
// both structured and unstructured objects.
44+
func NewInformersMap(config *rest.Config,
45+
scheme *runtime.Scheme,
46+
mapper meta.RESTMapper,
47+
resync time.Duration) *InformersMap {
48+
49+
return &InformersMap{
50+
structured: newStructuredInformersMap(config, scheme, mapper, resync),
51+
unstructured: newUnstructuredInformersMap(config, scheme, mapper, resync),
52+
53+
Scheme: scheme,
54+
}
55+
}
56+
57+
// Start calls Run on each of the informers and sets started to true. Blocks on the stop channel.
58+
func (m *InformersMap) Start(stop <-chan struct{}) error {
59+
go m.structured.Start(stop)
60+
go m.unstructured.Start(stop)
61+
<-stop
62+
return nil
63+
}
64+
65+
// WaitForCacheSync waits until all the caches have been synced.
66+
func (m *InformersMap) WaitForCacheSync(stop <-chan struct{}) bool {
67+
syncedFuncs := append([]cache.InformerSynced(nil), m.structured.HasSyncedFuncs()...)
68+
syncedFuncs = append(syncedFuncs, m.unstructured.HasSyncedFuncs()...)
69+
70+
return cache.WaitForCacheSync(stop, syncedFuncs...)
71+
}
72+
73+
// Get will create a new Informer and add it to the map of InformersMap if none exists. Returns
74+
// the Informer from the map.
75+
func (m *InformersMap) Get(gvk schema.GroupVersionKind, obj runtime.Object) (*MapEntry, error) {
76+
_, isUnstructured := obj.(*unstructured.Unstructured)
77+
_, isUnstructuredList := obj.(*unstructured.UnstructuredList)
78+
isUnstructured = isUnstructured || isUnstructuredList
79+
80+
if isUnstructured {
81+
return m.unstructured.Get(gvk, obj)
82+
}
83+
84+
return m.structured.Get(gvk, obj)
85+
}
86+
87+
// newStructuredInformersMap creates a new InformersMap for structured objects.
88+
func newStructuredInformersMap(config *rest.Config, scheme *runtime.Scheme, mapper meta.RESTMapper, resync time.Duration) *specificInformersMap {
89+
return newSpecificInformersMap(config, scheme, mapper, resync, createStructuredClient)
90+
}
91+
92+
// newUnstructuredInformersMap creates a new InformersMap for unstructured objects.
93+
func newUnstructuredInformersMap(config *rest.Config, scheme *runtime.Scheme, mapper meta.RESTMapper, resync time.Duration) *specificInformersMap {
94+
return newSpecificInformersMap(config, scheme, mapper, resync, createUnstructuredClient)
95+
}

pkg/cache/internal/informers_map.go

Lines changed: 91 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,32 @@ import (
3030
"k8s.io/apimachinery/pkg/watch"
3131
"k8s.io/client-go/rest"
3232
"k8s.io/client-go/tools/cache"
33+
3334
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
3435
)
3536

36-
// NewInformersMap returns a new InformersMap
37-
func NewInformersMap(config *rest.Config,
37+
// clientCreatorFunc knows how to create a client and the corresponding list object that it should
38+
// deserialize into for any given group-version-kind.
39+
type clientCreatorFunc func(gvk schema.GroupVersionKind,
40+
codecs serializer.CodecFactory,
41+
scheme *runtime.Scheme,
42+
baseConfig *rest.Config) (client rest.Interface, listObj runtime.Object, err error)
43+
44+
// newSpecificInformersMap returns a new specificInformersMap (like
45+
// the generical InformersMap, except that it doesn't implement WaitForCacheSync).
46+
func newSpecificInformersMap(config *rest.Config,
3847
scheme *runtime.Scheme,
3948
mapper meta.RESTMapper,
40-
resync time.Duration) *InformersMap {
41-
ip := &InformersMap{
42-
config: config,
43-
Scheme: scheme,
44-
mapper: mapper,
45-
informersByGVK: make(map[schema.GroupVersionKind]*MapEntry),
46-
unstructuredInformerByGVK: make(map[schema.GroupVersionKind]*MapEntry),
47-
codecs: serializer.NewCodecFactory(scheme),
48-
paramCodec: runtime.NewParameterCodec(scheme),
49-
resync: resync,
49+
resync time.Duration, createClient clientCreatorFunc) *specificInformersMap {
50+
ip := &specificInformersMap{
51+
config: config,
52+
Scheme: scheme,
53+
mapper: mapper,
54+
informersByGVK: make(map[schema.GroupVersionKind]*MapEntry),
55+
codecs: serializer.NewCodecFactory(scheme),
56+
paramCodec: runtime.NewParameterCodec(scheme),
57+
resync: resync,
58+
createClient: createClient,
5059
}
5160
return ip
5261
}
@@ -60,9 +69,9 @@ type MapEntry struct {
6069
Reader CacheReader
6170
}
6271

63-
// InformersMap create and caches Informers for (runtime.Object, schema.GroupVersionKind) pairs.
64-
//It uses a standard parameter codec constructed based on the given generated Scheme.
65-
type InformersMap struct {
72+
// specificInformersMap create and caches Informers for (runtime.Object, schema.GroupVersionKind) pairs.
73+
// It uses a standard parameter codec constructed based on the given generated Scheme.
74+
type specificInformersMap struct {
6675
// Scheme maps runtime.Objects to GroupVersionKinds
6776
Scheme *runtime.Scheme
6877

@@ -75,10 +84,6 @@ type InformersMap struct {
7584
// informersByGVK is the cache of informers keyed by groupVersionKind
7685
informersByGVK map[schema.GroupVersionKind]*MapEntry
7786

78-
// unstructuredInformerByGVK is a cache of informers for unstructured types
79-
// keyed by groupVersionKind
80-
unstructuredInformerByGVK map[schema.GroupVersionKind]*MapEntry
81-
8287
// codecs is used to create a new REST client
8388
codecs serializer.CodecFactory
8489

@@ -93,20 +98,22 @@ type InformersMap struct {
9398

9499
// mu guards access to the map
95100
mu sync.RWMutex
96-
// mu guards access to the unstructured map
97-
unstructuredMu sync.RWMutex
98101

99102
// start is true if the informers have been started
100103
started bool
104+
105+
// createClient knows how to create a client and a list object,
106+
// and allows for abstracting over the particulars of structured vs
107+
// unstructured objects.
108+
createClient clientCreatorFunc
101109
}
102110

103111
// Start calls Run on each of the informers and sets started to true. Blocks on the stop channel.
104-
func (ip *InformersMap) Start(stop <-chan struct{}) error {
112+
// It doesn't return start because it can't return an error, and it's not a runnable directly.
113+
func (ip *specificInformersMap) Start(stop <-chan struct{}) {
105114
func() {
106115
ip.mu.Lock()
107-
ip.unstructuredMu.Lock()
108116
defer ip.mu.Unlock()
109-
defer ip.unstructuredMu.Unlock()
110117

111118
// Set the stop channel so it can be passed to informers that are added later
112119
ip.stop = stop
@@ -116,52 +123,31 @@ func (ip *InformersMap) Start(stop <-chan struct{}) error {
116123
go informer.Informer.Run(stop)
117124
}
118125

119-
// Start each unstructured informer
120-
for _, informer := range ip.unstructuredInformerByGVK {
121-
go informer.Informer.Run(stop)
122-
}
123-
124126
// Set started to true so we immediately start any informers added later.
125127
ip.started = true
126128
}()
127129
<-stop
128-
return nil
129130
}
130131

131-
// WaitForCacheSync waits until all the caches have been synced
132-
func (ip *InformersMap) WaitForCacheSync(stop <-chan struct{}) bool {
133-
syncedFuncs := make([]cache.InformerSynced, 0, len(ip.informersByGVK)+len(ip.unstructuredInformerByGVK))
132+
// HasSyncedFuncs returns all the HasSynced functions for the informers in this map.
133+
func (ip *specificInformersMap) HasSyncedFuncs() []cache.InformerSynced {
134+
syncedFuncs := make([]cache.InformerSynced, 0, len(ip.informersByGVK))
134135
for _, informer := range ip.informersByGVK {
135136
syncedFuncs = append(syncedFuncs, informer.Informer.HasSynced)
136137
}
137-
for _, informer := range ip.unstructuredInformerByGVK {
138-
syncedFuncs = append(syncedFuncs, informer.Informer.HasSynced)
139-
}
140-
return cache.WaitForCacheSync(stop, syncedFuncs...)
141-
}
142-
143-
func (ip *InformersMap) getMapEntry(gvk schema.GroupVersionKind, isUnstructured bool) (*MapEntry, bool) {
144-
if isUnstructured {
145-
ip.unstructuredMu.RLock()
146-
defer ip.unstructuredMu.RUnlock()
147-
i, ok := ip.unstructuredInformerByGVK[gvk]
148-
return i, ok
149-
}
150-
ip.mu.RLock()
151-
defer ip.mu.RUnlock()
152-
i, ok := ip.informersByGVK[gvk]
153-
return i, ok
154-
138+
return syncedFuncs
155139
}
156140

157-
// Get will create a new Informer and add it to the map of InformersMap if none exists. Returns
141+
// Get will create a new Informer and add it to the map of specificInformersMap if none exists. Returns
158142
// the Informer from the map.
159-
func (ip *InformersMap) Get(gvk schema.GroupVersionKind, obj runtime.Object) (*MapEntry, error) {
160-
_, isUnstructured := obj.(*unstructured.Unstructured)
161-
_, isUnstructuredList := obj.(*unstructured.UnstructuredList)
162-
isUnstructured = isUnstructured || isUnstructuredList
143+
func (ip *specificInformersMap) Get(gvk schema.GroupVersionKind, obj runtime.Object) (*MapEntry, error) {
163144
// Return the informer if it is found
164-
i, ok := ip.getMapEntry(gvk, isUnstructured)
145+
i, ok := func() (*MapEntry, bool) {
146+
ip.mu.RLock()
147+
defer ip.mu.RUnlock()
148+
i, ok := ip.informersByGVK[gvk]
149+
return i, ok
150+
}()
165151
if ok {
166152
return i, nil
167153
}
@@ -170,27 +156,21 @@ func (ip *InformersMap) Get(gvk schema.GroupVersionKind, obj runtime.Object) (*M
170156
// need to be locked
171157
var sync bool
172158
i, err := func() (*MapEntry, error) {
173-
var ok bool
174-
var i *MapEntry
175-
// Check the caches to see if we already have an Informer. If we do, return the Informer.
159+
ip.mu.Lock()
160+
defer ip.mu.Unlock()
161+
162+
// Check the cache to see if we already have an Informer. If we do, return the Informer.
176163
// This is for the case where 2 routines tried to get the informer when it wasn't in the map
177164
// so neither returned early, but the first one created it.
178-
if isUnstructured {
179-
ip.unstructuredMu.Lock()
180-
defer ip.unstructuredMu.Unlock()
181-
i, ok = ip.unstructuredInformerByGVK[gvk]
182-
} else {
183-
ip.mu.Lock()
184-
defer ip.mu.Unlock()
185-
i, ok = ip.informersByGVK[gvk]
186-
}
165+
var ok bool
166+
i, ok := ip.informersByGVK[gvk]
187167
if ok {
188168
return i, nil
189169
}
190170

191171
// Create a NewSharedIndexInformer and add it to the map.
192172
var lw *cache.ListWatch
193-
lw, err := ip.newListWatch(gvk, isUnstructured)
173+
lw, err := ip.newListWatch(gvk)
194174
if err != nil {
195175
return nil, err
196176
}
@@ -201,7 +181,7 @@ func (ip *InformersMap) Get(gvk schema.GroupVersionKind, obj runtime.Object) (*M
201181
Informer: ni,
202182
Reader: CacheReader{indexer: ni.GetIndexer(), groupVersionKind: gvk},
203183
}
204-
ip.setMap(i, gvk, isUnstructured)
184+
ip.informersByGVK[gvk] = i
205185

206186
// Start the Informer if need by
207187
// TODO(seans): write thorough tests and document what happens here - can you add indexers?
@@ -226,18 +206,8 @@ func (ip *InformersMap) Get(gvk schema.GroupVersionKind, obj runtime.Object) (*M
226206
return i, err
227207
}
228208

229-
// setMap - helper function to decide which map to add to.
230-
func (ip *InformersMap) setMap(i *MapEntry, gvk schema.GroupVersionKind, isUnstructured bool) {
231-
if isUnstructured {
232-
ip.unstructuredInformerByGVK[gvk] = i
233-
} else {
234-
235-
ip.informersByGVK[gvk] = i
236-
}
237-
}
238-
239209
// newListWatch returns a new ListWatch object that can be used to create a SharedIndexInformer.
240-
func (ip *InformersMap) newListWatch(gvk schema.GroupVersionKind, isUnstructured bool) (*cache.ListWatch, error) {
210+
func (ip *specificInformersMap) newListWatch(gvk schema.GroupVersionKind) (*cache.ListWatch, error) {
241211
// Kubernetes APIs work against Resources, not GroupVersionKinds. Map the
242212
// groupVersionKind to the Resource API we will use.
243213
mapping, err := ip.mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
@@ -246,25 +216,10 @@ func (ip *InformersMap) newListWatch(gvk schema.GroupVersionKind, isUnstructured
246216
}
247217

248218
// Construct a RESTClient for the groupVersionKind that we will use to
249-
// talk to the apiserver.
250-
var client rest.Interface
251-
var listObj runtime.Object
252-
if isUnstructured {
253-
listObj = &unstructured.UnstructuredList{}
254-
client, err = apiutil.RESTUnstructuredClientForGVK(gvk, ip.config)
255-
if err != nil {
256-
return nil, err
257-
}
258-
} else {
259-
client, err = apiutil.RESTClientForGVK(gvk, ip.config, ip.codecs)
260-
if err != nil {
261-
return nil, err
262-
}
263-
listGVK := gvk.GroupVersion().WithKind(gvk.Kind + "List")
264-
listObj, err = ip.Scheme.New(listGVK)
265-
if err != nil {
266-
return nil, err
267-
}
219+
// talk to the apiserver, and the list object that we'll use to describe our results.
220+
client, listObj, err := ip.createClient(gvk, ip.codecs, ip.Scheme, ip.config)
221+
if err != nil {
222+
return nil, err
268223
}
269224

270225
// Create a new ListWatch for the obj
@@ -282,3 +237,38 @@ func (ip *InformersMap) newListWatch(gvk schema.GroupVersionKind, isUnstructured
282237
},
283238
}, nil
284239
}
240+
241+
// createStructuredClient is a ClientCreatorFunc for use with structured
242+
// objects (i.e. not Unstructured/UnstructuredList).
243+
func createStructuredClient(gvk schema.GroupVersionKind,
244+
codecs serializer.CodecFactory,
245+
scheme *runtime.Scheme,
246+
baseConfig *rest.Config) (rest.Interface, runtime.Object, error) {
247+
248+
client, err := apiutil.RESTClientForGVK(gvk, baseConfig, codecs)
249+
if err != nil {
250+
return nil, nil, err
251+
}
252+
listGVK := gvk.GroupVersion().WithKind(gvk.Kind + "List")
253+
listObj, err := scheme.New(listGVK)
254+
if err != nil {
255+
return nil, nil, err
256+
}
257+
258+
return client, listObj, nil
259+
}
260+
261+
// createUnstructuredClient is a ClientCreatorFunc for use with Unstructured and UnstructuredList.
262+
func createUnstructuredClient(gvk schema.GroupVersionKind,
263+
_ serializer.CodecFactory,
264+
_ *runtime.Scheme,
265+
baseConfig *rest.Config) (rest.Interface, runtime.Object, error) {
266+
267+
listObj := &unstructured.UnstructuredList{}
268+
client, err := apiutil.RESTUnstructuredClientForGVK(gvk, baseConfig)
269+
if err != nil {
270+
return nil, nil, err
271+
}
272+
273+
return client, listObj, nil
274+
}

0 commit comments

Comments
 (0)