@@ -34,7 +34,6 @@ import (
3434
3535 k8s_errors "k8s.io/apimachinery/pkg/api/errors"
3636 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
37- "k8s.io/apimachinery/pkg/types"
3837 ctrl "sigs.k8s.io/controller-runtime"
3938 "sigs.k8s.io/controller-runtime/pkg/client"
4039 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -362,7 +361,6 @@ func (d *Database) loadDatabaseAndAccountCRs(
362361 ctx context.Context ,
363362 h * helper.Helper ,
364363) error {
365- mariaDBDatabase := & MariaDBDatabase {}
366364 name := d .name
367365 namespace := d .namespace
368366 accountName := d .accountName
@@ -384,13 +382,7 @@ func (d *Database) loadDatabaseAndAccountCRs(
384382 )
385383 }
386384
387- err := h .GetClient ().Get (
388- ctx ,
389- types.NamespacedName {
390- Name : name ,
391- Namespace : namespace ,
392- },
393- mariaDBDatabase )
385+ mariaDBDatabase , err := GetDatabase (ctx , h , name , namespace )
394386
395387 if err != nil {
396388 if k8s_errors .IsNotFound (err ) {
@@ -532,6 +524,71 @@ func (d *Database) GetDatabaseClientConfig(s *tls.Service) string {
532524 return strings .Join (conn , "\n " )
533525}
534526
527+ // DeleteDatabaseAndAccountFinalizers performs the same tasks as
528+ // GetDatabaseByNameAndAccount and then database.DeleteFinalizer, but does
529+ // so such that all individual objects that exist are guaranteed to be updated,
530+ // even if other objects in the Database combination don't exist. This
531+ // includes the MariaDBDatabase, all MariaDBAccount objects and their
532+ // associated Secret objects.
533+ func DeleteDatabaseAndAccountFinalizers (
534+ ctx context.Context ,
535+ h * helper.Helper ,
536+ name string ,
537+ accountName string ,
538+ namespace string ,
539+ ) error {
540+
541+ databaseAccount , err := GetAccount (ctx , h , accountName , namespace )
542+ if err != nil && ! k8s_errors .IsNotFound (err ) {
543+ return err
544+ } else if err == nil {
545+ if databaseAccount .Spec .Secret != "" {
546+ dbSecret , _ , err := secret .GetSecret (ctx , h , databaseAccount .Spec .Secret , namespace )
547+ if err != nil && ! k8s_errors .IsNotFound (err ) {
548+ return err
549+ }
550+
551+ if err == nil && controllerutil .RemoveFinalizer (dbSecret , h .GetFinalizer ()) {
552+ err := h .GetClient ().Update (ctx , dbSecret )
553+ if err != nil && ! k8s_errors .IsNotFound (err ) {
554+ return err
555+ }
556+ util .LogForObject (h , fmt .Sprintf ("Removed finalizer %s from Secret %s" , h .GetFinalizer (), dbSecret .Name ), dbSecret )
557+ }
558+ }
559+
560+ if controllerutil .RemoveFinalizer (databaseAccount , h .GetFinalizer ()) {
561+ err := h .GetClient ().Update (ctx , databaseAccount )
562+ if err != nil && ! k8s_errors .IsNotFound (err ) {
563+ return err
564+ }
565+ util .LogForObject (h , fmt .Sprintf ("Removed finalizer %s from MariaDBAccount %s" , h .GetFinalizer (), databaseAccount .Name ), databaseAccount )
566+ }
567+ }
568+
569+ // also do a delete for "unused" MariaDBAccounts, associated with
570+ // this MariaDBDatabase.
571+ err = DeleteUnusedMariaDBAccountFinalizers (
572+ ctx , h , name , accountName , namespace ,
573+ )
574+ if err != nil && ! k8s_errors .IsNotFound (err ) {
575+ return err
576+ }
577+
578+ mariaDBDatabase , err := GetDatabase (ctx , h , name , namespace )
579+ if err != nil && ! k8s_errors .IsNotFound (err ) {
580+ return err
581+ } else if err == nil && controllerutil .RemoveFinalizer (mariaDBDatabase , h .GetFinalizer ()) {
582+ err := h .GetClient ().Update (ctx , mariaDBDatabase )
583+ if err != nil && ! k8s_errors .IsNotFound (err ) {
584+ return err
585+ }
586+ util .LogForObject (h , fmt .Sprintf ("Removed finalizer %s from MariaDBDatabase %s" , h .GetFinalizer (), mariaDBDatabase .Spec .Name ), mariaDBDatabase )
587+ }
588+
589+ return nil
590+ }
591+
535592// DeleteUnusedMariaDBAccountFinalizers searches for all MariaDBAccounts
536593// associated with the given MariaDBDatabase name and removes the finalizer for all
537594// of them except for the given named account.
@@ -669,6 +726,26 @@ func createOrPatchAccountAndSecret(
669726 return opAcc , errAcc
670727}
671728
729+ // GetDatabase returns an existing MariaDBDatabase object from the cluster
730+ func GetDatabase (ctx context.Context ,
731+ h * helper.Helper ,
732+ name string , namespace string ,
733+ ) (* MariaDBDatabase , error ) {
734+ mariaDBDatabase := & MariaDBDatabase {
735+ ObjectMeta : metav1.ObjectMeta {
736+ Name : name ,
737+ Namespace : namespace ,
738+ },
739+ }
740+ objectKey := client .ObjectKeyFromObject (mariaDBDatabase )
741+
742+ err := h .GetClient ().Get (ctx , objectKey , mariaDBDatabase )
743+ if err != nil {
744+ return nil , err
745+ }
746+ return mariaDBDatabase , err
747+ }
748+
672749// GetAccount returns an existing MariaDBAccount object from the cluster
673750func GetAccount (ctx context.Context ,
674751 h * helper.Helper ,
0 commit comments