@@ -143,20 +143,21 @@ var kubernetesNativeResources = map[schema.GroupVersionResource]sharedInformerFu
143143
144144// NewDataGatherer constructs a new instance of the generic K8s data-gatherer for the provided
145145func (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-
151146 if isNativeResource (c .GroupVersionResource ) {
152147 clientset , err := NewClientSet (c .KubeConfigPath )
153148 if err != nil {
154149 return nil , errors .WithStack (err )
155150 }
151+
156152 return c .newDataGathererWithClient (ctx , nil , clientset )
157- }
153+ } else {
154+ cl , err := NewDynamicClient (c .KubeConfigPath )
155+ if err != nil {
156+ return nil , err
157+ }
158158
159- return c .newDataGathererWithClient (ctx , cl , nil )
159+ return c .newDataGathererWithClient (ctx , cl , nil )
160+ }
160161}
161162
162163func (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
178179
179180 newDataGatherer := & DataGathererDynamic {
180181 ctx : ctx ,
181- cl : cl ,
182- k8sClientSet : clientset ,
183182 groupVersionResource : c .GroupVersionResource ,
184183 fieldSelector : fieldSelector .String (),
185184 namespaces : c .IncludeNamespaces ,
@@ -200,34 +199,22 @@ func (c *ConfigDynamic) newDataGathererWithClient(ctx context.Context, cl dynami
200199 informers .WithNamespace (metav1 .NamespaceAll ),
201200 informers .WithTweakListOptions (func (options * metav1.ListOptions ) {
202201 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 ()
215212 },
216- })
217- newDataGatherer .informer = informer
218- return newDataGatherer , nil
213+ )
214+ newDataGatherer .informer = factory .ForResource (c .GroupVersionResource ).Informer ()
219215 }
220216
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 {
231218 AddFunc : func (obj interface {}) {
232219 onAdd (obj , dgCache )
233220 },
@@ -238,7 +225,10 @@ func (c *ConfigDynamic) newDataGathererWithClient(ctx context.Context, cl dynami
238225 onDelete (obj , dgCache )
239226 },
240227 })
241- newDataGatherer .informer = informer
228+ if err != nil {
229+ return nil , err
230+ }
231+ newDataGatherer .registration = registration
242232
243233 return newDataGatherer , nil
244234}
@@ -251,10 +241,6 @@ func (c *ConfigDynamic) newDataGathererWithClient(ctx context.Context, cl dynami
251241// does not have registered as part of its `runtime.Scheme`.
252242type DataGathererDynamic struct {
253243 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
258244 // groupVersionResource is the name of the API group, version and resource
259245 // that should be fetched by this data gatherer.
260246 groupVersionResource schema.GroupVersionResource
@@ -270,19 +256,15 @@ type DataGathererDynamic struct {
270256 // 30 seconds purge time https://pkg.go.dev/github.com/patrickmn/go-cache
271257 cache * cache.Cache
272258 // 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
280261}
281262
282263// 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.
284266func (g * DataGathererDynamic ) Run (stopCh <- chan struct {}) error {
285- if g .dynamicSharedInformer == nil && g . nativeSharedInformer == nil {
267+ if g .informer == nil {
286268 return fmt .Errorf ("informer was not initialized, impossible to start" )
287269 }
288270
@@ -299,21 +281,15 @@ func (g *DataGathererDynamic) Run(stopCh <-chan struct{}) error {
299281 }
300282
301283 // 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 )
309285
310286 return nil
311287}
312288
313289// WaitForCacheSync waits for the data gatherer's informers cache to sync
314290// before collecting the resources.
315291func (g * DataGathererDynamic ) WaitForCacheSync (stopCh <- chan struct {}) error {
316- if ! k8scache .WaitForCacheSync (stopCh , g .informer .HasSynced ) {
292+ if ! k8scache .WaitForCacheSync (stopCh , g .registration .HasSynced ) {
317293 return fmt .Errorf ("timed out waiting for Kubernetes caches to sync" )
318294 }
319295
@@ -432,16 +408,6 @@ func redactList(list []*api.GatheredResource) error {
432408 return nil
433409}
434410
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-
445411// generateExcludedNamespacesFieldSelector creates a field selector string from
446412// a list of namespaces to exclude.
447413func generateExcludedNamespacesFieldSelector (excludeNamespaces []string ) fields.Selector {
0 commit comments