Skip to content

Commit 51a456a

Browse files
committed
api: add Hostname, ExtraHosts for pause container
This change adds Hostname and ExtraHosts to the pause container's docker config and host config respectively. This allows awsvpc tasks to have /etc/hosts entries for the ENI private ips
1 parent c5dd634 commit 51a456a

File tree

6 files changed

+87
-7
lines changed

6 files changed

+87
-7
lines changed

agent/acs/model/api/api-2.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@
206206
"ipv4Addresses":{"shape":"IPv4AddressList"},
207207
"ipv6Addresses":{"shape":"IPv6AddressList"},
208208
"domainName":{"shape":"StringList"},
209-
"domainNameServers":{"shape":"StringList"}
209+
"domainNameServers":{"shape":"StringList"},
210+
"privateDnsName":{"shape":"String"}
210211
}
211212
},
212213
"ElasticNetworkInterfaceList":{

agent/acs/model/ecsacs/api.go

+2
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ type ElasticNetworkInterface struct {
217217
Ipv6Addresses []*IPv6AddressAssignment `locationName:"ipv6Addresses" type:"list"`
218218

219219
MacAddress *string `locationName:"macAddress" type:"string"`
220+
221+
PrivateDnsName *string `locationName:"privateDnsName" type:"string"`
220222
}
221223

222224
// String returns the string representation

agent/api/eni.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ type ENI struct {
3838
// DomainNameSearchList specifies the search list for the domain
3939
// name lookup, for the eni
4040
DomainNameSearchList []string `json:",omitempty"`
41+
42+
// PrivateDnsName is the dns name assigned by the vpc to this eni
43+
PrivateDnsName string `json:",omitempty"`
4144
}
4245

4346
// GetIPV4Addresses returns a list of ipv4 addresses allocated to the ENI
@@ -60,6 +63,11 @@ func (eni *ENI) GetIPV6Addresses() []string {
6063
return addresses
6164
}
6265

66+
// GetHostname returns the hostname assigned to the ENI
67+
func (eni *ENI) GetHostname() string {
68+
return eni.PrivateDnsName
69+
}
70+
6371
// String returns a human readable version of the ENI object
6472
func (eni *ENI) String() string {
6573
var ipv4Addresses []string
@@ -71,8 +79,8 @@ func (eni *ENI) String() string {
7179
ipv6Addresses = append(ipv6Addresses, addr.Address)
7280
}
7381
return fmt.Sprintf(
74-
"eni id:%s, mac: %s, ipv4addresses: [%s], ipv6addresses: [%s], dns: [%s], dns search: [%s]",
75-
eni.ID, eni.MacAddress, strings.Join(ipv4Addresses, ","), strings.Join(ipv6Addresses, ","),
82+
"eni id:%s, mac: %s, hostname: %s, ipv4addresses: [%s], ipv6addresses: [%s], dns: [%s], dns search: [%s]",
83+
eni.ID, eni.MacAddress, eni.PrivateDnsName, strings.Join(ipv4Addresses, ","), strings.Join(ipv6Addresses, ","),
7684
strings.Join(eni.DomainNameServers, ","), strings.Join(eni.DomainNameSearchList, ","))
7785
}
7886

@@ -116,10 +124,11 @@ func ENIFromACS(acsenis []*ecsacs.ElasticNetworkInterface) (*ENI, error) {
116124
}
117125

118126
eni := &ENI{
119-
ID: aws.StringValue(acsenis[0].Ec2Id),
120-
IPV4Addresses: ipv4,
121-
IPV6Addresses: ipv6,
122-
MacAddress: aws.StringValue(acsenis[0].MacAddress),
127+
ID: aws.StringValue(acsenis[0].Ec2Id),
128+
IPV4Addresses: ipv4,
129+
IPV6Addresses: ipv6,
130+
MacAddress: aws.StringValue(acsenis[0].MacAddress),
131+
PrivateDnsName: aws.StringValue(acsenis[0].PrivateDnsName),
123132
}
124133
for _, nameserverIP := range acsenis[0].DomainNameServers {
125134
eni.DomainNameServers = append(eni.DomainNameServers, aws.StringValue(nameserverIP))

agent/api/eni_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func TestENIFromACS(t *testing.T) {
4646
MacAddress: aws.String("mac"),
4747
DomainNameServers: []*string{aws.String(defaultDNS), aws.String(customDNS)},
4848
DomainName: []*string{aws.String(customSearchDomain)},
49+
PrivateDnsName: aws.String("ip.region.compute.internal"),
4950
},
5051
}
5152

@@ -66,6 +67,7 @@ func TestENIFromACS(t *testing.T) {
6667
assert.Equal(t, customDNS, eni.DomainNameServers[1])
6768
assert.Len(t, eni.DomainNameSearchList, 1)
6869
assert.Equal(t, customSearchDomain, eni.DomainNameSearchList[0])
70+
assert.Equal(t, aws.StringValue(acsenis[0].PrivateDnsName), eni.PrivateDnsName)
6971
}
7072

7173
// TestValidateENIFromACS tests the validation of enis from acs

agent/api/task.go

+51
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,11 @@ func (task *Task) dockerConfig(container *Container, apiVersion dockerclient.Doc
473473
config.Labels = make(map[string]string)
474474
}
475475

476+
if container.Type == ContainerCNIPause {
477+
// apply hostname to pause container's docker config
478+
return task.applyENIHostname(config), nil
479+
}
480+
476481
return config, nil
477482
}
478483

