mirror of
https://github.com/containers/podman.git
synced 2025-06-25 20:26:51 +08:00
Add support for host-gateway
The `--add-host` option now accepts the special string `host-gateway` instead of an IP Address, which will be mapped to the host IP address. Signed-off-by: Gregor Eichelberger <gregor.eichelberger@tuwien.ac.at>
This commit is contained in:
@ -10,6 +10,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/containers/common/libnetwork/etchosts"
|
||||||
"github.com/containers/storage/pkg/regexp"
|
"github.com/containers/storage/pkg/regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -28,7 +29,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// validateExtraHost validates that the specified string is a valid extrahost and returns it.
|
// validateExtraHost validates that the specified string is a valid extrahost and returns it.
|
||||||
// ExtraHost is in the form of name:ip where the ip has to be a valid ip (ipv4 or ipv6).
|
// ExtraHost is in the form of name:ip where the ip has to be a valid ip (ipv4 or ipv6) or the special string HostGateway.
|
||||||
// for add-host flag
|
// for add-host flag
|
||||||
func ValidateExtraHost(val string) (string, error) {
|
func ValidateExtraHost(val string) (string, error) {
|
||||||
// allow for IPv6 addresses in extra hosts by only splitting on first ":"
|
// allow for IPv6 addresses in extra hosts by only splitting on first ":"
|
||||||
@ -36,6 +37,9 @@ func ValidateExtraHost(val string) (string, error) {
|
|||||||
if len(arr) != 2 || len(arr[0]) == 0 {
|
if len(arr) != 2 || len(arr[0]) == 0 {
|
||||||
return "", fmt.Errorf("bad format for add-host: %q", val)
|
return "", fmt.Errorf("bad format for add-host: %q", val)
|
||||||
}
|
}
|
||||||
|
if arr[1] == etchosts.HostGateway {
|
||||||
|
return val, nil
|
||||||
|
}
|
||||||
if _, err := validateIPAddress(arr[1]); err != nil {
|
if _, err := validateIPAddress(arr[1]); err != nil {
|
||||||
return "", fmt.Errorf("invalid IP address in add-host: %q", arr[1])
|
return "", fmt.Errorf("invalid IP address in add-host: %q", arr[1])
|
||||||
}
|
}
|
||||||
|
@ -3,9 +3,11 @@
|
|||||||
package parse
|
package parse
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/containers/common/libnetwork/etchosts"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -51,6 +53,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: "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: "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: fmt.Sprintf("foobar:%s", etchosts.HostGateway), wantErr: false},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
Reference in New Issue
Block a user