Skip to content

Commit 4d759dd

Browse files
committed
link MariaDBAccount to the Database api
This replaces the MariaDBDatabase.secret attribute with the use of MariaDBAccount instead. The MariaDBDatabase.secret attribute becomes optional and deprecated. Once all consuming operators have moved to the new Database API, MariaDBDatabase.secret can be removed entirely. also applies an interim fix to allow the encoding/collation to be present in the Database API, otherwise the database pod would fail. Amends MariaDBAccount to return from delete if the owning Galera resource is already deleted.
1 parent 69fbe67 commit 4d759dd

14 files changed

+282
-85
lines changed

api/bases/mariadb.openstack.org_mariadbdatabases.yaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,8 @@ spec:
4747
description: Name of the database in MariaDB
4848
type: string
4949
secret:
50-
description: Name of secret which contains DatabasePassword
50+
description: Name of secret which contains DatabasePassword (deprecated)
5151
type: string
52-
required:
53-
- defaultCharacterSet
54-
- defaultCollation
5552
type: object
5653
status:
5754
description: MariaDBDatabaseStatus defines the observed state of MariaDBDatabase

api/v1beta1/mariadbdatabase_funcs.go

Lines changed: 106 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
215266
func (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

api/v1beta1/mariadbdatabase_types.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ const (
3131

3232
// MariaDBDatabaseSpec defines the desired state of MariaDBDatabase
3333
type MariaDBDatabaseSpec struct {
34-
// Name of secret which contains DatabasePassword
35-
Secret string `json:"secret,omitempty"`
34+
// Name of secret which contains DatabasePassword (deprecated)
35+
Secret *string `json:"secret,omitempty"`
3636
// Name of the database in MariaDB
3737
Name string `json:"name,omitempty"`
3838
// +kubebuilder:default=utf8
3939
// Default character set for this database
40-
DefaultCharacterSet string `json:"defaultCharacterSet"`
40+
DefaultCharacterSet string `json:"defaultCharacterSet,omitempty"`
4141
// +kubebuilder:default=utf8_general_ci
4242
// Default collation for this database
43-
DefaultCollation string `json:"defaultCollation"`
43+
DefaultCollation string `json:"defaultCollation,omitempty"`
4444
}
4545

4646
// MariaDBDatabaseStatus defines the observed state of MariaDBDatabase
@@ -88,6 +88,7 @@ const (
8888
// Database -
8989
type Database struct {
9090
database *MariaDBDatabase
91+
account *MariaDBAccount
9192
databaseHostname string
9293
databaseName string
9394
databaseUser string

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/mariadb.openstack.org_mariadbdatabases.yaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,8 @@ spec:
4747
description: Name of the database in MariaDB
4848
type: string
4949
secret:
50-
description: Name of secret which contains DatabasePassword
50+
description: Name of secret which contains DatabasePassword (deprecated)
5151
type: string
52-
required:
53-
- defaultCharacterSet
54-
- defaultCollation
5552
type: object
5653
status:
5754
description: MariaDBDatabaseStatus defines the observed state of MariaDBDatabase

controllers/mariadbaccount_controller.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -411,16 +411,19 @@ func (r *MariaDBAccountReconciler) reconcileDelete(
411411

412412
// remove local finalizer
413413
controllerutil.RemoveFinalizer(instance, helper.GetFinalizer())
414-
}
415414

416-
instance.Status.Conditions.Set(condition.FalseCondition(
417-
databasev1beta1.MariaDBServerReadyCondition,
418-
condition.ErrorReason,
419-
condition.SeverityError,
420-
"Error retrieving MariaDB/Galera instance %s",
421-
err))
415+
// galera DB does not exist, so return
416+
return ctrl.Result{}, nil
417+
} else {
418+
instance.Status.Conditions.Set(condition.FalseCondition(
419+
databasev1beta1.MariaDBServerReadyCondition,
420+
condition.ErrorReason,
421+
condition.SeverityError,
422+
"Error retrieving MariaDB/Galera instance %s",
423+
err))
422424

423-
return ctrl.Result{}, err
425+
return ctrl.Result{}, err
426+
}
424427
}
425428

426429
var dbInstance, dbAdminSecret, dbContainerImage, serviceAccountName string

0 commit comments

Comments
 (0)