Skip to content

Commit

Permalink
EVEREST-1349 Fix DBs stuck in the "Deleting" state (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
recharte authored Aug 19, 2024
1 parent d7dc422 commit 80a11c9
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions controllers/common/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ const (
// EverestSecretsPrefix is the prefix for secrets created by Everest.
EverestSecretsPrefix = "everest-secrets-"

// DBBackupCleanupFinalizer is the finalizer for cleaning up DatabaseClusterBackup.
// Deprecated: We keep this for backward compatibility.
DBBackupCleanupFinalizer = "everest.percona.com/dbb-cleanup"

// UpstreamClusterCleanupFinalizer is the finalizer for cleaning up the upstream cluster.
UpstreamClusterCleanupFinalizer = "everest.percona.com/upstream-cluster-cleanup"

Expand Down
45 changes: 45 additions & 0 deletions controllers/common/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,51 @@ func GetRepoNameByBackupStorage(
return ""
}

// HandleDBBackupsCleanup handles the cleanup of the dbbackup objects.
// Returns true if cleanup is complete.
func HandleDBBackupsCleanup(
ctx context.Context,
c client.Client,
database *everestv1alpha1.DatabaseCluster,
) (bool, error) {
if controllerutil.ContainsFinalizer(database, DBBackupCleanupFinalizer) {
if done, err := deleteBackupsForDatabase(ctx, c, database.GetName(), database.GetNamespace()); err != nil {
return false, err
} else if !done {
return false, nil
}
controllerutil.RemoveFinalizer(database, DBBackupCleanupFinalizer)
return true, c.Update(ctx, database)
}
return true, nil
}

// Delete all dbbackups for the given database.
// Returns true if no dbbackups are found.
func deleteBackupsForDatabase(
ctx context.Context,
c client.Client,
dbName, dbNs string,
) (bool, error) {
backupList, err := ListDatabaseClusterBackups(ctx, c, dbName, dbNs)
if err != nil {
return false, err
}
if len(backupList.Items) == 0 {
return true, nil
}
for _, backup := range backupList.Items {
if !backup.GetDeletionTimestamp().IsZero() {
// Already deleting, continue to next.
continue
}
if err := c.Delete(ctx, &backup); err != nil {
return false, err
}
}
return false, nil
}

// HandleUpstreamClusterCleanup handles the cleanup of the psdmb objects.
// Returns true if cleanup is complete.
func HandleUpstreamClusterCleanup(
Expand Down
6 changes: 6 additions & 0 deletions controllers/providers/pg/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ func (p *Provider) Status(ctx context.Context) (everestv1alpha1.DatabaseClusterS

// Cleanup runs the cleanup routines and returns true if the cleanup is done.
func (p *Provider) Cleanup(ctx context.Context, database *everestv1alpha1.DatabaseCluster) (bool, error) {
// Even though we no longer set the DBBackupCleanupFinalizer, we still need
// to handle the cleanup to ensure backward compatibility.
done, err := common.HandleDBBackupsCleanup(ctx, p.C, database)
if err != nil || !done {
return done, err
}
return common.HandleUpstreamClusterCleanup(ctx, p.C, database, &pgv2.PerconaPGCluster{})
}

Expand Down
6 changes: 6 additions & 0 deletions controllers/providers/psmdb/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ func (p *Provider) Cleanup(ctx context.Context, database *everestv1alpha1.Databa
if err != nil {
return false, err
}
// Even though we no longer set the DBBackupCleanupFinalizer, we still need
// to handle the cleanup to ensure backward compatibility.
done, err := common.HandleDBBackupsCleanup(ctx, p.C, database)
if err != nil || !done {
return done, err
}
return common.HandleUpstreamClusterCleanup(ctx, p.C, database, &psmdbv1.PerconaServerMongoDB{})
}

Expand Down
6 changes: 6 additions & 0 deletions controllers/providers/pxc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ func (p *Provider) Status(ctx context.Context) (everestv1alpha1.DatabaseClusterS

// Cleanup runs the cleanup routines and returns true if the cleanup is done.
func (p *Provider) Cleanup(ctx context.Context, database *everestv1alpha1.DatabaseCluster) (bool, error) {
// Even though we no longer set the DBBackupCleanupFinalizer, we still need
// to handle the cleanup to ensure backward compatibility.
done, err := common.HandleDBBackupsCleanup(ctx, p.C, database)
if err != nil || !done {
return done, err
}
return common.HandleUpstreamClusterCleanup(ctx, p.C, database, &pxcv1.PerconaXtraDBCluster{})
}

Expand Down

0 comments on commit 80a11c9

Please sign in to comment.