@@ -1962,38 +1962,45 @@ func (r *MySQLClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.
19621962 }
19631963 })
19641964
1965- configMapHandler := handler . EnqueueRequestsFromMapFunc ( func (ctx context.Context , a client. Object ) []reconcile.Request {
1965+ requestsForIndexedClusters := func (ctx context.Context , namespace , field , value string ) []reconcile.Request {
19661966 clusters := & mocov1beta2.MySQLClusterList {}
1967- if err := r .List (ctx , clusters , client .InNamespace (a . GetNamespace ()) ); err != nil {
1967+ if err := r .List (ctx , clusters , client .InNamespace (namespace ), client. MatchingFields { field : value } ); err != nil {
19681968 return nil
19691969 }
1970- var req []reconcile.Request
1970+
1971+ req := make ([]reconcile.Request , 0 , len (clusters .Items ))
19711972 for _ , c := range clusters .Items {
1972- if c .Spec .MySQLConfigMapName == nil {
1973- continue
1974- }
1975- if * c .Spec .MySQLConfigMapName == a .GetName () {
1976- req = append (req , reconcile.Request {NamespacedName : client .ObjectKeyFromObject (& c )})
1977- }
1973+ req = append (req , reconcile.Request {NamespacedName : client .ObjectKeyFromObject (& c )})
19781974 }
19791975 return req
1980- })
1976+ }
19811977
1982- backupPolicyHandler := handler . EnqueueRequestsFromMapFunc ( func (ctx context. Context , a client.Object ) []reconcile. Request {
1983- clusters := & mocov1beta2.MySQLClusterList {}
1984- if err := r . List ( ctx , clusters , client . InNamespace ( a . GetNamespace ())); err ! = nil {
1978+ if err := mgr . GetFieldIndexer (). IndexField (ctx , & mocov1beta2. MySQLCluster {}, "spec.mysqlConfigMapName" , func ( rawObj client.Object ) []string {
1979+ c := rawObj .( * mocov1beta2.MySQLCluster )
1980+ if c . Spec . MySQLConfigMapName = = nil {
19851981 return nil
19861982 }
1987- var req []reconcile.Request
1988- for _ , c := range clusters .Items {
1989- if c .Spec .BackupPolicyName == nil {
1990- continue
1991- }
1992- if * c .Spec .BackupPolicyName == a .GetName () {
1993- req = append (req , reconcile.Request {NamespacedName : client .ObjectKeyFromObject (& c )})
1994- }
1983+ return []string {* c .Spec .MySQLConfigMapName }
1984+ }); err != nil {
1985+ return fmt .Errorf ("failed to index MySQLCluster by spec.mysqlConfigMapName: %w" , err )
1986+ }
1987+
1988+ configMapHandler := handler .EnqueueRequestsFromMapFunc (func (ctx context.Context , a client.Object ) []reconcile.Request {
1989+ return requestsForIndexedClusters (ctx , a .GetNamespace (), "spec.mysqlConfigMapName" , a .GetName ())
1990+ })
1991+
1992+ if err := mgr .GetFieldIndexer ().IndexField (ctx , & mocov1beta2.MySQLCluster {}, "spec.backupPolicyName" , func (rawObj client.Object ) []string {
1993+ c := rawObj .(* mocov1beta2.MySQLCluster )
1994+ if c .Spec .BackupPolicyName == nil {
1995+ return nil
19951996 }
1996- return req
1997+ return []string {* c .Spec .BackupPolicyName }
1998+ }); err != nil {
1999+ return fmt .Errorf ("failed to index MySQLCluster by spec.backupPolicyName: %w" , err )
2000+ }
2001+
2002+ backupPolicyHandler := handler .EnqueueRequestsFromMapFunc (func (ctx context.Context , a client.Object ) []reconcile.Request {
2003+ return requestsForIndexedClusters (ctx , a .GetNamespace (), "spec.backupPolicyName" , a .GetName ())
19972004 })
19982005
19992006 return ctrl .NewControllerManagedBy (mgr ).
0 commit comments