@@ -143,20 +143,21 @@ var kubernetesNativeResources = map[schema.GroupVersionResource]sharedInformerFu
143
143
144
144
// NewDataGatherer constructs a new instance of the generic K8s data-gatherer for the provided
145
145
func (c * ConfigDynamic ) NewDataGatherer (ctx context.Context ) (datagatherer.DataGatherer , error ) {
146
- cl , err := NewDynamicClient (c .KubeConfigPath )
147
- if err != nil {
148
- return nil , err
149
- }
150
-
151
146
if isNativeResource (c .GroupVersionResource ) {
152
147
clientset , err := NewClientSet (c .KubeConfigPath )
153
148
if err != nil {
154
149
return nil , errors .WithStack (err )
155
150
}
151
+
156
152
return c .newDataGathererWithClient (ctx , nil , clientset )
157
- }
153
+ } else {
154
+ cl , err := NewDynamicClient (c .KubeConfigPath )
155
+ if err != nil {
156
+ return nil , err
157
+ }
158
158
159
- return c .newDataGathererWithClient (ctx , cl , nil )
159
+ return c .newDataGathererWithClient (ctx , cl , nil )
160
+ }
160
161
}
161
162
162
163
func (c * ConfigDynamic ) newDataGathererWithClient (ctx context.Context , cl dynamic.Interface , clientset kubernetes.Interface ) (datagatherer.DataGatherer , error ) {
@@ -178,8 +179,6 @@ func (c *ConfigDynamic) newDataGathererWithClient(ctx context.Context, cl dynami
178
179
179
180
newDataGatherer := & DataGathererDynamic {
180
181
ctx : ctx ,
181
- cl : cl ,
182
- k8sClientSet : clientset ,
183
182
groupVersionResource : c .GroupVersionResource ,
184
183
fieldSelector : fieldSelector .String (),
185
184
namespaces : c .IncludeNamespaces ,
@@ -200,34 +199,22 @@ func (c *ConfigDynamic) newDataGathererWithClient(ctx context.Context, cl dynami
200
199
informers .WithNamespace (metav1 .NamespaceAll ),
201
200
informers .WithTweakListOptions (func (options * metav1.ListOptions ) {
202
201
options .FieldSelector = fieldSelector .String ()
203
- }))
204
- newDataGatherer .nativeSharedInformer = factory
205
- informer := informerFunc (factory )
206
- informer .AddEventHandler (k8scache.ResourceEventHandlerFuncs {
207
- AddFunc : func (obj interface {}) {
208
- onAdd (obj , dgCache )
209
- },
210
- UpdateFunc : func (old , new interface {}) {
211
- onUpdate (old , new , dgCache )
212
- },
213
- DeleteFunc : func (obj interface {}) {
214
- onDelete (obj , dgCache )
202
+ }),
203
+ )
204
+ newDataGatherer .informer = informerFunc (factory )
205
+ } else {
206
+ factory := dynamicinformer .NewFilteredDynamicSharedInformerFactory (
207
+ cl ,
208
+ 60 * time .Second ,
209
+ metav1 .NamespaceAll ,
210
+ func (options * metav1.ListOptions ) {
211
+ options .FieldSelector = fieldSelector .String ()
215
212
},
216
- })
217
- newDataGatherer .informer = informer
218
- return newDataGatherer , nil
213
+ )
214
+ newDataGatherer .informer = factory .ForResource (c .GroupVersionResource ).Informer ()
219
215
}
220
216
221
- factory := dynamicinformer .NewFilteredDynamicSharedInformerFactory (
222
- cl ,
223
- 60 * time .Second ,
224
- metav1 .NamespaceAll ,
225
- func (options * metav1.ListOptions ) { options .FieldSelector = fieldSelector .String () },
226
- )
227
- resourceInformer := factory .ForResource (c .GroupVersionResource )
228
- informer := resourceInformer .Informer ()
229
- newDataGatherer .dynamicSharedInformer = factory
230
- informer .AddEventHandler (k8scache.ResourceEventHandlerFuncs {
217
+ registration , err := newDataGatherer .informer .AddEventHandler (k8scache.ResourceEventHandlerFuncs {
231
218
AddFunc : func (obj interface {}) {
232
219
onAdd (obj , dgCache )
233
220
},
@@ -238,7 +225,10 @@ func (c *ConfigDynamic) newDataGathererWithClient(ctx context.Context, cl dynami
238
225
onDelete (obj , dgCache )
239
226
},
240
227
})
241
- newDataGatherer .informer = informer
228
+ if err != nil {
229
+ return nil , err
230
+ }
231
+ newDataGatherer .registration = registration
242
232
243
233
return newDataGatherer , nil
244
234
}
@@ -251,10 +241,6 @@ func (c *ConfigDynamic) newDataGathererWithClient(ctx context.Context, cl dynami
251
241
// does not have registered as part of its `runtime.Scheme`.
252
242
type DataGathererDynamic struct {
253
243
ctx context.Context
254
- // The 'dynamic' client used for fetching data.
255
- cl dynamic.Interface
256
- // The k8s clientset used for fetching known resources.
257
- k8sClientSet kubernetes.Interface
258
244
// groupVersionResource is the name of the API group, version and resource
259
245
// that should be fetched by this data gatherer.
260
246
groupVersionResource schema.GroupVersionResource
@@ -270,19 +256,15 @@ type DataGathererDynamic struct {
270
256
// 30 seconds purge time https://pkg.go.dev/github.com/patrickmn/go-cache
271
257
cache * cache.Cache
272
258
// informer watches the events around the targeted resource and updates the cache
273
- informer k8scache.SharedIndexInformer
274
- dynamicSharedInformer dynamicinformer.DynamicSharedInformerFactory
275
- nativeSharedInformer informers.SharedInformerFactory
276
-
277
- // isInitialized is set to true when data is first collected, prior to
278
- // this the fetch method will return an error
279
- isInitialized bool
259
+ informer k8scache.SharedIndexInformer
260
+ registration k8scache.ResourceEventHandlerRegistration
280
261
}
281
262
282
263
// Run starts the dynamic data gatherer's informers for resource collection.
283
- // Returns error if the data gatherer informer wasn't initialized
264
+ // Returns error if the data gatherer informer wasn't initialized, Run blocks
265
+ // until the stopCh is closed.
284
266
func (g * DataGathererDynamic ) Run (stopCh <- chan struct {}) error {
285
- if g .dynamicSharedInformer == nil && g . nativeSharedInformer == nil {
267
+ if g .informer == nil {
286
268
return fmt .Errorf ("informer was not initialized, impossible to start" )
287
269
}
288
270
@@ -299,21 +281,15 @@ func (g *DataGathererDynamic) Run(stopCh <-chan struct{}) error {
299
281
}
300
282
301
283
// start shared informer
302
- if g .dynamicSharedInformer != nil {
303
- g .dynamicSharedInformer .Start (stopCh )
304
- }
305
-
306
- if g .nativeSharedInformer != nil {
307
- g .nativeSharedInformer .Start (stopCh )
308
- }
284
+ g .informer .Run (stopCh )
309
285
310
286
return nil
311
287
}
312
288
313
289
// WaitForCacheSync waits for the data gatherer's informers cache to sync
314
290
// before collecting the resources.
315
291
func (g * DataGathererDynamic ) WaitForCacheSync (stopCh <- chan struct {}) error {
316
- if ! k8scache .WaitForCacheSync (stopCh , g .informer .HasSynced ) {
292
+ if ! k8scache .WaitForCacheSync (stopCh , g .registration .HasSynced ) {
317
293
return fmt .Errorf ("timed out waiting for Kubernetes caches to sync" )
318
294
}
319
295
@@ -432,16 +408,6 @@ func redactList(list []*api.GatheredResource) error {
432
408
return nil
433
409
}
434
410
435
- // namespaceResourceInterface will 'namespace' a NamespaceableResourceInterface
436
- // if the 'namespace' parameter is non-empty, otherwise it will return the
437
- // given ResourceInterface as-is.
438
- func namespaceResourceInterface (iface dynamic.NamespaceableResourceInterface , namespace string ) dynamic.ResourceInterface {
439
- if namespace == "" {
440
- return iface
441
- }
442
- return iface .Namespace (namespace )
443
- }
444
-
445
411
// generateExcludedNamespacesFieldSelector creates a field selector string from
446
412
// a list of namespaces to exclude.
447
413
func generateExcludedNamespacesFieldSelector (excludeNamespaces []string ) fields.Selector {
0 commit comments