@@ -23,39 +23,35 @@ import (
23
23
24
24
"k8s.io/apimachinery/pkg/api/meta"
25
25
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
- "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
27
26
"k8s.io/apimachinery/pkg/runtime"
28
27
"k8s.io/apimachinery/pkg/runtime/schema"
29
28
"k8s.io/apimachinery/pkg/runtime/serializer"
30
29
"k8s.io/apimachinery/pkg/watch"
30
+ "k8s.io/client-go/dynamic"
31
31
"k8s.io/client-go/rest"
32
32
"k8s.io/client-go/tools/cache"
33
33
34
34
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
35
35
)
36
36
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 )
37
+ // clientListWatcherFunc knows how to create a ListWatcher
38
+ type createListWatcherFunc func (gvk schema.GroupVersionKind , ip * specificInformersMap ) (* cache.ListWatch , error )
43
39
44
40
// newSpecificInformersMap returns a new specificInformersMap (like
45
41
// the generical InformersMap, except that it doesn't implement WaitForCacheSync).
46
42
func newSpecificInformersMap (config * rest.Config ,
47
43
scheme * runtime.Scheme ,
48
44
mapper meta.RESTMapper ,
49
- resync time.Duration , createClient clientCreatorFunc ) * specificInformersMap {
45
+ resync time.Duration , createListWatcher createListWatcherFunc ) * specificInformersMap {
50
46
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 ,
47
+ config : config ,
48
+ Scheme : scheme ,
49
+ mapper : mapper ,
50
+ informersByGVK : make (map [schema.GroupVersionKind ]* MapEntry ),
51
+ codecs : serializer .NewCodecFactory (scheme ),
52
+ paramCodec : runtime .NewParameterCodec (scheme ),
53
+ resync : resync ,
54
+ createListWatcher : createListWatcher ,
59
55
}
60
56
return ip
61
57
}
@@ -105,7 +101,7 @@ type specificInformersMap struct {
105
101
// createClient knows how to create a client and a list object,
106
102
// and allows for abstracting over the particulars of structured vs
107
103
// unstructured objects.
108
- createClient clientCreatorFunc
104
+ createListWatcher createListWatcherFunc
109
105
}
110
106
111
107
// Start calls Run on each of the informers and sets started to true. Blocks on the stop channel.
@@ -170,7 +166,7 @@ func (ip *specificInformersMap) Get(gvk schema.GroupVersionKind, obj runtime.Obj
170
166
171
167
// Create a NewSharedIndexInformer and add it to the map.
172
168
var lw * cache.ListWatch
173
- lw , err := ip .newListWatch (gvk )
169
+ lw , err := ip .createListWatcher (gvk , ip )
174
170
if err != nil {
175
171
return nil , err
176
172
}
@@ -207,17 +203,20 @@ func (ip *specificInformersMap) Get(gvk schema.GroupVersionKind, obj runtime.Obj
207
203
}
208
204
209
205
// newListWatch returns a new ListWatch object that can be used to create a SharedIndexInformer.
210
- func ( ip * specificInformersMap ) newListWatch ( gvk schema.GroupVersionKind ) (* cache.ListWatch , error ) {
206
+ func createStructuredListWatch ( gvk schema.GroupVersionKind , ip * specificInformersMap ) (* cache.ListWatch , error ) {
211
207
// Kubernetes APIs work against Resources, not GroupVersionKinds. Map the
212
208
// groupVersionKind to the Resource API we will use.
213
209
mapping , err := ip .mapper .RESTMapping (gvk .GroupKind (), gvk .Version )
214
210
if err != nil {
215
211
return nil , err
216
212
}
217
213
218
- // Construct a RESTClient for the groupVersionKind that we will use to
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 )
214
+ client , err := apiutil .RESTClientForGVK (gvk , ip .config , ip .codecs )
215
+ if err != nil {
216
+ return nil , err
217
+ }
218
+ listGVK := gvk .GroupVersion ().WithKind (gvk .Kind + "List" )
219
+ listObj , err := ip .Scheme .New (listGVK )
221
220
if err != nil {
222
221
return nil , err
223
222
}
@@ -238,37 +237,28 @@ func (ip *specificInformersMap) newListWatch(gvk schema.GroupVersionKind) (*cach
238
237
}, nil
239
238
}
240
239
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 )
240
+ func createUnstructuredListWatch (gvk schema.GroupVersionKind , ip * specificInformersMap ) (* cache.ListWatch , error ) {
241
+ // Kubernetes APIs work against Resources, not GroupVersionKinds. Map the
242
+ // groupVersionKind to the Resource API we will use.
243
+ mapping , err := ip .mapper .RESTMapping (gvk .GroupKind (), gvk .Version )
249
244
if err != nil {
250
- return nil , nil , err
245
+ return nil , err
251
246
}
252
- listGVK := gvk .GroupVersion ().WithKind (gvk .Kind + "List" )
253
- listObj , err := scheme .New (listGVK )
247
+ dynamicClient , err := dynamic .NewForConfig (ip .config )
254
248
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
249
+ return nil , err
271
250
}
272
251
273
- return client , listObj , nil
252
+ // Create a new ListWatch for the obj
253
+ return & cache.ListWatch {
254
+ ListFunc : func (opts metav1.ListOptions ) (runtime.Object , error ) {
255
+ return dynamicClient .Resource (mapping .Resource ).List (opts )
256
+ },
257
+ // Setup the watch function
258
+ WatchFunc : func (opts metav1.ListOptions ) (watch.Interface , error ) {
259
+ // Watch needs to be set to true separately
260
+ opts .Watch = true
261
+ return dynamicClient .Resource (mapping .Resource ).Watch (opts )
262
+ },
263
+ }, nil
274
264
}
0 commit comments