@@ -541,6 +541,47 @@ 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 {
544585 databaseAccount , err := GetAccount (ctx , h , accountName , namespace )
545586 if err != nil && ! k8s_errors .IsNotFound (err ) {
546587 return err
@@ -572,26 +613,6 @@ func DeleteDatabaseAndAccountFinalizers(
572613 }
573614 }
574615
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-
595616 return nil
596617}
597618
@@ -811,6 +832,32 @@ func EnsureMariaDBAccount(ctx context.Context,
811832 userNamePrefix string ,
812833) (* MariaDBAccount , * corev1.Secret , error ) {
813834
835+ return ensureMariaDBAccount (
836+ ctx , helper , accountName , namespace , requireTLS ,
837+ userNamePrefix , "" , map [string ]string {})
838+
839+ }
840+
841+ // EnsureMariaDBSystemAccount ensures a MariaDBAccount has been created for a given
842+ // operator calling the function, and returns the MariaDBAccount and its
843+ // Secret for use in consumption into a configuration.
844+ // Unlike EnsureMariaDBAccount, the function accepts an exact username that
845+ // expected to remain constant, supporting in-place password changes for the
846+ // account.
847+ func EnsureMariaDBSystemAccount (ctx context.Context ,
848+ helper * helper.Helper ,
849+ accountName string , galeraInstanceName string , namespace string , requireTLS bool ,
850+ exactUserName string ) (* MariaDBAccount , * corev1.Secret , error ) {
851+ return ensureMariaDBAccount (
852+ ctx , helper , accountName , namespace , requireTLS ,
853+ "" , exactUserName , map [string ]string {"dbName" : galeraInstanceName })
854+ }
855+
856+ func ensureMariaDBAccount (ctx context.Context ,
857+ helper * helper.Helper ,
858+ accountName string , namespace string , requireTLS bool ,
859+ userNamePrefix string , exactUserName string , labels map [string ]string ,
860+ ) (* MariaDBAccount , * corev1.Secret , error ) {
814861 if accountName == "" {
815862 return nil , nil , fmt .Errorf ("accountName is empty" )
816863 }
@@ -822,9 +869,20 @@ func EnsureMariaDBAccount(ctx context.Context,
822869 return nil , nil , err
823870 }
824871
825- username , err := generateUniqueUsername (userNamePrefix )
826- if err != nil {
827- return nil , nil , err
872+ var username string
873+ var accountType AccountType
874+
875+ if exactUserName == "" {
876+ accountType = "User"
877+ username , err = generateUniqueUsername (userNamePrefix )
878+ if err != nil {
879+ return nil , nil , err
880+ }
881+ } else if userNamePrefix != "" {
882+ return nil , nil , fmt .Errorf ("userNamePrefix and exactUserName are mutually exclusive" )
883+ } else {
884+ accountType = "System"
885+ username = exactUserName
828886 }
829887
830888 account = & MariaDBAccount {
@@ -837,9 +895,10 @@ func EnsureMariaDBAccount(ctx context.Context,
837895 // MariaDBAccount once this is filled in
838896 },
839897 Spec : MariaDBAccountSpec {
840- UserName : username ,
841- Secret : fmt .Sprintf ("%s-db-secret" , accountName ),
842- RequireTLS : requireTLS ,
898+ UserName : username ,
899+ Secret : fmt .Sprintf ("%s-db-secret" , accountName ),
900+ RequireTLS : requireTLS ,
901+ AccountType : accountType ,
843902 },
844903 }
845904
@@ -874,7 +933,7 @@ func EnsureMariaDBAccount(ctx context.Context,
874933 }
875934 }
876935
877- _ , err = createOrPatchAccountAndSecret (ctx , helper , account , dbSecret , map [ string ] string {} )
936+ _ , err = createOrPatchAccountAndSecret (ctx , helper , account , dbSecret , labels )
878937 if err != nil {
879938 return nil , nil , err
880939 }
@@ -890,6 +949,7 @@ func EnsureMariaDBAccount(ctx context.Context,
890949 )
891950
892951 return account , dbSecret , nil
952+
893953}
894954
895955// generateUniqueUsername creates a MySQL-compliant database username based on
0 commit comments