Browse code

libnet: New: plumb context

Signed-off-by: Albin Kerouanton <albinker@gmail.com>

Albin Kerouanton authored on 2025/04/08 16:25:37
Showing 16 changed files
... ...
@@ -849,14 +849,15 @@ func (daemon *Daemon) initNetworkController(cfg *config.Config, activeSandboxes
849 849
 		return err
850 850
 	}
851 851
 
852
-	daemon.netController, err = libnetwork.New(netOptions...)
852
+	ctx := context.TODO()
853
+	daemon.netController, err = libnetwork.New(ctx, netOptions...)
853 854
 	if err != nil {
854 855
 		return fmt.Errorf("error obtaining controller instance: %v", err)
855 856
 	}
856 857
 
857 858
 	if len(activeSandboxes) > 0 {
858
-		log.G(context.TODO()).Info("there are running containers, updated network configuration will not take affect")
859
-	} else if err := configureNetworking(daemon.netController, cfg); err != nil {
859
+		log.G(ctx).Info("there are running containers, updated network configuration will not take affect")
860
+	} else if err := configureNetworking(ctx, daemon.netController, cfg); err != nil {
860 861
 		return err
861 862
 	}
862 863
 
... ...
@@ -865,17 +866,17 @@ func (daemon *Daemon) initNetworkController(cfg *config.Config, activeSandboxes
865 865
 	return nil
866 866
 }
867 867
 
