Skip to content

Commit 74355cf

Browse files
authored
Merge pull request containerd#3640 from swagatbora90/add-custom-bridge-ip
feat: adds IP address configuration for default bridge network
2 parents bcc7d42 + 7d116bf commit 74355cf

File tree

13 files changed

+145
-20
lines changed

13 files changed

+145
-20
lines changed

cmd/nerdctl/helpers/flagutil.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ func ProcessRootCmdFlags(cmd *cobra.Command) (types.GlobalCommandOptions, error)
9999
if err != nil {
100100
return types.GlobalCommandOptions{}, err
101101
}
102+
bridgeIP, err := cmd.Flags().GetString("bridge-ip")
103+
if err != nil {
104+
return types.GlobalCommandOptions{}, err
105+
}
102106
return types.GlobalCommandOptions{
103107
Debug: debug,
104108
DebugFull: debugFull,
@@ -113,6 +117,7 @@ func ProcessRootCmdFlags(cmd *cobra.Command) (types.GlobalCommandOptions, error)
113117
HostsDir: hostsDir,
114118
Experimental: experimental,
115119
HostGatewayIP: hostGatewayIP,
120+
BridgeIP: bridgeIP,
116121
}, nil
117122
}
118123

cmd/nerdctl/internal/internal_oci_hook.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@ func internalOCIHookAction(cmd *cobra.Command, args []string) error {
5656
}
5757
cniPath := globalOptions.CNIPath
5858
cniNetconfpath := globalOptions.CNINetConfPath
59+
bridgeIP := globalOptions.BridgeIP
5960
return ocihook.Run(os.Stdin, os.Stderr, event,
6061
dataStore,
6162
cniPath,
6263
cniNetconfpath,
64+
bridgeIP,
6365
)
6466
}

cmd/nerdctl/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ func initRootCmdFlags(rootCmd *cobra.Command, tomlPath string) (*pflag.FlagSet,
183183
// Experimental enable experimental feature, see in https://github.com/containerd/nerdctl/blob/main/docs/experimental.md
184184
helpers.AddPersistentBoolFlag(rootCmd, "experimental", nil, nil, cfg.Experimental, "NERDCTL_EXPERIMENTAL", "Control experimental: https://github.com/containerd/nerdctl/blob/main/docs/experimental.md")
185185
helpers.AddPersistentStringFlag(rootCmd, "host-gateway-ip", nil, nil, nil, aliasToBeInherited, cfg.HostGatewayIP, "NERDCTL_HOST_GATEWAY_IP", "IP address that the special 'host-gateway' string in --add-host resolves to. Defaults to the IP address of the host. It has no effect without setting --add-host")
186+
helpers.AddPersistentStringFlag(rootCmd, "bridge-ip", nil, nil, nil, aliasToBeInherited, cfg.BridgeIP, "NERDCTL_BRIDGE_IP", "IP address for the default nerdctl bridge network")
186187
return aliasToBeInherited, nil
187188
}
188189

docs/config.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ experimental = true
4545
| `hosts_dir` | `--hosts-dir` | | `certs.d` directory | Since 0.16.0 |
4646
| `experimental` | `--experimental` | `NERDCTL_EXPERIMENTAL` | Enable [experimental features](experimental.md) | Since 0.22.3 |
4747
| `host_gateway_ip` | `--host-gateway-ip` | `NERDCTL_HOST_GATEWAY_IP` | IP address that the special 'host-gateway' string in --add-host resolves to. Defaults to the IP address of the host. It has no effect without setting --add-host | Since 1.3.0 |
48+
| `bridge_ip` | `--bridge-ip` | `NERDCTL_BRIDGE_IP` | IP address for the default nerdctl bridge network, e.g., 10.1.100.1/24 | Since 2.1.0 |
4849

4950
The properties are parsed in the following precedence:
5051
1. CLI flag

pkg/cmd/compose/compose.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func New(client *containerd.Client, globalOptions types.GlobalCommandOptions, op
6565
return nil, err
6666
}
6767

68-
cniEnv, err := netutil.NewCNIEnv(globalOptions.CNIPath, globalOptions.CNINetConfPath, netutil.WithNamespace(globalOptions.Namespace), netutil.WithDefaultNetwork())
68+
cniEnv, err := netutil.NewCNIEnv(globalOptions.CNIPath, globalOptions.CNINetConfPath, netutil.WithNamespace(globalOptions.Namespace), netutil.WithDefaultNetwork(globalOptions.BridgeIP))
6969
if err != nil {
7070
return nil, err
7171
}

