Skip to content

feat: add support for com.docker.network.bridge.enable_icc network option #4311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions cmd/nerdctl/network/network_create_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"gotest.tools/v3/assert"

"github.com/containerd/nerdctl/mod/tigron/expect"
"github.com/containerd/nerdctl/mod/tigron/require"
"github.com/containerd/nerdctl/mod/tigron/test"

"github.com/containerd/nerdctl/v2/pkg/testutil"
Expand Down Expand Up @@ -110,3 +111,100 @@ func TestNetworkCreate(t *testing.T) {

testCase.Run(t)
}

func TestNetworkCreateICC(t *testing.T) {
testCase := nerdtest.Setup()

testCase.Require = require.All(
require.Linux,
nerdtest.Rootful,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

)

testCase.SubTests = []*test.Case{
{
Description: "with enable_icc=false",
Require: nerdtest.CNIFirewallVersion("1.7.1"),
NoParallel: true,
Setup: func(data test.Data, helpers test.Helpers) {
// Create a network with ICC disabled
helpers.Ensure("network", "create", data.Identifier(), "--driver", "bridge",
"--opt", "com.docker.network.bridge.enable_icc=false")

// Run a container in that network
data.Labels().Set("container1", helpers.Capture("run", "-d", "--net", data.Identifier(),
"--name", data.Identifier("c1"), testutil.CommonImage, "sleep", "infinity"))

// Wait for container to be running
nerdtest.EnsureContainerStarted(helpers, data.Identifier("c1"))
},
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("container", "rm", "-f", data.Identifier("c1"))
helpers.Anyhow("network", "rm", data.Identifier())
},
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
// DEBUG
iptablesSave := "iptables-save | grep CNI-ISOLATION || true"
helpers.Custom("sh", "-ec", iptablesSave).Run(&test.Expected{})
// Try to ping the other container in the same network
// This should fail when ICC is disabled
return helpers.Command("run", "--rm", "--net", data.Identifier(),
testutil.CommonImage, "ping", "-c", "1", "-W", "1", data.Identifier("c1"))
},
Expected: test.Expects(expect.ExitCodeGenericFail, nil, nil), // Expect ping to fail with exit code 1
},
{
Description: "with enable_icc=true",
Require: nerdtest.CNIFirewallVersion("1.7.1"),
NoParallel: true,
Setup: func(data test.Data, helpers test.Helpers) {
// Create a network with ICC enabled (default)
helpers.Ensure("network", "create", data.Identifier(), "--driver", "bridge",
"--opt", "com.docker.network.bridge.enable_icc=true")

// Run a container in that network
data.Labels().Set("container1", helpers.Capture("run", "-d", "--net", data.Identifier(),
"--name", data.Identifier("c1"), testutil.CommonImage, "sleep", "infinity"))
// Wait for container to be running
nerdtest.EnsureContainerStarted(helpers, data.Identifier("c1"))
},
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("container", "rm", "-f", data.Identifier("c1"))
helpers.Anyhow("network", "rm", data.Identifier())
},
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
// Try to ping the other container in the same network
// This should succeed when ICC is enabled
return helpers.Command("run", "--rm", "--net", data.Identifier(),
testutil.CommonImage, "ping", "-c", "1", "-W", "1", data.Identifier("c1"))
},
Expected: test.Expects(0, nil, nil), // Expect ping to succeed with exit code 0
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a third case where enable_icc is not specified, to verify the default behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will add

{
Description: "with no enable_icc option set",
NoParallel: true,
Setup: func(data test.Data, helpers test.Helpers) {
// Create a network with ICC enabled (default)
helpers.Ensure("network", "create", data.Identifier(), "--driver", "bridge")

// Run a container in that network
data.Labels().Set("container1", helpers.Capture("run", "-d", "--net", data.Identifier(),
"--name", data.Identifier("c1"), testutil.CommonImage, "sleep", "infinity"))
// Wait for container to be running
nerdtest.EnsureContainerStarted(helpers, data.Identifier("c1"))
},
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("container", "rm", "-f", data.Identifier("c1"))
helpers.Anyhow("network", "rm", data.Identifier())
},
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
// Try to ping the other container in the same network
// This should succeed when no ICC is set
return helpers.Command("run", "--rm", "--net", data.Identifier(),
testutil.CommonImage, "ping", "-c", "1", "-W", "1", data.Identifier("c1"))
},
Expected: test.Expects(0, nil, nil), // Expect ping to succeed with exit code 0
},
}

