diff --git a/cmd/kube-apiserver/app/options/options.go b/cmd/kube-apiserver/app/options/options.go index 464d8fccbcf..408aeaa5afd 100644 --- a/cmd/kube-apiserver/app/options/options.go +++ b/cmd/kube-apiserver/app/options/options.go @@ -86,9 +86,17 @@ func NewServerRunOptions() *ServerRunOptions { Port: ports.KubeletPort, ReadOnlyPort: ports.KubeletReadOnlyPort, PreferredAddressTypes: []string{ + // --override-hostname string(api.NodeHostName), + + // internal, preferring DNS if reported + string(api.NodeInternalDNS), string(api.NodeInternalIP), + + // external, preferring DNS if reported + string(api.NodeExternalDNS), string(api.NodeExternalIP), + string(api.NodeLegacyHostIP), }, EnableHttps: true, diff --git a/pkg/api/types.go b/pkg/api/types.go index e5ccc248ac6..a0ec986ac27 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -2732,6 +2732,8 @@ const ( NodeHostName NodeAddressType = "Hostname" NodeExternalIP NodeAddressType = "ExternalIP" NodeInternalIP NodeAddressType = "InternalIP" + NodeExternalDNS NodeAddressType = "ExternalDNS" + NodeInternalDNS NodeAddressType = "InternalDNS" ) type NodeAddress struct { diff --git a/pkg/api/v1/types.go b/pkg/api/v1/types.go index 529b5de79c3..7c0c892ecd8 100644 --- a/pkg/api/v1/types.go +++ b/pkg/api/v1/types.go @@ -3153,6 +3153,8 @@ const ( NodeHostName NodeAddressType = "Hostname" NodeExternalIP NodeAddressType = "ExternalIP" NodeInternalIP NodeAddressType = "InternalIP" + NodeExternalDNS NodeAddressType = "ExternalDNS" + NodeInternalDNS NodeAddressType = "InternalDNS" ) // NodeAddress contains information for the node's address. diff --git a/pkg/cloudprovider/providers/aws/aws.go b/pkg/cloudprovider/providers/aws/aws.go index d3f9456767b..a83e7a5820e 100644 --- a/pkg/cloudprovider/providers/aws/aws.go +++ b/pkg/cloudprovider/providers/aws/aws.go @@ -909,6 +909,24 @@ func (c *Cloud) NodeAddresses(name types.NodeName) ([]v1.NodeAddress, error) { addresses = append(addresses, v1.NodeAddress{Type: v1.NodeExternalIP, Address: externalIP}) } + internalDNS, err := c.metadata.GetMetadata("local-hostname") + if err != nil || len(internalDNS) == 0 { + //TODO: It would be nice to be able to determine the reason for the failure, + // but the AWS client masks all failures with the same error description. + glog.V(2).Info("Could not determine private DNS from AWS metadata.") + } else { + addresses = append(addresses, v1.NodeAddress{Type: v1.NodeInternalDNS, Address: internalDNS}) + } + + externalDNS, err := c.metadata.GetMetadata("public-hostname") + if err != nil || len(externalDNS) == 0 { + //TODO: It would be nice to be able to determine the reason for the failure, + // but the AWS client masks all failures with the same error description. + glog.V(2).Info("Could not determine public DNS from AWS metadata.") + } else { + addresses = append(addresses, v1.NodeAddress{Type: v1.NodeExternalDNS, Address: externalDNS}) + } + return addresses, nil } instance, err := c.getInstanceByNodeName(name) @@ -940,6 +958,14 @@ func (c *Cloud) NodeAddresses(name types.NodeName) ([]v1.NodeAddress, error) { addresses = append(addresses, v1.NodeAddress{Type: v1.NodeExternalIP, Address: ip.String()}) } + if !isNilOrEmpty(instance.PrivateDnsName) { + addresses = append(addresses, v1.NodeAddress{Type: v1.NodeInternalDNS, Address: *instance.PrivateDnsName}) + } + + if !isNilOrEmpty(instance.PublicDnsName) { + addresses = append(addresses, v1.NodeAddress{Type: v1.NodeExternalDNS, Address: *instance.PublicDnsName}) + } + return addresses, nil } diff --git a/pkg/cloudprovider/providers/aws/aws_test.go b/pkg/cloudprovider/providers/aws/aws_test.go index 4096a7b4141..08a65053669 100644 --- a/pkg/cloudprovider/providers/aws/aws_test.go +++ b/pkg/cloudprovider/providers/aws/aws_test.go @@ -345,6 +345,8 @@ func (self *FakeMetadata) GetMetadata(key string) (string, error) { return aws.StringValue(i.InstanceId), nil } else if key == "local-hostname" { return aws.StringValue(i.PrivateDnsName), nil + } else if key == "public-hostname" { + return aws.StringValue(i.PublicDnsName), nil } else if key == "local-ipv4" { return aws.StringValue(i.PrivateIpAddress), nil } else if key == "public-ipv4" { @@ -553,6 +555,7 @@ func TestNodeAddresses(t *testing.T) { instance0.InstanceId = aws.String("i-0") instance0.PrivateDnsName = aws.String("instance-same.ec2.internal") instance0.PrivateIpAddress = aws.String("192.168.0.1") + instance0.PublicDnsName = aws.String("instance-same.ec2.external") instance0.PublicIpAddress = aws.String("1.2.3.4") instance0.InstanceType = aws.String("c3.large") instance0.Placement = &ec2.Placement{AvailabilityZone: aws.String("us-east-1a")} @@ -603,12 +606,14 @@ func TestNodeAddresses(t *testing.T) { if err3 != nil { t.Errorf("Should not error when instance found") } - if len(addrs3) != 3 { - t.Errorf("Should return exactly 3 NodeAddresses") + if len(addrs3) != 5 { + t.Errorf("Should return exactly 5 NodeAddresses") } testHasNodeAddress(t, addrs3, v1.NodeInternalIP, "192.168.0.1") testHasNodeAddress(t, addrs3, v1.NodeLegacyHostIP, "192.168.0.1") testHasNodeAddress(t, addrs3, v1.NodeExternalIP, "1.2.3.4") + testHasNodeAddress(t, addrs3, v1.NodeExternalDNS, "instance-same.ec2.external") + testHasNodeAddress(t, addrs3, v1.NodeInternalDNS, "instance-same.ec2.internal") // Fetch from metadata aws4, fakeServices := mockInstancesResp(&instance0, []*ec2.Instance{&instance0})