Skip to content

Commit d0416b6

Browse files
committed
limit velero backups & restores to most recent object
1 parent 2772722 commit d0416b6

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

pkg/analyze/velero.go

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"path/filepath"
7+
"sort"
78
"strings"
89

910
appsV1 "k8s.io/api/apps/v1"
@@ -65,6 +66,17 @@ func (a *AnalyzeVelero) veleroStatus(analyzer *troubleshootv1beta2.VeleroAnalyze
6566
oldVeleroRepoType = true
6667
}
6768

69+
// default to only the most recent Backup and Restore object if not specified in the analyzer spec
70+
backupCount := analyzer.BackupsCount
71+
if backupCount <= 0 {
72+
backupCount = 1
73+
}
74+
75+
restoreCount := analyzer.RestoresCount
76+
if restoreCount <= 0 {
77+
restoreCount = 1
78+
}
79+
6880
if oldVeleroRepoType == true {
6981
// old velero (v1.9.x) has a BackupRepositoryTypeRestic
7082
// get resticrepositories.velero.io
@@ -276,12 +288,12 @@ func (a *AnalyzeVelero) veleroStatus(analyzer *troubleshootv1beta2.VeleroAnalyze
276288

277289
results = append(results, analyzeLogs(nodeAgentlogs, "node-agent*")...)
278290
results = append(results, analyzeLogs(veleroLogs, "velero*")...)
279-
results = append(results, analyzeBackups(backups)...)
291+
results = append(results, analyzeBackups(backups, backupCount)...)
280292
results = append(results, analyzeBackupStorageLocations(backupStorageLocations)...)
281293
results = append(results, analyzeDeleteBackupRequests(deleteBackupRequests)...)
282294
results = append(results, analyzePodVolumeBackups(podVolumeBackups)...)
283295
results = append(results, analyzePodVolumeRestores(podVolumeRestores)...)
284-
results = append(results, analyzeRestores(restores)...)
296+
results = append(results, analyzeRestores(restores, restoreCount)...)
285297
results = append(results, analyzeSchedules(schedules)...)
286298
results = append(results, analyzeVolumeSnapshotLocations(volumeSnapshotLocations)...)
287299

@@ -357,9 +369,19 @@ func analyzeResticRepositories(resticRepositories []*restic_types.ResticReposito
357369
return results
358370
}
359371

360-
func analyzeBackups(backups []*velerov1.Backup) []*AnalyzeResult {
372+
func analyzeBackups(backups []*velerov1.Backup, count int) []*AnalyzeResult {
361373
results := []*AnalyzeResult{}
362374

375+
// Sort backups by StartTimestamp in descending order
376+
sort.SliceStable(backups, func(i, j int) bool {
377+
return backups[i].Status.StartTimestamp.After(backups[j].Status.StartTimestamp.Time)
378+
})
379+
380+
// Limit to the most recent backupCount items
381+
if len(backups) > count {
382+
backups = backups[:count]
383+
}
384+
363385
failedPhases := map[velerov1.BackupPhase]bool{
364386
velerov1.BackupPhaseFailed: true,
365387
velerov1.BackupPhasePartiallyFailed: true,
@@ -501,10 +523,20 @@ func analyzePodVolumeRestores(podVolumeRestores []*velerov1.PodVolumeRestore) []
501523
return results
502524
}
503525

504-
func analyzeRestores(restores []*velerov1.Restore) []*AnalyzeResult {
526+
func analyzeRestores(restores []*velerov1.Restore, count int) []*AnalyzeResult {
505527
results := []*AnalyzeResult{}
506528
failures := 0
507529

530+
// Sort restores by StartTimestamp in descending order
531+
sort.SliceStable(restores, func(i, j int) bool {
532+
return restores[i].Status.StartTimestamp.After(restores[j].Status.StartTimestamp.Time)
533+
})
534+
535+
// Limit to the most recent restoreCount items
536+
if len(restores) > count {
537+
restores = restores[:count]
538+
}
539+
508540
if len(restores) > 0 {
509541

510542
failedPhases := map[velerov1.RestorePhase]bool{
@@ -513,6 +545,10 @@ func analyzeRestores(restores []*velerov1.Restore) []*AnalyzeResult {
513545
velerov1.RestorePhaseFailedValidation: true,
514546
}
515547

548+
failureReasons := []string{
549+
"found a restore with status \"InProgress\" during the server starting, mark it as \"Failed\"",
550+
}
551+
516552
for _, restore := range restores {
517553
if failedPhases[restore.Status.Phase] {
518554
result := &AnalyzeResult{

pkg/apis/troubleshoot/v1beta2/analyzer_shared.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ type CephStatusAnalyze struct {
197197
}
198198

199199
type VeleroAnalyze struct {
200-
AnalyzeMeta `json:",inline" yaml:",inline"`
200+
AnalyzeMeta `json:",inline" yaml:",inline"`
201+
BackupsCount int `json:"backupCount,omitempty" yaml:"backupCount,omitempty"`
202+
RestoresCount int `json:"restoreCount,omitempty" yaml:"restoreCount,omitempty"`
201203
}
202204

203205
type LonghornAnalyze struct {

0 commit comments

Comments
 (0)