From 88f892a99c80dce3b92f70b67a4906bbec328ece Mon Sep 17 00:00:00 2001 From: Adnan Khan Date: Tue, 6 Mar 2018 13:14:51 -0800 Subject: [PATCH] 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 --- agent/api/eni.go | 8 +++++++ agent/api/task.go | 53 ++++++++++++++++++++++++++++++++++++++++++ agent/api/task_test.go | 3 +++ 3 files changed, 64 insertions(+) diff --git a/agent/api/eni.go b/agent/api/eni.go index 797f5d45121..95151538e3e 100644 --- a/agent/api/eni.go +++ b/agent/api/eni.go @@ -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 diff --git a/agent/api/task.go b/agent/api/task.go index 711fd3afe5b..b43327eeb80 100644 --- a/agent/api/task.go +++ b/agent/api/task.go @@ -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 } @@ -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 @@ -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 { diff --git a/agent/api/task_test.go b/agent/api/task_test.go index 314dc6532e2..dc63946b391 100644 --- a/agent/api/task_test.go +++ b/agent/api/task_test.go @@ -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) {