diff --git a/changelogs/unreleased/7115-reasonerjt b/changelogs/unreleased/7115-reasonerjt new file mode 100644 index 0000000000..5824427b07 --- /dev/null +++ b/changelogs/unreleased/7115-reasonerjt @@ -0,0 +1 @@ +Include plugin name in the error message by operations \ No newline at end of file diff --git a/pkg/controller/backup_operations_controller.go b/pkg/controller/backup_operations_controller.go index f00e9c2056..820936e601 100644 --- a/pkg/controller/backup_operations_controller.go +++ b/pkg/controller/backup_operations_controller.go @@ -19,8 +19,11 @@ package controller import ( "bytes" "context" + "fmt" "time" + v2 "github.com/vmware-tanzu/velero/pkg/plugin/velero/backupitemaction/v2" + "github.com/pkg/errors" "github.com/sirupsen/logrus" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -291,7 +294,7 @@ func getBackupItemOperationProgress( if err != nil { operation.Status.Phase = itemoperation.OperationPhaseFailed operation.Status.Error = err.Error() - errs = append(errs, err.Error()) + errs = append(errs, wrapErrMsg(err.Error(), bia)) changes = true failedCount++ continue @@ -300,7 +303,7 @@ func getBackupItemOperationProgress( if err != nil { operation.Status.Phase = itemoperation.OperationPhaseFailed operation.Status.Error = err.Error() - errs = append(errs, err.Error()) + errs = append(errs, wrapErrMsg(err.Error(), bia)) changes = true failedCount++ continue @@ -338,7 +341,7 @@ func getBackupItemOperationProgress( if operationProgress.Err != "" { operation.Status.Phase = itemoperation.OperationPhaseFailed operation.Status.Error = operationProgress.Err - errs = append(errs, operationProgress.Err) + errs = append(errs, wrapErrMsg(operationProgress.Err, bia)) changes = true failedCount++ continue @@ -353,7 +356,7 @@ func getBackupItemOperationProgress( _ = bia.Cancel(operation.Spec.OperationID, backup) operation.Status.Phase = itemoperation.OperationPhaseFailed operation.Status.Error = "Asynchronous action timed out" - errs = append(errs, operation.Status.Error) + errs = append(errs, wrapErrMsg(operation.Status.Error, bia)) changes = true failedCount++ continue @@ -373,3 +376,15 @@ func getBackupItemOperationProgress( } return inProgressOperations, changes, completedCount, failedCount, errs } + +// wrap the error message to include the BIA name +func wrapErrMsg(errMsg string, bia v2.BackupItemAction) string { + plugin := "unknown" + if bia != nil { + plugin = bia.Name() + } + if len(errMsg) > 0 { + errMsg += ", " + } + return fmt.Sprintf("%splugin: %s", errMsg, plugin) +} diff --git a/pkg/controller/backup_operations_controller_test.go b/pkg/controller/backup_operations_controller_test.go index 417294a7c3..383a6a3e77 100644 --- a/pkg/controller/backup_operations_controller_test.go +++ b/pkg/controller/backup_operations_controller_test.go @@ -21,6 +21,8 @@ import ( "testing" "time" + v2 "github.com/vmware-tanzu/velero/pkg/plugin/velero/backupitemaction/v2" + "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" @@ -308,3 +310,40 @@ func TestBackupOperationsReconcile(t *testing.T) { }) } } + +func TestWrapErrMsg(t *testing.T) { + bia2 := &biav2mocks.BackupItemAction{} + bia2.On("Name").Return("test-bia") + cases := []struct { + name string + inputErr string + plugin v2.BackupItemAction + expect string + }{ + { + name: "empty error message", + inputErr: "", + plugin: bia2, + expect: "plugin: test-bia", + }, + { + name: "nil bia", + inputErr: "some error happened", + plugin: nil, + expect: "some error happened, plugin: unknown", + }, + { + name: "regular error and bia", + inputErr: "some error happened", + plugin: bia2, + expect: "some error happened, plugin: test-bia", + }, + } + + for _, test := range cases { + t.Run(test.name, func(t *testing.T) { + got := wrapErrMsg(test.inputErr, test.plugin) + assert.Equal(t, test.expect, got) + }) + } +}