AWS: Build awsInstance as part of cloud provider creation

We need getSelfAWSInstance to be working anyway; we might as well build
it early, and then we can use its methods to extract e.g. the VPC ID
This commit is contained in:
Justin Santa Barbara 2016-02-27 09:44:48 -05:00
parent 0375fa057f
commit efa68a3590

View File

@ -637,15 +637,18 @@ func newAWSCloud(config io.Reader, awsServices AWSServices) (*AWSCloud, error) {
availabilityZone: zone, availabilityZone: zone,
} }
selfAWSInstance, err := awsCloud.buildSelfAWSInstance()
if err != nil {
return nil, err
}
awsCloud.selfAWSInstance = selfAWSInstance
filterTags := map[string]string{} filterTags := map[string]string{}
if cfg.Global.KubernetesClusterTag != "" { if cfg.Global.KubernetesClusterTag != "" {
filterTags[TagNameKubernetesCluster] = cfg.Global.KubernetesClusterTag filterTags[TagNameKubernetesCluster] = cfg.Global.KubernetesClusterTag
} else { } else {
selfInstance, err := awsCloud.getSelfAWSInstance() selfInstanceInfo, err := selfAWSInstance.getInfo()
if err != nil {
return nil, err
}
selfInstanceInfo, err := selfInstance.getInfo()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -1223,29 +1226,28 @@ func (self *awsDisk) deleteVolume() (bool, error) {
// Gets the awsInstance for the EC2 instance on which we are running // Gets the awsInstance for the EC2 instance on which we are running
// may return nil in case of error // may return nil in case of error
func (s *AWSCloud) getSelfAWSInstance() (*awsInstance, error) { func (c *AWSCloud) getSelfAWSInstance() (*awsInstance, error) {
// Note that we cache some state in awsInstance (mountpoints), so we must preserve the instance // Note that we cache some state in awsInstance (mountpoints), so we must preserve the instance
return c.selfAWSInstance, nil
}
s.mutex.Lock() // Builds the awsInstance for the EC2 instance on which we are running.
defer s.mutex.Unlock() // 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) {
i := s.selfAWSInstance if c.selfAWSInstance != nil {
if i == nil { panic("do not call buildSelfAWSInstance directly")
instanceId, err := s.metadata.GetMetadata("instance-id") }
instanceId, err := c.metadata.GetMetadata("instance-id")
if err != nil { if err != nil {
return nil, fmt.Errorf("error fetching instance-id from ec2 metadata service: %v", err) return nil, fmt.Errorf("error fetching instance-id from ec2 metadata service: %v", err)
} }
// privateDnsName, err := s.metadata.GetMetadata("local-hostname") // 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 // See #11543 - need to use ec2 API to get the privateDnsName in case of private dns zone e.g. mydomain.io
instance, err := s.getInstanceByID(instanceId) instance, err := c.getInstanceByID(instanceId)
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)
} }
i = newAWSInstance(s.ec2, instance) return newAWSInstance(c.ec2, instance), nil
s.selfAWSInstance = i
}
return i, nil
} }
// 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 == ""