Skip to content
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

Enable IPv6 on compose networks where enable_ipv6 is true #3686

Open
wants to merge 2 commits 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
72 changes: 72 additions & 0 deletions cmd/nerdctl/compose/compose_up_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,3 +579,75 @@ services:
}
c.Assert(expected)
}

func TestComposeUpWithIPv6(t *testing.T) {
base := testutil.NewBaseWithIPv6Compatible(t)

subnet := "2001:aaa::/64"
var dockerComposeYAML = fmt.Sprintf(`
services:
svc0:
image: %s
networks:
- net0
networks:
net0:
enable_ipv6: true
ipam:
config:
- subnet: %s`, testutil.CommonImage, subnet)

comp := testutil.NewComposeDir(t, dockerComposeYAML)
defer comp.CleanUp()
projectName := comp.ProjectName()
t.Logf("projectName=%q", projectName)
base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d").AssertOK()
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").Run()

inspectCmd := base.Cmd("network", "inspect", projectName+"_net0", "--format", "\"{{range .IPAM.Config}}{{.Subnet}} {{end}}\"")
result := inspectCmd.Run()
stdoutContent := result.Stdout() + result.Stderr()
assert.Assert(inspectCmd.Base.T, result.ExitCode == 0, stdoutContent)

if !strings.Contains(stdoutContent, subnet) {
log.L.Errorf("test failed, the actual subnets are %s", stdoutContent)
t.Fail()
return
}
}

func TestComposeUpWithIPv6Disabled(t *testing.T) {
base := testutil.NewBaseWithIPv6Compatible(t)

subnet := "2001:aab::/64"
var dockerComposeYAML = fmt.Sprintf(`
services:
svc0:
image: %s
networks:
- net0
networks:
net0:
enable_ipv6: false
ipam:
config:
- subnet: %s`, testutil.CommonImage, subnet)

comp := testutil.NewComposeDir(t, dockerComposeYAML)
defer comp.CleanUp()
projectName := comp.ProjectName()
t.Logf("projectName=%q", projectName)
base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d").AssertOK()
defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").Run()

inspectCmd := base.Cmd("network", "inspect", projectName+"_net0", "--format", "\"{{range .IPAM.Config}}{{.Subnet}} {{end}}\"")
result := inspectCmd.Run()
stdoutContent := result.Stdout() + result.Stderr()
assert.Assert(inspectCmd.Base.T, result.ExitCode == 0, stdoutContent)

if strings.Contains(stdoutContent, subnet) {
log.L.Errorf("test failed, the actual subnets are %s", stdoutContent)
t.Fail()
return
}
}
6 changes: 5 additions & 1 deletion pkg/composer/up_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (c *Composer) upNetwork(ctx context.Context, shortName string) error {
return nil
}

if unknown := reflectutil.UnknownNonEmptyFields(&net, "Name", "Ipam", "Driver", "DriverOpts"); len(unknown) > 0 {
if unknown := reflectutil.UnknownNonEmptyFields(&net, "Name", "Ipam", "Driver", "DriverOpts", "EnableIPv6"); len(unknown) > 0 {
log.G(ctx).Warnf("Ignoring: network %s: %+v", shortName, unknown)
}

Expand Down Expand Up @@ -83,6 +83,10 @@ func (c *Composer) upNetwork(ctx context.Context, shortName string) error {
}
}

if *net.EnableIPv6 {
createArgs = append(createArgs, "--ipv6")
}

createArgs = append(createArgs, fullName)

if c.DebugPrintFull {
Expand Down
Loading