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 {
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 {
n.Aliases = make([]string, copts.aliases.Len())
copy(n.Aliases, copts.aliases.GetAll())
@ -743,10 +749,12 @@ func applyContainerOptions(n *opts.NetworkAttachmentOpts, copts *containerOption
n.Links = make([]string, copts.links.Len())
copy(n.Links, copts.links.GetAll())
}
// TODO add IPv4/IPv6 options to the csv notation for --network, and error-out in case of conflicting options
n.IPv4Address = copts.ipv4Address
n.IPv6Address = copts.ipv6Address
if copts.ipv4Address != "" {
n.IPv4Address = copts.ipv4Address
}
if 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?)
if copts.linkLocalIPs.Len() > 0 {

View File

@ -449,7 +449,7 @@ func TestParseNetworkConfig(t *testing.T) {
"--network-alias", "web1",
"--network-alias", "web2",
"--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{
"net1": {
@ -465,20 +465,28 @@ func TestParseNetworkConfig(t *testing.T) {
"net2": {},
"net3": {
DriverOpts: map[string]string{"field3": "value3"},
Aliases: []string{"web3"},
IPAMConfig: &networktypes.EndpointIPAMConfig{
IPv4Address: "172.20.88.22",
IPv6Address: "2001:db8::8822",
},
Aliases: []string{"web3"},
},
},
expectedCfg: container.HostConfig{NetworkMode: "net1"},
},
{
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{
"net1": {
DriverOpts: map[string]string{
"field1": "value1",
"field2": "value2",
},
IPAMConfig: &networktypes.EndpointIPAMConfig{
IPv4Address: "172.20.88.22",
IPv6Address: "2001:db8::8822",
},
Aliases: []string{"web1", "web2"},
},
},
@ -496,10 +504,20 @@ func TestParseNetworkConfig(t *testing.T) {
expectedErr: `network "duplicate" is specified multiple times`,
},
{
name: "conflict-options",
name: "conflict-options-alias",
flags: []string{"--network", "name=net1,alias=web1", "--network-alias", "web1"},
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",
flags: []string{"--network", "name=host", "--network", "net1"},

View File

@ -8,9 +8,11 @@ import (
)
const (
networkOptName = "name"
networkOptAlias = "alias"
driverOpt = "driver-opt"
networkOptName = "name"
networkOptAlias = "alias"
networkOptIPv4Address = "ip"
networkOptIPv6Address = "ip6"
driverOpt = "driver-opt"
)
// NetworkAttachmentOpts represents the network options for endpoint creation
@ -19,8 +21,8 @@ type NetworkAttachmentOpts struct {
Aliases []string
DriverOpts map[string]string
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`
IPv6Address string // TODO add support for IPv6-address in the csv notation of `--network`
IPv4Address string
IPv6Address string
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
case networkOptAlias:
netOpt.Aliases = append(netOpt.Aliases, value)
case networkOptIPv4Address:
netOpt.IPv4Address = value
case networkOptIPv6Address:
netOpt.IPv6Address = value
case driverOpt:
key, value, err = parseDriverOpt(value)
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",
expected: []NetworkAttachmentOpts{