From da4b5e8dd998485a486b0e30d64648991f57d1a5 Mon Sep 17 00:00:00 2001 From: louisgong Date: Wed, 13 May 2020 11:23:54 +0800 Subject: [PATCH] implement aws InstanceMetadataByProviderID function --- .../k8s.io/legacy-cloud-providers/aws/aws.go | 22 ++++++++++++++++++- .../legacy-cloud-providers/aws/aws_test.go | 17 ++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/staging/src/k8s.io/legacy-cloud-providers/aws/aws.go b/staging/src/k8s.io/legacy-cloud-providers/aws/aws.go index 40ea45eb134..91ab6b5b4e9 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/aws/aws.go +++ b/staging/src/k8s.io/legacy-cloud-providers/aws/aws.go @@ -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. diff --git a/staging/src/k8s.io/legacy-cloud-providers/aws/aws_test.go b/staging/src/k8s.io/legacy-cloud-providers/aws/aws_test.go index 1f8978270c5..bd045ecce92 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/aws/aws_test.go +++ b/staging/src/k8s.io/legacy-cloud-providers/aws/aws_test.go @@ -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