@@ -121,6 +121,11 @@ func (d *Database) GetDatabase() *MariaDBDatabase {
121
121
return d .database
122
122
}
123
123
124
+ // GetAccount - returns the account
125
+ func (d * Database ) GetAccount () * MariaDBAccount {
126
+ return d .account
127
+ }
128
+
124
129
// CreateOrPatchDB - create or patch the service DB instance
125
130
// Deprecated. Use CreateOrPatchDBByName instead. If you want to use the
126
131
// default the DB service instance of the deployment then pass "openstack" as
@@ -162,6 +167,22 @@ func (d *Database) CreateOrPatchDBByName(
162
167
}
163
168
}
164
169
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
+ }
165
186
// set the database hostname on the db instance
166
187
err := d .setDatabaseHostname (ctx , h , name )
167
188
if err != nil {
@@ -174,8 +195,6 @@ func (d *Database) CreateOrPatchDBByName(
174
195
d .labels ,
175
196
)
176
197
177
- db .Spec .Secret = d .secret
178
-
179
198
err := controllerutil .SetControllerReference (h .GetBeforeObject (), db , h .GetScheme ())
180
199
if err != nil {
181
200
return err
@@ -200,6 +219,36 @@ func (d *Database) CreateOrPatchDBByName(
200
219
return ctrl.Result {RequeueAfter : time .Second * 5 }, nil
201
220
}
202
221
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
+
203
252
err = d .getDBWithName (
204
253
ctx ,
205
254
h ,
@@ -211,7 +260,9 @@ func (d *Database) CreateOrPatchDBByName(
211
260
return ctrl.Result {}, nil
212
261
}
213
262
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)
215
266
func (d * Database ) WaitForDBCreatedWithTimeout (
216
267
ctx context.Context ,
217
268
h * helper.Helper ,
@@ -226,7 +277,7 @@ func (d *Database) WaitForDBCreatedWithTimeout(
226
277
return ctrl.Result {}, err
227
278
}
228
279
229
- if ! d .database .Status .Completed || k8s_errors . IsNotFound ( err ) {
280
+ if ! d .database .Status .Conditions . IsTrue ( MariaDBDatabaseReadyCondition ) {
230
281
util .LogForObject (
231
282
h ,
232
283
fmt .Sprintf ("Waiting for service DB %s to be created" , d .database .Name ),
@@ -236,6 +287,26 @@ func (d *Database) WaitForDBCreatedWithTimeout(
236
287
return ctrl.Result {RequeueAfter : requeueAfter }, nil
237
288
}
238
289
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
+
239
310
return ctrl.Result {}, nil
240
311
}
241
312
@@ -262,13 +333,15 @@ func (d *Database) getDBWithName(
262
333
if namespace == "" {
263
334
namespace = h .GetBeforeObject ().GetNamespace ()
264
335
}
336
+
265
337
err := h .GetClient ().Get (
266
338
ctx ,
267
339
types.NamespacedName {
268
340
Name : name ,
269
341
Namespace : namespace ,
270
342
},
271
343
db )
344
+
272
345
if err != nil {
273
346
if k8s_errors .IsNotFound (err ) {
274
347
return util .WrapErrorForObject (
@@ -287,6 +360,35 @@ func (d *Database) getDBWithName(
287
360
288
361
d .database = db
289
362
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
+
290
392
return nil
291
393
}
292
394
0 commit comments