Skip to content

Commit

Permalink
Merge pull request #186 from zzzeek/fix_lookup_and_finalizers
Browse files Browse the repository at this point in the history
revise auto-generation of MariaDBAccount for deletion to work
  • Loading branch information
openshift-merge-bot[bot] authored Feb 1, 2024
2 parents 7c0df7d + ef11d42 commit 3dcb5d5
Showing 1 changed file with 50 additions and 15 deletions.
65 changes: 50 additions & 15 deletions api/v1beta1/mariadbdatabase_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,12 @@ func (d *Database) CreateOrPatchDBByName(

account := d.account
if account == nil {
// no account is present in this Database, so for forwards compatibility,
// make one. name it the same as the MariaDBDatabase so we can get it back
// again based on that name alone.
account = &MariaDBAccount{
ObjectMeta: metav1.ObjectMeta{
Name: strings.Replace(d.databaseUser, "_", "-", -1),
Name: d.name,
Namespace: d.namespace,
Labels: map[string]string{
"mariaDBDatabaseName": d.name,
Expand Down Expand Up @@ -289,7 +292,7 @@ func (d *Database) WaitForDBCreatedWithTimeout(
return ctrl.Result{RequeueAfter: requeueAfter}, nil
}

if !d.account.Status.Conditions.IsTrue(MariaDBAccountReadyCondition) {
if d.account != nil && !d.account.Status.Conditions.IsTrue(MariaDBAccountReadyCondition) {
util.LogForObject(
h,
fmt.Sprintf("Waiting for service account %s to be created", d.account.Name),
Expand Down Expand Up @@ -322,6 +325,9 @@ func (d *Database) WaitForDBCreated(
}

// getDBWithName - get DB object with name in namespace
// note this is legacy as a new function will be added that allows for
// lookup of Database based on mariadbdatabase name and mariadbaccount name
// individually
func (d *Database) getDBWithName(
ctx context.Context,
h *helper.Helper,
Expand Down Expand Up @@ -365,32 +371,51 @@ func (d *Database) getDBWithName(
account := &MariaDBAccount{}
username := d.databaseUser

err = h.GetClient().Get(
ctx,
types.NamespacedName{
Name: strings.Replace(username, "_", "-", -1),
Namespace: namespace,
},
account)
if username == "" {
// no username, so this is a legacy lookup. locate MariaDBAccount
// based on the same name as that of the MariaDBDatabase
err = h.GetClient().Get(
ctx,
types.NamespacedName{
Name: d.name,
Namespace: namespace,
},
account)
} else {
// username is given. locate MariaDBAccount based on that given
// username. this is also legacy and in practice should not occur
// for any current controller.
err = h.GetClient().Get(
ctx,
types.NamespacedName{
Name: strings.Replace(username, "_", "-", -1),
Namespace: namespace,
},
account)
}

if err != nil {
if k8s_errors.IsNotFound(err) {
return util.WrapErrorForObject(
fmt.Sprintf("Failed to get %s account %s ", username, namespace),
h.GetBeforeObject(),
err,
// if account can't be found, log it, but don't quit, still
// return the Database with MariaDBDatabase
util.LogForObject(
h,
fmt.Sprintf("Could not find account %s for Database named %s", username, namespace),
d.account,
)

// note that d.account remains nil in this case
}

return util.WrapErrorForObject(
fmt.Sprintf("account error %s %s ", username, namespace),
h.GetBeforeObject(),
err,
)
} else {
d.account = account
}

d.account = account

return nil
}

Expand All @@ -416,11 +441,21 @@ func (d *Database) DeleteFinalizer(
ctx context.Context,
h *helper.Helper,
) error {

if d.account != nil && controllerutil.RemoveFinalizer(d.account, h.GetFinalizer()) {
err := h.GetClient().Update(ctx, d.account)
if err != nil && !k8s_errors.IsNotFound(err) {
return err
}
util.LogForObject(h, fmt.Sprintf("Removed finalizer %s from MariaDBAccount object", h.GetFinalizer()), d.account)
}

if controllerutil.RemoveFinalizer(d.database, h.GetFinalizer()) {
err := h.GetClient().Update(ctx, d.database)
if err != nil && !k8s_errors.IsNotFound(err) {
return err
}
util.LogForObject(h, fmt.Sprintf("Removed finalizer %s from MariaDBDatabase object", h.GetFinalizer()), d.database)
}
return nil
}

0 comments on commit 3dcb5d5

Please sign in to comment.