From 1dfaa93ccdc55eecd2912150c1f389b2ac598a95 Mon Sep 17 00:00:00 2001 From: Robert Bailey Date: Wed, 27 May 2015 16:46:50 -0700 Subject: [PATCH] For GCE, compute the external IP by using the local metadata server. This is in many ways a revert of #7530 but after auditing the code I found that this function is now only used to determine an address of the node where it is currently running. --- pkg/cloudprovider/cloud.go | 3 +++ pkg/cloudprovider/gce/gce.go | 20 +++++--------------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/pkg/cloudprovider/cloud.go b/pkg/cloudprovider/cloud.go index 31054f1e3bd..ab5feea5b7a 100644 --- a/pkg/cloudprovider/cloud.go +++ b/pkg/cloudprovider/cloud.go @@ -81,6 +81,9 @@ type TCPLoadBalancer interface { // Instances is an abstract, pluggable interface for sets of instances. type Instances interface { // NodeAddresses returns the addresses of the specified instance. + // TODO(roberthbailey): This currently is only used in such a way that it + // returns the address of the calling instance. We should do a rename to + // make this clearer. NodeAddresses(name string) ([]api.NodeAddress, error) // ExternalID returns the cloud provider ID of the specified instance. ExternalID(name string) (string, error) diff --git a/pkg/cloudprovider/gce/gce.go b/pkg/cloudprovider/gce/gce.go index b7451b97bb3..863bc5ddae5 100644 --- a/pkg/cloudprovider/gce/gce.go +++ b/pkg/cloudprovider/gce/gce.go @@ -42,6 +42,8 @@ import ( "google.golang.org/cloud/compute/metadata" ) +const EXTERNAL_IP_METADATA_URL = "http://169.254.169.254/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip" + // GCECloud is an implementation of Interface, TCPLoadBalancer and Instances for Google Compute Engine. type GCECloud struct { service *compute.Service @@ -466,10 +468,10 @@ func (gce *GCECloud) getInstanceByName(name string) (*compute.Instance, error) { } // NodeAddresses is an implementation of Instances.NodeAddresses. -func (gce *GCECloud) NodeAddresses(instance string) ([]api.NodeAddress, error) { - externalIP, err := gce.getExternalIP(instance) +func (gce *GCECloud) NodeAddresses(_ string) ([]api.NodeAddress, error) { + externalIP, err := gce.metadataAccess(EXTERNAL_IP_METADATA_URL) if err != nil { - return nil, fmt.Errorf("couldn't get external IP for instance %s: %v", instance, err) + return nil, fmt.Errorf("couldn't get external IP: %v", err) } return []api.NodeAddress{ @@ -479,18 +481,6 @@ func (gce *GCECloud) NodeAddresses(instance string) ([]api.NodeAddress, error) { }, nil } -func (gce *GCECloud) getExternalIP(instance string) (string, error) { - inst, err := gce.getInstanceByName(instance) - if err != nil { - return "", err - } - ip := net.ParseIP(inst.NetworkInterfaces[0].AccessConfigs[0].NatIP) - if ip == nil { - return "", fmt.Errorf("invalid network IP: %s", inst.NetworkInterfaces[0].AccessConfigs[0].NatIP) - } - return ip.String(), nil -} - // ExternalID returns the cloud provider ID of the specified instance. func (gce *GCECloud) ExternalID(instance string) (string, error) { inst, err := gce.getInstanceByName(instance)