Skip to content

K8SPSMDB-1339: Validate PiTR target before starting restore #1908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ spec:
lastTransition:
format: date-time
type: string
lastWriteAt:
format: date-time
type: string
latestRestorableTime:
format: date-time
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ spec:
lastTransition:
format: date-time
type: string
lastWriteAt:
format: date-time
type: string
latestRestorableTime:
format: date-time
type: string
Expand Down
6 changes: 6 additions & 0 deletions deploy/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ spec:
lastTransition:
format: date-time
type: string
lastWriteAt:
format: date-time
type: string
latestRestorableTime:
format: date-time
type: string
Expand Down Expand Up @@ -260,6 +263,9 @@ spec:
lastTransition:
format: date-time
type: string
lastWriteAt:
format: date-time
type: string
latestRestorableTime:
format: date-time
type: string
Expand Down
6 changes: 6 additions & 0 deletions deploy/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ spec:
lastTransition:
format: date-time
type: string
lastWriteAt:
format: date-time
type: string
latestRestorableTime:
format: date-time
type: string
Expand Down Expand Up @@ -260,6 +263,9 @@ spec:
lastTransition:
format: date-time
type: string
lastWriteAt:
format: date-time
type: string
latestRestorableTime:
format: date-time
type: string
Expand Down
6 changes: 6 additions & 0 deletions deploy/cw-bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ spec:
lastTransition:
format: date-time
type: string
lastWriteAt:
format: date-time
type: string
latestRestorableTime:
format: date-time
type: string
Expand Down Expand Up @@ -260,6 +263,9 @@ spec:
lastTransition:
format: date-time
type: string
lastWriteAt:
format: date-time
type: string
latestRestorableTime:
format: date-time
type: string
Expand Down
6 changes: 6 additions & 0 deletions e2e-tests/version-service/conf/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ spec:
lastTransition:
format: date-time
type: string
lastWriteAt:
format: date-time
type: string
latestRestorableTime:
format: date-time
type: string
Expand Down Expand Up @@ -260,6 +263,9 @@ spec:
lastTransition:
format: date-time
type: string
lastWriteAt:
format: date-time
type: string
latestRestorableTime:
format: date-time
type: string
Expand Down
34 changes: 18 additions & 16 deletions pkg/apis/psmdb/v1/perconaservermongodbbackup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,26 @@ const (

// PerconaServerMongoDBBackupStatus defines the observed state of PerconaServerMongoDBBackup
type PerconaServerMongoDBBackupStatus struct {
Type defs.BackupType `json:"type,omitempty"`
State BackupState `json:"state,omitempty"`
StartAt *metav1.Time `json:"start,omitempty"`
CompletedAt *metav1.Time `json:"completed,omitempty"`
LastTransition *metav1.Time `json:"lastTransition,omitempty"`
Destination string `json:"destination,omitempty"`
StorageName string `json:"storageName,omitempty"`
S3 *BackupStorageS3Spec `json:"s3,omitempty"`
Azure *BackupStorageAzureSpec `json:"azure,omitempty"`
Filesystem *BackupStorageFilesystemSpec `json:"filesystem,omitempty"`
ReplsetNames []string `json:"replsetNames,omitempty"`
PBMname string `json:"pbmName,omitempty"`
Type defs.BackupType `json:"type,omitempty"`
State BackupState `json:"state,omitempty"`
Destination string `json:"destination,omitempty"`
StorageName string `json:"storageName,omitempty"`
S3 *BackupStorageS3Spec `json:"s3,omitempty"`
Azure *BackupStorageAzureSpec `json:"azure,omitempty"`
Filesystem *BackupStorageFilesystemSpec `json:"filesystem,omitempty"`
ReplsetNames []string `json:"replsetNames,omitempty"`
PBMname string `json:"pbmName,omitempty"`

// Deprecated: Use PBMPods instead
PBMPod string `json:"pbmPod,omitempty"`
PBMPods map[string]string `json:"pbmPods,omitempty"`
Error string `json:"error,omitempty"`
LatestRestorableTime *metav1.Time `json:"latestRestorableTime,omitempty"`
PBMPod string `json:"pbmPod,omitempty"`
PBMPods map[string]string `json:"pbmPods,omitempty"`
Error string `json:"error,omitempty"`

StartAt *metav1.Time `json:"start,omitempty"`
CompletedAt *metav1.Time `json:"completed,omitempty"`
LastWriteAt *metav1.Time `json:"lastWriteAt,omitempty"`
LastTransition *metav1.Time `json:"lastTransition,omitempty"`
LatestRestorableTime *metav1.Time `json:"latestRestorableTime,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
28 changes: 16 additions & 12 deletions pkg/apis/psmdb/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkg/controller/perconaservermongodbbackup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ func (b *Backup) Status(ctx context.Context, cr *api.PerconaServerMongoDBBackup)
status.CompletedAt = &metav1.Time{
Time: time.Unix(meta.LastTransitionTS, 0),
}
status.LastWriteAt = &metav1.Time{
Time: time.Unix(int64(meta.LastWriteTS.T), 0),
}
case defs.StatusStarting:
passed := time.Now().UTC().Sub(time.Unix(meta.StartTS, 0))
if passed >= pbmStartingDeadline {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (r *ReconcilePerconaServerMongoDBRestore) Reconcile(ctx context.Context, re
}

if cr.Status.State == psmdbv1.RestoreStateNew {
err := r.validate(ctx, cr, cluster)
err = r.validate(ctx, cr, cluster)
if err != nil {
if errors.Is(err, errWaitingPBM) {
err = nil
Expand All @@ -209,7 +209,7 @@ func (r *ReconcilePerconaServerMongoDBRestore) Reconcile(ctx context.Context, re
return rr, nil
}

return rr, errors.Wrap(err, "failed to validate restore")
return reconcile.Result{}, errors.Wrap(err, "failed to validate restore")
}

if cluster.Spec.Sharding.Enabled {
Expand Down
24 changes: 23 additions & 1 deletion pkg/controller/perconaservermongodbrestore/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/pkg/errors"

"github.com/percona/percona-backup-mongodb/pbm/defs"

psmdbv1 "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[goimports-reviser] reported by reviewdog 🐶

Suggested change
psmdbv1 "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1"
psmdbv1 "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1"

"github.com/percona/percona-server-mongodb-operator/pkg/psmdb/backup"
)
Expand Down Expand Up @@ -49,5 +48,28 @@ func (r *ReconcilePerconaServerMongoDBRestore) validate(ctx context.Context, cr
return errors.Wrap(err, "failed to validate backup")
}

if pitr := cr.Spec.PITR; pitr != nil {
switch {
case pitr.Type == psmdbv1.PITRestoreTypeDate && pitr.Date != nil:
if bcp.Status.LastWriteAt != nil {
if pitr.Date.Equal(bcp.Status.LastWriteAt) {
return errors.New("backup's last write is equal to target time")
}
if pitr.Date.Before(bcp.Status.LastWriteAt) {
return errors.New("backup's last write is later than target time")
}
}
ts := pitr.Date.Unix()
if _, err := pbmc.GetPITRChunkContains(ctx, ts); err != nil {
return err
}
case pitr.Type == psmdbv1.PITRestoreTypeLatest:
_, err := pbmc.GetLatestTimelinePITR(ctx)
if err != nil {
return err
}
}
}

return nil
}
Loading
Loading