Skip to content

K8SPG-638: handle get latest backup errors on cluster init #954

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

Merged
merged 4 commits into from
Dec 11, 2024
Merged
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
20 changes: 19 additions & 1 deletion percona/watcher/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ func WatchCommitTimestamps(ctx context.Context, cli client.Client, eventChan cha

latestBackup, err := getLatestBackup(ctx, cli, cr)
if err != nil {
log.Error(err, "get latest backup")
if localCr.Status.State != pgv2.AppStateInit || (!errors.Is(err, errRunningBackup) && !errors.Is(err, errNoBackups)) {
log.Error(err, "get latest backup")
}

continue
}

Expand Down Expand Up @@ -104,6 +107,11 @@ func WatchCommitTimestamps(ctx context.Context, cli client.Client, eventChan cha
}
}

var (
errRunningBackup = errors.New("backups are running")
errNoBackups = errors.New("no backups found")
)

func getLatestBackup(ctx context.Context, cli client.Client, cr *pgv2.PerconaPGCluster) (*pgv2.PerconaPGBackup, error) {
backupList := &pgv2.PerconaPGBackupList{}
err := cli.List(ctx, backupList, &client.ListOptions{
Expand All @@ -117,15 +125,25 @@ func getLatestBackup(ctx context.Context, cli client.Client, cr *pgv2.PerconaPGC
return nil, err
}

if len(backupList.Items) == 0 {
return nil, errNoBackups
}

latest := &pgv2.PerconaPGBackup{}
runningBackupExists := false
for _, backup := range backupList.Items {
backup := backup
if latest.Status.CompletedAt == nil || backup.Status.CompletedAt.After(latest.Status.CompletedAt.Time) {
latest = &backup
} else if backup.Status.State == pgv2.BackupStarting || backup.Status.State == pgv2.BackupRunning {
runningBackupExists = true
}
}

if latest.Status.CompletedAt == nil {
if runningBackupExists {
return nil, errRunningBackup
}
return nil, errors.New("no completed backups found")
}

Expand Down
Loading