pkg/cmd/container/kill.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func cleanupNetwork(ctx context.Context, container containerd.Container, globalO
154154
case nettype.Host, nettype.None, nettype.Container, nettype.Namespace:
155155
// NOP
156156
case nettype.CNI:
157-
e, err := netutil.NewCNIEnv(globalOpts.CNIPath, globalOpts.CNINetConfPath, netutil.WithNamespace(globalOpts.Namespace), netutil.WithDefaultNetwork())
157+
e, err := netutil.NewCNIEnv(globalOpts.CNIPath, globalOpts.CNINetConfPath, netutil.WithNamespace(globalOpts.Namespace), netutil.WithDefaultNetwork(globalOpts.BridgeIP))
158158
if err != nil {
159159
return err
160160
}

pkg/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type Config struct {
3939
HostsDir []string `toml:"hosts_dir"`
4040
Experimental bool `toml:"experimental"`
4141
HostGatewayIP string `toml:"host_gateway_ip"`
42+
BridgeIP string `toml:"bridge_ip, omitempty"`
4243
}
4344

4445
// New creates a default Config object statically,

pkg/containerutil/container_network_manager_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type cniNetworkManagerPlatform struct {
4040

4141
// Verifies that the internal network settings are correct.
4242
func (m *cniNetworkManager) VerifyNetworkOptions(_ context.Context) error {
43-
e, err := netutil.NewCNIEnv(m.globalOptions.CNIPath, m.globalOptions.CNINetConfPath, netutil.WithNamespace(m.globalOptions.Namespace), netutil.WithDefaultNetwork())
43+
e, err := netutil.NewCNIEnv(m.globalOptions.CNIPath, m.globalOptions.CNINetConfPath, netutil.WithNamespace(m.globalOptions.Namespace), netutil.WithDefaultNetwork(m.globalOptions.BridgeIP))
4444
if err != nil {
4545
return err
4646
}

pkg/containerutil/container_network_manager_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type cniNetworkManagerPlatform struct {
3636

3737
// Verifies that the internal network settings are correct.
3838
func (m *cniNetworkManager) VerifyNetworkOptions(_ context.Context) error {
39-
e, err := netutil.NewCNIEnv(m.globalOptions.CNIPath, m.globalOptions.CNINetConfPath, netutil.WithNamespace(m.globalOptions.Namespace), netutil.WithDefaultNetwork())
39+
e, err := netutil.NewCNIEnv(m.globalOptions.CNIPath, m.globalOptions.CNINetConfPath, netutil.WithNamespace(m.globalOptions.Namespace), netutil.WithDefaultNetwork(m.globalOptions.BridgeIP))
4040
if err != nil {
4141
return err
4242
}
@@ -67,7 +67,7 @@ func (m *cniNetworkManager) VerifyNetworkOptions(_ context.Context) error {
6767
}
6868

6969
func (m *cniNetworkManager) getCNI() (gocni.CNI, error) {
70-
e, err := netutil.NewCNIEnv(m.globalOptions.CNIPath, m.globalOptions.CNINetConfPath, netutil.WithNamespace(m.globalOptions.Namespace), netutil.WithDefaultNetwork())
70+
e, err := netutil.NewCNIEnv(m.globalOptions.CNIPath, m.globalOptions.CNINetConfPath, netutil.WithNamespace(m.globalOptions.Namespace), netutil.WithDefaultNetwork(m.globalOptions.BridgeIP))
7171
if err != nil {
7272
return nil, fmt.Errorf("failed to instantiate CNI env: %s", err)
7373
}

pkg/netutil/netutil.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ func namespaceUsedNetworks(ctx context.Context, containers []containerd.Containe
180180
return used, nil
181181
}
182182

183-
func WithDefaultNetwork() CNIEnvOpt {
183+
func WithDefaultNetwork(bridgeIP string) CNIEnvOpt {
184184
return func(e *CNIEnv) error {
185-
return e.ensureDefaultNetworkConfig()
185+
return e.ensureDefaultNetworkConfig(bridgeIP)
186186
}
187187
}
188188

@@ -323,7 +323,6 @@ func (e *CNIEnv) CreateNetwork(opts types.NetworkCreateOptions) (*NetworkConfig,
323323
if _, ok := netMap[opts.Name]; ok {
324324
return errdefs.ErrAlreadyExists
325325
}
326-
327326
ipam, err := e.generateIPAM(opts.IPAMDriver, opts.Subnets, opts.Gateway, opts.IPRange, opts.IPAMOptions, opts.IPv6)
328327
if err != nil {
329328
return err
@@ -406,31 +405,44 @@ func (e *CNIEnv) GetDefaultNetworkConfig() (*NetworkConfig, error) {
406405
return nil, nil
407406
}
408407

409-
func (e *CNIEnv) ensureDefaultNetworkConfig() error {
408+
func (e *CNIEnv) ensureDefaultNetworkConfig(bridgeIP string) error {
410409
defaultNet, err := e.GetDefaultNetworkConfig()
411410
if err != nil {
412411
return fmt.Errorf("failed to check for default network: %s", err)
413412
}
414413
if defaultNet == nil {
415-
if err := e.createDefaultNetworkConfig(); err != nil {
414+
if err := e.createDefaultNetworkConfig(bridgeIP); err != nil {
416415
return fmt.Errorf("failed to create default network: %s", err)
417416
}
418417
}
419418
return nil
420419
}
421420

422-
func (e *CNIEnv) createDefaultNetworkConfig() error {
421+
func (e *CNIEnv) createDefaultNetworkConfig(bridgeIP string) error {
423422
filename := e.getConfigPathForNetworkName(DefaultNetworkName)
424423
if _, err := os.Stat(filename); err == nil {
425424
return fmt.Errorf("already found existing network config at %q, cannot create new network named %q", filename, DefaultNetworkName)
426425
}
426+
427+
bridgeCIDR := DefaultCIDR
428+
bridgeGatewayIP := ""
429+
if bridgeIP != "" {
430+
bIP, bCIDR, err := net.ParseCIDR(bridgeIP)
431+
if err != nil {
432+
return fmt.Errorf("invalid bridge ip %s: %s", bridgeIP, err)
433+
}
434+
bridgeGatewayIP = bIP.String()
435+
bridgeCIDR = bCIDR.String()
436+
}
427437
opts := types.NetworkCreateOptions{
428438
Name: DefaultNetworkName,
429439
Driver: DefaultNetworkName,
430-
Subnets: []string{DefaultCIDR},
440+
Subnets: []string{bridgeCIDR},
441+
Gateway: bridgeGatewayIP,
431442
IPAMDriver: "default",
432443
Labels: []string{fmt.Sprintf("%s=true", labels.NerdctlDefaultNetwork)},
433444
}
445+
434446
_, err := e.CreateNetwork(opts)
435447
if err != nil && !errdefs.IsAlreadyExists(err) {
436448
return err

0 commit comments

Comments
 (0)