Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for special IP designator host-gateway in --add-hosts. #14392

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/podman/parse/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func ValidateExtraHost(val string) (string, error) { // nolint
if len(arr) != 2 || len(arr[0]) == 0 {
return "", fmt.Errorf("bad format for add-host: %q", val)
}
if (arr[1] == "host-gateway") {
return val, nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update the function comment please to cover this new case?

if _, err := validateIPAddress(arr[1]); err != nil {
return "", fmt.Errorf("invalid IP address in add-host: %q", arr[1])
}
Expand Down
1 change: 1 addition & 0 deletions cmd/podman/parse/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func TestValidateExtraHost(t *testing.T) {
{name: "bad-ipv6", args: args{val: "foobar:0db8:85a3:0000:0000:8a2e:0370:7334.0000.0000.000"}, want: "", wantErr: true},
{name: "noname-ipv6", args: args{val: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"}, want: "", wantErr: true},
{name: "noname-ipv6", args: args{val: ":2001:0db8:85a3:0000:0000:8a2e:0370:7334"}, want: "", wantErr: true},
{name: "host-gateway", args: args{val: "foobar:host-gateway"}, want: "foobar:host-gateway", wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
23 changes: 21 additions & 2 deletions libpod/container_internal_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2562,6 +2562,17 @@ func (c *Container) getHostsEntries() (etchosts.HostEntries, error) {
return entries, nil
}

// replaces a host IP of "host-gateway" with a corresponding gatewayIP in a host entry
func mapHostGateway(host string, gatewayIP string) string {
arr := strings.SplitN(host, ":", 2)

if len(arr) != 2 || arr[1] != "host-gateway" {
return host
}

return arr[0] + ":" + gatewayIP
}

func (c *Container) createHosts() error {
var containerIPsEntries etchosts.HostEntries
var err error
Expand All @@ -2579,12 +2590,20 @@ func (c *Container) createHosts() error {
return err
}

hostContainersInternalIP := etchosts.GetHostContainersInternalIP(c.runtime.config, c.state.NetworkStatus, c.runtime.network)

extraHosts := make([]string, len(c.config.HostAdd))

for i, host := range c.config.HostAdd {
extraHosts[i] = mapHostGateway(host, hostContainersInternalIP)
}

targetFile := filepath.Join(c.state.RunDir, "hosts")
err = etchosts.New(&etchosts.Params{
BaseFile: baseHostFile,
ExtraHosts: c.config.HostAdd,
ExtraHosts: extraHosts,
ContainerIPs: containerIPsEntries,
HostContainersInternalIP: etchosts.GetHostContainersInternalIP(c.runtime.config, c.state.NetworkStatus, c.runtime.network),
HostContainersInternalIP: hostContainersInternalIP,
TargetFile: targetFile,
})
if err != nil {
Expand Down