testCase.Run(t)
}
2 changes: 2 additions & 0 deletions docs/command-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,8 @@ Flags:
- :whale: `-o, --opt`: Set driver specific options
- :whale: `--opt=com.docker.network.driver.mtu=<MTU>`: Set the containers network MTU
- :nerd_face: `--opt=mtu=<MTU>`: Alias of `--opt=com.docker.network.driver.mtu=<MTU>`
- :whale: `--opt=com.docker.network.bridge.enable_icc=<true/false>`: Enable or Disable inter-container connectivity
- :nerd_face: `--opt=icc=<true/false>`: Alias of `--opt=com.docker.network.bridge.enable_icc`
- :whale: `--opt=macvlan_mode=(bridge)>`: Set macvlan network mode (default: bridge)
- :whale: `--opt=ipvlan_mode=(l2|l3)`: Set IPvlan network mode (default: l2)
- :nerd_face: `--opt=mode=(bridge|l2|l3)`: Alias of `--opt=macvlan_mode=(bridge)` and `--opt=ipvlan_mode=(l2|l3)`
Expand Down
9 changes: 7 additions & 2 deletions pkg/netutil/cni_plugin_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,18 @@ type firewallConfig struct {

// IngressPolicy is supported since firewall plugin v1.1.0.
// "same-bridge" mode replaces the deprecated "isolation" plugin.
// "isolated" mode has been added since firewall plugin v1.7.1
IngressPolicy string `json:"ingressPolicy,omitempty"`
}

