From 4e176771b694aa4847885a576ac3e8a1a347e04f Mon Sep 17 00:00:00 2001 From: Justin Santa Barbara Date: Wed, 8 Apr 2015 06:25:33 -0700 Subject: [PATCH] Make fetching the aws instance id optional, so we can use it on e2e --- cmd/e2e/e2e.go | 2 +- pkg/cloudprovider/aws/aws.go | 33 ++++++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/cmd/e2e/e2e.go b/cmd/e2e/e2e.go index 76fab3011d7..101d31304fa 100644 --- a/cmd/e2e/e2e.go +++ b/cmd/e2e/e2e.go @@ -80,7 +80,7 @@ func main() { var err error cloudConfig.Provider, err = cloudprovider.GetCloudProvider(context.Provider, strings.NewReader(awsConfig)) if err != nil { - glog.Error("Error building AWS provider: %v", err) + glog.Error("Error building AWS provider: ", err) os.Exit(1) } } diff --git a/pkg/cloudprovider/aws/aws.go b/pkg/cloudprovider/aws/aws.go index a917ff7c992..538df030cd1 100644 --- a/pkg/cloudprovider/aws/aws.go +++ b/pkg/cloudprovider/aws/aws.go @@ -84,12 +84,15 @@ type Volumes interface { // AWSCloud is an implementation of Interface, TCPLoadBalancer and Instances for Amazon Web Services. type AWSCloud struct { ec2 EC2 + metadata AWSMetadata cfg *AWSCloudConfig availabilityZone string region aws.Region // The AWS instance that we are running on selfAwsInstance *awsInstance + + mutex sync.Mutex } type AWSCloudConfig struct { @@ -245,14 +248,9 @@ func newAWSCloud(config io.Reader, authFunc AuthFunc, instanceId string, metadat cfg: cfg, region: region, availabilityZone: zone, + metadata: metadata, } - instanceIdBytes, err := metadata.GetMetaData("instance-id") - if err != nil { - return nil, fmt.Errorf("error fetching instance-id from ec2 metadata service: %v", err) - } - awsCloud.selfAwsInstance = newAwsInstance(ec2, string(instanceIdBytes)) - return awsCloud, nil } @@ -822,9 +820,23 @@ func (self *awsDisk) delete() error { // Gets the awsInstance for the EC2 instance on which we are running // may return nil in case of error -func (aws *AWSCloud) getSelfAwsInstance() *awsInstance { +func (aws *AWSCloud) getSelfAwsInstance() (*awsInstance, error) { // Note that we cache some state in awsInstance (mountpoints), so we must preserve the instance - return aws.selfAwsInstance + + aws.mutex.Lock() + defer aws.mutex.Unlock() + + i := aws.selfAwsInstance + if i == nil { + instanceIdBytes, err := aws.metadata.GetMetaData("instance-id") + if err != nil { + return nil, fmt.Errorf("error fetching instance-id from ec2 metadata service: %v", err) + } + i = newAwsInstance(aws.ec2, string(instanceIdBytes)) + aws.selfAwsInstance = i + } + + return i, nil } // Implements Volumes.AttachDisk @@ -836,7 +848,10 @@ func (aws *AWSCloud) AttachDisk(instanceName string, diskName string, readOnly b var awsInstance *awsInstance if instanceName == "" { - awsInstance = aws.selfAwsInstance + awsInstance, err = aws.getSelfAwsInstance() + if err != nil { + return "", fmt.Errorf("Error getting self-instance: %v", err) + } } else { instance, err := aws.getInstancesByDnsName(instanceName) if err != nil {