@@ -27,6 +27,8 @@ import (
2727 ctrl "sigs.k8s.io/controller-runtime"
2828 "sigs.k8s.io/controller-runtime/pkg/client"
2929 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
30+ "sigs.k8s.io/controller-runtime/pkg/handler"
31+ "sigs.k8s.io/controller-runtime/pkg/reconcile"
3032
3133 condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition"
3234 helper "github.com/openstack-k8s-operators/lib-common/modules/common/helper"
@@ -145,7 +147,7 @@ func (r *MariaDBDatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Requ
145147
146148 // here we know that Galera exists so add a finalizer to ourselves and to the db CR. Before this point there is no reason to have a finalizer on ourselves as nothing to cleanup.
147149 if instance .DeletionTimestamp .IsZero () || isNewInstance { // this condition can be removed if you wish as it is always true at this point otherwise we would returned earlier.
148- if controllerutil .AddFinalizer (dbGalera , fmt .Sprintf ("%s-%s" , helper .GetFinalizer (), instance .Name )) {
150+ if dbGalera . DeletionTimestamp . IsZero () && controllerutil .AddFinalizer (dbGalera , fmt .Sprintf ("%s-%s" , helper .GetFinalizer (), instance .Name )) {
149151 err := r .Update (ctx , dbGalera )
150152 if err != nil {
151153 return ctrl.Result {}, err
@@ -245,8 +247,42 @@ func (r *MariaDBDatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Requ
245247
246248// SetupWithManager -
247249func (r * MariaDBDatabaseReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
250+ updateStatusFn := func (ctx context.Context , o client.Object ) []reconcile.Request {
251+ log := GetLog (ctx , "MariaDBDatabase" )
252+
253+ result := []reconcile.Request {}
254+
255+ mariaDBDatabases := & databasev1beta1.MariaDBDatabaseList {}
256+
257+ listOpts := []client.ListOption {
258+ client .InNamespace (o .GetNamespace ()),
259+ }
260+ if err := r .Client .List (ctx , mariaDBDatabases , listOpts ... ); err != nil {
261+ log .Error (err , "Unable to retrieve MariaDBDatabase CRs %w" )
262+ return nil
263+ }
264+
265+ for _ , cr := range mariaDBDatabases .Items {
266+
267+ if o .GetName () == cr .GetLabels ()["dbName" ] {
268+ name := client.ObjectKey {
269+ Namespace : o .GetNamespace (),
270+ Name : cr .Name ,
271+ }
272+ log .Info (fmt .Sprintf ("Galera %s is used by MariaDBDatabase CR %s" , o .GetName (), cr .Name ))
273+ result = append (result , reconcile.Request {NamespacedName : name })
274+ }
275+ }
276+
277+ if len (result ) > 0 {
278+ return result
279+ }
280+ return nil
281+ }
282+
248283 return ctrl .NewControllerManagedBy (mgr ).
249284 For (& databasev1beta1.MariaDBDatabase {}).
285+ Watches (& databasev1beta1.Galera {}, handler .EnqueueRequestsFromMapFunc (updateStatusFn )).
250286 Complete (r )
251287}
252288
0 commit comments