Merge pull request #1803 from thaJeztah/add_ip_address_to_advanced_options

Add ip address to advanced options
This commit is contained in:
Silvin Lubecki 2019-12-04 15:16:15 +01:00 committed by GitHub
commit 8547dfcff7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 13 deletions

View File

@ -735,6 +735,12 @@ func applyContainerOptions(n *opts.NetworkAttachmentOpts, copts *containerOption
if len(n.Links) > 0 && copts.links.Len() > 0 { if len(n.Links) > 0 && copts.links.Len() > 0 {
return errdefs.InvalidParameter(errors.New("conflicting options: cannot specify both --link and per-network links")) return errdefs.InvalidParameter(errors.New("conflicting options: cannot specify both --link and per-network links"))
} }
if n.IPv4Address != "" && copts.ipv4Address != "" {
return errdefs.InvalidParameter(errors.New("conflicting options: cannot specify both --ip and per-network IPv4 address"))
}
if n.IPv6Address != "" && copts.ipv6Address != "" {
return errdefs.InvalidParameter(errors.New("conflicting options: cannot specify both --ip6 and per-network IPv6 address"))
}
if copts.aliases.Len() > 0 { if copts.aliases.Len() > 0 {
n.Aliases = make([]string, copts.aliases.Len()) n.Aliases = make([]string, copts.aliases.Len())
copy(n.Aliases, copts.aliases.GetAll()) copy(n.Aliases, copts.aliases.GetAll())
@ -743,10 +749,12 @@ func applyContainerOptions(n *opts.NetworkAttachmentOpts, copts *containerOption
n.Links = make([]string, copts.links.Len()) n.Links = make([]string, copts.links.Len())
copy(n.Links, copts.links.GetAll()) copy(n.Links, copts.links.GetAll())
} }
if copts.ipv4Address != "" {
// TODO add IPv4/IPv6 options to the csv notation for --network, and error-out in case of conflicting options
n.IPv4Address = copts.ipv4Address n.IPv4Address = copts.ipv4Address
}
if copts.ipv6Address != "" {
n.IPv6Address = copts.ipv6Address n.IPv6Address = copts.ipv6Address
}
// TODO should linkLocalIPs be added to the _first_ network only, or to _all_ networks? (should this be a per-network option as well?) // TODO should linkLocalIPs be added to the _first_ network only, or to _all_ networks? (should this be a per-network option as well?)
if copts.linkLocalIPs.Len() > 0 { if copts.linkLocalIPs.Len() > 0 {

View File

@ -449,7 +449,7 @@ func TestParseNetworkConfig(t *testing.T) {
"--network-alias", "web1", "--network-alias", "web1",
"--network-alias", "web2", "--network-alias", "web2",
"--network", "net2", "--network", "net2",
"--network", "name=net3,alias=web3,driver-opt=field3=value3", "--network", "name=net3,alias=web3,driver-opt=field3=value3,ip=172.20.88.22,ip6=2001:db8::8822",
}, },
expected: map[string]*networktypes.EndpointSettings{ expected: map[string]*networktypes.EndpointSettings{
"net1": { "net1": {
@ -465,6 +465,10 @@ func TestParseNetworkConfig(t *testing.T) {
"net2": {}, "net2": {},
"net3": { "net3": {
DriverOpts: map[string]string{"field3": "value3"}, DriverOpts: map[string]string{"field3": "value3"},
IPAMConfig: &networktypes.EndpointIPAMConfig{
IPv4Address: "172.20.88.22",
IPv6Address: "2001:db8::8822",
},
Aliases: []string{"web3"}, Aliases: []string{"web3"},
}, },
}, },
@ -472,13 +476,17 @@ func TestParseNetworkConfig(t *testing.T) {
}, },
{ {
name: "single-network-advanced-with-options", name: "single-network-advanced-with-options",
flags: []string{"--network", "name=net1,alias=web1,alias=web2,driver-opt=field1=value1,driver-opt=field2=value2"}, flags: []string{"--network", "name=net1,alias=web1,alias=web2,driver-opt=field1=value1,driver-opt=field2=value2,ip=172.20.88.22,ip6=2001:db8::8822"},
expected: map[string]*networktypes.EndpointSettings{ expected: map[string]*networktypes.EndpointSettings{
"net1": { "net1": {
DriverOpts: map[string]string{ DriverOpts: map[string]string{
"field1": "value1", "field1": "value1",
"field2": "value2", "field2": "value2",
}, },
IPAMConfig: &networktypes.EndpointIPAMConfig{
IPv4Address: "172.20.88.22",
IPv6Address: "2001:db8::8822",
},
Aliases: []string{"web1", "web2"}, Aliases: []string{"web1", "web2"},
}, },
}, },
@ -496,10 +504,20 @@ func TestParseNetworkConfig(t *testing.T) {
expectedErr: `network "duplicate" is specified multiple times`, expectedErr: `network "duplicate" is specified multiple times`,
}, },
{ {
name: "conflict-options", name: "conflict-options-alias",
flags: []string{"--network", "name=net1,alias=web1", "--network-alias", "web1"}, flags: []string{"--network", "name=net1,alias=web1", "--network-alias", "web1"},
expectedErr: `conflicting options: cannot specify both --network-alias and per-network alias`, expectedErr: `conflicting options: cannot specify both --network-alias and per-network alias`,
}, },
{
name: "conflict-options-ip",
flags: []string{"--network", "name=net1,ip=172.20.88.22,ip6=2001:db8::8822", "--ip", "172.20.88.22"},
expectedErr: `conflicting options: cannot specify both --ip and per-network IPv4 address`,
},
{
name: "conflict-options-ip6",
flags: []string{"--network", "name=net1,ip=172.20.88.22,ip6=2001:db8::8822", "--ip6", "2001:db8::8822"},
expectedErr: `conflicting options: cannot specify both --ip6 and per-network IPv6 address`,
},
{ {
name: "invalid-mixed-network-types", name: "invalid-mixed-network-types",
flags: []string{"--network", "name=host", "--network", "net1"}, flags: []string{"--network", "name=host", "--network", "net1"},

View File

@ -10,6 +10,8 @@ import (
const ( const (
networkOptName = "name" networkOptName = "name"
networkOptAlias = "alias" networkOptAlias = "alias"
networkOptIPv4Address = "ip"
networkOptIPv6Address = "ip6"
driverOpt = "driver-opt" driverOpt = "driver-opt"
) )
@ -19,8 +21,8 @@ type NetworkAttachmentOpts struct {
Aliases []string Aliases []string
DriverOpts map[string]string DriverOpts map[string]string
Links []string // TODO add support for links in the csv notation of `--network` Links []string // TODO add support for links in the csv notation of `--network`
IPv4Address string // TODO add support for IPv4-address in the csv notation of `--network` IPv4Address string
IPv6Address string // TODO add support for IPv6-address in the csv notation of `--network` IPv6Address string
LinkLocalIPs []string // TODO add support for LinkLocalIPs in the csv notation of `--network` ? LinkLocalIPs []string // TODO add support for LinkLocalIPs in the csv notation of `--network` ?
} }
@ -60,6 +62,10 @@ func (n *NetworkOpt) Set(value string) error {
netOpt.Target = value netOpt.Target = value
case networkOptAlias: case networkOptAlias:
netOpt.Aliases = append(netOpt.Aliases, value) netOpt.Aliases = append(netOpt.Aliases, value)
case networkOptIPv4Address:
netOpt.IPv4Address = value
case networkOptIPv6Address:
netOpt.IPv6Address = value
case driverOpt: case driverOpt:
key, value, err = parseDriverOpt(value) key, value, err = parseDriverOpt(value)
if err == nil { if err == nil {

View File

@ -58,6 +58,17 @@ func TestNetworkOptAdvancedSyntax(t *testing.T) {
}, },
}, },
}, },
{
value: "name=docknet1,ip=172.20.88.22,ip6=2001:db8::8822",
expected: []NetworkAttachmentOpts{
{
Target: "docknet1",
Aliases: []string{},
IPv4Address: "172.20.88.22",
IPv6Address: "2001:db8::8822",
},
},
},
{ {
value: "name=docknet1", value: "name=docknet1",
expected: []NetworkAttachmentOpts{ expected: []NetworkAttachmentOpts{