Skip to content

Commit 99869ff

Browse files
committed
network: support --internal flag
Signed-off-by: ChengyuZhu6 <[email protected]>
1 parent 781eeff commit 99869ff

File tree

5 files changed

+30
-12
lines changed

5 files changed

+30
-12
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
}

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"sort"
3030
"strconv"
3131

32-
"github.com/containernetworking/cni/libcni"
32+
libcni "github.com/containernetworking/cni/libcni"
3333

3434
containerd "github.com/containerd/containerd/v2/client"
3535
"github.com/containerd/containerd/v2/pkg/namespaces"
@@ -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 & 6 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
@@ -124,12 +124,21 @@ func (e *CNIEnv) generateCNIPlugins(driver string, name string, ipam map[string]
124124
bridge.MTU = mtu
125125
bridge.IPAM = ipam
126126
bridge.IsGW = true
127-
bridge.IPMasq = iPMasq
127+
// If internal, disable masquerade regardless of options
128+
if internal {
129+
bridge.IPMasq = false
130+
} else {
131+
bridge.IPMasq = iPMasq
132+
}
128133
bridge.HairpinMode = true
129134
if ipv6 {
130135
bridge.Capabilities["ips"] = true
131136
}
132-
plugins = []CNIPlugin{bridge, newPortMapPlugin(), newFirewallPlugin(), newTuningPlugin()}
137+
if internal {
138+
plugins = []CNIPlugin{bridge, newFirewallPlugin(), newTuningPlugin()}
139+
} else {
140+
plugins = []CNIPlugin{bridge, newPortMapPlugin(), newFirewallPlugin(), newTuningPlugin()}
141+
}
133142
if name != DefaultNetworkName {
134143
firewallPath := filepath.Join(e.Path, "firewall")
135144
ok, err := firewallPluginGEQ110(firewallPath)
@@ -186,13 +195,15 @@ func (e *CNIEnv) generateCNIPlugins(driver string, name string, ipam map[string]
186195
return plugins, nil
187196
}
188197

189-
func (e *CNIEnv) generateIPAM(driver string, subnets []string, gatewayStr, ipRangeStr string, opts map[string]string, ipv6 bool) (map[string]interface{}, error) {
198+
func (e *CNIEnv) generateIPAM(driver string, subnets []string, gatewayStr, ipRangeStr string, opts map[string]string, ipv6 bool, internal bool) (map[string]interface{}, error) {
190199
var ipamConfig interface{}
191200
switch driver {
192201
case "default", "host-local":
193202
ipamConf := newHostLocalIPAMConfig()
194-
ipamConf.Routes = []IPAMRoute{
195-
{Dst: "0.0.0.0/0"},
203+
if !internal {
204+
ipamConf.Routes = []IPAMRoute{
205+
{Dst: "0.0.0.0/0"},
206+
}
196207
}
197208
ranges, findIPv4, err := e.parseIPAMRanges(subnets, gatewayStr, ipRangeStr, ipv6)
198209
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:

0 commit comments

Comments
 (0)