Skip to content

Commit

Permalink
EVEREST-1293 Scheduled dbb creation fix (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
oksana-grishchenko authored Aug 5, 2024
1 parent efd4903 commit ee0a43e
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions controllers/databaseclusterbackup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"
"net/http"
"path/filepath"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -369,6 +368,24 @@ func (r *DatabaseClusterBackupReconciler) tryCreatePG(ctx context.Context, obj c
return nil
}

pg := &pgv2.PerconaPGCluster{}
if err := r.Get(ctx, types.NamespacedName{Namespace: obj.GetNamespace(), Name: pgBackup.Spec.PGCluster}, pg); err != nil {
// if such upstream cluster is not found - do nothing
if k8serrors.IsNotFound(err) {
return nil
}
return err
}

storages := &everestv1alpha1.BackupStorageList{}
if err := r.List(ctx, storages, &client.ListOptions{}); err != nil {
// if no backup storages found - do nothing
if k8serrors.IsNotFound(err) {
return nil
}
return err
}

backup := &everestv1alpha1.DatabaseClusterBackup{
ObjectMeta: metav1.ObjectMeta{
Name: namespacedName.Name,
Expand All @@ -392,7 +409,7 @@ func (r *DatabaseClusterBackupReconciler) tryCreatePG(ctx context.Context, obj c
if err != nil {
return err
}
name, nErr := backupStorageName(pgBackup.Spec.RepoName, cluster)
name, nErr := backupStorageName(pgBackup.Spec.RepoName, pg, storages)
if nErr != nil {
return nErr
}
Expand Down Expand Up @@ -879,23 +896,19 @@ func (r *DatabaseClusterBackupReconciler) reconcilePG(
return false, r.Status().Update(ctx, backup)
}

func backupStorageName(repoName string, cluster *everestv1alpha1.DatabaseCluster) (string, error) {
// repoNames in a PG cluster are in form "repo1", "repo2" etc.
// which is mapped to the DatabaseCluster schedules list.
// So here we figure out the BackupStorageName of the corresponding schedule.
scheduleInd, err := strconv.Atoi(strings.TrimPrefix(repoName, "repo"))
if err != nil {
return "", fmt.Errorf("unable to get the schedule index for the repo %s", repoName)
}
// repo1 is hardcoded in the PerconaPGCluster CR as a PVC-based repo and
// there is never a schedule for it, so there is always one less schedule
// than repos, hence the +1. Also, the repoNames for the schedules start
// from repo2. So we need to subtract 2 from the scheduleInd to get the
// correct index in the schedules list.
if len(cluster.Spec.Backup.Schedules)+1 < scheduleInd {
return "", fmt.Errorf("invalid schedule index %v in the repo %s", scheduleInd, repoName)
func backupStorageName(repoName string, pg *pgv2.PerconaPGCluster, storages *everestv1alpha1.BackupStorageList) (string, error) {
for _, repo := range pg.Spec.Backups.PGBackRest.Repos {
if repo.Name == repoName {
for _, storage := range storages.Items {
if repo.S3.Region == storage.Spec.Region &&
repo.S3.Bucket == storage.Spec.Bucket &&
repo.S3.Endpoint == storage.Spec.EndpointURL {
return storage.Name, nil
}
}
}
}
return cluster.Spec.Backup.Schedules[scheduleInd-2].BackupStorageName, nil
return "", fmt.Errorf("failed to find backup storage for repo %s", repoName)
}

func (r *DatabaseClusterBackupReconciler) handleStorageProtectionFinalizer(
Expand Down

0 comments on commit ee0a43e

Please sign in to comment.