From 4b659223aae60494f94069ecb899eb642fa91f32 Mon Sep 17 00:00:00 2001 From: Jerome Degroote Date: Thu, 29 Aug 2024 10:47:51 +0200 Subject: [PATCH] Add multiple host for single IP Signed-off-by: Jerome degroote --- libnetwork/etchosts/hosts.go | 10 ++++++---- libnetwork/etchosts/hosts_test.go | 10 +++++----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/libnetwork/etchosts/hosts.go b/libnetwork/etchosts/hosts.go index 7ca62137b..8a7cc534e 100644 --- a/libnetwork/etchosts/hosts.go +++ b/libnetwork/etchosts/hosts.go @@ -229,9 +229,10 @@ func checkIfEntryExists(current HostEntry, entries HostEntries) bool { return false } -// parseExtraHosts converts a slice of "name:ip" string to entries. -// Because podman and buildah both store the extra hosts in this format -// we convert it here instead of having to this on the caller side. +// parseExtraHosts converts a slice of "name1;name2;name3:ip" string to entries. +// Each entry can contain one or more hostnames separated by semicolons and an IP address separated by a colon. +// Because podman and buildah both store the extra hosts in this format, +// we convert it here instead of having to do this on the caller side. func parseExtraHosts(extraHosts []string, hostContainersInternalIP string) (HostEntries, error) { entries := make(HostEntries, 0, len(extraHosts)) for _, entry := range extraHosts { @@ -252,7 +253,8 @@ func parseExtraHosts(extraHosts []string, hostContainersInternalIP string) (Host } ip = hostContainersInternalIP } - e := HostEntry{IP: ip, Names: []string{values[0]}} + names := strings.Split(values[0], ";") + e := HostEntry{IP: ip, Names: names} entries = append(entries, e) } return entries, nil diff --git a/libnetwork/etchosts/hosts_test.go b/libnetwork/etchosts/hosts_test.go index b5678a9a5..6654125df 100644 --- a/libnetwork/etchosts/hosts_test.go +++ b/libnetwork/etchosts/hosts_test.go @@ -53,12 +53,12 @@ const baseFileContent4 = `127.0.0.1 localhost ` const targetFileContent4 = `1.1.1.1 name1 -2.2.2.2 name2 +2.2.2.2 name2 name3 name4 127.0.0.1 localhost ` -const targetFileContent5 = `1.1.1.1 name1 -2.2.2.2 name2 +const targetFileContent5 = `1.1.1.1 name1 name2 +2.2.2.2 name3 127.0.1.1 localhost ` @@ -137,13 +137,13 @@ func TestNew(t *testing.T) { { name: "extra hosts", baseFileContent: baseFileContent4, - extraHosts: []string{"name1:1.1.1.1", "name2:2.2.2.2"}, + extraHosts: []string{"name1:1.1.1.1", "name2;name3;name4:2.2.2.2"}, expectedTargetFileContent: targetFileContent4, }, { name: "extra hosts with localhost", baseFileContent: "", - extraHosts: []string{"name1:1.1.1.1", "name2:2.2.2.2", "localhost:127.0.1.1"}, + extraHosts: []string{"name1;name2:1.1.1.1", "name3:2.2.2.2", "localhost:127.0.1.1"}, expectedTargetFileContent: targetFileContent5, }, {