Skip to content

Commit

Permalink
api: add Hostname, ExtraHosts for pause container
Browse files Browse the repository at this point in the history
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
  • Loading branch information
adnxn committed Mar 8, 2018
1 parent 03a1e44 commit 88f892a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
8 changes: 8 additions & 0 deletions agent/api/eni.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ func (eni *ENI) GetIPV6Addresses() []string {
return addresses
}

// GetHostname returns the hostname assigned to the ENI
func (eni *ENI) GetHostname() string {
// WIP
// pending acs model change to include hostname
// return eni.Hostname
return "ip.region.compute.internal"
}

// String returns a human readable version of the ENI object
func (eni *ENI) String() string {
var ipv4Addresses []string
Expand Down
53 changes: 53 additions & 0 deletions agent/api/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,12 @@ func (task *Task) dockerConfig(container *Container, apiVersion dockerclient.Doc
config.Labels = make(map[string]string)
}

if container.Type == ContainerCNIPause {
// WIP
// hostname to pause container's docker config
return task.applyENIHostname(config), nil
}

return config, nil
}

Expand Down Expand Up @@ -625,6 +631,13 @@ func (task *Task) dockerHostConfig(container *Container, dockerContainerMap map[
hostConfig.NetworkMode = networkMode
// Override 'awsvpc' parameters if needed
if container.Type == ContainerCNIPause {

// WIP
// apply ExtraHosts to HostConfig for pause container
if hosts := task.generateENIExtraHosts(); hosts != nil {
hostConfig.ExtraHosts = append(hostConfig.ExtraHosts, hosts...)
}

// Override the DNS settings for the pause container if ENI has custom
// DNS settings
return task.overrideDNS(hostConfig), nil
Expand Down Expand Up @@ -695,6 +708,46 @@ func (task *Task) overrideDNS(hostConfig *docker.HostConfig) *docker.HostConfig
return hostConfig
}

// applyENIHostname adds the hostname provided by the ENI message to the
// container's docker config. At the time of implmentation, we are only using it
// to configure the pause container for awsvpc tasks
func (task *Task) applyENIHostname(dockerConfig *docker.Config) *docker.Config {
eni := task.GetTaskENI()
if eni == nil {
return dockerConfig
}

hostname := eni.GetHostname()
if hostname == "" {
return dockerConfig
}

dockerConfig.Hostname = hostname
return dockerConfig
}

// generateENIExtraHosts returns a slice of strings of the form "hostname:ip"
// that is generated using the hostname and ip addresses allocated to the ENI
func (task *Task) generateENIExtraHosts() []string {
eni := task.GetTaskENI()
if eni == nil {
return nil
}

hostname := eni.GetHostname()
if hostname == "" {
return nil
}

extraHosts := []string{}

for _, ip := range eni.GetIPV6Addresses() {
host := fmt.Sprintf("%s:%s", hostname, ip)
extraHosts = append(extraHosts, host)
}
return extraHosts
}

func (task *Task) dockerLinks(container *Container, dockerContainerMap map[string]*DockerContainer) ([]string, error) {
dockerLinkArr := make([]string, len(container.Links))
for i, link := range container.Links {
Expand Down
3 changes: 3 additions & 0 deletions agent/api/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ func TestDockerHostConfigPauseContainer(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, []string{"169.254.169.253"}, config.DNS)
assert.Equal(t, []string{"us-west-2.compute.internal"}, config.DNSSearch)

// Verify ExtraHosts are added for eni IPs

}

func TestBadDockerHostConfigRawConfig(t *testing.T) {
Expand Down

0 comments on commit 88f892a

Please sign in to comment.