func newFirewallPlugin() *firewallConfig {
func newFirewallPlugin(ingressPolicy string) *firewallConfig {
if ingressPolicy != "same-bridge" && ingressPolicy != "isolated" {
ingressPolicy = "same-bridge" // Default to "same-bridge" if invalid value provided
}

c := &firewallConfig{
PluginType: "firewall",
IngressPolicy: "same-bridge",
IngressPolicy: ingressPolicy,
}
if rootlessutil.IsRootless() {
// https://github.com/containerd/nerdctl/issues/2818
Expand Down
34 changes: 28 additions & 6 deletions pkg/netutil/netutil_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func (e *CNIEnv) generateCNIPlugins(driver string, name string, ipam map[string]
case "bridge":
mtu := 0
iPMasq := true
icc := true
for opt, v := range opts {
switch opt {
case "mtu", "com.docker.network.driver.mtu":
Expand All @@ -111,6 +112,11 @@ func (e *CNIEnv) generateCNIPlugins(driver string, name string, ipam map[string]
if err != nil {
return nil, err
}
case "icc", "com.docker.network.bridge.enable_icc":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs documentation

icc, err = strconv.ParseBool(v)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unsupported %q network option %q", driver, opt)
}
Expand All @@ -129,10 +135,25 @@ func (e *CNIEnv) generateCNIPlugins(driver string, name string, ipam map[string]
if ipv6 {
bridge.Capabilities["ips"] = true
}
plugins = []CNIPlugin{bridge, newPortMapPlugin(), newFirewallPlugin(), newTuningPlugin()}

// Determine the appropriate firewall ingress policy based on icc setting
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only matter for non-default network, right?

Maybe the entire section should be moved inside the if name != DefaultNetworkName { section?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, currently network options are only applied to non default network, which makes sense since the default network is created by nerdctl. That being said, there isn't a hard rule that these network options can never be applied to the default network. Docker actually allows you to configure the default network with these options on daemon startup. I think we can keep this as is, and let the callers decide how they want to invoke this.

ingressPolicy := "same-bridge" // Default policy
firewallPath := filepath.Join(e.Path, "firewall")
if !icc {
// Check if firewall plugin supports the "isolated" policy (v1.7.1+)
ok, err := FirewallPluginGEQVersion(firewallPath, "v1.7.1")
if err != nil {
log.L.WithError(err).Warnf("Failed to detect whether %q is newer than v1.7.1", firewallPath)
} else if ok {
ingressPolicy = "isolated"
} else {
log.L.Warnf("To use 'isolated' ingress policy, CNI plugin \"firewall\" (>= 1.7.1) needs to be installed in CNI_PATH (%q), see https://www.cni.dev/plugins/current/meta/firewall/", e.Path)
}
}

plugins = []CNIPlugin{bridge, newPortMapPlugin(), newFirewallPlugin(ingressPolicy), newTuningPlugin()}
if name != DefaultNetworkName {
firewallPath := filepath.Join(e.Path, "firewall")
ok, err := firewallPluginGEQ110(firewallPath)
ok, err := FirewallPluginGEQVersion(firewallPath, "v1.1.0")
if err != nil {
log.L.WithError(err).Warnf("Failed to detect whether %q is newer than v1.1.0", firewallPath)
}
Expand Down Expand Up @@ -281,7 +302,8 @@ func (e *CNIEnv) parseIPAMRanges(subnets []string, gateway, ipRange string, ipv6
return ranges, findIPv4, nil
}

func firewallPluginGEQ110(firewallPath string) (bool, error) {
// FirewallPluginGEQVersion checks if the firewall plugin is greater than or equal to the specified version
func FirewallPluginGEQVersion(firewallPath string, versionStr string) (bool, error) {
// TODO: guess true by default in 2023
guessed := false

Expand Down Expand Up @@ -310,8 +332,8 @@ func firewallPluginGEQ110(firewallPath string) (bool, error) {
if err != nil {
return guessed, fmt.Errorf("failed to guess the version of %q: %w", firewallPath, err)
}
ver110 := semver.MustParse("v1.1.0")
return ver.GreaterThan(ver110) || ver.Equal(ver110), nil
targetVer := semver.MustParse(versionStr)
return ver.GreaterThan(targetVer) || ver.Equal(targetVer), nil
}

// guessFirewallPluginVersion guess the version of the CNI firewall plugin (not the version of the implemented CNI spec).
Expand Down
5 changes: 5 additions & 0 deletions pkg/netutil/netutil_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package netutil

import (
"encoding/json"
"errors"
"fmt"
"net"

Expand Down Expand Up @@ -95,3 +96,7 @@ func (e *CNIEnv) generateIPAM(driver string, subnets []string, gatewayStr, ipRan
}
return ipam, nil
}

func FirewallPluginGEQVersion(firewallPath string, versionStr string) (bool, error) {
return false, errors.New("unsupported in windows")
}
23 changes: 23 additions & 0 deletions pkg/testutil/nerdtest/requirements.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/Masterminds/semver/v3"
Expand All @@ -33,8 +34,10 @@ import (

"github.com/containerd/nerdctl/v2/pkg/buildkitutil"
"github.com/containerd/nerdctl/v2/pkg/clientutil"
ncdefaults "github.com/containerd/nerdctl/v2/pkg/defaults"
"github.com/containerd/nerdctl/v2/pkg/infoutil"
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
"github.com/containerd/nerdctl/v2/pkg/netutil"
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest/platform"
Expand Down Expand Up @@ -439,3 +442,23 @@ func ContainerdVersion(v string) *test.Requirement {
},
}
}

// CNIFirewallVersion checks if the CNI firewall plugin version is greater than or equal to the specified version
func CNIFirewallVersion(requiredVersion string) *test.Requirement {
return &test.Requirement{
Check: func(data test.Data, helpers test.Helpers) (bool, string) {
cniPath := ncdefaults.CNIPath()
firewallPath := filepath.Join(cniPath, "firewall")
ok, err := netutil.FirewallPluginGEQVersion(firewallPath, requiredVersion)
if err != nil {
return false, fmt.Sprintf("Failed to check CNI firewall version: %v", err)
}

if !ok {
return false, fmt.Sprintf("CNI firewall plugin version is less than required version %s", requiredVersion)
}

return true, fmt.Sprintf("CNI firewall plugin version is greater than or equal to required version %s", requiredVersion)
},
}
}
Loading