mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 07:20:13 +00:00
Merge pull request #7040 from justinsb/aws_nodeaddresses
AWS: Return public & private addresses in GetNodeAddresses
This commit is contained in:
commit
7a122a42d4
@ -275,16 +275,36 @@ func (aws *AWSCloud) Zones() (cloudprovider.Zones, bool) {
|
||||
|
||||
// NodeAddresses is an implementation of Instances.NodeAddresses.
|
||||
func (aws *AWSCloud) NodeAddresses(name string) ([]api.NodeAddress, error) {
|
||||
inst, err := aws.getInstancesByDnsName(name)
|
||||
instance, err := aws.getInstancesByDnsName(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ip := net.ParseIP(inst.PrivateIpAddress)
|
||||
if ip == nil {
|
||||
return nil, fmt.Errorf("invalid network IP: %s", inst.PrivateIpAddress)
|
||||
|
||||
addresses := []api.NodeAddress{}
|
||||
|
||||
if instance.PrivateIpAddress != "" {
|
||||
ipAddress := instance.PrivateIpAddress
|
||||
ip := net.ParseIP(ipAddress)
|
||||
if ip == nil {
|
||||
return nil, fmt.Errorf("EC2 instance had invalid private address: %s (%s)", instance.InstanceId, ipAddress)
|
||||
}
|
||||
addresses = append(addresses, api.NodeAddress{Type: api.NodeInternalIP, Address: ip.String()})
|
||||
|
||||
// Legacy compatibility: the private ip was the legacy host ip
|
||||
addresses = append(addresses, api.NodeAddress{Type: api.NodeLegacyHostIP, Address: ip.String()})
|
||||
}
|
||||
|
||||
return []api.NodeAddress{{Type: api.NodeLegacyHostIP, Address: ip.String()}}, nil
|
||||
// TODO: Other IP addresses (multiple ips)?
|
||||
if instance.PublicIpAddress != "" {
|
||||
ipAddress := instance.PublicIpAddress
|
||||
ip := net.ParseIP(ipAddress)
|
||||
if ip == nil {
|
||||
return nil, fmt.Errorf("EC2 instance had invalid public address: %s (%s)", instance.InstanceId, ipAddress)
|
||||
}
|
||||
addresses = append(addresses, api.NodeAddress{Type: api.NodeExternalIP, Address: ip.String()})
|
||||
}
|
||||
|
||||
return addresses, nil
|
||||
}
|
||||
|
||||
// ExternalID returns the cloud provider ID of the specified instance.
|
||||
|
@ -280,12 +280,22 @@ func TestList(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func testHasNodeAddress(t *testing.T, addrs []api.NodeAddress, addressType api.NodeAddressType, address string) {
|
||||
for _, addr := range addrs {
|
||||
if addr.Type == addressType && addr.Address == address {
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Errorf("Did not find expected address: %s:%s in %v", addressType, address, addrs)
|
||||
}
|
||||
|
||||
func TestNodeAddresses(t *testing.T) {
|
||||
// Note these instances have the same name
|
||||
// (we test that this produces an error)
|
||||
instances := make([]ec2.Instance, 2)
|
||||
instances[0].PrivateDNSName = "instance1"
|
||||
instances[0].PrivateIpAddress = "192.168.0.1"
|
||||
instances[0].PublicIpAddress = "1.2.3.4"
|
||||
instances[0].State.Name = "running"
|
||||
instances[1].PrivateDNSName = "instance1"
|
||||
instances[1].PrivateIpAddress = "192.168.0.2"
|
||||
@ -308,12 +318,12 @@ func TestNodeAddresses(t *testing.T) {
|
||||
if err3 != nil {
|
||||
t.Errorf("Should not error when instance found")
|
||||
}
|
||||
if len(addrs3) != 1 {
|
||||
t.Errorf("Should return exactly one NodeAddress")
|
||||
}
|
||||
if e, a := instances[0].PrivateIpAddress, addrs3[0].Address; e != a {
|
||||
t.Errorf("Expected %v, got %v", e, a)
|
||||
if len(addrs3) != 3 {
|
||||
t.Errorf("Should return exactly 3 NodeAddresses")
|
||||
}
|
||||
testHasNodeAddress(t, addrs3, api.NodeInternalIP, "192.168.0.1")
|
||||
testHasNodeAddress(t, addrs3, api.NodeLegacyHostIP, "192.168.0.1")
|
||||
testHasNodeAddress(t, addrs3, api.NodeExternalIP, "1.2.3.4")
|
||||
}
|
||||
|
||||
func TestGetRegion(t *testing.T) {
|
||||
|
Loading…
Reference in New Issue
Block a user