Skip to content

Commit 8a7544f

Browse files
DirectXMan12Shawn Hurley
authored andcommitted
[wip] refactor structure details out of informer cache.
1 parent 0ebe461 commit 8a7544f

File tree

2 files changed

+175
-101
lines changed

2 files changed

+175
-101
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+
func (m *InformersMap) WaitForCacheSync(stop <-chan struct{}) bool {
66+
syncedFuncs := append([]cache.InformerSynced(nil), m.structured.HasSyncedFuncs()...)
67+
syncedFuncs = append(syncedFuncs, m.unstructured.HasSyncedFuncs()...)
68+
69+
return cache.WaitForCacheSync(stop, syncedFuncs...)
70+
}
71+
72+
// WaitForCacheSync waits until all the caches have been synced
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: 80 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,29 @@ 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, codecs serializer.CodecFactory, scheme *runtime.Scheme, baseConfig *rest.Config) (client rest.Interface, listObj runtime.Object, err error)
40+
41+
// newSpecificInformersMap returns a new specificInformersMap (like
42+
// the generical InformersMap, except that it doesn't implement WaitForCacheSync).
43+
func newSpecificInformersMap(config *rest.Config,
3844
scheme *runtime.Scheme,
3945
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,
46+
resync time.Duration, createClient clientCreatorFunc) *specificInformersMap {
47+
ip := &specificInformersMap{
48+
config: config,
49+
Scheme: scheme,
50+
mapper: mapper,
51+
informersByGVK: make(map[schema.GroupVersionKind]*MapEntry),
52+
codecs: serializer.NewCodecFactory(scheme),
53+
paramCodec: runtime.NewParameterCodec(scheme),
54+
resync: resync,
55+
createClient: createClient,
5056
}
5157
return ip
5258
}
@@ -60,9 +66,9 @@ type MapEntry struct {
6066
Reader CacheReader
6167
}
6268

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 {
69+
// specificInformersMap create and caches Informers for (runtime.Object, schema.GroupVersionKind) pairs.
70+
// It uses a standard parameter codec constructed based on the given generated Scheme.
71+
type specificInformersMap struct {
6672
// Scheme maps runtime.Objects to GroupVersionKinds
6773
Scheme *runtime.Scheme
6874

@@ -75,10 +81,6 @@ type InformersMap struct {
7581
// informersByGVK is the cache of informers keyed by groupVersionKind
7682
informersByGVK map[schema.GroupVersionKind]*MapEntry
7783

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

@@ -93,20 +95,22 @@ type InformersMap struct {
9395

9496
// mu guards access to the map
9597
mu sync.RWMutex
96-
// mu guards access to the unstructured map
97-
unstructuredMu sync.RWMutex
9898

9999
// start is true if the informers have been started
100100
started bool
101+
102+
// createClient knows how to create a client and a list object,
103+
// and allows for abstracting over the particulars of structured vs
104+
// unstructured objects.
105+
createClient clientCreatorFunc
101106
}
102107

103108
// 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 {
109+
// It doesn't return start because it can't return an error, and it's not a runnable directly.
110+
func (ip *specificInformersMap) Start(stop <-chan struct{}) {
105111
func() {
106112
ip.mu.Lock()
107-
ip.unstructuredMu.Lock()
108113
defer ip.mu.Unlock()
109-
defer ip.unstructuredMu.Unlock()
110114

111115
// Set the stop channel so it can be passed to informers that are added later
112116
ip.stop = stop
@@ -116,52 +120,31 @@ func (ip *InformersMap) Start(stop <-chan struct{}) error {
116120
go informer.Informer.Run(stop)
117121
}
118122

119-
// Start each unstructured informer
120-
for _, informer := range ip.unstructuredInformerByGVK {
121-
go informer.Informer.Run(stop)
122-
}
123-
124123
// Set started to true so we immediately start any informers added later.
125124
ip.started = true
126125
}()
127126
<-stop
128-
return nil
129127
}
130128

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))
129+
// HasSyncedFuncs returns all the HasSynced functions for the informers in this map.
130+
func (ip *specificInformersMap) HasSyncedFuncs() []cache.InformerSynced {
131+
syncedFuncs := make([]cache.InformerSynced, 0, len(ip.informersByGVK))
134132
for _, informer := range ip.informersByGVK {
135133
syncedFuncs = append(syncedFuncs, informer.Informer.HasSynced)
136134
}
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-
135+
return syncedFuncs
155136
}
156137

157-
// Get will create a new Informer and add it to the map of InformersMap if none exists. Returns
138+
// Get will create a new Informer and add it to the map of specificInformersMap if none exists. Returns
158139
// 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
140+
func (ip *specificInformersMap) Get(gvk schema.GroupVersionKind, obj runtime.Object) (*MapEntry, error) {
163141
// Return the informer if it is found
164-
i, ok := ip.getMapEntry(gvk, isUnstructured)
142+
i, ok := func() (*MapEntry, bool) {
143+
ip.mu.RLock()
144+
defer ip.mu.RUnlock()
145+
i, ok := ip.informersByGVK[gvk]
146+
return i, ok
147+
}()
165148
if ok {
166149
return i, nil
167150
}
@@ -170,27 +153,21 @@ func (ip *InformersMap) Get(gvk schema.GroupVersionKind, obj runtime.Object) (*M
170153
// need to be locked
171154
var sync bool
172155
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.
156+
ip.mu.Lock()
157+
defer ip.mu.Unlock()
158+
159+
// Check the cache to see if we already have an Informer. If we do, return the Informer.
176160
// This is for the case where 2 routines tried to get the informer when it wasn't in the map
177161
// 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-
}
162+
var ok bool
163+
i, ok := ip.informersByGVK[gvk]
187164
if ok {
188165
return i, nil
189166
}
190167

191168
// Create a NewSharedIndexInformer and add it to the map.
192169
var lw *cache.ListWatch
193-
lw, err := ip.newListWatch(gvk, isUnstructured)
170+
lw, err := ip.newListWatch(gvk)
194171
if err != nil {
195172
return nil, err
196173
}
@@ -201,7 +178,7 @@ func (ip *InformersMap) Get(gvk schema.GroupVersionKind, obj runtime.Object) (*M
201178
Informer: ni,
202179
Reader: CacheReader{indexer: ni.GetIndexer(), groupVersionKind: gvk},
203180
}
204-
ip.setMap(i, gvk, isUnstructured)
181+
ip.informersByGVK[gvk] = i
205182

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

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-
239206
// 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) {
207+
func (ip *specificInformersMap) newListWatch(gvk schema.GroupVersionKind) (*cache.ListWatch, error) {
241208
// Kubernetes APIs work against Resources, not GroupVersionKinds. Map the
242209
// groupVersionKind to the Resource API we will use.
243210
mapping, err := ip.mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
@@ -246,25 +213,10 @@ func (ip *InformersMap) newListWatch(gvk schema.GroupVersionKind, isUnstructured
246213
}
247214

248215
// 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-
}
216+
// talk to the apiserver, and the list object that we'll use to describe our results.
217+
client, listObj, err := ip.createClient(gvk, ip.codecs, ip.Scheme, ip.config)
218+
if err != nil {
219+
return nil, err
268220
}
269221

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

0 commit comments

Comments
 (0)