From dc5e9ef5b824289d54f5aa7dbe6ee2a1fb8f6cc6 Mon Sep 17 00:00:00 2001 From: Lyndon-Li Date: Wed, 31 May 2023 14:46:05 +0800 Subject: [PATCH] fix-issue-6297 Signed-off-by: Lyndon-Li --- .github/workflows/pr-ci-check.yml | 3 ++- changelogs/unreleased/6477-Lyndon-Li | 1 + go.mod | 2 +- pkg/repository/config/aws.go | 18 ++++++++++++++---- 4 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 changelogs/unreleased/6477-Lyndon-Li diff --git a/.github/workflows/pr-ci-check.yml b/.github/workflows/pr-ci-check.yml index c87ca494665..cd455a4be85 100644 --- a/.github/workflows/pr-ci-check.yml +++ b/.github/workflows/pr-ci-check.yml @@ -32,4 +32,5 @@ jobs: - name: Run staticcheck uses: dominikh/staticcheck-action@v1.3.0 with: - version: "2022.1.3" \ No newline at end of file + version: "2022.1.3" + install-go: false \ No newline at end of file diff --git a/changelogs/unreleased/6477-Lyndon-Li b/changelogs/unreleased/6477-Lyndon-Li new file mode 100644 index 00000000000..2f414395ec3 --- /dev/null +++ b/changelogs/unreleased/6477-Lyndon-Li @@ -0,0 +1 @@ +Enhance the code because of #6297, the return value of GetBucketRegion is not recorded, as a result, when it fails, we have no way to get the cause \ No newline at end of file diff --git a/go.mod b/go.mod index 5f926abf292..40c775e78ce 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/vmware-tanzu/velero -go 1.18 +go 1.20 require ( cloud.google.com/go/storage v1.21.0 diff --git a/pkg/repository/config/aws.go b/pkg/repository/config/aws.go index a3c63b949c6..ddc7eee9f3b 100644 --- a/pkg/repository/config/aws.go +++ b/pkg/repository/config/aws.go @@ -21,6 +21,8 @@ import ( "context" "os" + goerr "errors" + "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/endpoints" "github.com/aws/aws-sdk-go/aws/session" @@ -76,16 +78,20 @@ func GetS3Credentials(config map[string]string) (credentials.Value, error) { // GetAWSBucketRegion returns the AWS region that a bucket is in, or an error // if the region cannot be determined. func GetAWSBucketRegion(bucket string) (string, error) { - var region string - sess, err := session.NewSession() if err != nil { return "", errors.WithStack(err) } + var region string + var requestErrs []error + for _, partition := range endpoints.DefaultPartitions() { for regionHint := range partition.Regions() { - region, _ = s3manager.GetBucketRegion(context.Background(), sess, bucket, regionHint) + region, err = s3manager.GetBucketRegion(context.Background(), sess, bucket, regionHint) + if err != nil { + requestErrs = append(requestErrs, errors.Wrapf(err, "error to get region with hint %s", regionHint)) + } // we only need to try a single region hint per partition, so break after the first break @@ -96,5 +102,9 @@ func GetAWSBucketRegion(bucket string) (string, error) { } } - return "", errors.New("unable to determine bucket's region") + if requestErrs == nil { + return "", errors.Errorf("unable to determine region by bucket %s", bucket) + } else { + return "", errors.Wrapf(goerr.Join(requestErrs...), "error to get region by bucket %s", bucket) + } }