Skip to content

Commit

Permalink
add repo maintain result in history
Browse files Browse the repository at this point in the history
Signed-off-by: Lyndon-Li <lyonghui@vmware.com>
  • Loading branch information
Lyndon-Li committed Dec 23, 2024
1 parent 77f1141 commit 92390e9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
22 changes: 7 additions & 15 deletions pkg/controller/backup_repository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,27 +328,19 @@ func (r *BackupRepoReconciler) runMaintenanceIfDue(ctx context.Context, req *vel
}

func updateRepoMaintenanceHistory(repo *velerov1api.BackupRepository, result velerov1api.BackupRepositoryMaintenanceResult, startTime time.Time, completionTime time.Time, message string) {
length := defaultMaintenanceStatusQueueLength
if len(repo.Status.RecentMaintenanceStatus) < defaultMaintenanceStatusQueueLength {
length = len(repo.Status.RecentMaintenanceStatus) + 1
}

lru := make([]velerov1api.BackupRepositoryMaintenanceStatus, length)

if len(repo.Status.RecentMaintenanceStatus) >= defaultMaintenanceStatusQueueLength {
copy(lru[:length-1], repo.Status.RecentMaintenanceStatus[len(repo.Status.RecentMaintenanceStatus)-defaultMaintenanceStatusQueueLength+1:])
} else {
copy(lru[:length-1], repo.Status.RecentMaintenanceStatus[:])
}

lru[length-1] = velerov1api.BackupRepositoryMaintenanceStatus{
latest := velerov1api.BackupRepositoryMaintenanceStatus{
Result: result,
StartTimestamp: &metav1.Time{Time: startTime},
CompleteTimestamp: &metav1.Time{Time: completionTime},
Message: message,
}

repo.Status.RecentMaintenanceStatus = lru
startingPos := 0
if len(repo.Status.RecentMaintenance) >= defaultMaintenanceStatusQueueLength {
startingPos = len(repo.Status.RecentMaintenance) - defaultMaintenanceStatusQueueLength + 1
}

repo.Status.RecentMaintenance = append(repo.Status.RecentMaintenance[startingPos:], latest)
}

func dueForMaintenance(req *velerov1api.BackupRepository, now time.Time) bool {
Expand Down
14 changes: 7 additions & 7 deletions pkg/controller/backup_repository_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ func TestUpdateRepoMaintenanceHistory(t *testing.T) {
Name: "repo",
},
Status: velerov1api.BackupRepositoryStatus{
RecentMaintenanceStatus: []velerov1api.BackupRepositoryMaintenanceStatus{
RecentMaintenance: []velerov1api.BackupRepositoryMaintenanceStatus{
{
StartTimestamp: &metav1.Time{Time: standardTime.Add(-time.Hour * 24)},
CompleteTimestamp: &metav1.Time{Time: standardTime.Add(-time.Hour * 23)},
Expand All @@ -519,7 +519,7 @@ func TestUpdateRepoMaintenanceHistory(t *testing.T) {
Name: "repo",
},
Status: velerov1api.BackupRepositoryStatus{
RecentMaintenanceStatus: []velerov1api.BackupRepositoryMaintenanceStatus{
RecentMaintenance: []velerov1api.BackupRepositoryMaintenanceStatus{
{
StartTimestamp: &metav1.Time{Time: standardTime.Add(-time.Hour * 24)},
CompleteTimestamp: &metav1.Time{Time: standardTime.Add(-time.Hour * 23)},
Expand All @@ -545,7 +545,7 @@ func TestUpdateRepoMaintenanceHistory(t *testing.T) {
Name: "repo",
},
Status: velerov1api.BackupRepositoryStatus{
RecentMaintenanceStatus: []velerov1api.BackupRepositoryMaintenanceStatus{
RecentMaintenance: []velerov1api.BackupRepositoryMaintenanceStatus{
{
StartTimestamp: &metav1.Time{Time: standardTime.Add(-time.Hour * 24)},
CompleteTimestamp: &metav1.Time{Time: standardTime.Add(-time.Hour * 23)},
Expand Down Expand Up @@ -655,10 +655,10 @@ func TestUpdateRepoMaintenanceHistory(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
updateRepoMaintenanceHistory(test.backupRepo, test.result, standardTime, standardTime.Add(time.Hour), "fake-message-0")

for at := range test.backupRepo.Status.RecentMaintenanceStatus {
assert.Equal(t, test.expectedHistory[at].StartTimestamp.Time, test.backupRepo.Status.RecentMaintenanceStatus[at].StartTimestamp.Time)
assert.Equal(t, test.expectedHistory[at].CompleteTimestamp.Time, test.backupRepo.Status.RecentMaintenanceStatus[at].CompleteTimestamp.Time)
assert.Equal(t, test.expectedHistory[at].Message, test.backupRepo.Status.RecentMaintenanceStatus[at].Message)
for at := range test.backupRepo.Status.RecentMaintenance {
assert.Equal(t, test.expectedHistory[at].StartTimestamp.Time, test.backupRepo.Status.RecentMaintenance[at].StartTimestamp.Time)
assert.Equal(t, test.expectedHistory[at].CompleteTimestamp.Time, test.backupRepo.Status.RecentMaintenance[at].CompleteTimestamp.Time)
assert.Equal(t, test.expectedHistory[at].Message, test.backupRepo.Status.RecentMaintenance[at].Message)
}
})
}
Expand Down

0 comments on commit 92390e9

Please sign in to comment.