Skip to content

Commit a8bfa83

Browse files
committed
Fix: setup user chains even if there are running containers
Currently, the DOCKER-USER chains are set up on firewall reload or network creation. If there are running containers at startup, configureNetworking won't be called (daemon/daemon_unix.go), so the user chains won't be setup. This commit puts the setup logic on a separate function, and calls it on the original place and on initNetworkController. Signed-off-by: Andrés Maldonado <[email protected]>
1 parent 3bc752c commit a8bfa83

3 files changed

Lines changed: 45 additions & 5 deletions

File tree

daemon/daemon_unix.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,10 @@ func (daemon *Daemon) initNetworkController(cfg *config.Config, activeSandboxes
851851
return err
852852
}
853853

854+
if err := daemon.netController.SetupUserChains(); err != nil {
855+
log.G(context.TODO()).WithError(err).Warnf("initNetworkController")
856+
}
857+
854858
// Set HostGatewayIP to the default bridge's IP if it is empty
855859
setHostGatewayIP(daemon.netController, cfg)
856860
return nil

integration/daemon/daemon_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ func TestLiveRestore(t *testing.T) {
456456

457457
t.Run("volume references", testLiveRestoreVolumeReferences)
458458
t.Run("autoremove", testLiveRestoreAutoRemove)
459+
t.Run("user chains", testLiveRestoreUserChainsSetup)
459460
}
460461

461462
func testLiveRestoreAutoRemove(t *testing.T) {
@@ -674,6 +675,34 @@ func testLiveRestoreVolumeReferences(t *testing.T) {
674675
})
675676
}
676677

678+
func testLiveRestoreUserChainsSetup(t *testing.T) {
679+
skip.If(t, testEnv.IsRootless(), "rootless daemon uses it's own network namespace")
680+
681+
t.Parallel()
682+
ctx := testutil.StartSpan(baseContext, t)
683+
684+
t.Run("user chains should be inserted", func(t *testing.T) {
685+
d := daemon.New(t)
686+
d.StartWithBusybox(ctx, t, "--live-restore")
687+
t.Cleanup(func() {
688+
d.Stop(t)
689+
d.Cleanup(t)
690+
})
691+
692+
c := d.NewClientT(t)
693+
694+
cID := container.Run(ctx, t, c, container.WithCmd("top"))
695+
defer c.ContainerRemove(ctx, cID, containertypes.RemoveOptions{Force: true})
696+
697+
d.Stop(t)
698+
icmd.RunCommand("iptables", "--flush", "FORWARD").Assert(t, icmd.Success)
699+
d.Start(t, "--live-restore")
700+
701+
result := icmd.RunCommand("iptables", "-S", "FORWARD", "1")
702+
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")
703+
})
704+
}
705+
677706
func TestDaemonDefaultBridgeWithFixedCidrButNoBip(t *testing.T) {
678707
skip.If(t, runtime.GOOS == "windows")
679708

libnetwork/controller.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -706,15 +706,22 @@ addToStore:
706706
c.mu.Unlock()
707707
}
708708

709-
// Sets up the DOCKER-USER chain for each iptables version (IPv4, IPv6)
710-
// that's enabled in the controller's configuration.
709+
if err := c.SetupUserChains(); err != nil {
710+
log.G(context.TODO()).WithError(err).Warnf("Controller.NewNetwork %s:", name)
711+
}
712+
713+
return nw, nil
714+
}
715+
716+
// Sets up the DOCKER-USER chain for each iptables version (IPv4, IPv6) that's
717+
// enabled in the controller's configuration.
718+
func (c *Controller) SetupUserChains() error {
711719
for _, ipVersion := range c.enabledIptablesVersions() {
712720
if err := setupUserChain(ipVersion); err != nil {
713-
log.G(context.TODO()).WithError(err).Warnf("Controller.NewNetwork %s:", name)
721+
return err
714722
}
715723
}
716-
717-
return nw, nil
724+
return nil
718725
}
719726

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

0 commit comments

Comments
 (0)