Skip to content

Commit

Permalink
EVEREST-638 PXC restore to a new cluster (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
oksana-grishchenko authored Jan 25, 2024
1 parent aeb868d commit 2986214
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
7 changes: 7 additions & 0 deletions controllers/databasecluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2745,6 +2745,13 @@ func (r *DatabaseClusterReconciler) reconcileLabels(ctx context.Context, databas
needsUpdate = true
}
}
if database.Spec.Backup.PITR.BackupStorageName != nil {
if _, ok := database.ObjectMeta.Labels[fmt.Sprintf(backupStorageNameLabelTmpl, *database.Spec.Backup.PITR.BackupStorageName)]; !ok {
database.ObjectMeta.Labels[fmt.Sprintf(backupStorageNameLabelTmpl, *database.Spec.Backup.PITR.BackupStorageName)] = backupStorageLabelValue
needsUpdate = true
}
}

for _, schedule := range database.Spec.Backup.Schedules {
if _, ok := database.ObjectMeta.Labels[fmt.Sprintf(backupStorageNameLabelTmpl, schedule.BackupStorageName)]; !ok {
database.ObjectMeta.Labels[fmt.Sprintf(backupStorageNameLabelTmpl, schedule.BackupStorageName)] = backupStorageLabelValue
Expand Down
48 changes: 38 additions & 10 deletions controllers/databaseclusterrestore_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,17 +351,11 @@ func (r *DatabaseClusterRestoreReconciler) restorePXC(
}

if restore.Spec.DataSource.PITR != nil {
if db.Spec.Backup.PITR.BackupStorageName == nil || *db.Spec.Backup.PITR.BackupStorageName == "" {
return fmt.Errorf("no backup storage defined for PITR in %s cluster", db.Name)
}
pxcCR.Spec.PITR = &pxcv1.PITR{
BackupSource: &pxcv1.PXCBackupStatus{
StorageName: pitrStorageName(*db.Spec.Backup.PITR.BackupStorageName),
},
Type: string(restore.Spec.DataSource.PITR.Type),
// pxc accepts only the special format
Date: restore.Spec.DataSource.PITR.Date.Format(everestv1alpha1.DateFormatSpace),
spec, err := genPXCPitrRestoreSpec(restore.Spec.DataSource, *db)
if err != nil {
return err
}
pxcCR.Spec.PITR = spec
}

return nil
Expand Down Expand Up @@ -550,3 +544,37 @@ func parsePrefixFromDestination(url string) string {
// taking the third and the second last parts of the destination
return fmt.Sprintf("%s/%s", parts[l-3], parts[l-2])
}

func genPXCPitrRestoreSpec(dataSource everestv1alpha1.DataSource, db everestv1alpha1.DatabaseCluster) (*pxcv1.PITR, error) {
if db.Spec.Backup.PITR.BackupStorageName == nil || *db.Spec.Backup.PITR.BackupStorageName == "" {
return nil, fmt.Errorf("no backup storage defined for PITR in %s cluster", db.Name)
}

// use 'date' as default
if dataSource.PITR.Type == "" {
dataSource.PITR.Type = everestv1alpha1.PITRTypeDate
}

var date string
switch dataSource.PITR.Type {
case everestv1alpha1.PITRTypeDate:
if dataSource.PITR.Date == nil {
return nil, errors.New("no date provided for PITR of type 'date'")
}
date = dataSource.PITR.Date.Format(everestv1alpha1.DateFormatSpace)
case everestv1alpha1.PITRTypeLatest:
//nolint:godox
// TODO: figure out why "latest" doesn't work currently for Everest
return nil, errors.New("'latest' type is not supported by Everest yet")
default:
return nil, errors.New("unknown PITR type")
}

return &pxcv1.PITR{
BackupSource: &pxcv1.PXCBackupStatus{
StorageName: pitrStorageName(*db.Spec.Backup.PITR.BackupStorageName),
},
Type: string(dataSource.PITR.Type),
Date: date,
}, nil
}

0 comments on commit 2986214

Please sign in to comment.