Skip to content

Commit

Permalink
add macvlan/ipvlan network driver
Browse files Browse the repository at this point in the history
Signed-off-by: ye.sijun <junnplus@gmail.com>
  • Loading branch information
junnplus committed Feb 24, 2022
1 parent b1265f1 commit ceed737
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -961,12 +961,17 @@ Create a network
Usage: `nerdctl network create [OPTIONS] NETWORK`

Flags:
- :whale: `-d, --driver=(bridge|nat)`: Driver to manage the Network
- :whale: `-d, --driver=(bridge|nat|macvlan|ipvlan)`: Driver to manage the Network
- :whale: `--driver=bridge`: Default driver for unix
- :whale: `--driver=macvlan`: Macvlan network driver for unix
- :whale: `--driver=ipvlan`: IPvlan network driver for unix
- :whale: :blue_square: `--driver=nat`: Default driver for windows
- :whale: `-o, --opt`: Set driver specific options
- :whale: `--opt=com.docker.network.driver.mtu=<MTU>`: Set the containers network MTU
- :nerd_face: `--opt=mtu=<MTU>`: Alias of `--opt=com.docker.network.driver.mtu=<MTU>`
- :whale: `--opt=macvlan_mode=(bridge)>`: Set macvlan network mode (default: bridge)
- :whale: `--opt=ipvlan_mode=(l2|l3)`: Set IPvlan network mode (default: l2)
- :nerd_face: `--opt=mode=(bridge|l2|l3)`: Alias of `--opt=macvlan_mode=(bridge)` and `--opt=ipvlan_mode=(l2|l3)`
- :whale: `--subnet`: Subnet in CIDR format that represents a network segment, e.g. "10.5.0.0/16"
- :whale: `--gateway`: Gateway for the master subnet
- :whale: `--ip-range`: Allocate container ip from a sub-range
Expand Down
2 changes: 1 addition & 1 deletion cmd/nerdctl/network_create_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ const (
)

func shellCompleteNetworkDrivers(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
candidates := []string{"bridge"}
candidates := []string{"bridge", "macvlan", "ipvlan"}
return candidates, cobra.ShellCompDirectiveNoFileComp
}
19 changes: 19 additions & 0 deletions pkg/netutil/cni_plugin_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ func (*bridgeConfig) GetPluginType() string {
return "bridge"
}

// vlanConfig describes the macvlan/ipvlan config
type vlanConfig struct {
PluginType string `json:"type"`
Master string `json:"master"`
Mode string `json:"mode,omitempty"`
MTU int `json:"mtu,omitempty"`
IPAM map[string]interface{} `json:"ipam"`
}

func newVLANPlugin(pluginType string) *vlanConfig {
return &vlanConfig{
PluginType: pluginType,
}
}

func (c *vlanConfig) GetPluginType() string {
return c.PluginType
}

// portMapConfig describes the portmapping plugin
type portMapConfig struct {
PluginType string `json:"type"`
Expand Down
40 changes: 39 additions & 1 deletion pkg/netutil/netutil_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ package netutil

import (
"fmt"

"github.com/containerd/nerdctl/pkg/strutil"
)

const (
Expand All @@ -46,7 +48,7 @@ func GenerateCNIPlugins(driver string, id int, ipam map[string]interface{}, opts
return nil, err
}
default:
return nil, fmt.Errorf("unsupported network option %q", opt)
return nil, fmt.Errorf("unsupported %q network option %q", driver, opt)
}
}
bridge := newBridgePlugin(GetBridgeName(id))
Expand All @@ -56,6 +58,42 @@ func GenerateCNIPlugins(driver string, id int, ipam map[string]interface{}, opts
bridge.IPMasq = true
bridge.HairpinMode = true
plugins = []CNIPlugin{bridge, newPortMapPlugin(), newFirewallPlugin(), newTuningPlugin()}
case "macvlan", "ipvlan":
mtu := 0
mode := ""
master := ""
for opt, v := range opts {
switch opt {
case "mtu", "com.docker.network.driver.mtu":
mtu, err = ParseMTU(v)
if err != nil {
return nil, err
}
case "mode", "macvlan_mode", "ipvlan_mode":
if driver == "macvlan" && opt != "ipvlan_mode" {
if !strutil.InStringSlice([]string{"bridge"}, v) {
return nil, fmt.Errorf("unknown macvlan mode %q", v)
}
} else if driver == "ipvlan" && opt != "macvlan_mode" {
if !strutil.InStringSlice([]string{"l2", "l3"}, v) {
return nil, fmt.Errorf("unknown ipvlan mode %q", v)
}
} else {
return nil, fmt.Errorf("unsupported %q network option %q", driver, opt)
}
mode = v
case "parent":
master = v
default:
return nil, fmt.Errorf("unsupported %q network option %q", driver, opt)
}
}
vlan := newVLANPlugin(driver)
vlan.MTU = mtu
vlan.Master = master
vlan.Mode = mode
vlan.IPAM = ipam
plugins = []CNIPlugin{vlan}
default:
return nil, fmt.Errorf("unsupported cni driver %q", driver)
}
Expand Down

0 comments on commit ceed737

Please sign in to comment.