Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/dockerd/config_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func installConfigFlags(conf *config.Config, flags *pflag.FlagSet) {
flags.Int64Var(&conf.CPURealtimeRuntime, "cpu-rt-runtime", 0, "Limit the CPU real-time runtime in microseconds")
flags.StringVar(&conf.SeccompProfile, "seccomp-profile", "", "Path to seccomp profile")
flags.Var(&conf.ShmSize, "default-shm-size", "Default shm size for containers")
flags.Var(opts.NewPoolsOpt(&conf.NetworkConfig.DefaultAddressPools), "default-address-pools", "Set the default address pools for local/global scope networks")

attachExperimentalFlags(conf, flags)
}
17 changes: 17 additions & 0 deletions daemon/config/config_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/docker/docker/opts"
units "github.com/docker/go-units"
"github.com/docker/libnetwork/ipamutils"
)

// Config defines the configuration of a docker daemon.
Expand All @@ -18,6 +19,8 @@ type Config struct {
// These fields are common to all unix platforms.
CommonUnixConfig

NetworkConfig

// Fields below here are platform specific.
CgroupParent string `json:"cgroup-parent,omitempty"`
EnableSelinuxSupport bool `json:"selinux-enabled,omitempty"`
Expand Down Expand Up @@ -51,6 +54,12 @@ type BridgeConfig struct {
FixedCIDRv6 string `json:"fixed-cidr-v6,omitempty"`
}

// NetworkConfig stores the daemon-wide networking configurations
type NetworkConfig struct {
// Default address pools for docker networks
DefaultAddressPools []*ipamutils.PredefinedPools `json:"default-address-pools,omitempty"`
}

// IsSwarmCompatible defines if swarm mode can be enabled in this config
func (conf *Config) IsSwarmCompatible() error {
if conf.ClusterStore != "" || conf.ClusterAdvertise != "" {
Expand All @@ -61,3 +70,11 @@ func (conf *Config) IsSwarmCompatible() error {
}
return nil
}

// ProcessPoolsConfig applies the default address pools configuration, if present
func (conf *Config) ProcessPoolsConfig() error {
if len(conf.NetworkConfig.DefaultAddressPools) == 0 {
return nil
}
return ipamutils.InitAddressPools(conf.NetworkConfig.DefaultAddressPools)
}
4 changes: 4 additions & 0 deletions daemon/daemon_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,10 @@ func configureKernelSecuritySupport(config *config.Config, driverName string) er
}

func (daemon *Daemon) initNetworkController(config *config.Config, activeSandboxes map[string]interface{}) (libnetwork.NetworkController, error) {
if err := config.ProcessPoolsConfig(); err != nil {
return nil, err
}

netOptions, err := daemon.networkOptions(config, daemon.PluginStore, activeSandboxes)
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions docs/reference/commandline/dockerd.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Options:
--config-file string Daemon configuration file (default "/etc/docker/daemon.json")
--containerd string Path to containerd socket
-D, --debug Enable debug mode
--default-address-pools value Set the default address pools for local/global scope networks
--default-gateway value Container default gateway IPv4 address
--default-gateway-v6 value Container default gateway IPv6 address
--default-runtime string Default OCI runtime for containers (default "runc")
Expand Down
30 changes: 30 additions & 0 deletions integration-cli/docker_cli_daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,36 @@ func (s *DockerDaemonSuite) TestDaemonLinksIpTablesRulesWhenLinkAndUnlink(c *che
s.d.Cmd("kill", "parent")
}

func (s *DockerDaemonSuite) TestDaemonNetworkPools(c *check.C) {
testRequires(c, DaemonIsLinux, SameHostDaemon)

// Remove docker0 bridge and the start daemon defining the predefined address pools
defaultNetworkBridge := "docker0"
deleteInterface(c, defaultNetworkBridge)
s.d.Start(c,
"--default-address-pools", "scope=local,base=175.30.0.0/16,size=16",
"--default-address-pools", "scope=local,base=175.33.0.0/16,size=24")

// Verify bridge network's subnet
out, err := s.d.Cmd("network", "inspect", "--format", "'{{.IPAM.Config}}'", "bridge")
c.Assert(err, check.IsNil)
c.Assert(out, checker.Contains, "175.30.0.0/16")

// Create a bridge network and verify its subnet is the second default pool
_, err = s.d.Cmd("network", "create", "nw100")
c.Assert(err, check.IsNil)
out, err = s.d.Cmd("network", "inspect", "--format", "'{{.IPAM.Config}}'", "nw100")
c.Assert(err, check.IsNil)
c.Assert(out, checker.Contains, "175.33.0.0/24")

// Create a bridge network and verify its subnet is the third default pool
_, err = s.d.Cmd("network", "create", "nw101")
c.Assert(err, check.IsNil)
out, err = s.d.Cmd("network", "inspect", "--format", "'{{.IPAM.Config}}'", "nw101")
c.Assert(err, check.IsNil)
c.Assert(out, checker.Contains, "175.33.1.0/24")
}

func (s *DockerDaemonSuite) TestDaemonUlimitDefaults(c *check.C) {
testRequires(c, DaemonIsLinux)

Expand Down
6 changes: 6 additions & 0 deletions man/dockerd.8.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dockerd - Enable daemon mode
[**--config-file**[=*/etc/docker/daemon.json*]]
[**--containerd**[=*SOCKET-PATH*]]
[**-D**|**--debug**]
[**--default-address-pools**[=*DEFAULT-ADDRESS-POOLS*]]
[**--default-gateway**[=*DEFAULT-GATEWAY*]]
[**--default-gateway-v6**[=*DEFAULT-GATEWAY-V6*]]
[**--default-runtime**[=*runc*]]
Expand Down Expand Up @@ -155,6 +156,11 @@ $ sudo dockerd --add-runtime runc=runc --add-runtime custom=/usr/local/bin/my-ru
**-D**, **--debug**=*true*|*false*
Enable debug mode. Default is false.

**--default-address-pools**=""
Default address pools from which IPAM driver selects a subnet for the networks.
Example: scope=[local|global],base=172.30.0.0/16,size=24 will set the default
address pools for the selected scope networks to {172.30.[0-255].0/24}

**--default-gateway**=""
IPv4 address of the container default gateway; this address must be part of
the bridge subnet (which is defined by \-b or \--bip)
Expand Down
84 changes: 84 additions & 0 deletions opts/address_pools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package opts

import (
"encoding/csv"
"fmt"
"strconv"
"strings"

types "github.com/docker/libnetwork/ipamutils"
)

// PoolsOpt is a Value type for parsing the default address pools definitions
type PoolsOpt struct {
values *[]*types.PredefinedPools
}

// NewPoolsOpt creates a new PoolsOpt
func NewPoolsOpt(ref *[]*types.PredefinedPools) *PoolsOpt {
return &PoolsOpt{values: ref}
}

// Set predefined pools
func (p *PoolsOpt) Set(value string) error {
csvReader := csv.NewReader(strings.NewReader(value))
fields, err := csvReader.Read()
if err != nil {
return err
}

poolsDef := types.PredefinedPools{}

for _, field := range fields {
parts := strings.SplitN(field, "=", 2)
if len(parts) != 2 {
return fmt.Errorf("ninvalid field '%s' must be a key=value pair", field)
}

key := strings.ToLower(parts[0])
value := strings.ToLower(parts[1])

switch key {
case "scope":
poolsDef.Scope = value
case "base":
poolsDef.Base = value
case "size":
size, err := strconv.Atoi(value)
if err != nil {
return fmt.Errorf("invalid size value: %q (must be integer): %v", value, err)
}
poolsDef.Size = size
default:
return fmt.Errorf("unexpected key '%s' in '%s'", key, field)
}
}

*p.values = append(*p.values, &poolsDef)

return nil
}

// Type returns the type of this option
func (p *PoolsOpt) Type() string {
return "default-address-pools"
}

// String returns a string repr of this option
func (p *PoolsOpt) String() string {
pools := []string{}
for _, pool := range *p.values {
repr := fmt.Sprintf("%s %s %s", pool.Scope, pool.Base, pool.Size)
pools = append(pools, repr)
}
return strings.Join(pools, ", ")
}

// Value returns the mounts
func (p *PoolsOpt) Value() []*types.PredefinedPools {
var pd []*types.PredefinedPools
for _, p := range *p.values {
pd = append(pd, p)
}
return pd
}
104 changes: 101 additions & 3 deletions vendor/github.com/docker/libnetwork/ipamutils/utils.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.