868
-func configureNetworking(controller *libnetwork.Controller, conf *config.Config) error {
868
+func configureNetworking(ctx context.Context, controller *libnetwork.Controller, conf *config.Config) error {
869 869
 	// Create predefined network "none"
870 870
 	if n, _ := controller.NetworkByName(network.NetworkNone); n == nil {
871
-		if _, err := controller.NewNetwork(context.TODO(), "null", network.NetworkNone, "", libnetwork.NetworkOptionPersist(true)); err != nil {
871
+		if _, err := controller.NewNetwork(ctx, "null", network.NetworkNone, "", libnetwork.NetworkOptionPersist(true)); err != nil {
872 872
 			return errors.Wrapf(err, `error creating default %q network`, network.NetworkNone)
873 873
 		}
874 874
 	}
875 875
 
876 876
 	// Create predefined network "host"
877 877
 	if n, _ := controller.NetworkByName(network.NetworkHost); n == nil {
878
-		if _, err := controller.NewNetwork(context.TODO(), "host", network.NetworkHost, "", libnetwork.NetworkOptionPersist(true)); err != nil {
878
+		if _, err := controller.NewNetwork(ctx, "host", network.NetworkHost, "", libnetwork.NetworkOptionPersist(true)); err != nil {
879 879
 			return errors.Wrapf(err, `error creating default %q network`, network.NetworkHost)
880 880
 		}
881 881
 	}
... ...
@@ -892,7 +893,7 @@ func configureNetworking(controller *libnetwork.Controller, conf *config.Config)
892 892
 
893 893
 	if !conf.DisableBridge {
894 894
 		// Initialize default driver "bridge"
895
-		if err := initBridgeDriver(controller, conf.BridgeConfig); err != nil {
895
+		if err := initBridgeDriver(ctx, controller, conf.BridgeConfig); err != nil {
896 896
 			return err
897 897
 		}
898 898
 	} else {
... ...
@@ -981,7 +982,7 @@ type defBrOpts interface {
981 981
 	defGw() (gw net.IP, optName, auxAddrLabel string)
982 982
 }
983 983
 
984
-func initBridgeDriver(controller *libnetwork.Controller, cfg config.BridgeConfig) error {
984
+func initBridgeDriver(ctx context.Context, controller *libnetwork.Controller, cfg config.BridgeConfig) error {
985 985
 	bridgeName, userManagedBridge := getDefaultBridgeName(cfg)
986 986
 	netOption := map[string]string{
987 987
 		bridge.BridgeName:         bridgeName,
... ...
@@ -1009,7 +1010,7 @@ func initBridgeDriver(controller *libnetwork.Controller, cfg config.BridgeConfig
1009 1009
 	}
1010 1010
 
1011 1011
 	// Initialize default network on "bridge" with the same name
1012
-	_, err = controller.NewNetwork(context.TODO(), "bridge", network.NetworkBridge, "",
1012
+	_, err = controller.NewNetwork(ctx, "bridge", network.NetworkBridge, "",
1013 1013
 		libnetwork.NetworkOptionEnableIPv4(true),
1014 1014
 		libnetwork.NetworkOptionEnableIPv6(cfg.EnableIPv6),
1015 1015
 		libnetwork.NetworkOptionDriverOpts(netOption),
... ...
@@ -237,7 +237,7 @@ func (daemon *Daemon) initNetworkController(daemonCfg *config.Config, activeSand
237 237
 	if err != nil {
238 238
 		return err
239 239
 	}
240
-	daemon.netController, err = libnetwork.New(netOptions...)
240
+	daemon.netController, err = libnetwork.New(context.TODO(), netOptions...)
241 241
 	if err != nil {
242 242
 		return errors.Wrap(err, "error obtaining controller instance")
243 243
 	}
... ...
@@ -28,7 +28,7 @@ func setupFakeDaemon(t *testing.T, c *container.Container) *Daemon {
28 28
 	err := os.MkdirAll(rootfs, 0o755)
29 29
 	assert.NilError(t, err)
30 30
 
31
-	netController, err := libnetwork.New(nwconfig.OptionDataDir(t.TempDir()))
31
+	netController, err := libnetwork.New(context.Background(), nwconfig.OptionDataDir(t.TempDir()))
32 32
 	assert.NilError(t, err)
33 33
 
34 34
 	d := &Daemon{
... ...
@@ -1,6 +1,7 @@
1 1
 package daemon // import "github.com/docker/docker/daemon"
2 2
 
3 3
 import (
4
+	"context"
4 5
 	"os"
5 6
 	"testing"
6 7
 
... ...
@@ -310,7 +311,7 @@ func TestDaemonReloadNetworkDiagnosticPort(t *testing.T) {
310 310
 	if err != nil {
311 311
 		t.Fatal(err)
312 312
 	}
313
-	controller, err := libnetwork.New(netOptions...)
313
+	controller, err := libnetwork.New(context.Background(), netOptions...)
314 314
 	if err != nil {
315 315
 		t.Fatal(err)
316 316
 	}
... ...
@@ -54,6 +54,7 @@ import (
54 54
 	"time"
55 55
 
56 56
 	"github.com/containerd/log"
57
+	"github.com/docker/docker/internal/otelutil"
57 58
 	"github.com/docker/docker/libnetwork/cluster"
58 59
 	"github.com/docker/docker/libnetwork/config"
59 60
 	"github.com/docker/docker/libnetwork/datastore"
... ...
@@ -140,7 +141,13 @@ type Controller struct {
140 140
 }
141 141
 
142 142
 // New creates a new instance of network controller.
143
-func New(cfgOptions ...config.Option) (*Controller, error) {
143
+func New(ctx context.Context, cfgOptions ...config.Option) (_ *Controller, retErr error) {
144
+	ctx, span := otel.Tracer("").Start(ctx, "libnetwork.New")
145
+	defer func() {
146
+		otelutil.RecordStatus(span, retErr)
147
+		span.End()
148
+	}()
149
+
144 150
 	cfg := config.New(cfgOptions...)
145 151
 	store, err := datastore.New(cfg.DataDir, cfg.DatastoreBucket)
146 152
 	if err != nil {
... ...
@@ -179,8 +186,8 @@ func New(cfgOptions ...config.Option) (*Controller, error) {
179 179
 
180 180
 	c.WalkNetworks(func(nw *Network) bool {
181 181
 		if n := nw; n.hasSpecialDriver() && !n.ConfigOnly() {
182
-			if err := n.getController().addNetwork(context.TODO(), n); err != nil {
183
-				log.G(context.TODO()).Warnf("Failed to populate network %q with driver %q", nw.Name(), nw.Type())
182
+			if err := n.getController().addNetwork(ctx, n); err != nil {
183
+				log.G(ctx).Warnf("Failed to populate network %q with driver %q", nw.Name(), nw.Type())
184 184
 			}
185 185
 		}
186 186
 		return false
... ...
@@ -192,12 +199,12 @@ func New(cfgOptions ...config.Option) (*Controller, error) {
192 192
 	c.reservePools()
193 193
 
194 194
 	if err := c.sandboxRestore(c.cfg.ActiveSandboxes); err != nil {
195
-		log.G(context.TODO()).WithError(err).Error("error during sandbox cleanup")
195
+		log.G(ctx).WithError(err).Error("error during sandbox cleanup")
196 196
 	}
197 197
 
198 198
 	// Cleanup resources
199 199
 	if err := c.cleanupLocalEndpoints(); err != nil {
200
-		log.G(context.TODO()).WithError(err).Warnf("error during endpoint cleanup")
200
+		log.G(ctx).WithError(err).Warnf("error during endpoint cleanup")
201 201
 	}
202 202
 	c.networkCleanup()
203 203
 
... ...
@@ -13,7 +13,7 @@ import (
13 13
 
14 14
 func TestEndpointStore(t *testing.T) {
15 15
 	configOption := config.OptionDataDir(t.TempDir())
16
-	c, err := New(configOption)
16
+	c, err := New(context.Background(), configOption)
17 17
 	assert.NilError(t, err)
18 18
 	defer c.Stop()
19 19
 
... ...
@@ -1,6 +1,7 @@
1 1
 package libnetwork
2 2
 
3 3
 import (
4
+	"context"
4 5
 	"fmt"
5 6
 	"strings"
6 7
 	"testing"
... ...
@@ -60,6 +61,7 @@ func TestUserChain(t *testing.T) {
60 60
 			defer resetIptables(t)
61 61
 
62 62
 			c, err := New(
63
+				context.Background(),
63 64
 				config.OptionDataDir(t.TempDir()),
64 65
 				config.OptionDriverConfig("bridge", map[string]any{
65 66
 					netlabel.GenericData: options.Generic{
... ...
@@ -322,7 +322,7 @@ func compareNwLists(a, b []*net.IPNet) bool {
322 322
 func TestAuxAddresses(t *testing.T) {
323 323
 	defer netnsutils.SetupTestOSContext(t)()
324 324
 
325
-	c, err := New(config.OptionDataDir(t.TempDir()))
325
+	c, err := New(context.Background(), config.OptionDataDir(t.TempDir()))
326 326
 	if err != nil {
327 327
 		t.Fatal(err)
328 328
 	}
... ...
@@ -398,7 +398,7 @@ func TestUpdateSvcRecord(t *testing.T) {
398 398
 	for _, tc := range tests {
399 399
 		t.Run(tc.name, func(t *testing.T) {
400 400
 			defer netnsutils.SetupTestOSContext(t)()
401
-			ctrlr, err := New(config.OptionDataDir(t.TempDir()))
401
+			ctrlr, err := New(context.Background(), config.OptionDataDir(t.TempDir()))
402 402
 			assert.NilError(t, err)
403 403
 			defer ctrlr.Stop()
404 404
 
... ...
@@ -477,7 +477,7 @@ func TestSRVServiceQuery(t *testing.T) {
477 477
 
478 478
 	defer netnsutils.SetupTestOSContext(t)()
479 479
 
480
-	c, err := New(config.OptionDataDir(t.TempDir()),
480
+	c, err := New(context.Background(), config.OptionDataDir(t.TempDir()),
481 481
 		config.OptionDefaultAddressPoolConfig(ipamutils.GetLocalScopeDefaultNetworks()))
482 482
 	if err != nil {
483 483
 		t.Fatal(err)
... ...
@@ -578,7 +578,7 @@ func TestServiceVIPReuse(t *testing.T) {
578 578
 
579 579
 	defer netnsutils.SetupTestOSContext(t)()
580 580
 
581
-	c, err := New(config.OptionDataDir(t.TempDir()),
581
+	c, err := New(context.Background(), config.OptionDataDir(t.TempDir()),
582 582
 		config.OptionDefaultAddressPoolConfig(ipamutils.GetLocalScopeDefaultNetworks()))
583 583
 	if err != nil {
584 584
 		t.Fatal(err)
... ...
@@ -699,7 +699,7 @@ func TestIpamReleaseOnNetDriverFailures(t *testing.T) {
699 699
 
700 700
 	defer netnsutils.SetupTestOSContext(t)()
701 701
 
702
-	c, err := New(config.OptionDataDir(t.TempDir()))
702
+	c, err := New(context.Background(), config.OptionDataDir(t.TempDir()))
703 703
 	if err != nil {
704 704
 		t.Fatal(err)
705 705
 	}
... ...
@@ -46,6 +46,7 @@ const (
46 46
 func newController(t *testing.T) *libnetwork.Controller {
47 47
 	t.Helper()
48 48
 	c, err := libnetwork.New(
49
+		context.Background(),
49 50
 		config.OptionDataDir(t.TempDir()),
50 51
 		config.OptionDriverConfig(bridgeNetType, map[string]interface{}{
51 52
 			netlabel.GenericData: options.Generic{
... ...
@@ -888,7 +889,7 @@ func TestInvalidRemoteDriver(t *testing.T) {
888 888
 	err = os.WriteFile(filepath.Join(specPath, "invalid-network-driver.spec"), []byte(server.URL), 0o644)
889 889
 	assert.NilError(t, err)
890 890
 
891
-	ctrlr, err := libnetwork.New(config.OptionDataDir(t.TempDir()))
891
+	ctrlr, err := libnetwork.New(context.Background(), config.OptionDataDir(t.TempDir()))
892 892
 	assert.NilError(t, err)
893 893
 	defer ctrlr.Stop()
894 894
 
... ...
@@ -13,7 +13,7 @@ import (
13 13
 
14 14
 func TestNetworkStore(t *testing.T) {
15 15
 	configOption := config.OptionDataDir(t.TempDir())
16
-	c, err := New(configOption)
16
+	c, err := New(context.Background(), configOption)
17 17
 	assert.NilError(t, err)
18 18
 	defer c.Stop()
19 19
 
... ...
@@ -16,7 +16,7 @@ import (
16 16
 // test only works on linux
17 17
 func TestDNSIPQuery(t *testing.T) {
18 18
 	defer netnsutils.SetupTestOSContext(t)()
19
-	c, err := New(config.OptionDataDir(t.TempDir()),
19
+	c, err := New(context.Background(), config.OptionDataDir(t.TempDir()),
20 20
 		config.OptionDefaultAddressPoolConfig(ipamutils.GetLocalScopeDefaultNetworks()))
21 21
 	if err != nil {
22 22
 		t.Fatal(err)
... ...
@@ -114,7 +114,8 @@ func TestDNSProxyServFail(t *testing.T) {
114 114
 	osctx := netnsutils.SetupTestOSContextEx(t)
115 115
 	defer osctx.Cleanup(t)
116 116
 
117
-	c, err := New(config.OptionDataDir(t.TempDir()),
117
+	c, err := New(context.Background(),
118
+		config.OptionDataDir(t.TempDir()),
118 119
 		config.OptionDefaultAddressPoolConfig(ipamutils.GetLocalScopeDefaultNetworks()))
119 120
 	if err != nil {
120 121
 		t.Fatal(err)
... ...
@@ -13,7 +13,7 @@ import (
13 13
 )
14 14
 
15 15
 func TestDNSOptions(t *testing.T) {
16
-	c, err := New(config.OptionDataDir(t.TempDir()))
16
+	c, err := New(context.Background(), config.OptionDataDir(t.TempDir()))
17 17
 	assert.NilError(t, err)
18 18
 
19 19
 	sb, err := c.NewSandbox(context.Background(), "cnt1", nil)
... ...
@@ -22,6 +22,7 @@ import (
22 22
 func getTestEnv(t *testing.T, opts ...[]NetworkOption) (*Controller, []*Network) {
23 23
 	const netType = "bridge"
24 24
 	c, err := New(
25
+		context.Background(),
25 26
 		config.OptionDataDir(t.TempDir()),
26 27
 		config.OptionDriverConfig(netType, map[string]any{
27 28
 			netlabel.GenericData: options.Generic{"EnableIPForwarding": true},
... ...
@@ -15,7 +15,7 @@ import (
15 15
 
16 16
 func TestCleanupServiceDiscovery(t *testing.T) {
17 17
 	defer netnsutils.SetupTestOSContext(t)()
18
-	c, err := New(config.OptionDataDir(t.TempDir()),
18
+	c, err := New(context.Background(), config.OptionDataDir(t.TempDir()),
19 19
 		config.OptionDefaultAddressPoolConfig(ipamutils.GetLocalScopeDefaultNetworks()))
20 20
 	assert.NilError(t, err)
21 21
 	defer c.Stop()
... ...
@@ -17,7 +17,7 @@ func TestBoltdbBackend(t *testing.T) {
17 17
 
18 18
 func TestNoPersist(t *testing.T) {
19 19
 	configOption := config.OptionDataDir(t.TempDir())
20
-	testController, err := New(configOption)
20
+	testController, err := New(context.Background(), configOption)
21 21
 	if err != nil {
22 22
 		t.Fatalf("Error creating new controller: %v", err)
23 23
 	}
... ...
@@ -34,7 +34,7 @@ func TestNoPersist(t *testing.T) {
34 34
 
35 35
 	// Create a new controller using the same database-file. The network
36 36
 	// should not have persisted.
37
-	testController, err = New(configOption)
37
+	testController, err = New(context.Background(), configOption)
38 38
 	if err != nil {
39 39
 		t.Fatalf("Error creating new controller: %v", err)
40 40
 	}
... ...
@@ -18,7 +18,7 @@ func testLocalBackend(t *testing.T, path, bucket string) {
18 18
 		}),
19 19
 	}
20 20
 
21
-	testController, err := New(cfgOptions...)
21
+	testController, err := New(context.Background(), cfgOptions...)
22 22
 	if err != nil {
23 23
 		t.Fatalf("Error new controller: %v", err)
24 24
 	}
... ...
@@ -52,7 +52,7 @@ func testLocalBackend(t *testing.T, path, bucket string) {
52 52
 	testController.Stop()
53 53
 
54 54
 	// test restore of local store
55
-	testController, err = New(cfgOptions...)
55
+	testController, err = New(context.Background(), cfgOptions...)
56 56
 	if err != nil {
57 57
 		t.Fatalf("Error creating controller: %v", err)
58 58
 	}