From 7c93f302113cedb89e665b977daa9539b8cfa0ac Mon Sep 17 00:00:00 2001 From: wangyelei Date: Thu, 26 Sep 2024 09:28:11 +0800 Subject: [PATCH] chore: collect the log of the backup failed (#8208) --- .../log_collection_controller.go | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/controllers/dataprotection/log_collection_controller.go b/controllers/dataprotection/log_collection_controller.go index 64111440c9d..e361aedd8f9 100644 --- a/controllers/dataprotection/log_collection_controller.go +++ b/controllers/dataprotection/log_collection_controller.go @@ -42,6 +42,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/predicate" dpv1alpha1 "github.com/apecloud/kubeblocks/apis/dataprotection/v1alpha1" + "github.com/apecloud/kubeblocks/pkg/constant" intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil" dprestore "github.com/apecloud/kubeblocks/pkg/dataprotection/restore" dptypes "github.com/apecloud/kubeblocks/pkg/dataprotection/types" @@ -171,18 +172,28 @@ func (r *LogCollectionReconciler) patchBackupStatus(reqCtx intctrlutil.RequestCt if err := r.Client.Get(reqCtx.Ctx, client.ObjectKey{Name: backupName, Namespace: reqCtx.Req.Namespace}, backup); err != nil { return err } - if backup.Status.FailureReason != "" { - return nil - } - errorLogs, err := r.collectErrorLogs(reqCtx, job) - if err != nil { - return fmt.Errorf("collect error logs failed: %s", err.Error()) - } - if errorLogs == "" { - return nil + for i := range backup.Status.Actions { + action := &backup.Status.Actions[i] + if action.ObjectRef == nil { + continue + } + if action.ObjectRef.Kind != constant.JobKind || action.ObjectRef.Name != job.Name { + continue + } + if strings.HasPrefix(action.FailureReason, dptypes.LogCollectorOutput) { + return nil + } + errorLogs, err := r.collectErrorLogs(reqCtx, job) + if err != nil { + return fmt.Errorf("collect error logs failed: %s", err.Error()) + } + if errorLogs == "" { + return nil + } + action.FailureReason = fmt.Sprintf("%s: %s", dptypes.LogCollectorOutput, errorLogs) + return r.Client.Status().Update(reqCtx.Ctx, backup) } - backup.Status.FailureReason = errorLogs - return r.Client.Status().Update(reqCtx.Ctx, backup) + return nil } func (r *LogCollectionReconciler) patchRestoreStatus(reqCtx intctrlutil.RequestCtx, job *batchv1.Job, restoreName string) error {