AWS: Refactor newAWSInstance

Now that we can't build an awsInstance from metadata, because of the
PrivateDnsName issue, we might as well simplify the arguments.

Create a 'placeholder' method though - newAWSInstanceFromMetadata - that
documents the desire to use metadata, shows how we would get it, but
links to the bug which explains why we can't use it.
This commit is contained in:
Justin Santa Barbara 2016-02-27 09:23:05 -05:00
parent 6d9e0ed038
commit 0375fa057f

View File

@ -939,8 +939,15 @@ type awsInstance struct {
deviceMappings map[mountDevice]string deviceMappings map[mountDevice]string
} }
func newAWSInstance(ec2 EC2, awsID, nodeName, availabilityZone, instanceType string) *awsInstance { // newAWSInstance creates a new awsInstance object
self := &awsInstance{ec2: ec2, awsID: awsID, nodeName: nodeName, availabilityZone: availabilityZone, instanceType: instanceType} func newAWSInstance(ec2 EC2, instance *ec2.Instance) *awsInstance {
self := &awsInstance{
ec2: ec2,
awsID: aws.StringValue(instance.InstanceId),
nodeName: aws.StringValue(instance.PrivateDnsName),
availabilityZone: aws.StringValue(instance.Placement.AvailabilityZone),
instanceType: aws.StringValue(instance.InstanceType),
}
// We lazy-init deviceMappings // We lazy-init deviceMappings
self.deviceMappings = nil self.deviceMappings = nil
@ -948,6 +955,29 @@ func newAWSInstance(ec2 EC2, awsID, nodeName, availabilityZone, instanceType str
return self return self
} }
// newAwsInstanceFromMetadata would build a new awsInstance from the metadata service,
// avoiding an EC2 API call. BUT if we have a VPC with a custom DNS suffix, the metadata
// service returns the wrong PrivateDnsName. So we can't use it until we figure out a workaround
// or no longer require the nodeName (e.g. if we use AWS InstanceID as the NodeName)
//func newAWSInstanceFromMetadata() *awsInstance {
// instanceId, err := s.metadata.GetMetadata("instance-id")
// if err != nil {
// return nil, fmt.Errorf("error fetching instance-id from ec2 metadata service: %v", err)
// }
// // privateDnsName, err := s.metadata.GetMetadata("local-hostname")
// // See #11543 - need to use ec2 API to get the privateDnsName in case of private dns zone e.g. mydomain.io
// privateDnsName := aws.StringValue(instance.PrivateDnsName)
// availabilityZone, err := getAvailabilityZone(s.metadata)
// if err != nil {
// return nil, fmt.Errorf("error fetching availability zone from ec2 metadata service: %v", err)
// }
// instanceType, err := getInstanceType(s.metadata)
// if err != nil {
// return nil, fmt.Errorf("error fetching instance type from ec2 metadata service: %v", err)
// }
//
//}
// Gets the awsInstanceType that models the instance type of this instance // Gets the awsInstanceType that models the instance type of this instance
func (self *awsInstance) getInstanceType() *awsInstanceType { func (self *awsInstance) getInstanceType() *awsInstanceType {
// TODO: Make this real // TODO: Make this real
@ -1211,17 +1241,7 @@ func (s *AWSCloud) getSelfAWSInstance() (*awsInstance, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("error finding instance %s: %v", instanceId, err) return nil, fmt.Errorf("error finding instance %s: %v", instanceId, err)
} }
privateDnsName := aws.StringValue(instance.PrivateDnsName) i = newAWSInstance(s.ec2, instance)
availabilityZone, err := getAvailabilityZone(s.metadata)
if err != nil {
return nil, fmt.Errorf("error fetching availability zone from ec2 metadata service: %v", err)
}
instanceType, err := getInstanceType(s.metadata)
if err != nil {
return nil, fmt.Errorf("error fetching instance type from ec2 metadata service: %v", err)
}
i = newAWSInstance(s.ec2, instanceId, privateDnsName, availabilityZone, instanceType)
s.selfAWSInstance = i s.selfAWSInstance = i
} }
@ -1243,7 +1263,7 @@ func (aws *AWSCloud) getAwsInstance(nodeName string) (*awsInstance, error) {
return nil, fmt.Errorf("error finding instance %s: %v", nodeName, err) return nil, fmt.Errorf("error finding instance %s: %v", nodeName, err)
} }
awsInstance = newAWSInstance(aws.ec2, orEmpty(instance.InstanceId), orEmpty(instance.PrivateDnsName), orEmpty(instance.Placement.AvailabilityZone), orEmpty(instance.InstanceType)) awsInstance = newAWSInstance(aws.ec2, instance)
} }
return awsInstance, nil return awsInstance, nil