@@ -25,6 +25,7 @@ import (
25
25
26
26
"github.com/aws/aws-sdk-go/aws"
27
27
"github.com/aws/aws-sdk-go/service/ec2"
28
+ "github.com/aws/aws-sdk-go/service/ec2/ec2iface"
28
29
"github.com/pkg/errors"
29
30
"k8s.io/utils/ptr"
30
31
@@ -913,6 +914,8 @@ func (s *Service) SDKToInstance(v *ec2.Instance) (*infrav1.Instance, error) {
913
914
914
915
func (s * Service ) getInstanceAddresses (instance * ec2.Instance ) []clusterv1.MachineAddress {
915
916
addresses := []clusterv1.MachineAddress {}
917
+ // Check if the DHCP Option Set has domain name set
918
+ domainName := s .GetDHCPOptionSetDomainName (s .EC2Client , instance .VpcId )
916
919
for _ , eni := range instance .NetworkInterfaces {
917
920
privateDNSAddress := clusterv1.MachineAddress {
918
921
Type : clusterv1 .MachineInternalDNS ,
@@ -922,8 +925,18 @@ func (s *Service) getInstanceAddresses(instance *ec2.Instance) []clusterv1.Machi
922
925
Type : clusterv1 .MachineInternalIP ,
923
926
Address : aws .StringValue (eni .PrivateIpAddress ),
924
927
}
928
+
925
929
addresses = append (addresses , privateDNSAddress , privateIPAddress )
926
930
931
+ if domainName != nil {
932
+ // Add secondary private DNS Name with domain name set in DHCP Option Set
933
+ additionalPrivateDNSAddress := clusterv1.MachineAddress {
934
+ Type : clusterv1 .MachineInternalDNS ,
935
+ Address : fmt .Sprintf ("%s.%s" , strings .Split (privateDNSAddress .Address , "." )[0 ], * domainName ),
936
+ }
937
+ addresses = append (addresses , additionalPrivateDNSAddress )
938
+ }
939
+
927
940
// An elastic IP is attached if association is non nil pointer
928
941
if eni .Association != nil {
929
942
publicDNSAddress := clusterv1.MachineAddress {
@@ -937,6 +950,7 @@ func (s *Service) getInstanceAddresses(instance *ec2.Instance) []clusterv1.Machi
937
950
addresses = append (addresses , publicDNSAddress , publicIPAddress )
938
951
}
939
952
}
953
+
940
954
return addresses
941
955
}
942
956
@@ -1035,6 +1049,54 @@ func (s *Service) ModifyInstanceMetadataOptions(instanceID string, options *infr
1035
1049
return nil
1036
1050
}
1037
1051
1052
+ // GetDHCPOptionSetDomainName returns the domain DNS name for the VPC from the DHCP Options.
1053
+ func (s * Service ) GetDHCPOptionSetDomainName (ec2client ec2iface.EC2API , vpcID * string ) * string {
1054
+ log := s .scope .GetLogger ()
1055
+
1056
+ if vpcID == nil {
1057
+ log .Info ("vpcID is nil, skipping DHCP Option Set discovery" )
1058
+ return nil
1059
+ }
1060
+
1061
+ vpcInput := & ec2.DescribeVpcsInput {
1062
+ VpcIds : []* string {vpcID },
1063
+ }
1064
+
1065
+ vpcResult , err := ec2client .DescribeVpcs (vpcInput )
1066
+ if err != nil {
1067
+ log .Info ("failed to describe VPC, skipping DHCP Option Set discovery" , "vpcID" , * vpcID , "Error" , err .Error ())
1068
+ return nil
1069
+ }
1070
+
1071
+ dhcpInput := & ec2.DescribeDhcpOptionsInput {
1072
+ DhcpOptionsIds : []* string {vpcResult .Vpcs [0 ].DhcpOptionsId },
1073
+ }
1074
+
1075
+ dhcpResult , err := ec2client .DescribeDhcpOptions (dhcpInput )
1076
+ if err != nil {
1077
+ log .Error (err , "failed to describe DHCP Options Set" , "DhcpOptionsSet" , * dhcpResult )
1078
+ return nil
1079
+ }
1080
+
1081
+ for _ , dhcpConfig := range dhcpResult .DhcpOptions [0 ].DhcpConfigurations {
1082
+ if * dhcpConfig .Key == "domain-name" {
1083
+ if len (dhcpConfig .Values ) == 0 {
1084
+ return nil
1085
+ }
1086
+ domainName := dhcpConfig .Values [0 ].Value
1087
+ // default domainName is 'ec2.internal' in us-east-1 and 'region.compute.internal' in the other regions.
1088
+ if (s .scope .Region () == "us-east-1" && * domainName == "ec2.internal" ) ||
1089
+ (s .scope .Region () != "us-east-1" && * domainName == fmt .Sprintf ("%s.compute.internal" , s .scope .Region ())) {
1090
+ return nil
1091
+ }
1092
+
1093
+ return domainName
1094
+ }
1095
+ }
1096
+
1097
+ return nil
1098
+ }
1099
+
1038
1100
// filterGroups filters a list for a string.
1039
1101
func filterGroups (list []string , strToFilter string ) (newList []string ) {
1040
1102
for _ , item := range list {
0 commit comments