Skip to content

Commit

Permalink
CreateVolumeSnapshot region fix (#4544)
Browse files Browse the repository at this point in the history
* WIP: CreateVolumeSnapshot region fix

* Error handling
  • Loading branch information
pavannd1 authored and Ilya Kislenko committed Dec 11, 2018
1 parent 730f32a commit ec29060
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
18 changes: 18 additions & 0 deletions pkg/blockstorage/awsebs/awsebs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/jpillora/backoff"
Expand Down Expand Up @@ -543,3 +544,20 @@ func waitOnSnapshotID(ctx context.Context, ec2Cli *EC2, snapID string) error {
return false, nil
})
}

// GetRegionFromEC2Metadata retrieves the region from the EC2 metadata service.
// Only works when the call is performed from inside AWS
func GetRegionFromEC2Metadata() (string, error) {
log.Debug("Retrieving region from metadata")
conf := aws.Config{
HTTPClient: &http.Client{
Transport: http.DefaultTransport,
Timeout: 2 * time.Second,
},
MaxRetries: aws.Int(1),
}
ec2MetaData := ec2metadata.New(session.Must(session.NewSession()), &conf)

awsRegion, err := ec2MetaData.Region()
return awsRegion, errors.Wrap(err, "Failed to get AWS Region")
}
12 changes: 10 additions & 2 deletions pkg/function/create_volume_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,15 @@ func getPVCInfo(ctx context.Context, kubeCli kubernetes.Interface, namespace str
if err = ValidateProfile(tp.Profile); err != nil {
return nil, errors.Wrap(err, "Profile validation failed")
}
region = tp.Profile.Location.S3Compliant.Region
// Get Region from PV label or EC2 metadata
if pvRegion, ok := pvLabels[kube.PVRegionLabelName]; ok {
region = pvRegion
} else {
region, err = awsebs.GetRegionFromEC2Metadata()
if err != nil {
return nil, err
}
}
if pvZone, ok := pvLabels[kube.PVZoneLabelName]; ok {
config[awsebs.ConfigRegion] = region
config[awsebs.AccessKeyID] = tp.Profile.Credential.KeyPair.ID
Expand All @@ -189,7 +197,7 @@ func getPVCInfo(ctx context.Context, kubeCli kubernetes.Interface, namespace str
}
return nil, errors.Errorf("PV zone label is empty, pvName: %s, namespace: %s", pvName, namespace)
}
return nil, errors.New("Storage type not supported!")
return nil, errors.New("Storage type not supported")
}

func getPVCList(tp param.TemplateParams) ([]string, error) {
Expand Down

0 comments on commit ec29060

Please sign in to comment.