Skip to content

Commit a4b51e2

Browse files
committed
bridge network use free subnet
Signed-off-by: Ye Sijun <junnplus@gmail.com>
1 parent 8e278e2 commit a4b51e2

16 files changed

+533
-247
lines changed

cmd/nerdctl/network_create.go

+28-44
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ package main
1919
import (
2020
"fmt"
2121

22+
"github.com/containerd/containerd/errdefs"
2223
"github.com/containerd/containerd/identifiers"
23-
"github.com/containerd/nerdctl/pkg/lockutil"
2424
"github.com/containerd/nerdctl/pkg/netutil"
2525
"github.com/containerd/nerdctl/pkg/strutil"
2626

@@ -95,52 +95,36 @@ func networkCreateAction(cmd *cobra.Command, args []string) error {
9595
if err != nil {
9696
return err
9797
}
98+
labels = strutil.DedupeStrSlice(labels)
9899

99-
fn := func() error {
100-
e, err := netutil.NewCNIEnv(cniPath, cniNetconfpath)
101-
if err != nil {
102-
return err
103-
}
104-
for _, n := range e.Networks {
105-
if n.Name == name {
106-
return fmt.Errorf("network with name %s already exists", name)
107-
}
108-
// TODO: check CIDR collision
109-
}
110-
id, err := e.AcquireNextID()
111-
if err != nil {
112-
return err
113-
}
114-
115-
if subnetStr == "" {
116-
if gatewayStr != "" || ipRangeStr != "" {
117-
return fmt.Errorf("cannot set gateway or ip-range without subnet, specify --subnet manually")
118-
}
119-
if id > 255 {
120-
return fmt.Errorf("cannot determine subnet for ID %d, specify --subnet manually", id)
121-
}
122-
subnetStr = fmt.Sprintf("10.4.%d.0/24", id)
100+
if subnetStr == "" {
101+
if gatewayStr != "" || ipRangeStr != "" {
102+
return fmt.Errorf("cannot set gateway or ip-range without subnet, specify --subnet manually")
123103
}
104+
}
124105

125-
labels := strutil.DedupeStrSlice(labels)
126-
ipam, err := netutil.GenerateIPAM(ipamDriver, subnetStr, gatewayStr, ipRangeStr, strutil.ConvertKVStringsToMap(ipamOpts))
127-
if err != nil {
128-
return err
129-
}
130-
cniPlugins, err := e.GenerateCNIPlugins(driver, id, name, ipam, strutil.ConvertKVStringsToMap(opts))
131-
if err != nil {
132-
return err
133-
}
134-
net, err := e.GenerateNetworkConfig(labels, id, name, cniPlugins)
135-
if err != nil {
136-
return err
137-
}
138-
if err := e.WriteNetworkConfig(net); err != nil {
139-
return err
106+
e, err := netutil.NewCNIEnv(cniPath, cniNetconfpath)
107+
if err != nil {
108+
return err
109+
}
110+
createOpts := netutil.CreateOptions{
111+
Name: name,
112+
Driver: driver,
113+
Options: strutil.ConvertKVStringsToMap(opts),
114+
IPAMDriver: ipamDriver,
115+
IPAMOptions: strutil.ConvertKVStringsToMap(ipamOpts),
116+
Subnet: subnetStr,
117+
Gateway: gatewayStr,
118+
IPRange: ipRangeStr,
119+
Labels: labels,
120+
}
121+
net, err := e.CreateNetwork(createOpts)
122+
if err != nil {
123+
if errdefs.IsAlreadyExists(err) {
124+
return fmt.Errorf("network with name %s already exists", name)
140125
}
141-
fmt.Fprintf(cmd.OutOrStdout(), "%d\n", id)
142-
return nil
126+
return err
143127
}
144-
145-
return lockutil.WithDirLock(cniNetconfpath, fn)
128+
_, err = fmt.Fprintf(cmd.OutOrStdout(), "%s\n", *net.NerdctlID)
129+
return err
146130
}

cmd/nerdctl/network_create_linux_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ import (
2020
"testing"
2121

2222
"github.com/containerd/nerdctl/pkg/testutil"
23+
"gotest.tools/v3/assert"
2324
)
2425

2526
func TestNetworkCreateWithMTU(t *testing.T) {
27+
t.Parallel()
2628
testNetwork := testutil.Identifier(t)
2729
base := testutil.NewBase(t)
2830

@@ -35,3 +37,22 @@ func TestNetworkCreateWithMTU(t *testing.T) {
3537

3638
base.Cmd("run", "--rm", "--net", testNetwork, testutil.AlpineImage, "ifconfig", "eth0").AssertOutContains("MTU:9216")
3739
}
40+
41+
func TestNetworkCreate(t *testing.T) {
42+
t.Parallel()
43+
base := testutil.NewBase(t)
44+
testNetwork := testutil.Identifier(t)
45+
46+
base.Cmd("network", "create", testNetwork).AssertOK()
47+
defer base.Cmd("network", "rm", testNetwork).Run()
48+
49+
net := base.InspectNetwork(testNetwork)
50+
assert.Equal(t, len(net.IPAM.Config), 1)
51+
52+
base.Cmd("run", "--rm", "--net", testNetwork, testutil.CommonImage, "ip", "route").AssertOutContains(net.IPAM.Config[0].Subnet)
53+
54+
base.Cmd("network", "create", testNetwork+"-1").AssertOK()
55+
defer base.Cmd("network", "rm", testNetwork+"-1").Run()
56+
57+
base.Cmd("run", "--rm", "--net", testNetwork+"-1", testutil.CommonImage, "ip", "route").AssertNoOut(net.IPAM.Config[0].Subnet)
58+
}

cmd/nerdctl/network_ls.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"bytes"
2121
"errors"
2222
"fmt"
23-
"strconv"
2423
"text/tabwriter"
2524
"text/template"
2625

@@ -105,7 +104,10 @@ func networkLsAction(cmd *cobra.Command, args []string) error {
105104
file: n.File,
106105
}
107106
if n.NerdctlID != nil {
108-
p.ID = strconv.Itoa(*n.NerdctlID)
107+
p.ID = *n.NerdctlID
108+
if len(p.ID) > 12 {
109+
p.ID = p.ID[:12]
110+
}
109111
}
110112
if n.NerdctlLabels != nil {
111113
p.Labels = formatLabels(*n.NerdctlLabels)

cmd/nerdctl/network_rm.go

+19-29
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ package main
1818

1919
import (
2020
"fmt"
21-
"os"
2221

23-
"github.com/containerd/nerdctl/pkg/lockutil"
2422
"github.com/containerd/nerdctl/pkg/netutil"
2523

2624
"github.com/spf13/cobra"
@@ -54,36 +52,28 @@ func networkRmAction(cmd *cobra.Command, args []string) error {
5452
if err != nil {
5553
return err
5654
}
57-
fn := func() error {
58-
netMap := e.NetworkMap()
55+
netMap := e.NetworkMap()
5956

60-
for _, name := range args {
61-
if name == "host" || name == "none" {
62-
return fmt.Errorf("pseudo network %q cannot be removed", name)
63-
}
64-
l, ok := netMap[name]
65-
if !ok {
66-
return fmt.Errorf("no such network: %s", name)
67-
}
68-
if l.NerdctlID == nil {
69-
return fmt.Errorf("%s is managed outside nerdctl and cannot be removed", name)
70-
}
71-
if l.File == "" {
72-
return fmt.Errorf("%s is a pre-defined network and cannot be removed", name)
73-
}
74-
if err := os.RemoveAll(l.File); err != nil {
75-
return err
76-
}
77-
// Remove the bridge network interface on the host.
78-
if l.Plugins[0].Network.Type == "bridge" {
79-
netIf := netutil.GetBridgeName(*l.NerdctlID)
80-
removeBridgeNetworkInterface(netIf)
81-
}
82-
fmt.Fprintln(cmd.OutOrStdout(), name)
57+
for _, name := range args {
58+
if name == "host" || name == "none" {
59+
return fmt.Errorf("pseudo network %q cannot be removed", name)
8360
}
84-
return nil
61+
net, ok := netMap[name]
62+
if !ok {
63+
return fmt.Errorf("no such network: %s", name)
64+
}
65+
if net.NerdctlID == nil {
66+
return fmt.Errorf("%s is managed outside nerdctl and cannot be removed", name)
67+
}
68+
if net.File == "" {
69+
return fmt.Errorf("%s is a pre-defined network and cannot be removed", name)
70+
}
71+
if err := e.RemoveNetwork(net); err != nil {
72+
return err
73+
}
74+
fmt.Fprintln(cmd.OutOrStdout(), name)
8575
}
86-
return lockutil.WithDirLock(cniNetconfpath, fn)
76+
return nil
8777
}
8878

8979
func networkRmShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {

cmd/nerdctl/network_rm_freebsd.go

-19
This file was deleted.

cmd/nerdctl/network_rm_linux_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 main
18+
19+
import (
20+
"testing"
21+
22+
"github.com/containerd/nerdctl/pkg/rootlessutil"
23+
"github.com/containerd/nerdctl/pkg/testutil"
24+
"github.com/vishvananda/netlink"
25+
"gotest.tools/v3/assert"
26+
)
27+
28+
func TestNetworkRemove(t *testing.T) {
29+
t.Parallel()
30+
if rootlessutil.IsRootless() {
31+
t.Skip("test skipped for remove rootless network")
32+
}
33+
base := testutil.NewBase(t)
34+
networkName := testutil.Identifier(t)
35+
36+
base.Cmd("network", "create", networkName).AssertOK()
37+
defer base.Cmd("network", "rm", networkName).Run()
38+
39+
networkID := base.InspectNetwork(networkName).ID
40+
41+
tID := testutil.Identifier(t)
42+
base.Cmd("run", "--rm", "--net", networkName, "--name", tID, testutil.CommonImage).AssertOK()
43+
44+
_, err := netlink.LinkByName("br-" + networkID[:12])
45+
assert.NilError(t, err)
46+
47+
base.Cmd("network", "rm", networkName).AssertOK()
48+
49+
_, err = netlink.LinkByName("br-" + networkID[:12])
50+
assert.Error(t, err, "Link not found")
51+
}

cmd/nerdctl/network_rm_windows.go

-19
This file was deleted.

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ require (
124124
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect
125125
github.com/minio/sha256-simd v1.0.0 // indirect
126126
github.com/mitchellh/go-homedir v1.1.0 // indirect
127-
github.com/mitchellh/mapstructure v1.5.0 // indirect
127+
github.com/mitchellh/mapstructure v1.5.0
128128
github.com/moby/locker v1.0.1 // indirect
129129
github.com/moby/sys/mountinfo v0.6.2 // indirect
130130
github.com/moby/sys/signal v0.7.0

pkg/inspecttypes/dockercompat/dockercompat.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ func NetworkFromNative(n *native.Network) (*Network, error) {
463463
}
464464

465465
if n.NerdctlID != nil {
466-
res.ID = strconv.Itoa(*n.NerdctlID)
466+
res.ID = *n.NerdctlID
467467
}
468468

469469
if n.NerdctlLabels != nil {

pkg/inspecttypes/native/network.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import "encoding/json"
2121
// Network corresponds to pkg/netutil.NetworkConfigList
2222
type Network struct {
2323
CNI json.RawMessage `json:"CNI,omitempty"`
24-
NerdctlID *int `json:"NerdctlID"`
24+
NerdctlID *string `json:"NerdctlID"`
2525
NerdctlLabels *map[string]string `json:"NerdctlLabels,omitempty"`
2626
File string `json:"File,omitempty"`
2727
}

0 commit comments

Comments
 (0)