Merge pull request #8297 from kaovilai/aws-getbucketregion-hint
Set hinting region to use for GetBucketRegion() in pkg/repository/config/aws.gopull/8509/head
commit
eb48cbd60f
|
@ -0,0 +1 @@
|
||||||
|
Set hinting region to use for GetBucketRegion() in pkg/repository/config/aws.go
|
|
@ -124,13 +124,24 @@ func GetS3Credentials(config map[string]string) (*aws.Credentials, error) {
|
||||||
|
|
||||||
// GetAWSBucketRegion returns the AWS region that a bucket is in, or an error
|
// GetAWSBucketRegion returns the AWS region that a bucket is in, or an error
|
||||||
// if the region cannot be determined.
|
// if the region cannot be determined.
|
||||||
func GetAWSBucketRegion(bucket string) (string, error) {
|
// It will use us-east-1 as hinting server and requires config param to use as credentials
|
||||||
cfg, err := awsconfig.LoadDefaultConfig(context.Background())
|
func GetAWSBucketRegion(bucket string, config map[string]string) (string, error) {
|
||||||
|
cfg, err := awsconfig.LoadDefaultConfig(context.Background(), awsconfig.WithCredentialsProvider(
|
||||||
|
aws.CredentialsProviderFunc(
|
||||||
|
func(context.Context) (aws.Credentials, error) {
|
||||||
|
s3creds, err := GetS3Credentials(config)
|
||||||
|
if s3creds == nil {
|
||||||
|
return aws.Credentials{}, err
|
||||||
|
}
|
||||||
|
return *s3creds, err
|
||||||
|
},
|
||||||
|
),
|
||||||
|
))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.WithStack(err)
|
return "", errors.WithStack(err)
|
||||||
}
|
}
|
||||||
client := s3.NewFromConfig(cfg)
|
client := s3.NewFromConfig(cfg)
|
||||||
region, err := s3manager.GetBucketRegion(context.Background(), client, bucket)
|
region, err := s3manager.GetBucketRegion(context.Background(), client, bucket, func(o *s3.Options) { o.Region = "us-east-1" })
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.WithStack(err)
|
return "", errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,7 +72,7 @@ func getRepoPrefix(location *velerov1api.BackupStorageLocation) (string, error)
|
||||||
var err error
|
var err error
|
||||||
region := location.Spec.Config["region"]
|
region := location.Spec.Config["region"]
|
||||||
if region == "" {
|
if region == "" {
|
||||||
region, err = getAWSBucketRegion(bucket)
|
region, err = getAWSBucketRegion(bucket, location.Spec.Config)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.Wrapf(err, "failed to detect the region via bucket: %s", bucket)
|
return "", errors.Wrapf(err, "failed to detect the region via bucket: %s", bucket)
|
||||||
|
|
|
@ -30,7 +30,7 @@ func TestGetRepoIdentifier(t *testing.T) {
|
||||||
name string
|
name string
|
||||||
bsl *velerov1api.BackupStorageLocation
|
bsl *velerov1api.BackupStorageLocation
|
||||||
repoName string
|
repoName string
|
||||||
getAWSBucketRegion func(string) (string, error)
|
getAWSBucketRegion func(s string, config map[string]string) (string, error)
|
||||||
expected string
|
expected string
|
||||||
expectedErr string
|
expectedErr string
|
||||||
}{
|
}{
|
||||||
|
@ -101,7 +101,7 @@ func TestGetRepoIdentifier(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
repoName: "repo-1",
|
repoName: "repo-1",
|
||||||
getAWSBucketRegion: func(string) (string, error) {
|
getAWSBucketRegion: func(s string, config map[string]string) (string, error) {
|
||||||
return "", errors.New("no region found")
|
return "", errors.New("no region found")
|
||||||
},
|
},
|
||||||
expected: "",
|
expected: "",
|
||||||
|
@ -120,7 +120,7 @@ func TestGetRepoIdentifier(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
repoName: "repo-1",
|
repoName: "repo-1",
|
||||||
getAWSBucketRegion: func(string) (string, error) {
|
getAWSBucketRegion: func(string, map[string]string) (string, error) {
|
||||||
return "eu-west-1", nil
|
return "eu-west-1", nil
|
||||||
},
|
},
|
||||||
expected: "s3:s3-eu-west-1.amazonaws.com/bucket/restic/repo-1",
|
expected: "s3:s3-eu-west-1.amazonaws.com/bucket/restic/repo-1",
|
||||||
|
@ -139,7 +139,7 @@ func TestGetRepoIdentifier(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
repoName: "repo-1",
|
repoName: "repo-1",
|
||||||
getAWSBucketRegion: func(string) (string, error) {
|
getAWSBucketRegion: func(s string, config map[string]string) (string, error) {
|
||||||
return "eu-west-1", nil
|
return "eu-west-1", nil
|
||||||
},
|
},
|
||||||
expected: "s3:s3-eu-west-1.amazonaws.com/bucket/prefix/restic/repo-1",
|
expected: "s3:s3-eu-west-1.amazonaws.com/bucket/prefix/restic/repo-1",
|
||||||
|
@ -161,7 +161,7 @@ func TestGetRepoIdentifier(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
repoName: "repo-1",
|
repoName: "repo-1",
|
||||||
getAWSBucketRegion: func(string) (string, error) {
|
getAWSBucketRegion: func(s string, config map[string]string) (string, error) {
|
||||||
return "eu-west-1", nil
|
return "eu-west-1", nil
|
||||||
},
|
},
|
||||||
expected: "s3:alternate-url/bucket/prefix/restic/repo-1",
|
expected: "s3:alternate-url/bucket/prefix/restic/repo-1",
|
||||||
|
@ -183,7 +183,7 @@ func TestGetRepoIdentifier(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
repoName: "aws-repo",
|
repoName: "aws-repo",
|
||||||
getAWSBucketRegion: func(string) (string, error) {
|
getAWSBucketRegion: func(s string, config map[string]string) (string, error) {
|
||||||
return "eu-west-1", nil
|
return "eu-west-1", nil
|
||||||
},
|
},
|
||||||
expected: "s3:s3-us-west-1.amazonaws.com/bucket/prefix/restic/aws-repo",
|
expected: "s3:s3-us-west-1.amazonaws.com/bucket/prefix/restic/aws-repo",
|
||||||
|
@ -205,7 +205,7 @@ func TestGetRepoIdentifier(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
repoName: "aws-repo",
|
repoName: "aws-repo",
|
||||||
getAWSBucketRegion: func(string) (string, error) {
|
getAWSBucketRegion: func(s string, config map[string]string) (string, error) {
|
||||||
return "eu-west-1", nil
|
return "eu-west-1", nil
|
||||||
},
|
},
|
||||||
expected: "s3:alternate-url-with-trailing-slash/bucket/prefix/restic/aws-repo",
|
expected: "s3:alternate-url-with-trailing-slash/bucket/prefix/restic/aws-repo",
|
||||||
|
|
|
@ -529,7 +529,7 @@ func getStorageVariables(backupLocation *velerov1api.BackupStorageLocation, repo
|
||||||
var err error
|
var err error
|
||||||
if s3URL == "" {
|
if s3URL == "" {
|
||||||
if region == "" {
|
if region == "" {
|
||||||
region, err = getS3BucketRegion(bucket)
|
region, err = getS3BucketRegion(bucket, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return map[string]string{}, errors.Wrap(err, "error get s3 bucket region")
|
return map[string]string{}, errors.Wrap(err, "error get s3 bucket region")
|
||||||
}
|
}
|
||||||
|
|
|
@ -222,7 +222,7 @@ func TestGetStorageVariables(t *testing.T) {
|
||||||
repoName string
|
repoName string
|
||||||
repoBackend string
|
repoBackend string
|
||||||
repoConfig map[string]string
|
repoConfig map[string]string
|
||||||
getS3BucketRegion func(string) (string, error)
|
getS3BucketRegion func(bucket string, config map[string]string) (string, error)
|
||||||
expected map[string]string
|
expected map[string]string
|
||||||
expectedErr string
|
expectedErr string
|
||||||
}{
|
}{
|
||||||
|
@ -291,7 +291,7 @@ func TestGetStorageVariables(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
getS3BucketRegion: func(bucket string) (string, error) {
|
getS3BucketRegion: func(bucket string, config map[string]string) (string, error) {
|
||||||
return "region from bucket: " + bucket, nil
|
return "region from bucket: " + bucket, nil
|
||||||
},
|
},
|
||||||
repoBackend: "fake-repo-type",
|
repoBackend: "fake-repo-type",
|
||||||
|
@ -313,7 +313,7 @@ func TestGetStorageVariables(t *testing.T) {
|
||||||
Config: map[string]string{},
|
Config: map[string]string{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
getS3BucketRegion: func(bucket string) (string, error) {
|
getS3BucketRegion: func(bucket string, config map[string]string) (string, error) {
|
||||||
return "", errors.New("fake error")
|
return "", errors.New("fake error")
|
||||||
},
|
},
|
||||||
expected: map[string]string{},
|
expected: map[string]string{},
|
||||||
|
@ -339,7 +339,7 @@ func TestGetStorageVariables(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
getS3BucketRegion: func(bucket string) (string, error) {
|
getS3BucketRegion: func(bucket string, config map[string]string) (string, error) {
|
||||||
return "region from bucket: " + bucket, nil
|
return "region from bucket: " + bucket, nil
|
||||||
},
|
},
|
||||||
repoBackend: "fake-repo-type",
|
repoBackend: "fake-repo-type",
|
||||||
|
@ -374,7 +374,7 @@ func TestGetStorageVariables(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
getS3BucketRegion: func(bucket string) (string, error) {
|
getS3BucketRegion: func(bucket string, config map[string]string) (string, error) {
|
||||||
return "region from bucket: " + bucket, nil
|
return "region from bucket: " + bucket, nil
|
||||||
},
|
},
|
||||||
repoBackend: "fake-repo-type",
|
repoBackend: "fake-repo-type",
|
||||||
|
|
Loading…
Reference in New Issue