@@ -541,6 +541,50 @@ func DeleteDatabaseAndAccountFinalizers(
541541 namespace string ,
542542) error {
543543
544+ err := DeleteAccountFinalizers (
545+ ctx ,
546+ h ,
547+ accountName ,
548+ namespace ,
549+ )
550+ if err != nil {
551+ return err
552+ }
553+
554+ // also do a delete for "unused" MariaDBAccounts, associated with
555+ // this MariaDBDatabase.
556+ err = DeleteUnusedMariaDBAccountFinalizers (
557+ ctx , h , name , accountName , namespace ,
558+ )
559+ if err != nil && ! k8s_errors .IsNotFound (err ) {
560+ return err
561+ }
562+
563+ mariaDBDatabase , err := GetDatabase (ctx , h , name , namespace )
564+ if err != nil && ! k8s_errors .IsNotFound (err ) {
565+ return err
566+ } else if err == nil && controllerutil .RemoveFinalizer (mariaDBDatabase , h .GetFinalizer ()) {
567+ err := h .GetClient ().Update (ctx , mariaDBDatabase )
568+ if err != nil && ! k8s_errors .IsNotFound (err ) {
569+ return err
570+ }
571+ util .LogForObject (h , fmt .Sprintf ("Removed finalizer %s from MariaDBDatabase %s" , h .GetFinalizer (), mariaDBDatabase .Spec .Name ), mariaDBDatabase )
572+ }
573+
574+ return nil
575+ }
576+
577+ // DeleteAccountFinalizers performs just the primary account + secret finalizer
578+ // removal part of DeleteDatabaseAndAccountFinalizers
579+ func DeleteAccountFinalizers (
580+ ctx context.Context ,
581+ h * helper.Helper ,
582+ accountName string ,
583+ namespace string ,
584+ ) error {
585+ if accountName == "" {
586+ return fmt .Errorf ("Account name is blank" )
587+ }
544588 databaseAccount , err := GetAccount (ctx , h , accountName , namespace )
545589 if err != nil && ! k8s_errors .IsNotFound (err ) {
546590 return err
@@ -572,26 +616,6 @@ func DeleteDatabaseAndAccountFinalizers(
572616 }
573617 }
574618
575- // also do a delete for "unused" MariaDBAccounts, associated with
576- // this MariaDBDatabase.
577- err = DeleteUnusedMariaDBAccountFinalizers (
578- ctx , h , name , accountName , namespace ,
579- )
580- if err != nil && ! k8s_errors .IsNotFound (err ) {
581- return err
582- }
583-
584- mariaDBDatabase , err := GetDatabase (ctx , h , name , namespace )
585- if err != nil && ! k8s_errors .IsNotFound (err ) {
586- return err
587- } else if err == nil && controllerutil .RemoveFinalizer (mariaDBDatabase , h .GetFinalizer ()) {
588- err := h .GetClient ().Update (ctx , mariaDBDatabase )
589- if err != nil && ! k8s_errors .IsNotFound (err ) {
590- return err
591- }
592- util .LogForObject (h , fmt .Sprintf ("Removed finalizer %s from MariaDBDatabase %s" , h .GetFinalizer (), mariaDBDatabase .Spec .Name ), mariaDBDatabase )
593- }
594-
595619 return nil
596620}
597621
@@ -806,6 +830,32 @@ func EnsureMariaDBAccount(ctx context.Context,
806830 userNamePrefix string ,
807831) (* MariaDBAccount , * corev1.Secret , error ) {
808832
833+ return ensureMariaDBAccount (
834+ ctx , helper , accountName , namespace , requireTLS ,
835+ userNamePrefix , "" , "" , map [string ]string {})
836+
837+ }
838+
839+ // EnsureMariaDBSystemAccount ensures a MariaDBAccount has been created for a given
840+ // operator calling the function, and returns the MariaDBAccount and its
841+ // Secret for use in consumption into a configuration.
842+ // Unlike EnsureMariaDBAccount, the function accepts an exact username that
843+ // expected to remain constant, supporting in-place password changes for the
844+ // account.
845+ func EnsureMariaDBSystemAccount (ctx context.Context ,
846+ helper * helper.Helper ,
847+ accountName string , galeraInstanceName string , namespace string , requireTLS bool ,
848+ exactUserName string , exactPassword string ) (* MariaDBAccount , * corev1.Secret , error ) {
849+ return ensureMariaDBAccount (
850+ ctx , helper , accountName , namespace , requireTLS ,
851+ "" , exactUserName , exactPassword , map [string ]string {"dbName" : galeraInstanceName })
852+ }
853+
854+ func ensureMariaDBAccount (ctx context.Context ,
855+ helper * helper.Helper ,
856+ accountName string , namespace string , requireTLS bool ,
857+ userNamePrefix string , exactUserName string , exactPassword string , labels map [string ]string ,
858+ ) (* MariaDBAccount , * corev1.Secret , error ) {
809859 if accountName == "" {
810860 return nil , nil , fmt .Errorf ("accountName is empty" )
811861 }
@@ -817,9 +867,20 @@ func EnsureMariaDBAccount(ctx context.Context,
817867 return nil , nil , err
818868 }
819869
820- username , err := generateUniqueUsername (userNamePrefix )
821- if err != nil {
822- return nil , nil , err
870+ var username string
871+ var accountType AccountType
872+
873+ if exactUserName == "" {
874+ accountType = "User"
875+ username , err = generateUniqueUsername (userNamePrefix )
876+ if err != nil {
877+ return nil , nil , err
878+ }
879+ } else if userNamePrefix != "" {
880+ return nil , nil , fmt .Errorf ("userNamePrefix and exactUserName are mutually exclusive" )
881+ } else {
882+ accountType = "System"
883+ username = exactUserName
823884 }
824885
825886 account = & MariaDBAccount {
@@ -832,9 +893,10 @@ func EnsureMariaDBAccount(ctx context.Context,
832893 // MariaDBAccount once this is filled in
833894 },
834895 Spec : MariaDBAccountSpec {
835- UserName : username ,
836- Secret : fmt .Sprintf ("%s-db-secret" , accountName ),
837- RequireTLS : requireTLS ,
896+ UserName : username ,
897+ Secret : fmt .Sprintf ("%s-db-secret" , accountName ),
898+ RequireTLS : requireTLS ,
899+ AccountType : accountType ,
838900 },
839901 }
840902
@@ -844,6 +906,7 @@ func EnsureMariaDBAccount(ctx context.Context,
844906 if account .Spec .Secret == "" {
845907 account .Spec .Secret = fmt .Sprintf ("%s-db-secret" , accountName )
846908 }
909+
847910 }
848911
849912 dbSecret , _ , err := secret .GetSecret (ctx , helper , account .Spec .Secret , namespace )
@@ -853,9 +916,14 @@ func EnsureMariaDBAccount(ctx context.Context,
853916 return nil , nil , err
854917 }
855918
856- dbPassword , err := generateDBPassword ()
857- if err != nil {
858- return nil , nil , err
919+ var dbPassword string
920+ if exactPassword == "" {
921+ dbPassword , err = generateDBPassword ()
922+ if err != nil {
923+ return nil , nil , err
924+ }
925+ } else {
926+ dbPassword = exactPassword
859927 }
860928
861929 dbSecret = & corev1.Secret {
@@ -869,7 +937,7 @@ func EnsureMariaDBAccount(ctx context.Context,
869937 }
870938 }
871939
872- _ , err = createOrPatchAccountAndSecret (ctx , helper , account , dbSecret , map [ string ] string {} )
940+ _ , err = createOrPatchAccountAndSecret (ctx , helper , account , dbSecret , labels )
873941 if err != nil {
874942 return nil , nil , err
875943 }
@@ -885,6 +953,7 @@ func EnsureMariaDBAccount(ctx context.Context,
885953 )
886954
887955 return account , dbSecret , nil
956+
888957}
889958
890959// generateUniqueUsername creates a MySQL-compliant database username based on
0 commit comments