Write HostAliases aliases on same line per host IP

* change HostAliases to put all aliases for an IP
  on the same line in /etc/hosts rather than writing
  one line per IP-alias pair
* having multiple entries in /etc/hosts for the same IP
  causes issues with DNS resolution for some software
* https://unix.stackexchange.com/questions/102660/hosts-file-is-it-incorrect-to-have-the-same-ip-address-on-multiple-lines
pull/58/head
Bill Warshaw 2018-09-29 22:14:21 -04:00
parent 8307fb2fb3
commit ab507dfc1f
3 changed files with 9 additions and 23 deletions

View File

@ -356,11 +356,9 @@ func hostsEntriesFromHostAliases(hostAliases []v1.HostAlias) []byte {
var buffer bytes.Buffer
buffer.WriteString("\n")
buffer.WriteString("# Entries added by HostAliases.\n")
// write each IP/hostname pair as an entry into hosts file
// for each IP, write all aliases onto single line in hosts file
for _, hostAlias := range hostAliases {
for _, hostname := range hostAlias.Hostnames {
buffer.WriteString(fmt.Sprintf("%s\t%s\n", hostAlias.IP, hostname))
}
buffer.WriteString(fmt.Sprintf("%s\t%s\n", hostAlias.IP, strings.Join(hostAlias.Hostnames, "\t")))
}
return buffer.Bytes()
}

View File

@ -183,9 +183,7 @@ fe00::2 ip6-allrouters
123.45.67.89 some.domain
# Entries added by HostAliases.
123.45.67.89 foo
123.45.67.89 bar
123.45.67.89 baz
123.45.67.89 foo bar baz
`,
},
{
@ -214,12 +212,8 @@ fe00::2 ip6-allrouters
12.34.56.78 another.domain
# Entries added by HostAliases.
123.45.67.89 foo
123.45.67.89 bar
123.45.67.89 baz
456.78.90.123 park
456.78.90.123 doo
456.78.90.123 boo
123.45.67.89 foo bar baz
456.78.90.123 park doo boo
`,
},
}
@ -300,9 +294,7 @@ fe00::2 ip6-allrouters
203.0.113.1 podFoo.domainFoo podFoo
# Entries added by HostAliases.
123.45.67.89 foo
123.45.67.89 bar
123.45.67.89 baz
123.45.67.89 foo bar baz
`,
},
{
@ -323,12 +315,8 @@ fe00::2 ip6-allrouters
203.0.113.1 podFoo.domainFoo podFoo
# Entries added by HostAliases.
123.45.67.89 foo
123.45.67.89 bar
123.45.67.89 baz
456.78.90.123 park
456.78.90.123 doo
456.78.90.123 boo
123.45.67.89 foo bar baz
456.78.90.123 park doo boo
`,
},
}

View File

@ -175,7 +175,7 @@ var _ = framework.KubeDescribe("Kubelet", func() {
buf.ReadFrom(rc)
hostsFileContent := buf.String()
if !strings.Contains(hostsFileContent, "123.45.67.89\tfoo") || !strings.Contains(hostsFileContent, "123.45.67.89\tbar") {
if !strings.Contains(hostsFileContent, "123.45.67.89\tfoo\tbar") {
return fmt.Errorf("expected hosts file to contain entries from HostAliases. Got:\n%+v", hostsFileContent)
}