Merge pull request #7115 from reasonerjt/wrap-bia-err

Include plugin name in the error message by operations
pull/7143/head
Wenkai Yin(尹文开) 2023-11-20 14:48:18 +08:00 committed by GitHub
commit e3fb94833d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 4 deletions

View File

@ -0,0 +1 @@
Include plugin name in the error message by operations

View File

@ -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"
@ -293,7 +296,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
@ -302,7 +305,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
@ -340,7 +343,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
@ -355,7 +358,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
@ -375,3 +378,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)
}

View File

@ -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"
@ -286,6 +288,7 @@ func TestBackupOperationsReconcile(t *testing.T) {
backupStore.On("PutBackupItemOperations", mock.Anything, mock.Anything).Return(nil)
backupStore.On("PutBackupMetadata", mock.Anything, mock.Anything).Return(nil)
for _, operation := range test.backupOperations {
bia.On("Name").Return("test")
bia.On("Progress", operation.Spec.OperationID, mock.Anything).
Return(velero.OperationProgress{
Completed: test.operationComplete,
@ -308,3 +311,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)
})
}
}