implement aws InstanceMetadataByProviderID function

This commit is contained in:
louisgong 2020-05-13 11:23:54 +08:00
parent 260a9005a0
commit da4b5e8dd9
2 changed files with 38 additions and 1 deletions

View File

@ -1648,7 +1648,27 @@ func (c *Cloud) InstanceShutdownByProviderID(ctx context.Context, providerID str
// InstanceMetadataByProviderID returns metadata of the specified instance.
func (c *Cloud) InstanceMetadataByProviderID(ctx context.Context, providerID string) (*cloudprovider.InstanceMetadata, error) {
return nil, fmt.Errorf("unimplemented")
instanceID, err := KubernetesInstanceID(providerID).MapToAWSInstanceID()
if err != nil {
return nil, err
}
instance, err := describeInstance(c.ec2, instanceID)
if err != nil {
return nil, err
}
// TODO ignore checking whether `*instance.State.Name == ec2.InstanceStateNameTerminated` here.
// If not behave as expected, add it.
addresses, err := extractNodeAddresses(instance)
if err != nil {
return nil, err
}
return &cloudprovider.InstanceMetadata{
ProviderID: providerID,
Type: aws.StringValue(instance.InstanceType),
NodeAddresses: addresses,
}, nil
}
// InstanceID returns the cloud provider ID of the node with the specified nodeName.

View File

@ -681,6 +681,23 @@ func TestNodeAddressesWithMetadata(t *testing.T) {
}
}
func TestInstanceMetadataByProviderID(t *testing.T) {
instance0 := makeInstance(0, "192.168.0.1", "1.2.3.4", "instance-same.ec2.internal", "instance-same.ec2.external", true)
aws1, _ := mockInstancesResp(&instance0, []*ec2.Instance{&instance0})
// change node name so it uses the instance instead of metadata
aws1.selfAWSInstance.nodeName = "foo"
md, err := aws1.InstanceMetadataByProviderID(context.TODO(), "/us-east-1a/i-0")
if err != nil {
t.Errorf("should not error when instance found")
}
if md.ProviderID != "/us-east-1a/i-0" || md.Type != "c3.large" {
t.Errorf("expect providerID %s get %s, expect type %s get %s", "/us-east-1a/i-0", md.ProviderID, "c3.large", md.Type)
}
testHasNodeAddress(t, md.NodeAddresses, v1.NodeInternalIP, "192.168.0.1")
testHasNodeAddress(t, md.NodeAddresses, v1.NodeExternalIP, "1.2.3.4")
}
func TestParseMetadataLocalHostname(t *testing.T) {
tests := []struct {
name string