mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 03:41:45 +00:00
Merge pull request #8899 from roberthbailey/ip-from-metadata
For GCE, compute the external IP by using the local metadata server
This commit is contained in:
commit
9b67435cf3
@ -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)
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user