@@ -121,6 +121,11 @@ func (d *Database) GetDatabase() *MariaDBDatabase {
121121 return d .database
122122}
123123
124+ // GetAccount - returns the account
125+ func (d * Database ) GetAccount () * MariaDBAccount {
126+ return d .account
127+ }
128+
124129// CreateOrPatchDB - create or patch the service DB instance
125130// Deprecated. Use CreateOrPatchDBByName instead. If you want to use the
126131// default the DB service instance of the deployment then pass "openstack" as
@@ -162,6 +167,22 @@ func (d *Database) CreateOrPatchDBByName(
162167 }
163168 }
164169
170+ account := d .account
171+ if account == nil {
172+ account = & MariaDBAccount {
173+ ObjectMeta : metav1.ObjectMeta {
174+ Name : d .databaseUser ,
175+ Namespace : d .namespace ,
176+ Labels : map [string ]string {
177+ "mariaDBDatabaseName" : d .name ,
178+ },
179+ },
180+ Spec : MariaDBAccountSpec {
181+ UserName : d .databaseUser ,
182+ Secret : d .secret ,
183+ },
184+ }
185+ }
165186 // set the database hostname on the db instance
166187 err := d .setDatabaseHostname (ctx , h , name )
167188 if err != nil {
@@ -174,8 +195,6 @@ func (d *Database) CreateOrPatchDBByName(
174195 d .labels ,
175196 )
176197
177- db .Spec .Secret = d .secret
178-
179198 err := controllerutil .SetControllerReference (h .GetBeforeObject (), db , h .GetScheme ())
180199 if err != nil {
181200 return err
@@ -200,6 +219,36 @@ func (d *Database) CreateOrPatchDBByName(
200219 return ctrl.Result {RequeueAfter : time .Second * 5 }, nil
201220 }
202221
222+ op_acc , err_acc := controllerutil .CreateOrPatch (ctx , h .GetClient (), account , func () error {
223+ account .Labels = util .MergeStringMaps (
224+ account .GetLabels (),
225+ d .labels ,
226+ )
227+
228+ err := controllerutil .SetControllerReference (h .GetBeforeObject (), account , h .GetScheme ())
229+ if err != nil {
230+ return err
231+ }
232+
233+ // If the service object doesn't have our finalizer, add it.
234+ controllerutil .AddFinalizer (account , h .GetFinalizer ())
235+
236+ return nil
237+ })
238+
239+ if err_acc != nil && ! k8s_errors .IsNotFound (err_acc ) {
240+ return ctrl.Result {}, util .WrapErrorForObject (
241+ fmt .Sprintf ("Error create or update account object %s" , account .Name ),
242+ account ,
243+ err_acc ,
244+ )
245+ }
246+
247+ if op_acc != controllerutil .OperationResultNone {
248+ util .LogForObject (h , fmt .Sprintf ("Account object %s created or patched" , account .Name ), account )
249+ return ctrl.Result {RequeueAfter : time .Second * 5 }, nil
250+ }
251+
203252 err = d .getDBWithName (
204253 ctx ,
205254 h ,
@@ -211,7 +260,9 @@ func (d *Database) CreateOrPatchDBByName(
211260 return ctrl.Result {}, nil
212261}
213262
214- // WaitForDBCreatedWithTimeout - wait until the MariaDBDatabase is initialized and reports Status.Completed == true
263+ // WaitForDBCreatedWithTimeout - wait until the MariaDBDatabase and MariaDBAccounts are
264+ // initialized and reports Status.Conditions.IsTrue(MariaDBDatabaseReadyCondition)
265+ // and Status.Conditions.IsTrue(MariaDBAccountReadyCondition)
215266func (d * Database ) WaitForDBCreatedWithTimeout (
216267 ctx context.Context ,
217268 h * helper.Helper ,
@@ -226,7 +277,7 @@ func (d *Database) WaitForDBCreatedWithTimeout(
226277 return ctrl.Result {}, err
227278 }
228279
229- if ! d .database .Status .Completed || k8s_errors . IsNotFound ( err ) {
280+ if ! d .database .Status .Conditions . IsTrue ( MariaDBDatabaseReadyCondition ) {
230281 util .LogForObject (
231282 h ,
232283 fmt .Sprintf ("Waiting for service DB %s to be created" , d .database .Name ),
@@ -236,6 +287,26 @@ func (d *Database) WaitForDBCreatedWithTimeout(
236287 return ctrl.Result {RequeueAfter : requeueAfter }, nil
237288 }
238289
290+ if ! d .account .Status .Conditions .IsTrue (MariaDBAccountReadyCondition ) {
291+ util .LogForObject (
292+ h ,
293+ fmt .Sprintf ("Waiting for service account %s to be created" , d .account .Name ),
294+ d .account ,
295+ )
296+
297+ return ctrl.Result {RequeueAfter : requeueAfter }, nil
298+ }
299+
300+ if k8s_errors .IsNotFound (err ) {
301+ util .LogForObject (
302+ h ,
303+ fmt .Sprintf ("DB or account objects not yet found %s" , d .database .Name ),
304+ d .database ,
305+ )
306+
307+ return ctrl.Result {RequeueAfter : requeueAfter }, nil
308+ }
309+
239310 return ctrl.Result {}, nil
240311}
241312
@@ -262,13 +333,15 @@ func (d *Database) getDBWithName(
262333 if namespace == "" {
263334 namespace = h .GetBeforeObject ().GetNamespace ()
264335 }
336+
265337 err := h .GetClient ().Get (
266338 ctx ,
267339 types.NamespacedName {
268340 Name : name ,
269341 Namespace : namespace ,
270342 },
271343 db )
344+
272345 if err != nil {
273346 if k8s_errors .IsNotFound (err ) {
274347 return util .WrapErrorForObject (
@@ -287,6 +360,35 @@ func (d *Database) getDBWithName(
287360
288361 d .database = db
289362
363+ account := & MariaDBAccount {}
364+ username := d .databaseUser
365+
366+ err = h .GetClient ().Get (
367+ ctx ,
368+ types.NamespacedName {
369+ Name : username ,
370+ Namespace : namespace ,
371+ },
372+ account )
373+
374+ if err != nil {
375+ if k8s_errors .IsNotFound (err ) {
376+ return util .WrapErrorForObject (
377+ fmt .Sprintf ("Failed to get %s account %s " , username , namespace ),
378+ h .GetBeforeObject (),
379+ err ,
380+ )
381+ }
382+
383+ return util .WrapErrorForObject (
384+ fmt .Sprintf ("account error %s %s " , username , namespace ),
385+ h .GetBeforeObject (),
386+ err ,
387+ )
388+ }
389+
390+ d .account = account
391+
290392 return nil
291393}
292394
0 commit comments