Skip to content

Commit

Permalink
libnetwork: Add special IP designator support
Browse files Browse the repository at this point in the history
This change adds support for the special IP designator `host-gateway` to
the etchosts package.

The first part of fixing containers/podman#14390

Signed-off-by: Gregor Eichelberger <[email protected]>
  • Loading branch information
geichelberger committed Jul 6, 2023
1 parent 2b3ce8f commit e613065
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 7 additions & 3 deletions libnetwork/etchosts/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func Remove(file string, entries HostEntries) error {

// new see comment on New()
func newHost(params *Params) error {
entries, err := parseExtraHosts(params.ExtraHosts)
entries, err := parseExtraHosts(params.ExtraHosts, params.HostContainersInternalIP)
if err != nil {
return err
}
Expand Down Expand Up @@ -230,7 +230,7 @@ func checkIfEntryExists(current HostEntry, entries HostEntries) bool {
// 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.
func parseExtraHosts(extraHosts []string) (HostEntries, error) {
func parseExtraHosts(extraHosts []string, hostContainersInternalIP string) (HostEntries, error) {
entries := make(HostEntries, 0, len(extraHosts))
for _, entry := range extraHosts {
values := strings.SplitN(entry, ":", 2)
Expand All @@ -243,7 +243,11 @@ func parseExtraHosts(extraHosts []string) (HostEntries, error) {
if values[1] == "" {
return nil, fmt.Errorf("IP address in host entry %q is empty", entry)
}
e := HostEntry{IP: values[1], Names: []string{values[0]}}
ip := values[1]
if values[1] == "host-gateway" {
ip = hostContainersInternalIP
}
e := HostEntry{IP: ip, Names: []string{values[0]}}
entries = append(entries, e)
}
return entries, nil
Expand Down
7 changes: 7 additions & 0 deletions libnetwork/etchosts/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@ func TestNew(t *testing.T) {
hostContainersInternal: "10.0.0.1",
expectedTargetFileContent: targetFileContent1 + "10.0.0.1\thost.containers.internal\n",
},
{
name: "with host.containers.internal ip and host-gateway",
baseFileContent: baseFileContent1Spaces,
extraHosts: []string{"gatewayname:host-gateway"},
hostContainersInternal: "10.0.0.1",
expectedTargetFileContent: "10.0.0.1\tgatewayname\n" + targetFileContent1 + "10.0.0.1\thost.containers.internal\n",
},
{
name: "host.containers.internal not added when already present in extra hosts",
baseFileContent: baseFileContent1Spaces,
Expand Down

0 comments on commit e613065

Please sign in to comment.