Skip to content

Commit 2b084fd

Browse files
authored
Merge pull request #672 from Junnplus/network-refactor
refactor network generate
2 parents 7617f0d + c621bde commit 2b084fd

File tree

9 files changed

+424
-121
lines changed

9 files changed

+424
-121
lines changed

cmd/nerdctl/network_create.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,15 @@ func networkCreateAction(cmd *cobra.Command, args []string) error {
9898
}
9999

100100
labels := strutil.DedupeStrSlice(labels)
101-
l, err := netutil.GenerateConfigList(e, labels, id, name, subnet)
101+
ipam, err := netutil.GenerateIPAM("", subnet)
102+
if err != nil {
103+
return err
104+
}
105+
cniPlugins, err := netutil.GenerateCNIPlugins("", id, ipam)
106+
if err != nil {
107+
return err
108+
}
109+
l, err := netutil.GenerateConfigList(e, labels, id, name, cniPlugins)
102110
if err != nil {
103111
return err
104112
}

cmd/nerdctl/network_rm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func networkRmAction(cmd *cobra.Command, args []string) error {
8484
}
8585
// Remove the bridge network interface on the host.
8686
if l.Plugins[0].Network.Type == "bridge" {
87-
netIf := fmt.Sprintf("nerdctl%d", *l.NerdctlID)
87+
netIf := netutil.GetBridgeName(*l.NerdctlID)
8888
removeBridgeNetworkInterface(netIf)
8989
}
9090
fmt.Fprintln(cmd.OutOrStdout(), name)

docs/cni.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,53 @@ system, the supported CNI plugin types are `nat` only.
1313
The default network `bridge` for Linux and `nat` for Windows if you
1414
don't set any network options.
1515

16+
Configuration of the default network `bridge` of Linux:
17+
18+
```json
19+
{
20+
"cniVersion": "0.4.0",
21+
"name": "bridge",
22+
"plugins": [
23+
{
24+
"type": "bridge",
25+
"bridge": "nerdctl0",
26+
"isGateway": true,
27+
"ipMasq": true,
28+
"hairpinMode": true,
29+
"ipam": {
30+
"type": "host-local",
31+
"routes": [{ "dst": "0.0.0.0/0" }],
32+
"ranges": [
33+
[
34+
{
35+
"subnet": "10.4.0.1",
36+
"gateway": "10.4.0.0/24"
37+
}
38+
]
39+
]
40+
}
41+
},
42+
{
43+
"type": "portmap",
44+
"capabilities": {
45+
"portMappings": true
46+
}
47+
},
48+
{
49+
"type": "firewall"
50+
},
51+
{
52+
"type": "tuning"
53+
},
54+
{
55+
"type": "isolation"
56+
}
57+
]
58+
}
59+
```
60+
61+
When CNI plugin `isolation` be installed, will inject isolation configuration `{"type":"isolation"}` automatically.
62+
1663
## Custom networks
1764

1865
You can also customize your CNI network by providing configuration files.

pkg/netutil/cni_plugin.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package netutil
18+
19+
type CNIPlugin interface {
20+
GetPluginType() string
21+
}
22+
23+
type IPRange struct {
24+
Subnet string `json:"subnet"`
25+
RangeStart string `json:"rangeStart,omitempty"`
26+
RangeEnd string `json:"rangeEnd,omitempty"`
27+
Gateway string `json:"gateway,omitempty"`
28+
}
29+
30+
type IPAMRoute struct {
31+
Dst string `json:"dst,omitempty"`
32+
GW string `json:"gw,omitempty"`
33+
Gateway string `json:"gateway,omitempty"`
34+
}
35+
36+
type isolationConfig struct {
37+
PluginType string `json:"type"`
38+
}
39+
40+
func newIsolationPlugin() *isolationConfig {
41+
return &isolationConfig{
42+
PluginType: "isolation",
43+
}
44+
}
45+
46+
func (*isolationConfig) GetPluginType() string {
47+
return "isolation"
48+
}

