Skip to content

Commit

Permalink
Merge pull request moby#48577 from am97/48560-setup-user-chains
Browse files Browse the repository at this point in the history
Fix: setup user chains during libnetwork controller initialization
  • Loading branch information
akerouanton authored Oct 21, 2024
2 parents e3c4ed1 + a8bfa83 commit 89ff523
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
4 changes: 4 additions & 0 deletions daemon/daemon_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,10 @@ func (daemon *Daemon) initNetworkController(cfg *config.Config, activeSandboxes
return err
}

if err := daemon.netController.SetupUserChains(); err != nil {
log.G(context.TODO()).WithError(err).Warnf("initNetworkController")
}

// Set HostGatewayIP to the default bridge's IP if it is empty
setHostGatewayIP(daemon.netController, cfg)
return nil
Expand Down
29 changes: 29 additions & 0 deletions integration/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ func TestLiveRestore(t *testing.T) {

t.Run("volume references", testLiveRestoreVolumeReferences)
t.Run("autoremove", testLiveRestoreAutoRemove)
t.Run("user chains", testLiveRestoreUserChainsSetup)
}

func testLiveRestoreAutoRemove(t *testing.T) {
Expand Down Expand Up @@ -674,6 +675,34 @@ func testLiveRestoreVolumeReferences(t *testing.T) {
})
}

func testLiveRestoreUserChainsSetup(t *testing.T) {
skip.If(t, testEnv.IsRootless(), "rootless daemon uses it's own network namespace")

t.Parallel()
ctx := testutil.StartSpan(baseContext, t)

t.Run("user chains should be inserted", func(t *testing.T) {
d := daemon.New(t)
d.StartWithBusybox(ctx, t, "--live-restore")
t.Cleanup(func() {
d.Stop(t)
d.Cleanup(t)
})

c := d.NewClientT(t)

cID := container.Run(ctx, t, c, container.WithCmd("top"))
defer c.ContainerRemove(ctx, cID, containertypes.RemoveOptions{Force: true})

d.Stop(t)
icmd.RunCommand("iptables", "--flush", "FORWARD").Assert(t, icmd.Success)
d.Start(t, "--live-restore")

result := icmd.RunCommand("iptables", "-S", "FORWARD", "1")
assert.Check(t, is.Equal(strings.TrimSpace(result.Stdout()), "-A FORWARD -j DOCKER-USER"), "the jump to DOCKER-USER should be the first rule in the FORWARD chain")
})
}

func TestDaemonDefaultBridgeWithFixedCidrButNoBip(t *testing.T) {
skip.If(t, runtime.GOOS == "windows")

Expand Down
17 changes: 12 additions & 5 deletions libnetwork/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,15 +706,22 @@ addToStore:
c.mu.Unlock()
}

// Sets up the DOCKER-USER chain for each iptables version (IPv4, IPv6)
// that's enabled in the controller's configuration.
if err := c.SetupUserChains(); err != nil {
log.G(context.TODO()).WithError(err).Warnf("Controller.NewNetwork %s:", name)
}

return nw, nil
}

// Sets up the DOCKER-USER chain for each iptables version (IPv4, IPv6) that's
// enabled in the controller's configuration.
func (c *Controller) SetupUserChains() error {
for _, ipVersion := range c.enabledIptablesVersions() {
if err := setupUserChain(ipVersion); err != nil {
log.G(context.TODO()).WithError(err).Warnf("Controller.NewNetwork %s:", name)
return err
}
}

return nw, nil
return nil
}

var joinCluster NetworkWalker = func(nw *Network) bool {
Expand Down

0 comments on commit 89ff523

Please sign in to comment.