Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add macvlan/ipvlan network driver #836

Merged
merged 1 commit into from
Feb 24, 2022
Merged
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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -961,12 +961,18 @@ 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: `--opt=parent=<INTERFACE>`: Set valid parent interface on host
- :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