From 6f33dc63e724770f16d2e7d212a043699befadc0 Mon Sep 17 00:00:00 2001 From: Lyndon-Li Date: Wed, 15 Jan 2025 16:18:14 +0800 Subject: [PATCH] get maintenance result only for failed jobs Signed-off-by: Lyndon-Li --- pkg/repository/maintenance/maintenance.go | 24 ++++++++++++----- .../maintenance/maintenance_test.go | 26 +++++++++++++++++-- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/pkg/repository/maintenance/maintenance.go b/pkg/repository/maintenance/maintenance.go index b854046ba0..a01a2c464d 100644 --- a/pkg/repository/maintenance/maintenance.go +++ b/pkg/repository/maintenance/maintenance.go @@ -250,11 +250,16 @@ func WaitJobComplete(cli client.Client, ctx context.Context, jobName, ns string, return velerov1api.BackupRepositoryMaintenanceStatus{}, errors.Wrap(err, "error to wait for maintenance job complete") } - log.Info("Maintenance repo complete") + log.Infof("Maintenance repo complete, succeeded %v, failed %v", maintenanceJob.Status.Succeeded, maintenanceJob.Status.Failed) - result, err := getResultFromJob(cli, maintenanceJob) - if err != nil { - log.WithError(err).Warn("Failed to get maintenance job result") + result := "" + if maintenanceJob.Status.Failed > 0 { + if r, err := getResultFromJob(cli, maintenanceJob); err != nil { + log.WithError(err).Warn("Failed to get maintenance job result") + result = "Repo maintenance failed but result is not retrieveable" + } else { + result = r + } } return composeStatusFromJob(maintenanceJob, result), nil @@ -303,9 +308,14 @@ func WaitAllJobsComplete(ctx context.Context, cli client.Client, repo *velerov1a job = updated } - message, err := getResultFromJob(cli, job) - if err != nil { - return nil, errors.Wrapf(err, "error getting maintenance job[%s] result", job.Name) + message := "" + if job.Status.Failed > 0 { + if msg, err := getResultFromJob(cli, job); err != nil { + log.WithError(err).Warnf("Failed to get result of maintenance job %s", job.Name) + message = "Repo maintenance failed but result is not retrieveable" + } else { + message = msg + } } history = append(history, composeStatusFromJob(job, message)) diff --git a/pkg/repository/maintenance/maintenance_test.go b/pkg/repository/maintenance/maintenance_test.go index e4335a61fe..4a543426b3 100644 --- a/pkg/repository/maintenance/maintenance_test.go +++ b/pkg/repository/maintenance/maintenance_test.go @@ -602,13 +602,35 @@ func TestWaitAllJobsComplete(t *testing.T) { expectedError: "error waiting maintenance job[job1] complete: context deadline exceeded", }, { - name: "get result error", + name: "get result error on succeded job", ctx: context.TODO(), runtimeScheme: scheme, kubeClientObj: []runtime.Object{ jobSucceeded1, }, - expectedError: "error getting maintenance job[job1] result: no pod found for job job1", + expectedStatus: []velerov1api.BackupRepositoryMaintenanceStatus{ + { + Result: velerov1api.BackupRepositoryMaintenanceSucceeded, + StartTimestamp: &metav1.Time{Time: now}, + CompleteTimestamp: &metav1.Time{Time: now.Add(time.Hour)}, + }, + }, + }, + { + name: "get result error on failed job", + ctx: context.TODO(), + runtimeScheme: scheme, + kubeClientObj: []runtime.Object{ + jobFailed1, + }, + expectedStatus: []velerov1api.BackupRepositoryMaintenanceStatus{ + { + Result: velerov1api.BackupRepositoryMaintenanceFailed, + StartTimestamp: &metav1.Time{Time: now.Add(time.Hour)}, + CompleteTimestamp: &metav1.Time{Time: now.Add(time.Hour * 2)}, + Message: "Repo maintenance failed but result is not retrieveable", + }, + }, }, { name: "less than limit",