@@ -625,6 +630,12 @@ func (task *Task) dockerHostConfig(container *Container, dockerContainerMap map[
625630
hostConfig.NetworkMode = networkMode
626631
// Override 'awsvpc' parameters if needed
627632
if container.Type == ContainerCNIPause {
633+
634+
// apply ExtraHosts to HostConfig for pause container
635+
if hosts := task.generateENIExtraHosts(); hosts != nil {
636+
hostConfig.ExtraHosts = append(hostConfig.ExtraHosts, hosts...)
637+
}
638+
628639
// Override the DNS settings for the pause container if ENI has custom
629640
// DNS settings
630641
return task.overrideDNS(hostConfig), nil
@@ -695,6 +706,46 @@ func (task *Task) overrideDNS(hostConfig *docker.HostConfig) *docker.HostConfig
695706
return hostConfig
696707
}
697708

709+
// applyENIHostname adds the hostname provided by the ENI message to the
710+
// container's docker config. At the time of implmentation, we are only using it
711+
// to configure the pause container for awsvpc tasks
712+
func (task *Task) applyENIHostname(dockerConfig *docker.Config) *docker.Config {
713+
eni := task.GetTaskENI()
714+
if eni == nil {
715+
return dockerConfig
716+
}
717+
718+
hostname := eni.GetHostname()
719+
if hostname == "" {
720+
return dockerConfig
721+
}
722+
723+
dockerConfig.Hostname = hostname
724+
return dockerConfig
725+
}
726+
727+
// generateENIExtraHosts returns a slice of strings of the form "hostname:ip"
728+
// that is generated using the hostname and ip addresses allocated to the ENI
729+
func (task *Task) generateENIExtraHosts() []string {
730+
eni := task.GetTaskENI()
731+
if eni == nil {
732+
return nil
733+
}
734+
735+
hostname := eni.GetHostname()
736+
if hostname == "" {
737+
return nil
738+
}
739+
740+
extraHosts := []string{}
741+
742+
for _, ip := range eni.GetIPV4Addresses() {
743+
host := fmt.Sprintf("%s:%s", hostname, ip)
744+
extraHosts = append(extraHosts, host)
745+
}
746+
return extraHosts
747+
}
748+
698749
func (task *Task) dockerLinks(container *Container, dockerContainerMap map[string]*DockerContainer) ([]string, error) {
699750
dockerLinkArr := make([]string, len(container.Links))
700751
for i, link := range container.Links {

agent/api/task_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,21 @@ func TestDockerHostConfigPauseContainer(t *testing.T) {
281281
assert.Nil(t, err)
282282
assert.Equal(t, []string{"169.254.169.253"}, config.DNS)
283283
assert.Equal(t, []string{"us-west-2.compute.internal"}, config.DNSSearch)
284+
285+
// Verify eni ExtraHosts added to HostConfig for "pause" container
286+
ipaddr := &ENIIPV4Address{Primary: true, Address: "10.0.1.1"}
287+
testTask.ENI.IPV4Addresses = []*ENIIPV4Address{ipaddr}
288+
testTask.ENI.PrivateDnsName = "eni.ip.region.compute.internal"
289+
290+
config, err = testTask.DockerHostConfig(testTask.Containers[2], dockerMap(testTask), defaultDockerClientAPIVersion)
291+
assert.Nil(t, err)
292+
assert.Equal(t, []string{"eni.ip.region.compute.internal:10.0.1.1"}, config.ExtraHosts)
293+
294+
// Verify eni Hostname is added to DockerConfig for "pause" container
295+
dockerconfig, dockerConfigErr := testTask.DockerConfig(testTask.Containers[2], defaultDockerClientAPIVersion)
296+
assert.Nil(t, dockerConfigErr)
297+
assert.Equal(t, "eni.ip.region.compute.internal", dockerconfig.Hostname)
298+
284299
}
285300

286301
func TestBadDockerHostConfigRawConfig(t *testing.T) {

0 commit comments

Comments
 (0)