@@ -61,6 +61,10 @@ const (
6161 maxReferenceDepthReached = "MaxReferenceDepthReached"
6262 selfReference = "SelfReference"
6363 crossNamespaceReference = "CrossNamespaceReference"
64+
65+ dataSourcePvcField = "spec.source.pvc"
66+ dataSourceSnapshotField = "spec.source.snapshot"
67+ dataSourceDataSourceField = "spec.source.dataSource"
6468)
6569
6670// Reconcile loop for DataSourceReconciler
@@ -237,55 +241,20 @@ func NewDataSourceController(mgr manager.Manager, log logr.Logger, installerLabe
237241}
238242
239243func addDataSourceControllerWatches (mgr manager.Manager , c controller.Controller , log logr.Logger ) error {
240- const dataSourcePvcField = "spec.source.pvc"
241- const dataSourceSnapshotField = "spec.source.snapshot"
242- const dataSourceDataSourceField = "spec.source.dataSource"
243-
244- getKey := func (namespace , name string ) string {
245- return namespace + "/" + name
246- }
247-
248- appendMatchingDataSourceRequests := func (ctx context.Context , indexingKey string , obj client.Object , reqs []reconcile.Request ) []reconcile.Request {
249- var dataSources cdiv1.DataSourceList
250- matchingFields := client.MatchingFields {indexingKey : getKey (obj .GetNamespace (), obj .GetName ())}
251- if err := mgr .GetClient ().List (ctx , & dataSources , matchingFields ); err != nil {
252- log .Error (err , "Unable to list DataSources" , "matchingFields" , matchingFields )
253- return reqs
254- }
255- for _ , ds := range dataSources .Items {
256- reqs = append (reqs , reconcile.Request {NamespacedName : types.NamespacedName {Namespace : ds .Namespace , Name : ds .Name }})
257- }
258- return reqs
244+ if err := setupIndexers (mgr ); err != nil {
245+ return err
259246 }
260-
261- if err := c .Watch (source .Kind (mgr .GetCache (), & cdiv1.DataSource {},
262- handler.TypedEnqueueRequestsFromMapFunc [* cdiv1.DataSource ](func (ctx context.Context , obj * cdiv1.DataSource ) []reconcile.Request {
263- reqs := []reconcile.Request {
264- {
265- NamespacedName : types.NamespacedName {
266- Name : obj .Name ,
267- Namespace : obj .Namespace ,
268- },
269- },
270- }
271- return appendMatchingDataSourceRequests (ctx , dataSourceDataSourceField , obj , reqs )
272- }),
273- predicate.TypedFuncs [* cdiv1.DataSource ]{
274- CreateFunc : func (e event.TypedCreateEvent [* cdiv1.DataSource ]) bool { return true },
275- DeleteFunc : func (e event.TypedDeleteEvent [* cdiv1.DataSource ]) bool { return true },
276- UpdateFunc : func (e event.TypedUpdateEvent [* cdiv1.DataSource ]) bool {
277- return ! sameSourceSpec (e .ObjectOld , e .ObjectNew ) ||
278- ! sameConditions (e .ObjectOld , e .ObjectNew )
279- },
280- },
281- )); err != nil {
247+ if err := setupWatches (mgr , c , log ); err != nil {
282248 return err
283249 }
250+ return nil
251+ }
284252
253+ func setupIndexers (mgr manager.Manager ) error {
285254 if err := mgr .GetFieldIndexer ().IndexField (context .TODO (), & cdiv1.DataSource {}, dataSourcePvcField , func (obj client.Object ) []string {
286255 if pvc := obj .(* cdiv1.DataSource ).Spec .Source .PVC ; pvc != nil {
287256 ns := cc .GetNamespace (pvc .Namespace , obj .GetNamespace ())
288- return []string {getKey ( ns , pvc .Name )}
257+ return []string {types. NamespacedName { Name : pvc .Name , Namespace : ns }. String ( )}
289258 }
290259 return nil
291260 }); err != nil {
@@ -295,32 +264,54 @@ func addDataSourceControllerWatches(mgr manager.Manager, c controller.Controller
295264 if err := mgr .GetFieldIndexer ().IndexField (context .TODO (), & cdiv1.DataSource {}, dataSourceSnapshotField , func (obj client.Object ) []string {
296265 if snapshot := obj .(* cdiv1.DataSource ).Spec .Source .Snapshot ; snapshot != nil {
297266 ns := cc .GetNamespace (snapshot .Namespace , obj .GetNamespace ())
298- return []string {getKey ( ns , snapshot .Name )}
267+ return []string {types. NamespacedName { Name : snapshot .Name , Namespace : ns }. String ( )}
299268 }
300269 return nil
301270 }); err != nil {
302271 return err
303272 }
304273
305274 if err := mgr .GetFieldIndexer ().IndexField (context .TODO (), & cdiv1.DataSource {}, dataSourceDataSourceField , func (obj client.Object ) []string {
306- ds := obj .(* cdiv1.DataSource )
307- if sourceDS := ds .Spec .Source .DataSource ; sourceDS != nil {
308- ns := cc .GetNamespace (sourceDS .Namespace , ds .GetNamespace ())
309- return []string {getKey (ns , sourceDS .Name )}
275+ if sourceDS := obj .(* cdiv1.DataSource ).Spec .Source .DataSource ; sourceDS != nil {
276+ ns := cc .GetNamespace (sourceDS .Namespace , obj .GetNamespace ())
277+ return []string {types.NamespacedName {Name : sourceDS .Name , Namespace : ns }.String ()}
310278 }
311279 return nil
312280 }); err != nil {
313281 return err
314282 }
315283
316- mapToDataSource := func (ctx context.Context , obj client.Object ) []reconcile.Request {
317- reqs := appendMatchingDataSourceRequests (ctx , dataSourcePvcField , obj , nil )
318- return appendMatchingDataSourceRequests (ctx , dataSourceSnapshotField , obj , reqs )
284+ return nil
285+ }
286+
287+ func setupWatches (mgr manager.Manager , c controller.Controller , log logr.Logger ) error {
288+ if err := c .Watch (source .Kind (mgr .GetCache (), & cdiv1.DataSource {},
289+ handler .TypedEnqueueRequestsFromMapFunc (func (ctx context.Context , obj * cdiv1.DataSource ) []reconcile.Request {
290+ reqs := []reconcile.Request {
291+ {
292+ NamespacedName : types.NamespacedName {
293+ Name : obj .Name ,
294+ Namespace : obj .Namespace ,
295+ },
296+ },
297+ }
298+ return appendMatchingDataSourceRequests (ctx , mgr , dataSourceDataSourceField , obj , reqs , log )
299+ }),
300+ predicate.TypedFuncs [* cdiv1.DataSource ]{
301+ CreateFunc : func (e event.TypedCreateEvent [* cdiv1.DataSource ]) bool { return true },
302+ DeleteFunc : func (e event.TypedDeleteEvent [* cdiv1.DataSource ]) bool { return true },
303+ UpdateFunc : func (e event.TypedUpdateEvent [* cdiv1.DataSource ]) bool {
304+ return ! sameSourceSpec (e .ObjectOld , e .ObjectNew ) ||
305+ ! sameConditions (e .ObjectOld , e .ObjectNew )
306+ },
307+ },
308+ )); err != nil {
309+ return err
319310 }
320311
321312 if err := c .Watch (source .Kind (mgr .GetCache (), & cdiv1.DataVolume {},
322- handler.TypedEnqueueRequestsFromMapFunc [ * cdiv1. DataVolume ] (func (ctx context.Context , obj * cdiv1.DataVolume ) []reconcile.Request {
323- return mapToDataSource (ctx , obj )
313+ handler .TypedEnqueueRequestsFromMapFunc (func (ctx context.Context , obj * cdiv1.DataVolume ) []reconcile.Request {
314+ return mapToDataSource (ctx , mgr , obj , log )
324315 }),
325316 predicate.TypedFuncs [* cdiv1.DataVolume ]{
326317 CreateFunc : func (e event.TypedCreateEvent [* cdiv1.DataVolume ]) bool { return true },
@@ -336,8 +327,8 @@ func addDataSourceControllerWatches(mgr manager.Manager, c controller.Controller
336327 }
337328
338329 if err := c .Watch (source .Kind (mgr .GetCache (), & corev1.PersistentVolumeClaim {},
339- handler.TypedEnqueueRequestsFromMapFunc [ * corev1. PersistentVolumeClaim ] (func (ctx context.Context , obj * corev1.PersistentVolumeClaim ) []reconcile.Request {
340- return mapToDataSource (ctx , obj )
330+ handler .TypedEnqueueRequestsFromMapFunc (func (ctx context.Context , obj * corev1.PersistentVolumeClaim ) []reconcile.Request {
331+ return mapToDataSource (ctx , mgr , obj , log )
341332 }),
342333 predicate.TypedFuncs [* corev1.PersistentVolumeClaim ]{
343334 CreateFunc : func (e event.TypedCreateEvent [* corev1.PersistentVolumeClaim ]) bool { return true },
@@ -361,8 +352,8 @@ func addDataSourceControllerWatches(mgr manager.Manager, c controller.Controller
361352 }
362353 }
363354 if err := c .Watch (source .Kind (mgr .GetCache (), & snapshotv1.VolumeSnapshot {},
364- handler.TypedEnqueueRequestsFromMapFunc [ * snapshotv1. VolumeSnapshot ] (func (ctx context.Context , obj * snapshotv1.VolumeSnapshot ) []reconcile.Request {
365- return mapToDataSource (ctx , obj )
355+ handler .TypedEnqueueRequestsFromMapFunc (func (ctx context.Context , obj * snapshotv1.VolumeSnapshot ) []reconcile.Request {
356+ return mapToDataSource (ctx , mgr , obj , log )
366357 }),
367358 predicate.TypedFuncs [* snapshotv1.VolumeSnapshot ]{
368359 CreateFunc : func (e event.TypedCreateEvent [* snapshotv1.VolumeSnapshot ]) bool { return true },
@@ -379,6 +370,24 @@ func addDataSourceControllerWatches(mgr manager.Manager, c controller.Controller
379370 return nil
380371}
381372
373+ func appendMatchingDataSourceRequests (ctx context.Context , mgr manager.Manager , indexingKey string , obj client.Object , reqs []reconcile.Request , log logr.Logger ) []reconcile.Request {
374+ var dataSources cdiv1.DataSourceList
375+ matchingFields := client.MatchingFields {indexingKey : client .ObjectKeyFromObject (obj ).String ()}
376+ if err := mgr .GetClient ().List (ctx , & dataSources , matchingFields ); err != nil {
377+ log .Error (err , "Unable to list DataSources" , "matchingFields" , matchingFields )
378+ return reqs
379+ }
380+ for _ , ds := range dataSources .Items {
381+ reqs = append (reqs , reconcile.Request {NamespacedName : types.NamespacedName {Namespace : ds .Namespace , Name : ds .Name }})
382+ }
383+ return reqs
384+ }
385+
386+ func mapToDataSource (ctx context.Context , mgr manager.Manager , obj client.Object , log logr.Logger ) []reconcile.Request {
387+ reqs := appendMatchingDataSourceRequests (ctx , mgr , dataSourcePvcField , obj , nil , log )
388+ return appendMatchingDataSourceRequests (ctx , mgr , dataSourceSnapshotField , obj , reqs , log )
389+ }
390+
382391func sameSourceSpec (objOld , objNew client.Object ) bool {
383392 dsOld , okOld := objOld .(* cdiv1.DataSource )
384393 dsNew , okNew := objNew .(* cdiv1.DataSource )
0 commit comments