pkg/netutil/cni_plugin_unix.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
//go:build freebsd || linux
2+
// +build freebsd linux
3+
4+
/*
5+
Copyright The containerd Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package netutil
21+
22+
// bridgeConfig describes the bridge plugin
23+
type bridgeConfig struct {
24+
PluginType string `json:"type"`
25+
BrName string `json:"bridge,omitempty"`
26+
IsGW bool `json:"isGateway,omitempty"`
27+
IsDefaultGW bool `json:"isDefaultGateway,omitempty"`
28+
ForceAddress bool `json:"forceAddress,omitempty"`
29+
IPMasq bool `json:"ipMasq,omitempty"`
30+
MTU int `json:"mtu,omitempty"`
31+
HairpinMode bool `json:"hairpinMode,omitempty"`
32+
PromiscMode bool `json:"promiscMode,omitempty"`
33+
Vlan int `json:"vlan,omitempty"`
34+
IPAM map[string]interface{} `json:"ipam"`
35+
}
36+
37+
func newBridgePlugin(bridgeName string) *bridgeConfig {
38+
return &bridgeConfig{
39+
PluginType: "bridge",
40+
BrName: bridgeName,
41+
}
42+
}
43+
44+
func (*bridgeConfig) GetPluginType() string {
45+
return "bridge"
46+
}
47+
48+
// portMapConfig describes the portmapping plugin
49+
type portMapConfig struct {
50+
PluginType string `json:"type"`
51+
Capabilities map[string]bool `json:"capabilities"`
52+
}
53+
54+
func newPortMapPlugin() *portMapConfig {
55+
return &portMapConfig{
56+
PluginType: "portmap",
57+
Capabilities: map[string]bool{
58+
"portMappings": true,
59+
},
60+
}
61+
}
62+
63+
func (*portMapConfig) GetPluginType() string {
64+
return "portmap"
65+
}
66+
67+
// firewallConfig describes the firewall plugin
68+
type firewallConfig struct {
69+
PluginType string `json:"type"`
70+
Backend string `json:"backend,omitempty"`
71+
}
72+
73+
func newFirewallPlugin() *firewallConfig {
74+
return &firewallConfig{
75+
PluginType: "firewall",
76+
}
77+
}
78+
79+
func (*firewallConfig) GetPluginType() string {
80+
return "firewall"
81+
}
82+
83+
// tuningConfig describes the tuning plugin
84+
type tuningConfig struct {
85+
PluginType string `json:"type"`
86+
}
87+
88+
func newTuningPlugin() *tuningConfig {
89+
return &tuningConfig{
90+
PluginType: "tuning",
91+
}
92+
}
93+
94+
func (*tuningConfig) GetPluginType() string {
95+
return "tuning"
96+
}
97+
98+
// https://github.com/containernetworking/plugins/blob/v1.0.1/plugins/ipam/host-local/backend/allocator/config.go#L47-L56
99+
type hostLocalIPAMConfig struct {
100+
Type string `json:"type"`
101+
Routes []IPAMRoute `json:"routes,omitempty"`
102+
ResolveConf string `json:"resolveConf,omitempty"`
103+
DataDir string `json:"dataDir,omitempty"`
104+
Ranges [][]IPRange `json:"ranges,omitempty"`
105+
}
106+
107+
func newHostLocalIPAMConfig() *hostLocalIPAMConfig {
108+
return &hostLocalIPAMConfig{
109+
Type: "host-local",
110+
}
111+
}

pkg/netutil/cni_plugin_windows.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package netutil
18+
19+
type natConfig struct {
20+
PluginType string `json:"type"`
21+
Master string `json:"master,omitempty"`
22+
IPAM map[string]interface{} `json:"ipam"`
23+
}
24+
25+
func (*natConfig) GetPluginType() string {
26+
return "nat"
27+
}
28+
29+
func newNatPlugin(master string) *natConfig {
30+
return &natConfig{
31+
PluginType: "nat",
32+
Master: master,
33+
}
34+
}
35+
36+
// https://github.com/microsoft/windows-container-networking/blob/v0.2.0/cni/cni.go#L55-L63
37+
type windowsIpamConfig struct {
38+
Type string `json:"type"`
39+
Environment string `json:"environment,omitempty"`
40+
AddrSpace string `json:"addressSpace,omitempty"`
41+
Subnet string `json:"subnet,omitempty"`
42+
Address string `json:"ipAddress,omitempty"`
43+
QueryInterval string `json:"queryInterval,omitempty"`
44+
Routes []IPAMRoute `json:"routes,omitempty"`
45+
}
46+
47+
func newWindowsIPAMConfig() *windowsIpamConfig {
48+
return &windowsIpamConfig{}
49+
}

0 commit comments

Comments
 (0)