Skip to content

Commit

Permalink
Delete MariaDBAccount and Database
Browse files Browse the repository at this point in the history
Both CR is created by the nova controller for the given cell, so when
that cell is deleted the CRs also needs to be deleted. We also need to
delete the secret connected to the MariaDBAccount as mariadb-operator
does not delete it today.

I also added graceful handling of NotFound during resource deletion.
  • Loading branch information
gibizer committed Sep 27, 2024
1 parent 8625719 commit 48f7fb3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
72 changes: 63 additions & 9 deletions controllers/nova_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,15 +684,54 @@ func DeleteDatabaseAndAccountFinalizers(
return err
}

account, err := mariadbv1.GetAccount(ctx, h, accountName, namespace)
if err != nil && !k8s_errors.IsNotFound(err) {
return err
}
if err == nil {
// NOTE(gibi): this might be controversial as we cannot be sure if
// Nova got a pre-created account from outside or created its own.
// Today it is always the nova controller who creates the Account but
// it might change in the future.
// Still as we need to delete the MariaDBDatabase we need to get rid
// of the Account. Also that Account in that DB will be pointless
// if the DB is gone.
err := h.GetClient().Delete(ctx, account)
if err != nil {
return err
}
util.LogForObject(h, "Deleted MariaDBAccount", account)
}

// NOTE(gibi): This is also controversial as we don't know if the account
// secret was created by us by falling back to create the MariaDBAccount.
// Still MariaDBAccount cleanup does not delete the secret so we have to.
err = secret.DeleteSecretsWithName(ctx, h, account.Spec.Secret, namespace)
if err != nil && !k8s_errors.IsNotFound(err) {
return err
}

mariaDBDatabase, err := GetDatabase(ctx, h, name, namespace)
if err != nil && !k8s_errors.IsNotFound(err) {
return err
} else if err == nil && controllerutil.RemoveFinalizer(mariaDBDatabase, h.GetFinalizer()) {
err := h.GetClient().Update(ctx, mariaDBDatabase)
if err != nil && !k8s_errors.IsNotFound(err) {
} else if err == nil {
if controllerutil.RemoveFinalizer(mariaDBDatabase, h.GetFinalizer()) {
err := h.GetClient().Update(ctx, mariaDBDatabase)
if err != nil && !k8s_errors.IsNotFound(err) {
return err
}
util.LogForObject(
h,
fmt.Sprintf(
"Removed finalizer %s from MariaDBDatabase %s", h.GetFinalizer(), mariaDBDatabase.Spec.Name),
mariaDBDatabase)
}

err := h.GetClient().Delete(ctx, mariaDBDatabase)
if err != nil {
return err
}
util.LogForObject(h, fmt.Sprintf("Removed finalizer %s from MariaDBDatabase %s", h.GetFinalizer(), mariaDBDatabase.Spec.Name), mariaDBDatabase)
util.LogForObject(h, "Deleted MariaDBDatabase", mariaDBDatabase)
}

return nil
Expand Down Expand Up @@ -765,7 +804,16 @@ func (r *NovaReconciler) ensureCellDeleted(

secretName := getNovaCellCRName(instance.Name, cellName)
err = secret.DeleteSecretsWithName(ctx, h, secretName, instance.Namespace)
if err != nil {
if err != nil && !k8s_errors.IsNotFound(err) {
return err
}
configSecret, scriptSecret := r.getNovaManageJobSecretNames(cell)
err = secret.DeleteSecretsWithName(ctx, h, configSecret, instance.Namespace)
if err != nil && !k8s_errors.IsNotFound(err) {
return err
}
err = secret.DeleteSecretsWithName(ctx, h, scriptSecret, instance.Namespace)
if err != nil && !k8s_errors.IsNotFound(err) {
return err
}

Expand All @@ -777,8 +825,7 @@ func (r *NovaReconciler) ensureCellDeleted(
},
}
err = r.Client.Delete(ctx, transportURL)

if err != nil {
if err != nil && !k8s_errors.IsNotFound(err) {
return err
}

Expand Down Expand Up @@ -899,6 +946,14 @@ func (r *NovaReconciler) initConditions(
return nil
}

func (r *NovaReconciler) getNovaManageJobSecretNames(
cell *novav1.NovaCell,
) (configName string, scriptName string) {
configName = fmt.Sprintf("%s-config-data", cell.Name+"-manage")
scriptName = fmt.Sprintf("%s-scripts", cell.Name+"-manage")
return
}

func (r *NovaReconciler) ensureNovaManageJobSecret(
ctx context.Context, h *helper.Helper, instance *novav1.Nova,
cell *novav1.NovaCell,
Expand All @@ -907,8 +962,7 @@ func (r *NovaReconciler) ensureNovaManageJobSecret(
cellTransportURL string,
cellDB *mariadbv1.Database,
) (map[string]env.Setter, string, string, error) {
configName := fmt.Sprintf("%s-config-data", cell.Name+"-manage")
scriptName := fmt.Sprintf("%s-scripts", cell.Name+"-manage")
configName, scriptName := r.getNovaManageJobSecretNames(cell)

cmLabels := labels.GetLabels(
instance, labels.GetGroupLabel(NovaLabelPrefix), map[string]string{},
Expand Down
6 changes: 4 additions & 2 deletions test/functional/nova_reconfiguration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ func CreateNovaWith3CellsAndEnsureReady(novaNames NovaNames) {

cell1Account, cell1Secret := mariadb.CreateMariaDBAccountAndSecret(
cell1.MariaDBAccountName, mariadbv1.MariaDBAccountSpec{})
DeferCleanup(k8sClient.Delete, ctx, cell1Account)
DeferCleanup(k8sClient.Delete, ctx, cell1Secret)
DeferCleanup(th.DeleteInstance, cell1Account)
DeferCleanup(
th.DeleteSecret,
types.NamespacedName{Name: cell1Secret.Name, Namespace: cell1Secret.Namespace})

cell2Account, cell2Secret := mariadb.CreateMariaDBAccountAndSecret(
cell2.MariaDBAccountName, mariadbv1.MariaDBAccountSpec{})
Expand Down

0 comments on commit 48f7fb3

Please sign in to comment.