Skip to content

Commit

Permalink
Delete NovaCell cr only when deletion job pass
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkisaolamb committed Jan 15, 2025
1 parent 8f8c512 commit 1a46e05
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
40 changes: 21 additions & 19 deletions controllers/nova_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,14 +603,16 @@ func (r *NovaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (resul
for _, cr := range novaCellList.Items {
_, ok := instance.Spec.CellTemplates[cr.Spec.CellName]
if !ok {
err := r.ensureCellDeleted(ctx, h, instance,
result, err := r.ensureCellDeleted(ctx, h, instance,
cr.Spec.CellName, apiTransportURL,
secret, apiDB, cellDBs[novav1.Cell0Name].Database.GetDatabaseHostname(), cells[novav1.Cell0Name])
if err != nil {
return ctrl.Result{}, err
}
Log.Info("Cell deleted", "cell", cr.Spec.CellName)
delete(instance.Status.RegisteredCells, cr.Name)
if result == nova.CellDeleteComplete {
Log.Info("Cell deleted", "cell", cr.Spec.CellName)
delete(instance.Status.RegisteredCells, cr.Name)
}
}

}
Expand Down Expand Up @@ -669,7 +671,7 @@ func (r *NovaReconciler) ensureCellDeleted(
apiDB *mariadbv1.Database,
APIDatabaseHostname string,
cell0 *novav1.NovaCell,
) error {
) (nova.CellDeploymentStatus, error) {
Log := r.GetLogger(ctx)
cell := &novav1.NovaCell{}
fullCellName := types.NamespacedName{
Expand All @@ -682,29 +684,29 @@ func (r *NovaReconciler) ensureCellDeleted(
// We cannot do further cleanup of the MariaDBDatabase and
// MariaDBAccount as their name is only available in the NovaCell CR
// since the cell definition is removed from the Nova CR already.
return nil
return nova.CellDeleteFailed, nil
}
if err != nil {
return err
return nova.CellDeleteFailed, err
}
// If it is not created by us, we don't touch it
if !OwnedBy(cell, instance) {
Log.Info("Cell isn't defined in the Nova, but there is a "+
"Cell CR not owned by us. Not deleting it.",
"cell", cell)
return nil
return nova.CellDeleteFailed, nil
}

dbName, accountName := novaapi.ServiceName+"-"+cell.Spec.CellName, cell.Spec.CellDatabaseAccount

configHash, scriptName, configName, err := r.ensureNovaManageJobSecret(ctx, h, instance,
cell0, topLevelSecret, APIDatabaseHostname, apiTransportURL, apiDB)
if err != nil {
return err
return nova.CellDeleteFailed, err
}
inputHash, err := util.HashOfInputHashes(configHash)
if err != nil {
return err
return nova.CellDeleteFailed, err
}

labels := map[string]string{
Expand All @@ -718,22 +720,22 @@ func (r *NovaReconciler) ensureCellDeleted(

_, err = job.DoJob(ctx, h)
if err != nil {
return err
return nova.CellDeleteFailed, err
}

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

// Delete transportURL cr
Expand All @@ -745,18 +747,18 @@ func (r *NovaReconciler) ensureCellDeleted(
}
err = r.Client.Delete(ctx, transportURL)
if err != nil && !k8s_errors.IsNotFound(err) {
return err
return nova.CellDeleteFailed, err
}

err = mariadbv1.DeleteDatabaseAndAccountFinalizers(
ctx, h, dbName, accountName, instance.Namespace)
if err != nil {
return err
return nova.CellDeleteFailed, err
}

err = r.ensureAccountDeletedIfOwned(ctx, h, instance, accountName)
if err != nil {
return err
return nova.CellDeleteFailed, err
}

database := &mariadbv1.MariaDBDatabase{
Expand All @@ -767,7 +769,7 @@ func (r *NovaReconciler) ensureCellDeleted(
}
err = r.Client.Delete(ctx, database)
if err != nil && !k8s_errors.IsNotFound(err) {
return err
return nova.CellDeleteFailed, err
}
Log.Info("Deleted MariaDBDatabase", "database", database)

Expand All @@ -776,11 +778,11 @@ func (r *NovaReconciler) ensureCellDeleted(
// what to clean up.
err = r.Client.Delete(ctx, cell)
if err != nil && k8s_errors.IsNotFound(err) {
return err
return nova.CellDeleteFailed, err
}

Log.Info("Cell isn't defined in the Nova CR, so it is deleted", "cell", cell)
return nil
return nova.CellDeleteComplete, nil
}

func (r *NovaReconciler) initStatus(
Expand Down
6 changes: 6 additions & 0 deletions pkg/nova/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ const (
// CellComputeDiscoveryReady indicates that all NovaComputes in cell reached the Ready status and
// the hosts in the cell have been successfully discovered
CellComputeDiscoveryReady CellDeploymentStatus = iota
// CellDeleteInProgress indicates that the NovaCell deletion is in progress
CellDeleteInProgress CellDeploymentStatus = iota
// CellDeleteFailed indicates that the NovaCell deletion failed
CellDeleteFailed CellDeploymentStatus = iota
// CellDeleteComplete indicates that the NovaCell deletion is complete
CellDeleteComplete CellDeploymentStatus = iota
)

// Database -
Expand Down
14 changes: 8 additions & 6 deletions test/functional/nova_reconfiguration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,20 @@ var _ = Describe("Nova reconfiguration", func() {
g.Expect(k8sClient.Update(ctx, nova)).To(Succeed())
}, timeout, interval).Should(Succeed())

Eventually(func(g Gomega) {
nova := GetNova(novaNames.NovaName)
g.Expect(nova.Status.RegisteredCells).NotTo(HaveKey(cell1.CellCRName.Name))
}, timeout, interval).Should(Succeed())

NovaCellNotExists(cell1.CellCRName)
Eventually(func(g Gomega) {
mappingJob := th.GetJob(cell1.CellDeleteJobName)
newJobInputHash := GetEnvVarValue(
mappingJob.Spec.Template.Spec.Containers[0].Env, "INPUT_HASH", "")
g.Expect(newJobInputHash).NotTo(BeNil())
}, timeout, interval).Should(Succeed())
th.SimulateJobSuccess(cell1.CellDeleteJobName)
Eventually(func(g Gomega) {
nova := GetNova(novaNames.NovaName)
g.Expect(nova.Status.RegisteredCells).NotTo(HaveKey(cell1.CellCRName.Name))
}, timeout, interval).Should(Succeed())

NovaCellNotExists(cell1.CellCRName)

th.AssertSecretDoesNotExist(cell1.InternalCellSecretName)

Eventually(func(g Gomega) {
Expand Down

0 comments on commit 1a46e05

Please sign in to comment.