1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #17046 from mavenugo/dopts

driver-opts for network create
This commit is contained in:
Tibor Vass 2015-10-16 15:23:59 -07:00
commit 365a0db0f0
9 changed files with 54 additions and 23 deletions

View file

@ -34,6 +34,7 @@ func (cli *DockerCli) CmdNetwork(args ...string) error {
func (cli *DockerCli) CmdNetworkCreate(args ...string) error { func (cli *DockerCli) CmdNetworkCreate(args ...string) error {
cmd := Cli.Subcmd("network create", []string{"NETWORK-NAME"}, "Creates a new network with a name specified by the user", false) cmd := Cli.Subcmd("network create", []string{"NETWORK-NAME"}, "Creates a new network with a name specified by the user", false)
flDriver := cmd.String([]string{"d", "-driver"}, "bridge", "Driver to manage the Network") flDriver := cmd.String([]string{"d", "-driver"}, "bridge", "Driver to manage the Network")
flOpts := opts.NewMapOpts(nil, nil)
flIpamDriver := cmd.String([]string{"-ipam-driver"}, "default", "IP Address Management Driver") flIpamDriver := cmd.String([]string{"-ipam-driver"}, "default", "IP Address Management Driver")
flIpamSubnet := opts.NewListOpts(nil) flIpamSubnet := opts.NewListOpts(nil)
@ -41,10 +42,11 @@ func (cli *DockerCli) CmdNetworkCreate(args ...string) error {
flIpamGateway := opts.NewListOpts(nil) flIpamGateway := opts.NewListOpts(nil)
flIpamAux := opts.NewMapOpts(nil, nil) flIpamAux := opts.NewMapOpts(nil, nil)
cmd.Var(&flIpamSubnet, []string{"-subnet"}, "Subnet in CIDR format that represents a network segment") cmd.Var(&flIpamSubnet, []string{"-subnet"}, "subnet in CIDR format that represents a network segment")
cmd.Var(&flIpamIPRange, []string{"-ip-range"}, "allocate container ip from a sub-range") cmd.Var(&flIpamIPRange, []string{"-ip-range"}, "allocate container ip from a sub-range")
cmd.Var(&flIpamGateway, []string{"-gateway"}, "ipv4 or ipv6 Gateway for the master subnet") cmd.Var(&flIpamGateway, []string{"-gateway"}, "ipv4 or ipv6 Gateway for the master subnet")
cmd.Var(flIpamAux, []string{"-aux-address"}, "Auxiliary ipv4 or ipv6 addresses used by network driver") cmd.Var(flIpamAux, []string{"-aux-address"}, "auxiliary ipv4 or ipv6 addresses used by Network driver")
cmd.Var(flOpts, []string{"o", "-opt"}, "set driver specific options")
cmd.Require(flag.Exact, 1) cmd.Require(flag.Exact, 1)
err := cmd.ParseFlags(args, true) err := cmd.ParseFlags(args, true)
@ -62,6 +64,7 @@ func (cli *DockerCli) CmdNetworkCreate(args ...string) error {
Name: cmd.Arg(0), Name: cmd.Arg(0),
Driver: *flDriver, Driver: *flDriver,
IPAM: network.IPAM{Driver: *flIpamDriver, Config: ipamCfg}, IPAM: network.IPAM{Driver: *flIpamDriver, Config: ipamCfg},
Options: flOpts.GetAll(),
CheckDuplicate: true, CheckDuplicate: true,
} }
obj, _, err := readBody(cli.call("POST", "/networks/create", nc, nil)) obj, _, err := readBody(cli.call("POST", "/networks/create", nc, nil))

View file

@ -96,7 +96,7 @@ func (n *networkRouter) postNetworkCreate(ctx context.Context, w http.ResponseWr
warning = fmt.Sprintf("Network with name %s (id : %s) already exists", nw.Name(), nw.ID()) warning = fmt.Sprintf("Network with name %s (id : %s) already exists", nw.Name(), nw.ID())
} }
nw, err = n.daemon.CreateNetwork(create.Name, create.Driver, create.IPAM) nw, err = n.daemon.CreateNetwork(create.Name, create.Driver, create.IPAM, create.Options)
if err != nil { if err != nil {
return err return err
} }
@ -174,6 +174,7 @@ func buildNetworkResource(nw libnetwork.Network) *types.NetworkResource {
r.ID = nw.ID() r.ID = nw.ID()
r.Scope = nw.Info().Scope() r.Scope = nw.Info().Scope()
r.Driver = nw.Type() r.Driver = nw.Type()
r.Options = nw.Info().DriverOptions()
r.Containers = make(map[string]types.EndpointResource) r.Containers = make(map[string]types.EndpointResource)
buildIpamResources(r, nw) buildIpamResources(r, nw)

View file

@ -319,6 +319,7 @@ type NetworkResource struct {
Driver string `json:"driver"` Driver string `json:"driver"`
IPAM network.IPAM `json:"ipam"` IPAM network.IPAM `json:"ipam"`
Containers map[string]EndpointResource `json:"containers"` Containers map[string]EndpointResource `json:"containers"`
Options map[string]string `json:"options"`
} }
//EndpointResource contains network resources allocated and usd for a container in a network //EndpointResource contains network resources allocated and usd for a container in a network
@ -331,10 +332,11 @@ type EndpointResource struct {
// NetworkCreate is the expected body of the "create network" http request message // NetworkCreate is the expected body of the "create network" http request message
type NetworkCreate struct { type NetworkCreate struct {
Name string `json:"name"` Name string `json:"name"`
CheckDuplicate bool `json:"check_duplicate"` CheckDuplicate bool `json:"check_duplicate"`
Driver string `json:"driver"` Driver string `json:"driver"`
IPAM network.IPAM `json:"ipam"` IPAM network.IPAM `json:"ipam"`
Options map[string]string `json:"options"`
} }
// NetworkCreateResponse is the response message sent by the server for network create call // NetworkCreateResponse is the response message sent by the server for network create call

View file

@ -80,7 +80,7 @@ func (daemon *Daemon) GetNetworksByID(partialID string) []libnetwork.Network {
} }
// CreateNetwork creates a network with the given name, driver and other optional parameters // CreateNetwork creates a network with the given name, driver and other optional parameters
func (daemon *Daemon) CreateNetwork(name, driver string, ipam network.IPAM) (libnetwork.Network, error) { func (daemon *Daemon) CreateNetwork(name, driver string, ipam network.IPAM, options map[string]string) (libnetwork.Network, error) {
c := daemon.netController c := daemon.netController
if driver == "" { if driver == "" {
driver = c.Config().Daemon.DefaultDriver driver = c.Config().Daemon.DefaultDriver
@ -96,6 +96,7 @@ func (daemon *Daemon) CreateNetwork(name, driver string, ipam network.IPAM) (lib
if len(ipam.Config) > 0 { if len(ipam.Config) > 0 {
nwOptions = append(nwOptions, libnetwork.NetworkOptionIpam(ipam.Driver, "", v4Conf, v6Conf)) nwOptions = append(nwOptions, libnetwork.NetworkOptionIpam(ipam.Driver, "", v4Conf, v6Conf))
} }
nwOptions = append(nwOptions, libnetwork.NetworkOptionDriverOpts(options))
return c.NewNetwork(driver, name, nwOptions...) return c.NewNetwork(driver, name, nwOptions...)
} }

View file

@ -20,7 +20,7 @@ clone git github.com/tchap/go-patricia v2.1.0
clone git golang.org/x/net 3cffabab72adf04f8e3b01c5baf775361837b5fe https://github.com/golang/net.git clone git golang.org/x/net 3cffabab72adf04f8e3b01c5baf775361837b5fe https://github.com/golang/net.git
#get libnetwork packages #get libnetwork packages
clone git github.com/docker/libnetwork dd1c5f0ffe7697f75a82bd8e4bbd6f225ec5e383 clone git github.com/docker/libnetwork 2934f6bf585fa24c86048cc85f7506a5bb626bf5
clone git github.com/armon/go-metrics eb0af217e5e9747e41dd5303755356b62d28e3ec clone git github.com/armon/go-metrics eb0af217e5e9747e41dd5303755356b62d28e3ec
clone git github.com/hashicorp/go-msgpack 71c2886f5a673a35f909803f38ece5810165097b clone git github.com/hashicorp/go-msgpack 71c2886f5a673a35f909803f38ece5810165097b
clone git github.com/hashicorp/memberlist 9a1e242e454d2443df330bdd51a436d5a9058fc4 clone git github.com/hashicorp/memberlist 9a1e242e454d2443df330bdd51a436d5a9058fc4

View file

@ -74,9 +74,10 @@ func (s *DockerSuite) TestApiNetworkInspect(c *check.C) {
Config: []network.IPAMConfig{{Subnet: "172.28.0.0/16", IPRange: "172.28.5.0/24", Gateway: "172.28.5.254"}}, Config: []network.IPAMConfig{{Subnet: "172.28.0.0/16", IPRange: "172.28.5.0/24", Gateway: "172.28.5.254"}},
} }
config := types.NetworkCreate{ config := types.NetworkCreate{
Name: "br0", Name: "br0",
Driver: "bridge", Driver: "bridge",
IPAM: ipam, IPAM: ipam,
Options: map[string]string{"foo": "bar", "opts": "dopts"},
} }
id0 := createNetwork(c, config, true) id0 := createNetwork(c, config, true)
c.Assert(isNetworkAvailable(c, "br0"), checker.Equals, true) c.Assert(isNetworkAvailable(c, "br0"), checker.Equals, true)
@ -86,6 +87,9 @@ func (s *DockerSuite) TestApiNetworkInspect(c *check.C) {
c.Assert(nr.IPAM.Config[0].Subnet, checker.Equals, "172.28.0.0/16") c.Assert(nr.IPAM.Config[0].Subnet, checker.Equals, "172.28.0.0/16")
c.Assert(nr.IPAM.Config[0].IPRange, checker.Equals, "172.28.5.0/24") c.Assert(nr.IPAM.Config[0].IPRange, checker.Equals, "172.28.5.0/24")
c.Assert(nr.IPAM.Config[0].Gateway, checker.Equals, "172.28.5.254") c.Assert(nr.IPAM.Config[0].Gateway, checker.Equals, "172.28.5.254")
c.Assert(nr.Options["foo"], checker.Equals, "bar")
c.Assert(nr.Options["opts"], checker.Equals, "dopts")
// delete the network and make sure it is deleted // delete the network and make sure it is deleted
deleteNetwork(c, id0, true) deleteNetwork(c, id0, true)
c.Assert(isNetworkAvailable(c, "br0"), checker.Equals, false) c.Assert(isNetworkAvailable(c, "br0"), checker.Equals, false)

View file

@ -15,11 +15,15 @@ import (
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/integration/checker" "github.com/docker/docker/pkg/integration/checker"
"github.com/docker/libnetwork/driverapi" "github.com/docker/libnetwork/driverapi"
remoteapi "github.com/docker/libnetwork/drivers/remote/api"
"github.com/docker/libnetwork/netlabel"
"github.com/go-check/check" "github.com/go-check/check"
) )
const dummyNetworkDriver = "dummy-network-driver" const dummyNetworkDriver = "dummy-network-driver"
var remoteDriverNetworkRequest remoteapi.CreateNetworkRequest
func init() { func init() {
check.Suite(&DockerNetworkSuite{ check.Suite(&DockerNetworkSuite{
ds: &DockerSuite{}, ds: &DockerSuite{},
@ -57,6 +61,11 @@ func (s *DockerNetworkSuite) SetUpSuite(c *check.C) {
}) })
mux.HandleFunc(fmt.Sprintf("/%s.CreateNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc(fmt.Sprintf("/%s.CreateNetwork", driverapi.NetworkPluginEndpointType), func(w http.ResponseWriter, r *http.Request) {
err := json.NewDecoder(r.Body).Decode(&remoteDriverNetworkRequest)
if err != nil {
http.Error(w, "Unable to decode JSON payload: "+err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json") w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
fmt.Fprintf(w, "null") fmt.Fprintf(w, "null")
}) })
@ -256,3 +265,16 @@ func (s *DockerNetworkSuite) TestDockerNetworkIpamInvalidCombinations(c *check.C
c.Assert(err, check.NotNil) c.Assert(err, check.NotNil)
dockerCmd(c, "network", "rm", "test0") dockerCmd(c, "network", "rm", "test0")
} }
func (s *DockerNetworkSuite) TestDockerNetworkDriverOptions(c *check.C) {
dockerCmd(c, "network", "create", "-d", dummyNetworkDriver, "-o", "opt1=drv1", "-o", "opt2=drv2", "testopt")
assertNwIsAvailable(c, "testopt")
gopts := remoteDriverNetworkRequest.Options[netlabel.GenericData]
c.Assert(gopts, checker.NotNil)
opts, ok := gopts.(map[string]interface{})
c.Assert(ok, checker.Equals, true)
c.Assert(opts["opt1"], checker.Equals, "drv1")
c.Assert(opts["opt2"], checker.Equals, "drv2")
dockerCmd(c, "network", "rm", "testopt")
}

View file

@ -74,7 +74,6 @@ type NetworkController interface {
Config() config.Config Config() config.Config
// Create a new network. The options parameter carries network specific options. // Create a new network. The options parameter carries network specific options.
// Labels support will be added in the near future.
NewNetwork(networkType, name string, options ...NetworkOption) (Network, error) NewNetwork(networkType, name string, options ...NetworkOption) (Network, error)
// Networks returns the list of Network(s) managed by this controller. // Networks returns the list of Network(s) managed by this controller.

View file

@ -33,7 +33,6 @@ type Network interface {
// Create a new endpoint to this network symbolically identified by the // Create a new endpoint to this network symbolically identified by the
// specified unique name. The options parameter carry driver specific options. // specified unique name. The options parameter carry driver specific options.
// Labels support will be added in the near future.
CreateEndpoint(name string, options ...EndpointOption) (Endpoint, error) CreateEndpoint(name string, options ...EndpointOption) (Endpoint, error)
// Delete the network. // Delete the network.
@ -58,7 +57,7 @@ type Network interface {
// NetworkInfo returns some configuration and operational information about the network // NetworkInfo returns some configuration and operational information about the network
type NetworkInfo interface { type NetworkInfo interface {
IpamConfig() (string, []*IpamConf, []*IpamConf) IpamConfig() (string, []*IpamConf, []*IpamConf)
Labels() map[string]string DriverOptions() map[string]string
Scope() string Scope() string
} }
@ -402,7 +401,7 @@ func (n *network) UnmarshalJSON(b []byte) (err error) {
if v, ok := netMap["generic"]; ok { if v, ok := netMap["generic"]; ok {
n.generic = v.(map[string]interface{}) n.generic = v.(map[string]interface{})
// Restore labels in their map[string]string form // Restore opts in their map[string]string form
if v, ok := n.generic[netlabel.GenericData]; ok { if v, ok := n.generic[netlabel.GenericData]; ok {
var lmap map[string]string var lmap map[string]string
ba, err := json.Marshal(v) ba, err := json.Marshal(v)
@ -484,19 +483,19 @@ func NetworkOptionIpam(ipamDriver string, addrSpace string, ipV4 []*IpamConf, ip
} }
} }
// NetworkOptionLabels function returns an option setter for any parameter described by a map // NetworkOptionDriverOpts function returns an option setter for any parameter described by a map
func NetworkOptionLabels(labels map[string]string) NetworkOption { func NetworkOptionDriverOpts(opts map[string]string) NetworkOption {
return func(n *network) { return func(n *network) {
if n.generic == nil { if n.generic == nil {
n.generic = make(map[string]interface{}) n.generic = make(map[string]interface{})
} }
if labels == nil { if opts == nil {
labels = make(map[string]string) opts = make(map[string]string)
} }
// Store the options // Store the options
n.generic[netlabel.GenericData] = labels n.generic[netlabel.GenericData] = opts
// Decode and store the endpoint options of libnetwork interest // Decode and store the endpoint options of libnetwork interest
if val, ok := labels[netlabel.EnableIPv6]; ok { if val, ok := opts[netlabel.EnableIPv6]; ok {
var err error var err error
if n.enableIPv6, err = strconv.ParseBool(val); err != nil { if n.enableIPv6, err = strconv.ParseBool(val); err != nil {
log.Warnf("Failed to parse %s' value: %s (%s)", netlabel.EnableIPv6, val, err.Error()) log.Warnf("Failed to parse %s' value: %s (%s)", netlabel.EnableIPv6, val, err.Error())
@ -1056,7 +1055,7 @@ func (n *network) Info() NetworkInfo {
return n return n
} }
func (n *network) Labels() map[string]string { func (n *network) DriverOptions() map[string]string {
n.Lock() n.Lock()
defer n.Unlock() defer n.Unlock()
if n.generic != nil { if n.generic != nil {