Skip to content

Commit 6aa6b72

Browse files
authored
Merge pull request #4454 from ChengyuZhu6/network-internal
network: support --internal flag
2 parents 2b4e092 + ab47199 commit 6aa6b72

File tree

8 files changed

+92
-13
lines changed

8 files changed

+92
-13
lines changed

cmd/nerdctl/network/network_create.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func createCommand() *cobra.Command {
5151
cmd.Flags().String("ip-range", "", `Allocate container ip from a sub-range`)
5252
cmd.Flags().StringArray("label", nil, "Set metadata for a network")
5353
cmd.Flags().Bool("ipv6", false, "Enable IPv6 networking")
54+
cmd.Flags().Bool("internal", false, "Restrict external access to the network")
5455
return cmd
5556
}
5657

@@ -100,6 +101,10 @@ func createAction(cmd *cobra.Command, args []string) error {
100101
if err != nil {
101102
return err
102103
}
104+
internal, err := cmd.Flags().GetBool("internal")
105+
if err != nil {
106+
return err
107+
}
103108

104109
return network.Create(types.NetworkCreateOptions{
105110
GOptions: globalOptions,
@@ -113,5 +118,6 @@ func createAction(cmd *cobra.Command, args []string) error {
113118
IPRange: ipRangeStr,
114119
Labels: labels,
115120
IPv6: ipv6,
121+
Internal: internal,
116122
}, cmd.OutOrStdout())
117123
}

cmd/nerdctl/network/network_create_linux_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package network
1818

1919
import (
20+
"encoding/json"
2021
"fmt"
2122
"net"
2223
"strings"
@@ -107,6 +108,53 @@ func TestNetworkCreate(t *testing.T) {
107108
}
108109
},
109110
},
111+
{
112+
Description: "internal enabled",
113+
Setup: func(data test.Data, helpers test.Helpers) {
114+
helpers.Ensure("network", "create", "--internal", data.Identifier())
115+
netw := nerdtest.InspectNetwork(helpers, data.Identifier())
116+
assert.Equal(t, len(netw.IPAM.Config), 1)
117+
data.Labels().Set("subnet", netw.IPAM.Config[0].Subnet)
118+
},
119+
Cleanup: func(data test.Data, helpers test.Helpers) {
120+
helpers.Anyhow("network", "rm", data.Identifier())
121+
},
122+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
123+
return helpers.Command("run", "--rm", "--net", data.Identifier(), testutil.CommonImage, "ip", "route")
124+
},
125+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
126+
return &test.Expected{
127+
ExitCode: 0,
128+
Output: func(stdout string, t tig.T) {
129+
assert.Assert(t, strings.Contains(stdout, data.Labels().Get("subnet")))
130+
assert.Assert(t, !strings.Contains(stdout, "default "))
131+
if nerdtest.IsDocker() {
132+
return
133+
}
134+
nativeNet := nerdtest.InspectNetworkNative(helpers, data.Identifier())
135+
var cni struct {
136+
Plugins []struct {
137+
Type string `json:"type"`
138+
IsGW bool `json:"isGateway"`
139+
IPMasq bool `json:"ipMasq"`
140+
} `json:"plugins"`
141+
}
142+
_ = json.Unmarshal(nativeNet.CNI, &cni)
143+
// bridge plugin assertions and no portmap
144+
foundBridge := false
145+
for _, p := range cni.Plugins {
146+
assert.Assert(t, p.Type != "portmap")
147+
if p.Type == "bridge" {
148+
foundBridge = true
149+
assert.Assert(t, !p.IsGW)
150+
assert.Assert(t, !p.IPMasq)
151+
}
152+
}
153+
assert.Assert(t, foundBridge)
154+
},
155+
}
156+
},
157+
},
110158
}
111159

112160
testCase.Run(t)

docs/command-reference.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1207,8 +1207,9 @@ Flags:
12071207
- :whale: `--ip-range`: Allocate container ip from a sub-range
12081208
- :whale: `--label`: Set metadata on a network
12091209
- :whale: `--ipv6`: Enable IPv6. Should be used with a valid subnet.
1210+
- :whale: `--internal`: Restrict external access to the network.
12101211

1211-
Unimplemented `docker network create` flags: `--attachable`, `--aux-address`, `--config-from`, `--config-only`, `--ingress`, `--internal`, `--scope`
1212+
Unimplemented `docker network create` flags: `--attachable`, `--aux-address`, `--config-from`, `--config-only`, `--ingress`, `--scope`
12121213

12131214
### :whale: nerdctl network ls
12141215

pkg/api/types/network_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type NetworkCreateOptions struct {
3535
IPRange string
3636
Labels []string
3737
IPv6 bool
38+
Internal bool
3839
}
3940

4041
// NetworkInspectOptions specifies options for `nerdctl network inspect`.

