mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
implement aws InstanceMetadataByProviderID function
This commit is contained in:
parent
260a9005a0
commit
da4b5e8dd9
@ -1648,7 +1648,27 @@ func (c *Cloud) InstanceShutdownByProviderID(ctx context.Context, providerID str
|
|||||||
|
|
||||||
// InstanceMetadataByProviderID returns metadata of the specified instance.
|
// InstanceMetadataByProviderID returns metadata of the specified instance.
|
||||||
func (c *Cloud) InstanceMetadataByProviderID(ctx context.Context, providerID string) (*cloudprovider.InstanceMetadata, error) {
|
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.
|
// InstanceID returns the cloud provider ID of the node with the specified nodeName.
|
||||||
|
@ -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) {
|
func TestParseMetadataLocalHostname(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
Loading…
Reference in New Issue
Block a user