4
4
"encoding/json"
5
5
"fmt"
6
6
"path/filepath"
7
+ "sort"
7
8
"strings"
8
9
9
10
appsV1 "k8s.io/api/apps/v1"
@@ -65,6 +66,17 @@ func (a *AnalyzeVelero) veleroStatus(analyzer *troubleshootv1beta2.VeleroAnalyze
65
66
oldVeleroRepoType = true
66
67
}
67
68
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
+
68
80
if oldVeleroRepoType == true {
69
81
// old velero (v1.9.x) has a BackupRepositoryTypeRestic
70
82
// get resticrepositories.velero.io
@@ -276,12 +288,12 @@ func (a *AnalyzeVelero) veleroStatus(analyzer *troubleshootv1beta2.VeleroAnalyze
276
288
277
289
results = append (results , analyzeLogs (nodeAgentlogs , "node-agent*" )... )
278
290
results = append (results , analyzeLogs (veleroLogs , "velero*" )... )
279
- results = append (results , analyzeBackups (backups )... )
291
+ results = append (results , analyzeBackups (backups , backupCount )... )
280
292
results = append (results , analyzeBackupStorageLocations (backupStorageLocations )... )
281
293
results = append (results , analyzeDeleteBackupRequests (deleteBackupRequests )... )
282
294
results = append (results , analyzePodVolumeBackups (podVolumeBackups )... )
283
295
results = append (results , analyzePodVolumeRestores (podVolumeRestores )... )
284
- results = append (results , analyzeRestores (restores )... )
296
+ results = append (results , analyzeRestores (restores , restoreCount )... )
285
297
results = append (results , analyzeSchedules (schedules )... )
286
298
results = append (results , analyzeVolumeSnapshotLocations (volumeSnapshotLocations )... )
287
299
@@ -357,9 +369,19 @@ func analyzeResticRepositories(resticRepositories []*restic_types.ResticReposito
357
369
return results
358
370
}
359
371
360
- func analyzeBackups (backups []* velerov1.Backup ) []* AnalyzeResult {
372
+ func analyzeBackups (backups []* velerov1.Backup , count int ) []* AnalyzeResult {
361
373
results := []* AnalyzeResult {}
362
374
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
+
363
385
failedPhases := map [velerov1.BackupPhase ]bool {
364
386
velerov1 .BackupPhaseFailed : true ,
365
387
velerov1 .BackupPhasePartiallyFailed : true ,
@@ -501,10 +523,20 @@ func analyzePodVolumeRestores(podVolumeRestores []*velerov1.PodVolumeRestore) []
501
523
return results
502
524
}
503
525
504
- func analyzeRestores (restores []* velerov1.Restore ) []* AnalyzeResult {
526
+ func analyzeRestores (restores []* velerov1.Restore , count int ) []* AnalyzeResult {
505
527
results := []* AnalyzeResult {}
506
528
failures := 0
507
529
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
+
508
540
if len (restores ) > 0 {
509
541
510
542
failedPhases := map [velerov1.RestorePhase ]bool {
@@ -513,6 +545,10 @@ func analyzeRestores(restores []*velerov1.Restore) []*AnalyzeResult {
513
545
velerov1 .RestorePhaseFailedValidation : true ,
514
546
}
515
547
548
+ failureReasons := []string {
549
+ "found a restore with status \" InProgress\" during the server starting, mark it as \" Failed\" " ,
550
+ }
551
+
516
552
for _ , restore := range restores {
517
553
if failedPhases [restore .Status .Phase ] {
518
554
result := & AnalyzeResult {
0 commit comments