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 863910e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
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", "l3s"}, 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 863910e

Please sign in to comment.