Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cherry-pick] Fix-issue-6297 #6477

Merged
merged 1 commit into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/pr-ci-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ jobs:
- name: Run staticcheck
uses: dominikh/[email protected]
with:
version: "2022.1.3"
version: "2023.1.3"
install-go: false
1 change: 1 addition & 0 deletions changelogs/unreleased/6477-Lyndon-Li
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 14 additions & 4 deletions pkg/repository/config/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}