Skip to content

Commit

Permalink
Merge pull request #2 from beclab/feat/backup-list-api
Browse files Browse the repository at this point in the history
feat: modify backup list api
  • Loading branch information
aby913 authored May 16, 2024
2 parents 581c737 + 677e125 commit 9432e45
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 6 deletions.
78 changes: 78 additions & 0 deletions pkg/modules/backup/v1/backup_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v1
import (
"context"
"strings"
"time"

sysv1 "bytetrade.io/web3os/backup-server/pkg/apis/sys.bytetrade.io/v1"
"bytetrade.io/web3os/backup-server/pkg/client"
Expand Down Expand Up @@ -269,6 +270,49 @@ func (o *BackupPlan) hasInProgressBackup(ctx context.Context) (bool, error) {
// return nil
// }

func (o *BackupPlan) GetLatest(ctx context.Context, name string) (*ListBackupsDetails, error) {
bc, err := o.manager.GetBackupConfig(ctx, name)
if err != nil {
return nil, err
}

if err = o.manager.FormatSysBackupTimeofDay(bc); err != nil {
log.Errorf("convert time to timestamp error: %v", err)
}

rs := ListBackupsDetails{
Name: name,
SnapshotFrequency: bc.Spec.BackupPolicy.SnapshotFrequency,
}

l, err := o.manager.ListSysBackups(ctx, name)
if err != nil {
return nil, err
}

var latestSysBackup *sysv1.Backup
if l != nil && l.Items != nil && len(l.Items) > 0 {
latestSysBackup = &l.Items[0]
}

if latestSysBackup != nil && latestSysBackup.Spec.Size != nil {
rs.Size = latestSysBackup.Spec.Size
}

phase, message := o.GetBackupResult(latestSysBackup)

rs.NextBackupTimestamp = o.GetNextBackupTime(*bc.Spec.BackupPolicy)
rs.Phase = phase
rs.FailedMessage = message

if latestSysBackup != nil {
rs.SnapshotName = latestSysBackup.Name
rs.CreationTimestamp = latestSysBackup.ObjectMeta.CreationTimestamp.Unix()
}

return &rs, nil
}

func (o *BackupPlan) Get(ctx context.Context, name string) (*ResponseDescribeBackup, error) {
bc, err := o.manager.GetBackupConfig(ctx, name)
if err != nil {
Expand Down Expand Up @@ -296,6 +340,7 @@ func (o *BackupPlan) Get(ctx context.Context, name string) (*ResponseDescribeBac
if phase == nil || middlewarePhase == nil {
continue
}

if *phase == velero.VeleroBackupCompleted && util.ListContains([]string{velero.Succeed, velero.Success}, *middlewarePhase) {
if i.Spec.Size != nil {
r.Size = i.Spec.Size
Expand All @@ -310,3 +355,36 @@ func (o *BackupPlan) Get(ctx context.Context, name string) (*ResponseDescribeBac
func (o *BackupPlan) Del(ctx context.Context, name string) error {
return errors.New("to be implement")
}

func (o *BackupPlan) GetBackupResult(sysBackup *sysv1.Backup) (string, string) {
if sysBackup == nil {
return "", ""
}
resticPhase := util.ListContains([]string{velero.Succeed, velero.Success, velero.VeleroBackupCompleted}, *sysBackup.Spec.ResticPhase)
middlewarePhase := util.ListContains([]string{velero.Succeed, velero.Success, velero.VeleroBackupCompleted}, *sysBackup.Spec.MiddleWarePhase)

if resticPhase && middlewarePhase && *sysBackup.Spec.Phase == velero.VeleroBackupCompleted {
return velero.VeleroBackupCompleted, ""
} else if !resticPhase {
return velero.Failed, *sysBackup.Spec.ResticFailedMessage
} else {
return velero.Failed, *sysBackup.Spec.FailedMessage
}
}

func (o *BackupPlan) GetNextBackupTime(bp sysv1.BackupPolicy) *int64 {
var res int64
var n = time.Now().Local()
var prefix int64 = util.ParseToInt64(bp.TimesOfDay) / 1000
var incr = util.ParseToNextUnixTime(bp.SnapshotFrequency, bp.TimesOfDay, bp.DayOfWeek)

switch bp.SnapshotFrequency {
case "@weekly":
var midweek = util.GetFirstDayOfWeek(n).AddDate(0, 0, bp.DayOfWeek)
res = midweek.Unix() + incr + prefix
default:
var midnight = time.Date(n.Year(), n.Month(), n.Day(), 0, 0, 0, 0, n.Location())
res = midnight.Unix() + incr + prefix
}
return &res
}
7 changes: 4 additions & 3 deletions pkg/modules/backup/v1/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,18 @@ func (h *Handler) list(req *restful.Request, resp *restful.Response) {
return
}

var res []*ResponseDescribeBackup
var res []*ListBackupsDetails

for _, b := range l.Items {
var r *ResponseDescribeBackup
r, err = NewBackupPlan(owner, h.factory, h.veleroBackupManager).Get(ctx, b.Name)
var r *ListBackupsDetails
r, err = NewBackupPlan(owner, h.factory, h.veleroBackupManager).GetLatest(ctx, b.Name)
if err != nil {
log.Warnf("failed to get backup plan %q: %v", b.Name, err)
} else {
res = append(res, r)
}
}

response.Success(resp, response.NewListResult(res))
}

Expand Down
25 changes: 22 additions & 3 deletions pkg/modules/backup/v1/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ type BackupCreate struct {
}

type Snapshot struct {
Name string `json:"name"`
Name string `json:"name,omitempty"`

CreationTimestamp int64 `json:"creationTimestamp"`
CreationTimestamp int64 `json:"creationTimestamp,omitempty"`
NextBackupTimestamp *int64 `json:"nextBackupTimestamp,omitempty"`

Size *int64 `json:"size"`
Size *int64 `json:"size,omitempty"`

Phase *string `json:"phase"`

Expand Down Expand Up @@ -78,6 +79,24 @@ type SnapshotDetails struct {
BackupConfigName string `json:"backupConfigName"`
}

type ListBackupsDetails struct {
Name string `json:"name"`

Size *int64 `json:"size,omitempty"`

SnapshotName string `json:"snapshotName"`

SnapshotFrequency string `json:"snapshotFrequency"`

CreationTimestamp int64 `json:"creationTimestamp,omitempty"`

NextBackupTimestamp *int64 `json:"nextBackupTimestamp,omitempty"`

Phase string `json:"phase,omitempty"`

FailedMessage string `json:"failedMessage,omitempty"`
}

type ResponseDescribeBackup struct {
Name string `json:"name"`

Expand Down
29 changes: 29 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ import (
"github.com/pkg/errors"
)

func ParseToInt64(v string) int64 {
i, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return 0
}
return i
}

// ToJSON returns a json string
func ToJSON(v any) string {
var buf bytes.Buffer
Expand Down Expand Up @@ -157,3 +165,24 @@ func ParseLocalToTimestamp(value string) (string, error) {
var utcTimeStr = strconv.FormatInt(utcTime.UnixMilli(), 10)
return utcTimeStr, nil
}

func ParseToNextUnixTime(frequency, timesOfDay string, dayOfWeek int) int64 {
switch frequency {
case "@daily":
return 86400
case "@weekly":
return 604800
default:
return 0
}
}

func GetFirstDayOfWeek(t time.Time) time.Time {
weekday := int(t.Weekday())
if weekday == 0 {
weekday = 7
}
offset := -time.Duration(weekday-1) * 24 * time.Hour

return t.Add(offset).Truncate(24 * time.Hour)
}

0 comments on commit 9432e45

Please sign in to comment.