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

ebs br: pass region name for aws EBS volume operation #5195

Merged
merged 4 commits into from
Jul 25, 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: 3 additions & 0 deletions pkg/backup/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,7 @@ const (
ClusterBackupMeta = "clustermeta"
ClusterRestoreMeta = "restoremeta"
MetaFile = "backupmeta"

// AWSRegionEnv is the aws region environment variable
AWSRegionEnv = "AWS_REGION"
)
3 changes: 3 additions & 0 deletions pkg/backup/restore/restore_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/onsi/gomega"
. "github.com/onsi/gomega"
"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/backup/constants"
"github.com/pingcap/tidb-operator/pkg/backup/testutils"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -436,6 +437,8 @@ func TestBRRestoreByEBS(t *testing.T) {
g.Expect(err).To(Succeed())
}()

os.Setenv(constants.AWSRegionEnv, "us-west-1")

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {

Expand Down
27 changes: 25 additions & 2 deletions pkg/backup/util/aws_ebs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@
package util

import (
"os"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ebs"
"github.com/aws/aws-sdk-go/service/ebs/ebsiface"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/pingcap/errors"
"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/backup/constants"
"go.uber.org/atomic"
"golang.org/x/sync/errgroup"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -119,7 +123,17 @@ func NewEC2Session(concurrency uint) (*EC2Session, error) {
if err != nil {
return nil, errors.Trace(err)
}
ec2Session := ec2.New(sess)

region := os.Getenv(constants.AWSRegionEnv)
if region == "" {
ec2Metadata := ec2metadata.New(sess)
region, err = ec2Metadata.Region()
if err != nil {
return nil, errors.Annotate(err, "get ec2 region")
}
}

ec2Session := ec2.New(sess, aws.NewConfig().WithRegion(region))
return &EC2Session{EC2: ec2Session, concurrency: concurrency}, nil
}

Expand Down Expand Up @@ -201,6 +215,15 @@ func NewEBSSession(concurrency uint) (*EBSSession, error) {
if err != nil {
return nil, errors.Trace(err)
}
ebsSession := ebs.New(sess)
region := os.Getenv(constants.AWSRegionEnv)
if region == "" {
ec2Metadata := ec2metadata.New(sess)
region, err = ec2Metadata.Region()
if err != nil {
return nil, errors.Annotate(err, "get ec2 region")
}
}

ebsSession := ebs.New(sess, aws.NewConfig().WithRegion(region))
return &EBSSession{EBS: ebsSession, concurrency: concurrency}, nil
}