Browse code

Merge pull request #50601 from thaJeztah/cleanup_buildSandboxOptions

daemon: make buildSandboxOptions, buildSandboxPlatformOptions more atomic

Sebastiaan van Stijn authored on 2025/08/05 20:49:10
Showing 3 changed files
... ...
@@ -61,10 +61,13 @@ func buildSandboxOptions(cfg *config.Config, ctr *container.Container) ([]libnet
61 61
 		sboxOptions = append(sboxOptions, libnetwork.OptionUseExternalKey())
62 62
 	}
63 63
 
64
-	// Add platform-specific Sandbox options.
65
-	if err := buildSandboxPlatformOptions(ctr, cfg, &sboxOptions); err != nil {
64
+	// Update the container with platform-specific options, and
65
+	// add platform-specific Sandbox options.
66
+	platformOpts, err := buildSandboxPlatformOptions(ctr, cfg)
67
+	if err != nil {
66 68
 		return nil, err
67 69
 	}
70
+	sboxOptions = append(sboxOptions, platformOpts...)
68 71
 
69 72
 	if len(ctr.HostConfig.DNS) > 0 {
70 73
 		dnsAddrs, err := toNetIP(ctr.HostConfig.DNS)
... ...
@@ -504,9 +504,11 @@ func serviceDiscoveryOnDefaultNetwork() bool {
504 504
 	return false
505 505
 }
506 506
 
507
-func buildSandboxPlatformOptions(ctr *container.Container, cfg *config.Config, sboxOptions *[]libnetwork.SandboxOption) error {
508
-	var err error
509
-	var originResolvConfPath string
507
+func buildSandboxPlatformOptions(ctr *container.Container, cfg *config.Config) ([]libnetwork.SandboxOption, error) {
508
+	var (
509
+		sboxOptions          []libnetwork.SandboxOption
510
+		originResolvConfPath string
511
+	)
510 512
 
511 513
 	// Set the correct paths for /etc/hosts and /etc/resolv.conf, based on the
512 514
 	// networking-mode of the container. Note that containers with "container"
... ...
@@ -517,10 +519,7 @@ func buildSandboxPlatformOptions(ctr *container.Container, cfg *config.Config, s
517 517
 		// In host-mode networking, the container does not have its own networking
518 518
 		// namespace, so both `/etc/hosts` and `/etc/resolv.conf` should be the same
519 519
 		// as on the host itself. The container gets a copy of these files.
520
-		*sboxOptions = append(
521
-			*sboxOptions,
522
-			libnetwork.OptionOriginHostsPath("/etc/hosts"),
523
-		)
520
+		sboxOptions = append(sboxOptions, libnetwork.OptionOriginHostsPath("/etc/hosts"))
524 521
 		originResolvConfPath = "/etc/resolv.conf"
525 522
 	case ctr.HostConfig.NetworkMode.IsUserDefined():
526 523
 		// The container uses a user-defined network. We use the embedded DNS
... ...
@@ -550,26 +549,31 @@ func buildSandboxPlatformOptions(ctr *container.Container, cfg *config.Config, s
550 550
 		originResolvConfPath = cfg.GetResolvConf()
551 551
 	}
552 552
 
553
-	// Allow tests to point at their own resolv.conf file.
553
+	// Allow tests to point at their own resolv.conf file. Note that
554
+	// this only overrides the resolvConf path, not "/etc/hosts", which
555
+	// for containers using the "host" network namespace is set above.
554 556
 	if envPath := os.Getenv("DOCKER_TEST_RESOLV_CONF_PATH"); envPath != "" {
555 557
 		log.G(context.TODO()).Infof("Using OriginResolvConfPath from env: %s", envPath)
556 558
 		originResolvConfPath = envPath
557 559
 	}
558
-	*sboxOptions = append(*sboxOptions, libnetwork.OptionOriginResolvConfPath(originResolvConfPath))
560
+	sboxOptions = append(sboxOptions, libnetwork.OptionOriginResolvConfPath(originResolvConfPath))
559 561
 
560
-	ctr.HostsPath, err = ctr.GetRootResourcePath("hosts")
562
+	hostsPath, err := ctr.GetRootResourcePath("hosts")
561 563
 	if err != nil {
562
-		return err
564
+		return nil, err
563 565
 	}
564
-	*sboxOptions = append(*sboxOptions, libnetwork.OptionHostsPath(ctr.HostsPath))
565
-
566
-	ctr.ResolvConfPath, err = ctr.GetRootResourcePath("resolv.conf")
566
+	resolvConfPath, err := ctr.GetRootResourcePath("resolv.conf")
567 567
 	if err != nil {
568
-		return err
568
+		return nil, err
569 569
 	}
570
-	*sboxOptions = append(*sboxOptions, libnetwork.OptionResolvConfPath(ctr.ResolvConfPath))
571 570
 
572
-	return nil
571
+	ctr.HostsPath, ctr.ResolvConfPath = hostsPath, resolvConfPath
572
+	sboxOptions = append(sboxOptions,
573
+		libnetwork.OptionHostsPath(hostsPath),
574
+		libnetwork.OptionResolvConfPath(resolvConfPath),
575
+	)
576
+
577
+	return sboxOptions, nil
573 578
 }
574 579
 
575 580
 func (daemon *Daemon) initializeNetworkingPaths(ctr *container.Container, nc *container.Container) error {
... ...
@@ -170,8 +170,8 @@ func serviceDiscoveryOnDefaultNetwork() bool {
170 170
 	return true
171 171
 }
172 172
 
173
-func buildSandboxPlatformOptions(ctr *container.Container, cfg *config.Config, sboxOptions *[]libnetwork.SandboxOption) error {
174
-	return nil
173
+func buildSandboxPlatformOptions(ctr *container.Container, cfg *config.Config) ([]libnetwork.SandboxOption, error) {
174
+	return nil, nil
175 175
 }
176 176
 
177 177
 func (daemon *Daemon) initializeNetworkingPaths(ctr *container.Container, nc *container.Container) error {