mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 09:49:50 +00:00
Make fetching the aws instance id optional, so we can use it on e2e
This commit is contained in:
parent
c7c969564a
commit
4e176771b6
@ -80,7 +80,7 @@ func main() {
|
|||||||
var err error
|
var err error
|
||||||
cloudConfig.Provider, err = cloudprovider.GetCloudProvider(context.Provider, strings.NewReader(awsConfig))
|
cloudConfig.Provider, err = cloudprovider.GetCloudProvider(context.Provider, strings.NewReader(awsConfig))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Error("Error building AWS provider: %v", err)
|
glog.Error("Error building AWS provider: ", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -84,12 +84,15 @@ type Volumes interface {
|
|||||||
// AWSCloud is an implementation of Interface, TCPLoadBalancer and Instances for Amazon Web Services.
|
// AWSCloud is an implementation of Interface, TCPLoadBalancer and Instances for Amazon Web Services.
|
||||||
type AWSCloud struct {
|
type AWSCloud struct {
|
||||||
ec2 EC2
|
ec2 EC2
|
||||||
|
metadata AWSMetadata
|
||||||
cfg *AWSCloudConfig
|
cfg *AWSCloudConfig
|
||||||
availabilityZone string
|
availabilityZone string
|
||||||
region aws.Region
|
region aws.Region
|
||||||
|
|
||||||
// The AWS instance that we are running on
|
// The AWS instance that we are running on
|
||||||
selfAwsInstance *awsInstance
|
selfAwsInstance *awsInstance
|
||||||
|
|
||||||
|
mutex sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
type AWSCloudConfig struct {
|
type AWSCloudConfig struct {
|
||||||
@ -245,14 +248,9 @@ func newAWSCloud(config io.Reader, authFunc AuthFunc, instanceId string, metadat
|
|||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
region: region,
|
region: region,
|
||||||
availabilityZone: zone,
|
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
|
return awsCloud, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -822,9 +820,23 @@ func (self *awsDisk) delete() 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 (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
|
// 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
|
// Implements Volumes.AttachDisk
|
||||||
@ -836,7 +848,10 @@ func (aws *AWSCloud) AttachDisk(instanceName string, diskName string, readOnly b
|
|||||||
|
|
||||||
var awsInstance *awsInstance
|
var awsInstance *awsInstance
|
||||||
if instanceName == "" {
|
if instanceName == "" {
|
||||||
awsInstance = aws.selfAwsInstance
|
awsInstance, err = aws.getSelfAwsInstance()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("Error getting self-instance: %v", err)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
instance, err := aws.getInstancesByDnsName(instanceName)
|
instance, err := aws.getInstancesByDnsName(instanceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user