瀏覽代碼

Make /etc/hosts records consistent

Fixes #8972

Signed-off-by: Alexandr Morozov <lk4d4@docker.com>
Alexandr Morozov 10 年之前
父節點
當前提交
6cbe1fa726
共有 2 個文件被更改,包括 35 次插入21 次删除
  1. 4 4
      daemon/container.go
  2. 31 17
      pkg/networkfs/etchosts/etchosts.go

+ 4 - 4
daemon/container.go

@@ -420,7 +420,7 @@ func (container *Container) buildHostsFiles(IP string) error {
 	}
 	container.HostsPath = hostsPath
 
-	extraContent := make(map[string]string)
+	var extraContent []etchosts.Record
 
 	children, err := container.daemon.Children(container.Name)
 	if err != nil {
@@ -429,15 +429,15 @@ func (container *Container) buildHostsFiles(IP string) error {
 
 	for linkAlias, child := range children {
 		_, alias := path.Split(linkAlias)
-		extraContent[alias] = child.NetworkSettings.IPAddress
+		extraContent = append(extraContent, etchosts.Record{Hosts: alias, IP: child.NetworkSettings.IPAddress})
 	}
 
 	for _, extraHost := range container.hostConfig.ExtraHosts {
 		parts := strings.Split(extraHost, ":")
-		extraContent[parts[0]] = parts[1]
+		extraContent = append(extraContent, etchosts.Record{Hosts: parts[0], IP: parts[1]})
 	}
 
-	return etchosts.Build(container.HostsPath, IP, container.Config.Hostname, container.Config.Domainname, &extraContent)
+	return etchosts.Build(container.HostsPath, IP, container.Config.Hostname, container.Config.Domainname, extraContent)
 }
 
 func (container *Container) buildHostnameAndHostsFiles(IP string) error {

+ 31 - 17
pkg/networkfs/etchosts/etchosts.go

@@ -3,40 +3,54 @@ package etchosts
 import (
 	"bytes"
 	"fmt"
+	"io"
 	"io/ioutil"
 	"regexp"
 )
 
-var defaultContent = map[string]string{
-	"localhost":                            "127.0.0.1",
-	"localhost ip6-localhost ip6-loopback": "::1",
-	"ip6-localnet":                         "fe00::0",
-	"ip6-mcastprefix":                      "ff00::0",
-	"ip6-allnodes":                         "ff02::1",
-	"ip6-allrouters":                       "ff02::2",
+type Record struct {
+	Hosts string
+	IP    string
 }
 
-func Build(path, IP, hostname, domainname string, extraContent *map[string]string) error {
+func (r Record) WriteTo(w io.Writer) (int64, error) {
+	n, err := fmt.Fprintf(w, "%s\t%s\n", r.IP, r.Hosts)
+	return int64(n), err
+}
+
+var defaultContent = []Record{
+	{Hosts: "localhost", IP: "127.0.0.1"},
+	{Hosts: "localhost ip6-localhost ip6-loopback", IP: "::1"},
+	{Hosts: "ip6-localnet", IP: "fe00::0"},
+	{Hosts: "ip6-mcastprefix", IP: "ff00::0"},
+	{Hosts: "ip6-allnodes", IP: "ff02::1"},
+	{Hosts: "ip6-allrouters", IP: "ff02::2"},
+}
+
+func Build(path, IP, hostname, domainname string, extraContent []Record) error {
 	content := bytes.NewBuffer(nil)
 	if IP != "" {
+		var mainRec Record
+		mainRec.IP = IP
 		if domainname != "" {
-			content.WriteString(fmt.Sprintf("%s\t%s.%s %s\n", IP, hostname, domainname, hostname))
+			mainRec.Hosts = fmt.Sprintf("%s.%s %s", hostname, domainname, hostname)
 		} else {
-			content.WriteString(fmt.Sprintf("%s\t%s\n", IP, hostname))
+			mainRec.Hosts = hostname
+		}
+		if _, err := mainRec.WriteTo(content); err != nil {
+			return err
 		}
 	}
 
-	for hosts, ip := range defaultContent {
-		if _, err := content.WriteString(fmt.Sprintf("%s\t%s\n", ip, hosts)); err != nil {
+	for _, r := range defaultContent {
+		if _, err := r.WriteTo(content); err != nil {
 			return err
 		}
 	}
 
-	if extraContent != nil {
-		for hosts, ip := range *extraContent {
-			if _, err := content.WriteString(fmt.Sprintf("%s\t%s\n", ip, hosts)); err != nil {
-				return err
-			}
+	for _, r := range extraContent {
+		if _, err := r.WriteTo(content); err != nil {
+			return err
 		}
 	}