Skip to content

Commit

Permalink
Merge pull request #2139 from jerome59/main
Browse files Browse the repository at this point in the history
Add multiple host for single IP
  • Loading branch information
openshift-merge-bot[bot] authored Aug 29, 2024
2 parents 8483ef6 + 4b65922 commit 0a8ded3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
10 changes: 6 additions & 4 deletions libnetwork/etchosts/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions libnetwork/etchosts/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`

Expand Down Expand Up @@ -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,
},
{
Expand Down

0 comments on commit 0a8ded3

Please sign in to comment.