mirror of
https://github.com/containers/podman.git
synced 2025-07-04 10:10:32 +08:00
Podman CLI --add-host with multiple host for a single IP
Signed-off-by: Jerome degroote <jeromedu59230@gmx.fr>
This commit is contained in:
@ -29,14 +29,23 @@ var (
|
||||
)
|
||||
|
||||
// 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) or the special string HostGateway.
|
||||
// ExtraHost is in the form of name1;name2;name3:ip where the ip has to be a valid ip (ipv4 or ipv6) or the special string HostGateway.
|
||||
// for add-host flag
|
||||
func ValidateExtraHost(val string) (string, error) {
|
||||
// allow for IPv6 addresses in extra hosts by only splitting on first ":"
|
||||
name, ip, hasIP := strings.Cut(val, ":")
|
||||
if !hasIP || len(name) == 0 {
|
||||
names, ip, hasIP := strings.Cut(val, ":")
|
||||
if !hasIP || len(names) == 0 {
|
||||
return "", fmt.Errorf("bad format for add-host: %q", val)
|
||||
}
|
||||
|
||||
// Split the hostnames by semicolon and validate each one
|
||||
nameList := strings.Split(names, ";")
|
||||
for _, name := range nameList {
|
||||
if len(name) == 0 {
|
||||
return "", fmt.Errorf("hostname in add-host %q is empty", val)
|
||||
}
|
||||
}
|
||||
|
||||
if ip == etchosts.HostGateway {
|
||||
return val, nil
|
||||
}
|
||||
|
@ -42,17 +42,23 @@ func TestValidateExtraHost(t *testing.T) {
|
||||
}{
|
||||
// 2001:0db8:85a3:0000:0000:8a2e:0370:7334
|
||||
{name: "good-ipv4", args: args{val: "foobar:192.168.1.1"}, want: "foobar:192.168.1.1", wantErr: false},
|
||||
{name: "good-multiple-ipv4", args: args{val: "host1;host2;host3:192.168.1.1"}, want: "host1;host2;host3:192.168.1.1", wantErr: false},
|
||||
{name: "bad-ipv4", args: args{val: "foobar:999.999.999.99"}, want: "", wantErr: true},
|
||||
{name: "bad-ipv4", args: args{val: "foobar:999.999.999"}, want: "", wantErr: true},
|
||||
{name: "bad-multiple-ipv4", args: args{val: "host1;host2;host3:999.999.999"}, want: "", wantErr: true},
|
||||
{name: "noname-ipv4", args: args{val: "192.168.1.1"}, want: "", wantErr: true},
|
||||
{name: "noname-ipv4", args: args{val: ":192.168.1.1"}, want: "", wantErr: true},
|
||||
{name: "noname-multiple-ipv4", args: args{val: "host1;;host3:192.168.1.1"}, want: "", wantErr: true},
|
||||
{name: "noip", args: args{val: "foobar:"}, want: "", wantErr: true},
|
||||
{name: "noip", args: args{val: "foobar"}, want: "", wantErr: true},
|
||||
{name: "good-ipv6", args: args{val: "foobar:2001:0db8:85a3:0000:0000:8a2e:0370:7334"}, want: "foobar:2001:0db8:85a3:0000:0000:8a2e:0370:7334", wantErr: false},
|
||||
{name: "good-multiple-ipv6", args: args{val: "host1;host2;host3:2001:0db8:85a3:0000:0000:8a2e:0370:7334"}, want: "host1;host2;host3:2001:0db8:85a3:0000:0000:8a2e:0370:7334", wantErr: false},
|
||||
{name: "bad-ipv6", args: args{val: "foobar:0db8:85a3:0000:0000:8a2e:0370:7334"}, want: "", wantErr: true},
|
||||
{name: "bad-ipv6", args: args{val: "foobar:0db8:85a3:0000:0000:8a2e:0370:7334.0000.0000.000"}, want: "", wantErr: true},
|
||||
{name: "bad-multiple-ipv6", args: args{val: "host1;host2;host3: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-multiple-ipv6", args: args{val: "host1;;host3: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 {
|
||||
|
@ -1629,7 +1629,7 @@ msgstr ""
|
||||
#: ../../source/markdown/podman-pod-create.1.md:39
|
||||
#: ../../source/markdown/podman-run.1.md:94
|
||||
msgid ""
|
||||
"Add a line to /etc/hosts. The format is hostname:ip. The **--add-host** "
|
||||
"Add a line to /etc/hosts. The format is hostname1;hostname2;hostname3:ip. Multiple hostnames for the same IP can be separated by semicolons. The **--add-host** "
|
||||
"option can be set multiple times. Conflicts with the **--no-hosts** "
|
||||
"option."
|
||||
msgstr ""
|
||||
|
@ -5,6 +5,7 @@
|
||||
#### **--add-host**=*host:ip*
|
||||
|
||||
Add a custom host-to-IP mapping (host:ip)
|
||||
Multiple hostnames for the same IP can be separated by semicolons.
|
||||
|
||||
Add a line to /etc/hosts. The format is hostname:ip. The **--add-host**
|
||||
Add a line to /etc/hosts. The format is hostname:ip or hostname1;hostname2;hostname3:ip if you want to map multiple hostnames to the same ip without duplicating the --add-host parameter. The **--add-host**
|
||||
option can be set multiple times. Conflicts with the **--no-hosts** option.
|
||||
|
Reference in New Issue
Block a user