pkg/netutil/netutil.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,11 @@ func (e *CNIEnv) CreateNetwork(opts types.NetworkCreateOptions) (*NetworkConfig,
306306
if _, ok := netMap[opts.Name]; ok {
307307
return nil, errdefs.ErrAlreadyExists
308308
}
309-
ipam, err := e.generateIPAM(opts.IPAMDriver, opts.Subnets, opts.Gateway, opts.IPRange, opts.IPAMOptions, opts.IPv6)
309+
ipam, err := e.generateIPAM(opts.IPAMDriver, opts.Subnets, opts.Gateway, opts.IPRange, opts.IPAMOptions, opts.IPv6, opts.Internal)
310310
if err != nil {
311311
return nil, err
312312
}
313-
plugins, err := e.generateCNIPlugins(opts.Driver, opts.Name, ipam, opts.Options, opts.IPv6)
313+
plugins, err := e.generateCNIPlugins(opts.Driver, opts.Name, ipam, opts.Options, opts.IPv6, opts.Internal)
314314
if err != nil {
315315
return nil, err
316316
}

pkg/netutil/netutil_unix.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (n *NetworkConfig) clean() error {
9090
return nil
9191
}
9292

93-
func (e *CNIEnv) generateCNIPlugins(driver string, name string, ipam map[string]interface{}, opts map[string]string, ipv6 bool) ([]CNIPlugin, error) {
93+
func (e *CNIEnv) generateCNIPlugins(driver string, name string, ipam map[string]interface{}, opts map[string]string, ipv6 bool, internal bool) ([]CNIPlugin, error) {
9494
var (
9595
plugins []CNIPlugin
9696
err error
@@ -123,13 +123,21 @@ func (e *CNIEnv) generateCNIPlugins(driver string, name string, ipam map[string]
123123
}
124124
bridge.MTU = mtu
125125
bridge.IPAM = ipam
126-
bridge.IsGW = true
127-
bridge.IPMasq = iPMasq
126+
bridge.IsGW = !internal
127+
if internal {
128+
bridge.IPMasq = false
129+
} else {
130+
bridge.IPMasq = iPMasq
131+
}
128132
bridge.HairpinMode = true
129133
if ipv6 {
130134
bridge.Capabilities["ips"] = true
131135
}
132-
plugins = []CNIPlugin{bridge, newPortMapPlugin(), newFirewallPlugin(), newTuningPlugin()}
136+
if internal {
137+
plugins = []CNIPlugin{bridge, newFirewallPlugin(), newTuningPlugin()}
138+
} else {
139+
plugins = []CNIPlugin{bridge, newPortMapPlugin(), newFirewallPlugin(), newTuningPlugin()}
140+
}
133141
if name != DefaultNetworkName {
134142
firewallPath := filepath.Join(e.Path, "firewall")
135143
ok, err := firewallPluginGEQ110(firewallPath)
@@ -186,13 +194,15 @@ func (e *CNIEnv) generateCNIPlugins(driver string, name string, ipam map[string]
186194
return plugins, nil
187195
}
188196

189-
func (e *CNIEnv) generateIPAM(driver string, subnets []string, gatewayStr, ipRangeStr string, opts map[string]string, ipv6 bool) (map[string]interface{}, error) {
197+
func (e *CNIEnv) generateIPAM(driver string, subnets []string, gatewayStr, ipRangeStr string, opts map[string]string, ipv6 bool, internal bool) (map[string]interface{}, error) {
190198
var ipamConfig interface{}
191199
switch driver {
192200
case "default", "host-local":
193201
ipamConf := newHostLocalIPAMConfig()
194-
ipamConf.Routes = []IPAMRoute{
195-
{Dst: "0.0.0.0/0"},
202+
if !internal {
203+
ipamConf.Routes = []IPAMRoute{
204+
{Dst: "0.0.0.0/0"},
205+
}
196206
}
197207
ranges, findIPv4, err := e.parseIPAMRanges(subnets, gatewayStr, ipRangeStr, ipv6)
198208
if err != nil {

pkg/netutil/netutil_windows.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const (
3030

3131
// When creating non-default network without passing in `--subnet` option,
3232
// nerdctl assigns subnet address for the creation starting from `StartingCIDR`
33-
// This prevents subnet address overlapping with `DefaultCIDR` used by the default networkß
33+
// This prevents subnet address overlapping with `DefaultCIDR` used by the default network
3434
StartingCIDR = "10.4.1.0/24"
3535
)
3636

@@ -58,7 +58,7 @@ func (n *NetworkConfig) clean() error {
5858
return nil
5959
}
6060

61-
func (e *CNIEnv) generateCNIPlugins(driver string, name string, ipam map[string]interface{}, opts map[string]string, ipv6 bool) ([]CNIPlugin, error) {
61+
func (e *CNIEnv) generateCNIPlugins(driver string, name string, ipam map[string]interface{}, opts map[string]string, ipv6 bool, internal bool) ([]CNIPlugin, error) {
6262
var plugins []CNIPlugin
6363
switch driver {
6464
case "nat":
@@ -71,7 +71,7 @@ func (e *CNIEnv) generateCNIPlugins(driver string, name string, ipam map[string]
7171
return plugins, nil
7272
}
7373

74-
func (e *CNIEnv) generateIPAM(driver string, subnets []string, gatewayStr, ipRangeStr string, opts map[string]string, ipv6 bool) (map[string]interface{}, error) {
74+
func (e *CNIEnv) generateIPAM(driver string, subnets []string, gatewayStr, ipRangeStr string, opts map[string]string, ipv6 bool, internal bool) (map[string]interface{}, error) {
7575
switch driver {
7676
case "default":
7777
default:

pkg/testutil/nerdtest/utilities.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ func InspectNetwork(helpers test.Helpers, name string) dockercompat.Network {
8888
return res
8989
}
9090

91+
func InspectNetworkNative(helpers test.Helpers, name string) native.Network {
92+
helpers.T().Helper()
93+
var res native.Network
94+
cmd := helpers.Command("network", "inspect", "--mode", "native", name)
95+
cmd.Run(&test.Expected{
96+
Output: expect.JSON([]native.Network{}, func(dc []native.Network, t tig.T) {
97+
assert.Equal(t, 1, len(dc), "Unexpectedly got multiple results")
98+
res = dc[0]
99+
}),
100+
})
101+
return res
102+
}
103+
91104
func InspectImage(helpers test.Helpers, name string) dockercompat.Image {
92105
helpers.T().Helper()
93106
var res dockercompat.Image

0 commit comments

Comments
 (0)