Skip to content

Commit 6df4458

Browse files
Merge pull request containers#1549 from geichelberger/add-host-gateway-support
libnetwork: Add support for host-gatway
2 parents 74c49fc + 9d93afa commit 6df4458

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

libnetwork/etchosts/hosts.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
const (
1616
HostContainersInternal = "host.containers.internal"
17+
HostGateway = "host-gateway"
1718
localhost = "localhost"
1819
)
1920

@@ -98,7 +99,7 @@ func Remove(file string, entries HostEntries) error {
9899

99100
// new see comment on New()
100101
func newHost(params *Params) error {
101-
entries, err := parseExtraHosts(params.ExtraHosts)
102+
entries, err := parseExtraHosts(params.ExtraHosts, params.HostContainersInternalIP)
102103
if err != nil {
103104
return err
104105
}
@@ -230,7 +231,7 @@ func checkIfEntryExists(current HostEntry, entries HostEntries) bool {
230231
// parseExtraHosts converts a slice of "name:ip" string to entries.
231232
// Because podman and buildah both store the extra hosts in this format
232233
// we convert it here instead of having to this on the caller side.
233-
func parseExtraHosts(extraHosts []string) (HostEntries, error) {
234+
func parseExtraHosts(extraHosts []string, hostContainersInternalIP string) (HostEntries, error) {
234235
entries := make(HostEntries, 0, len(extraHosts))
235236
for _, entry := range extraHosts {
236237
values := strings.SplitN(entry, ":", 2)
@@ -243,7 +244,14 @@ func parseExtraHosts(extraHosts []string) (HostEntries, error) {
243244
if values[1] == "" {
244245
return nil, fmt.Errorf("IP address in host entry %q is empty", entry)
245246
}
246-
e := HostEntry{IP: values[1], Names: []string{values[0]}}
247+
ip := values[1]
248+
if values[1] == HostGateway {
249+
if hostContainersInternalIP == "" {
250+
return nil, fmt.Errorf("unable to replace %q of host entry %q: host containers internal IP address is empty", HostGateway, entry)
251+
}
252+
ip = hostContainersInternalIP
253+
}
254+
e := HostEntry{IP: ip, Names: []string{values[0]}}
247255
entries = append(entries, e)
248256
}
249257
return entries, nil

libnetwork/etchosts/hosts_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ func TestNew(t *testing.T) {
218218
hostContainersInternal: "10.0.0.1",
219219
expectedTargetFileContent: targetFileContent1 + "10.0.0.1\thost.containers.internal\n",
220220
},
221+
{
222+
name: "with host.containers.internal ip and host-gateway",
223+
baseFileContent: baseFileContent1Spaces,
224+
extraHosts: []string{"gatewayname:host-gateway"},
225+
hostContainersInternal: "10.0.0.1",
226+
expectedTargetFileContent: "10.0.0.1\tgatewayname\n" + targetFileContent1 + "10.0.0.1\thost.containers.internal\n",
227+
},
221228
{
222229
name: "host.containers.internal not added when already present in extra hosts",
223230
baseFileContent: baseFileContent1Spaces,
@@ -267,6 +274,12 @@ func TestNew(t *testing.T) {
267274
extraHosts: []string{"name"},
268275
wantErrString: "unable to parse host entry \"name\": incorrect format",
269276
},
277+
{
278+
name: "invalid host-gateway",
279+
baseFileContent: baseFileContent1Spaces,
280+
extraHosts: []string{"gatewayname:host-gateway"},
281+
wantErrString: "host containers internal IP address is empty",
282+
},
270283
}
271284

272285
for _, tt := range tests {

0 commit comments

Comments
 (0)