From ab507dfc1f0c824386ea6ca0efc1abe7968981fd Mon Sep 17 00:00:00 2001 From: Bill Warshaw Date: Sat, 29 Sep 2018 22:14:21 -0400 Subject: [PATCH] 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 --- pkg/kubelet/kubelet_pods.go | 6 ++---- pkg/kubelet/kubelet_pods_test.go | 24 ++++++------------------ test/e2e/common/kubelet.go | 2 +- 3 files changed, 9 insertions(+), 23 deletions(-) diff --git a/pkg/kubelet/kubelet_pods.go b/pkg/kubelet/kubelet_pods.go index ca7f6336608..59a2fd60bb4 100644 --- a/pkg/kubelet/kubelet_pods.go +++ b/pkg/kubelet/kubelet_pods.go @@ -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() } diff --git a/pkg/kubelet/kubelet_pods_test.go b/pkg/kubelet/kubelet_pods_test.go index 3e34435adb8..42221bdb40c 100644 --- a/pkg/kubelet/kubelet_pods_test.go +++ b/pkg/kubelet/kubelet_pods_test.go @@ -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 `, }, } diff --git a/test/e2e/common/kubelet.go b/test/e2e/common/kubelet.go index 8038d325aa4..bfe791f1b51 100644 --- a/test/e2e/common/kubelet.go +++ b/test/e2e/common/kubelet.go @@ -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) }