add repo maintain result in history

Signed-off-by: Lyndon-Li <lyonghui@vmware.com>
pull/8532/head
Lyndon-Li 2024-12-19 15:54:25 +08:00
parent c9bfd33077
commit 3b2c50b459
6 changed files with 32 additions and 6 deletions

View File

@ -0,0 +1 @@
Fix issue #7810, add maintenance history for backupRepository CRs

View File

@ -119,6 +119,12 @@ spec:
description: Message is a message about the current status of description: Message is a message about the current status of
the repo maintenance. the repo maintenance.
type: string type: string
result:
description: Result is the result of the repo maintenance.
enum:
- Succeeded
- Failed
type: string
startTimestamp: startTimestamp:
description: StartTimestamp is the start time of the repo maintenance. description: StartTimestamp is the start time of the repo maintenance.
format: date-time format: date-time

File diff suppressed because one or more lines are too long

View File

@ -81,7 +81,20 @@ type BackupRepositoryStatus struct {
RecentMaintenanceStatus []BackupRepositoryMaintenanceStatus `json:"recentMaintenanceStatus,omitempty"` RecentMaintenanceStatus []BackupRepositoryMaintenanceStatus `json:"recentMaintenanceStatus,omitempty"`
} }
// BackupRepositoryMaintenanceResult represents the result of a repo maintenance.
// +kubebuilder:validation:Enum=Succeeded;Failed
type BackupRepositoryMaintenanceResult string
const (
BackupRepositoryMaintenanceSucceeded BackupRepositoryMaintenanceResult = "Succeeded"
BackupRepositoryMaintenanceFailed BackupRepositoryMaintenanceResult = "Failed"
)
type BackupRepositoryMaintenanceStatus struct { type BackupRepositoryMaintenanceStatus struct {
// Result is the result of the repo maintenance.
// +optional
Result BackupRepositoryMaintenanceResult `json:"result,omitempty"`
// StartTimestamp is the start time of the repo maintenance. // StartTimestamp is the start time of the repo maintenance.
// +optional // +optional
// +nullable // +nullable

View File

@ -316,18 +316,18 @@ func (r *BackupRepoReconciler) runMaintenanceIfDue(ctx context.Context, req *vel
if err := r.repositoryManager.PruneRepo(req); err != nil { if err := r.repositoryManager.PruneRepo(req); err != nil {
log.WithError(err).Warn("error pruning repository") log.WithError(err).Warn("error pruning repository")
return r.patchBackupRepository(ctx, req, func(rr *velerov1api.BackupRepository) { return r.patchBackupRepository(ctx, req, func(rr *velerov1api.BackupRepository) {
updateRepoMaintenanceHistory(rr, startTime, r.clock.Now(), err.Error()) updateRepoMaintenanceHistory(rr, velerov1api.BackupRepositoryMaintenanceFailed, startTime, r.clock.Now(), err.Error())
}) })
} }
return r.patchBackupRepository(ctx, req, func(rr *velerov1api.BackupRepository) { return r.patchBackupRepository(ctx, req, func(rr *velerov1api.BackupRepository) {
completionTime := r.clock.Now() completionTime := r.clock.Now()
rr.Status.LastMaintenanceTime = &metav1.Time{Time: completionTime} rr.Status.LastMaintenanceTime = &metav1.Time{Time: completionTime}
updateRepoMaintenanceHistory(rr, startTime, completionTime, "") updateRepoMaintenanceHistory(rr, velerov1api.BackupRepositoryMaintenanceSucceeded, startTime, completionTime, "")
}) })
} }
func updateRepoMaintenanceHistory(repo *velerov1api.BackupRepository, startTime time.Time, completionTime time.Time, result string) { func updateRepoMaintenanceHistory(repo *velerov1api.BackupRepository, result velerov1api.BackupRepositoryMaintenanceResult, startTime time.Time, completionTime time.Time, message string) {
length := defaultMaintenanceStatusQueueLength length := defaultMaintenanceStatusQueueLength
if len(repo.Status.RecentMaintenanceStatus) < defaultMaintenanceStatusQueueLength { if len(repo.Status.RecentMaintenanceStatus) < defaultMaintenanceStatusQueueLength {
length = len(repo.Status.RecentMaintenanceStatus) + 1 length = len(repo.Status.RecentMaintenanceStatus) + 1
@ -342,9 +342,10 @@ func updateRepoMaintenanceHistory(repo *velerov1api.BackupRepository, startTime
} }
lru[length-1] = velerov1api.BackupRepositoryMaintenanceStatus{ lru[length-1] = velerov1api.BackupRepositoryMaintenanceStatus{
Result: result,
StartTimestamp: &metav1.Time{Time: startTime}, StartTimestamp: &metav1.Time{Time: startTime},
CompleteTimestamp: &metav1.Time{Time: completionTime}, CompleteTimestamp: &metav1.Time{Time: completionTime},
Message: result, Message: message,
} }
repo.Status.RecentMaintenanceStatus = lru repo.Status.RecentMaintenanceStatus = lru

View File

@ -573,11 +573,13 @@ func TestUpdateRepoMaintenanceHistory(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
backupRepo *velerov1api.BackupRepository backupRepo *velerov1api.BackupRepository
result velerov1api.BackupRepositoryMaintenanceResult
expectedHistory []velerov1api.BackupRepositoryMaintenanceStatus expectedHistory []velerov1api.BackupRepositoryMaintenanceStatus
}{ }{
{ {
name: "empty history", name: "empty history",
backupRepo: backupRepoWithoutHistory, backupRepo: backupRepoWithoutHistory,
result: velerov1api.BackupRepositoryMaintenanceSucceeded,
expectedHistory: []velerov1api.BackupRepositoryMaintenanceStatus{ expectedHistory: []velerov1api.BackupRepositoryMaintenanceStatus{
{ {
StartTimestamp: &metav1.Time{Time: standardTime}, StartTimestamp: &metav1.Time{Time: standardTime},
@ -589,6 +591,7 @@ func TestUpdateRepoMaintenanceHistory(t *testing.T) {
{ {
name: "less than history queue length", name: "less than history queue length",
backupRepo: backupRepoWithHistory, backupRepo: backupRepoWithHistory,
result: velerov1api.BackupRepositoryMaintenanceSucceeded,
expectedHistory: []velerov1api.BackupRepositoryMaintenanceStatus{ expectedHistory: []velerov1api.BackupRepositoryMaintenanceStatus{
{ {
StartTimestamp: &metav1.Time{Time: standardTime.Add(-time.Hour * 24)}, StartTimestamp: &metav1.Time{Time: standardTime.Add(-time.Hour * 24)},
@ -605,6 +608,7 @@ func TestUpdateRepoMaintenanceHistory(t *testing.T) {
{ {
name: "full history", name: "full history",
backupRepo: backupRepoWithFullHistory, backupRepo: backupRepoWithFullHistory,
result: velerov1api.BackupRepositoryMaintenanceFailed,
expectedHistory: []velerov1api.BackupRepositoryMaintenanceStatus{ expectedHistory: []velerov1api.BackupRepositoryMaintenanceStatus{
{ {
StartTimestamp: &metav1.Time{Time: standardTime.Add(-time.Hour * 22)}, StartTimestamp: &metav1.Time{Time: standardTime.Add(-time.Hour * 22)},
@ -626,6 +630,7 @@ func TestUpdateRepoMaintenanceHistory(t *testing.T) {
{ {
name: "over full history", name: "over full history",
backupRepo: backupRepoWithOverFullHistory, backupRepo: backupRepoWithOverFullHistory,
result: velerov1api.BackupRepositoryMaintenanceFailed,
expectedHistory: []velerov1api.BackupRepositoryMaintenanceStatus{ expectedHistory: []velerov1api.BackupRepositoryMaintenanceStatus{
{ {
StartTimestamp: &metav1.Time{Time: standardTime.Add(-time.Hour * 20)}, StartTimestamp: &metav1.Time{Time: standardTime.Add(-time.Hour * 20)},
@ -648,7 +653,7 @@ func TestUpdateRepoMaintenanceHistory(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
updateRepoMaintenanceHistory(test.backupRepo, standardTime, standardTime.Add(time.Hour), "fake-message-0") updateRepoMaintenanceHistory(test.backupRepo, test.result, standardTime, standardTime.Add(time.Hour), "fake-message-0")
for at := range test.backupRepo.Status.RecentMaintenanceStatus { 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].StartTimestamp.Time, test.backupRepo.Status.RecentMaintenanceStatus[at].StartTimestamp.Time)