AWS: Remove getSelfAWSInstance, use field directly

Now that we always populate the local instance, we don't need a getter.
This commit is contained in:
Justin Santa Barbara 2016-02-27 11:44:19 -05:00
parent 8c492c7536
commit af9efa02b4

View File

@ -205,6 +205,7 @@ type AWSCloud struct {
filterTags map[string]string filterTags map[string]string
// The AWS instance that we are running on // The AWS instance that we are running on
// Note that we cache some state in awsInstance (mountpoints), so we must preserve the instance
selfAWSInstance *awsInstance selfAWSInstance *awsInstance
mutex sync.Mutex mutex sync.Mutex
@ -368,9 +369,8 @@ func (self *AWSCloud) AddSSHKeyToAllInstances(user string, keyData []byte) error
return errors.New("unimplemented") return errors.New("unimplemented")
} }
func (a *AWSCloud) CurrentNodeName(hostname string) (string, error) { func (c *AWSCloud) CurrentNodeName(hostname string) (string, error) {
selfInstance := a.getSelfAWSInstance() return c.selfAWSInstance.nodeName, nil
return selfInstance.nodeName, nil
} }
// Implementation of EC2.Instances // Implementation of EC2.Instances
@ -711,12 +711,11 @@ func (aws *AWSCloud) Routes() (cloudprovider.Routes, bool) {
} }
// NodeAddresses is an implementation of Instances.NodeAddresses. // NodeAddresses is an implementation of Instances.NodeAddresses.
func (aws *AWSCloud) NodeAddresses(name string) ([]api.NodeAddress, error) { func (c *AWSCloud) NodeAddresses(name string) ([]api.NodeAddress, error) {
self := aws.getSelfAWSInstance() if c.selfAWSInstance.nodeName == name || len(name) == 0 {
if self.nodeName == name || len(name) == 0 {
addresses := []api.NodeAddress{} addresses := []api.NodeAddress{}
internalIP, err := aws.metadata.GetMetadata("local-ipv4") internalIP, err := c.metadata.GetMetadata("local-ipv4")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -724,7 +723,7 @@ func (aws *AWSCloud) NodeAddresses(name string) ([]api.NodeAddress, error) {
// Legacy compatibility: the private ip was the legacy host ip // Legacy compatibility: the private ip was the legacy host ip
addresses = append(addresses, api.NodeAddress{Type: api.NodeLegacyHostIP, Address: internalIP}) addresses = append(addresses, api.NodeAddress{Type: api.NodeLegacyHostIP, Address: internalIP})
externalIP, err := aws.metadata.GetMetadata("public-ipv4") externalIP, err := c.metadata.GetMetadata("public-ipv4")
if err != nil { if err != nil {
//TODO: It would be nice to be able to determine the reason for the failure, //TODO: It would be nice to be able to determine the reason for the failure,
// but the AWS client masks all failures with the same error description. // but the AWS client masks all failures with the same error description.
@ -735,7 +734,7 @@ func (aws *AWSCloud) NodeAddresses(name string) ([]api.NodeAddress, error) {
return addresses, nil return addresses, nil
} }
instance, err := aws.getInstanceByNodeName(name) instance, err := c.getInstanceByNodeName(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -768,15 +767,14 @@ func (aws *AWSCloud) NodeAddresses(name string) ([]api.NodeAddress, error) {
} }
// ExternalID returns the cloud provider ID of the specified instance (deprecated). // ExternalID returns the cloud provider ID of the specified instance (deprecated).
func (aws *AWSCloud) ExternalID(name string) (string, error) { func (c *AWSCloud) ExternalID(name string) (string, error) {
awsInstance := aws.getSelfAWSInstance() if c.selfAWSInstance.nodeName == name {
if awsInstance.nodeName == name {
// We assume that if this is run on the instance itself, the instance exists and is alive // We assume that if this is run on the instance itself, the instance exists and is alive
return awsInstance.awsID, nil return c.selfAWSInstance.awsID, nil
} else { } else {
// We must verify that the instance still exists // We must verify that the instance still exists
// Note that if the instance does not exist or is no longer running, we must return ("", cloudprovider.InstanceNotFound) // Note that if the instance does not exist or is no longer running, we must return ("", cloudprovider.InstanceNotFound)
instance, err := aws.findInstanceByNodeName(name) instance, err := c.findInstanceByNodeName(name)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -788,14 +786,13 @@ func (aws *AWSCloud) ExternalID(name string) (string, error) {
} }
// InstanceID returns the cloud provider ID of the specified instance. // InstanceID returns the cloud provider ID of the specified instance.
func (aws *AWSCloud) InstanceID(name string) (string, error) { func (c *AWSCloud) InstanceID(name string) (string, error) {
awsInstance := aws.getSelfAWSInstance()
// In the future it is possible to also return an endpoint as: // In the future it is possible to also return an endpoint as:
// <endpoint>/<zone>/<instanceid> // <endpoint>/<zone>/<instanceid>
if awsInstance.nodeName == name { if c.selfAWSInstance.nodeName == name {
return "/" + awsInstance.availabilityZone + "/" + awsInstance.awsID, nil return "/" + c.selfAWSInstance.availabilityZone + "/" + c.selfAWSInstance.awsID, nil
} else { } else {
inst, err := aws.getInstanceByNodeName(name) inst, err := c.getInstanceByNodeName(name)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -804,12 +801,11 @@ func (aws *AWSCloud) InstanceID(name string) (string, error) {
} }
// InstanceType returns the type of the specified instance. // InstanceType returns the type of the specified instance.
func (aws *AWSCloud) InstanceType(name string) (string, error) { func (c *AWSCloud) InstanceType(name string) (string, error) {
awsInstance := aws.getSelfAWSInstance() if c.selfAWSInstance.nodeName == name {
if awsInstance.nodeName == name { return c.selfAWSInstance.instanceType, nil
return awsInstance.instanceType, nil
} else { } else {
inst, err := aws.getInstanceByNodeName(name) inst, err := c.getInstanceByNodeName(name)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -878,9 +874,8 @@ func (aws *AWSCloud) List(filter string) ([]string, error) {
// GetZone implements Zones.GetZone // GetZone implements Zones.GetZone
func (c *AWSCloud) GetZone() (cloudprovider.Zone, error) { func (c *AWSCloud) GetZone() (cloudprovider.Zone, error) {
i := c.getSelfAWSInstance()
return cloudprovider.Zone{ return cloudprovider.Zone{
FailureDomain: i.availabilityZone, FailureDomain: c.selfAWSInstance.availabilityZone,
Region: c.region, Region: c.region,
}, nil }, nil
} }
@ -1209,13 +1204,6 @@ func (self *awsDisk) deleteVolume() (bool, error) {
return true, nil return true, nil
} }
// Gets the awsInstance for the EC2 instance on which we are running
// may return nil in case of error
func (c *AWSCloud) getSelfAWSInstance() *awsInstance {
// Note that we cache some state in awsInstance (mountpoints), so we must preserve the instance
return c.selfAWSInstance
}
// Builds the awsInstance for the EC2 instance on which we are running. // Builds the awsInstance for the EC2 instance on which we are running.
// This is called when the AWSCloud is initialized, and should not be called otherwise (because the awsInstance for the local instance is a singleton with drive mapping state) // This is called when the AWSCloud is initialized, and should not be called otherwise (because the awsInstance for the local instance is a singleton with drive mapping state)
func (c *AWSCloud) buildSelfAWSInstance() (*awsInstance, error) { func (c *AWSCloud) buildSelfAWSInstance() (*awsInstance, error) {
@ -1236,17 +1224,17 @@ func (c *AWSCloud) buildSelfAWSInstance() (*awsInstance, error) {
} }
// Gets the awsInstance with node-name nodeName, or the 'self' instance if nodeName == "" // Gets the awsInstance with node-name nodeName, or the 'self' instance if nodeName == ""
func (aws *AWSCloud) getAwsInstance(nodeName string) (*awsInstance, error) { func (c *AWSCloud) getAwsInstance(nodeName string) (*awsInstance, error) {
var awsInstance *awsInstance var awsInstance *awsInstance
if nodeName == "" { if nodeName == "" {
awsInstance = aws.getSelfAWSInstance() awsInstance = c.selfAWSInstance
} else { } else {
instance, err := aws.getInstanceByNodeName(nodeName) instance, err := c.getInstanceByNodeName(nodeName)
if err != nil { if err != nil {
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, instance) awsInstance = newAWSInstance(c.ec2, instance)
} }
return awsInstance, nil return awsInstance, nil
@ -1387,11 +1375,11 @@ func (aws *AWSCloud) DetachDisk(diskName string, instanceName string) (string, e
func (s *AWSCloud) CreateDisk(volumeOptions *VolumeOptions) (string, error) { func (s *AWSCloud) CreateDisk(volumeOptions *VolumeOptions) (string, error) {
// Default to creating in the current zone // Default to creating in the current zone
// TODO: Spread across zones? // TODO: Spread across zones?
selfInstance := s.getSelfAWSInstance() createAZ := s.selfAWSInstance.availabilityZone
// TODO: Should we tag this with the cluster id (so it gets deleted when the cluster does?) // TODO: Should we tag this with the cluster id (so it gets deleted when the cluster does?)
request := &ec2.CreateVolumeInput{} request := &ec2.CreateVolumeInput{}
request.AvailabilityZone = &selfInstance.availabilityZone request.AvailabilityZone = &createAZ
volSize := int64(volumeOptions.CapacityGB) volSize := int64(volumeOptions.CapacityGB)
request.Size = &volSize request.Size = &volSize
request.VolumeType = aws.String(DefaultVolumeType) request.VolumeType = aws.String(DefaultVolumeType)