Skip to content

Commit

Permalink
Support for com.docker.network.bridge.container_interface_prefix label
Browse files Browse the repository at this point in the history
Signed-off-by: Wolfgang Nagele <mail@wnagele.com>
  • Loading branch information
wnagele committed Mar 1, 2017
1 parent bdc9dce commit 40bae11
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
33 changes: 20 additions & 13 deletions drivers/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import (
)

const (
networkType = "bridge"
vethPrefix = "veth"
vethLen = 7
containerVethPrefix = "eth"
maxAllocatePortAttempts = 10
networkType = "bridge"
vethPrefix = "veth"
vethLen = 7
defaultContainerVethPrefix = "eth"
maxAllocatePortAttempts = 10
)

const (
Expand All @@ -55,14 +55,15 @@ type configuration struct {

// networkConfiguration for network specific configuration
type networkConfiguration struct {
ID string
BridgeName string
EnableIPv6 bool
EnableIPMasquerade bool
EnableICC bool
Mtu int
DefaultBindingIP net.IP
DefaultBridge bool
ID string
BridgeName string
EnableIPv6 bool
EnableIPMasquerade bool
EnableICC bool
Mtu int
DefaultBindingIP net.IP
DefaultBridge bool
ContainerIfacePrefix string
// Internal fields set after ipam data parsing
AddressIPv4 *net.IPNet
AddressIPv6 *net.IPNet
Expand Down Expand Up @@ -239,6 +240,8 @@ func (c *networkConfiguration) fromLabels(labels map[string]string) error {
if c.DefaultBindingIP = net.ParseIP(value); c.DefaultBindingIP == nil {
return parseErr(label, value, "nil ip")
}
case netlabel.ContainerIfacePrefix:
c.ContainerIfacePrefix = value
}
}

Expand Down Expand Up @@ -1217,6 +1220,10 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo,
}

iNames := jinfo.InterfaceName()
containerVethPrefix := defaultContainerVethPrefix
if network.config.ContainerIfacePrefix != "" {
containerVethPrefix = network.config.ContainerIfacePrefix
}
err = iNames.SetNames(endpoint.srcName, containerVethPrefix)
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions drivers/bridge/bridge_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func (ncfg *networkConfiguration) MarshalJSON() ([]byte, error) {
nMap["DefaultBindingIP"] = ncfg.DefaultBindingIP.String()
nMap["DefaultGatewayIPv4"] = ncfg.DefaultGatewayIPv4.String()
nMap["DefaultGatewayIPv6"] = ncfg.DefaultGatewayIPv6.String()
nMap["ContainerIfacePrefix"] = ncfg.ContainerIfacePrefix
nMap["BridgeIfaceCreator"] = ncfg.BridgeIfaceCreator

if ncfg.AddressIPv4 != nil {
Expand Down Expand Up @@ -178,6 +179,10 @@ func (ncfg *networkConfiguration) UnmarshalJSON(b []byte) error {
}
}

if v, ok := nMap["ContainerIfacePrefix"]; ok {
ncfg.ContainerIfacePrefix = v.(string)
}

ncfg.DefaultBridge = nMap["DefaultBridge"].(bool)
ncfg.DefaultBindingIP = net.ParseIP(nMap["DefaultBindingIP"].(string))
ncfg.DefaultGatewayIPv4 = net.ParseIP(nMap["DefaultGatewayIPv4"].(string))
Expand Down
3 changes: 3 additions & 0 deletions netlabel/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ const (

// Internal constant represents that the network is internal which disables default gateway service
Internal = Prefix + ".internal"

// ContainerIfacePrefix can be used to override the interface prefix used inside the container
ContainerIfacePrefix = Prefix + ".container_iface_prefix"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions osl/interface_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ func (n *networkNamespace) AddInterface(srcName, dstPrefix string, options ...If
if n.isDefault {
i.dstName = i.srcName
} else {
i.dstName = fmt.Sprintf("%s%d", i.dstName, n.nextIfIndex)
n.nextIfIndex++
i.dstName = fmt.Sprintf("%s%d", dstPrefix, n.nextIfIndex[dstPrefix])
n.nextIfIndex[dstPrefix]++
}

path := n.path
Expand Down
10 changes: 5 additions & 5 deletions osl/namespace_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type networkNamespace struct {
gwv6 net.IP
staticRoutes []*types.StaticRoute
neighbors []*neigh
nextIfIndex int
nextIfIndex map[string]int
isDefault bool
nlHandle *netlink.Handle
loV6Enabled bool
Expand Down Expand Up @@ -203,7 +203,7 @@ func NewSandbox(key string, osCreate, isRestore bool) (Sandbox, error) {
once.Do(createBasePath)
}

n := &networkNamespace{path: key, isDefault: !osCreate}
n := &networkNamespace{path: key, isDefault: !osCreate, nextIfIndex: make(map[string]int)}

sboxNs, err := netns.GetFromPath(n.path)
if err != nil {
Expand Down Expand Up @@ -256,7 +256,7 @@ func GetSandboxForExternalKey(basePath string, key string) (Sandbox, error) {
if err := mountNetworkNamespace(basePath, key); err != nil {
return nil, err
}
n := &networkNamespace{path: key}
n := &networkNamespace{path: key, nextIfIndex: make(map[string]int)}

sboxNs, err := netns.GetFromPath(n.path)
if err != nil {
Expand Down Expand Up @@ -495,8 +495,8 @@ func (n *networkNamespace) Restore(ifsopt map[string][]IfaceOption, routes []*ty
}
index++
n.Lock()
if index > n.nextIfIndex {
n.nextIfIndex = index
if index > n.nextIfIndex[dstPrefix] {
n.nextIfIndex[dstPrefix] = index
}
n.iFaces = append(n.iFaces, i)
n.Unlock()
Expand Down

0 comments on commit 40bae11

Please sign in to comment.