Browse code

runconfig: remove exported errors

These errors were not used as sentinel error, and used as any other
"invalid parameter" / "invalid argument" error, so remove them, and
just produce errors where used.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2025/07/28 02:47:28
Showing 8 changed files
... ...
@@ -28,7 +28,6 @@ import (
28 28
 	"github.com/docker/docker/daemon/network"
29 29
 	"github.com/docker/docker/daemon/pkg/opts"
30 30
 	"github.com/docker/docker/errdefs"
31
-	"github.com/docker/docker/runconfig"
32 31
 	"github.com/docker/go-connections/nat"
33 32
 	containertypes "github.com/moby/moby/api/types/container"
34 33
 	"github.com/moby/moby/api/types/events"
... ...
@@ -174,7 +173,7 @@ func (daemon *Daemon) updateNetworkSettings(ctr *container.Container, n *libnetw
174 174
 	}
175 175
 
176 176
 	if !ctr.HostConfig.NetworkMode.IsHost() && containertypes.NetworkMode(n.Type()).IsHost() {
177
-		return runconfig.ErrConflictConnectToHostNetwork
177
+		return cerrdefs.ErrInvalidArgument.WithMessage("cannot connect container to host network - container must be created in host network mode")
178 178
 	}
179 179
 
180 180
 	for s, v := range ctr.NetworkSettings.Networks {
... ...
@@ -194,13 +193,11 @@ func (daemon *Daemon) updateNetworkSettings(ctr *container.Container, n *libnetw
194 194
 			// Avoid duplicate config
195 195
 			return nil
196 196
 		}
197
-		if !containertypes.NetworkMode(sn.Type()).IsPrivate() ||
198
-			!containertypes.NetworkMode(n.Type()).IsPrivate() {
199
-			return runconfig.ErrConflictSharedNetwork
197
+		if !containertypes.NetworkMode(sn.Type()).IsPrivate() || !containertypes.NetworkMode(n.Type()).IsPrivate() {
198
+			return cerrdefs.ErrInvalidArgument.WithMessage("container sharing network namespace with another container or host cannot be connected to any other network")
200 199
 		}
201
-		if containertypes.NetworkMode(sn.Name()).IsNone() ||
202
-			containertypes.NetworkMode(n.Name()).IsNone() {
203
-			return runconfig.ErrConflictNoNetwork
200
+		if containertypes.NetworkMode(sn.Name()).IsNone() || containertypes.NetworkMode(n.Name()).IsNone() {
201
+			return cerrdefs.ErrInvalidArgument.WithMessage("container cannot be connected to multiple networks with one of the networks in private (none) mode")
204 202
 		}
205 203
 	}
206 204
 
... ...
@@ -549,10 +546,10 @@ func validateEndpointSettings(nw *libnetwork.Network, nwName string, epConfig *n
549 549
 		hasStaticAddresses := ipamConfig.IPv4Address != "" || ipamConfig.IPv6Address != ""
550 550
 		// On Linux, user specified IP address is accepted only by networks with user specified subnets.
551 551
 		if hasStaticAddresses && !enableIPOnPredefinedNetwork() {
552
-			errs = append(errs, runconfig.ErrUnsupportedNetworkAndIP)
552
+			errs = append(errs, cerrdefs.ErrInvalidArgument.WithMessage("user specified IP address is supported on user defined networks only"))
553 553
 		}
554 554
 		if len(epConfig.Aliases) > 0 && !serviceDiscoveryOnDefaultNetwork() {
555
-			errs = append(errs, runconfig.ErrUnsupportedNetworkAndAlias)
555
+			errs = append(errs, cerrdefs.ErrInvalidArgument.WithMessage("network-scoped alias is supported only for containers in user defined networks"))
556 556
 		}
557 557
 	}
558 558
 
... ...
@@ -671,7 +668,7 @@ func (daemon *Daemon) connectToNetwork(ctx context.Context, cfg *config.Config,
671 671
 	start := time.Now()
672 672
 
673 673
 	if ctr.HostConfig.NetworkMode.IsContainer() {
674
-		return runconfig.ErrConflictSharedNetwork
674
+		return cerrdefs.ErrInvalidArgument.WithMessage("container sharing network namespace with another container or host cannot be connected to any other network")
675 675
 	}
676 676
 	if cfg.DisableBridge && containertypes.NetworkMode(idOrName).IsBridge() {
677 677
 		ctr.Config.NetworkDisabled = true
... ...
@@ -1046,7 +1043,7 @@ func (daemon *Daemon) DisconnectFromNetwork(ctx context.Context, ctr *container.
1046 1046
 		delete(ctr.NetworkSettings.Networks, networkName)
1047 1047
 	} else if err == nil {
1048 1048
 		if ctr.HostConfig.NetworkMode.IsHost() && containertypes.NetworkMode(n.Type()).IsHost() {
1049
-			return runconfig.ErrConflictDisconnectFromHostNetwork
1049
+			return cerrdefs.ErrInvalidArgument.WithMessage("cannot disconnect container from host network - container was created in host network mode")
1050 1050
 		}
1051 1051
 
1052 1052
 		if err := daemon.disconnectFromNetwork(ctx, ctr, n, false); err != nil {
... ...
@@ -23,7 +23,6 @@ import (
23 23
 	"github.com/docker/docker/daemon/internal/otelutil"
24 24
 	"github.com/docker/docker/daemon/server/backend"
25 25
 	"github.com/docker/docker/errdefs"
26
-	"github.com/docker/docker/runconfig"
27 26
 	containertypes "github.com/moby/moby/api/types/container"
28 27
 	"github.com/moby/moby/api/types/events"
29 28
 	networktypes "github.com/moby/moby/api/types/network"
... ...
@@ -77,7 +76,7 @@ func (daemon *Daemon) containerCreate(ctx context.Context, daemonCfg *configStor
77 77
 
78 78
 	start := time.Now()
79 79
 	if opts.params.Config == nil {
80
-		return containertypes.CreateResponse{}, errdefs.InvalidParameter(runconfig.ErrEmptyConfig)
80
+		return containertypes.CreateResponse{}, errdefs.InvalidParameter(errors.New("config cannot be empty in order to create a container"))
81 81
 	}
82 82
 
83 83
 	// Normalize some defaults. Doing this "ad-hoc" here for now, as there's
... ...
@@ -40,7 +40,6 @@ import (
40 40
 	volumemounts "github.com/docker/docker/daemon/volume/mounts"
41 41
 	"github.com/docker/docker/errdefs"
42 42
 	"github.com/docker/docker/pkg/sysinfo"
43
-	"github.com/docker/docker/runconfig"
44 43
 	"github.com/moby/moby/api/types/blkiodev"
45 44
 	containertypes "github.com/moby/moby/api/types/container"
46 45
 	"github.com/moby/moby/api/types/network"
... ...
@@ -1565,7 +1564,7 @@ func (daemon *Daemon) registerLinks(container *container.Container, hostConfig *
1565 1565
 			}
1566 1566
 		}
1567 1567
 		if child.HostConfig.NetworkMode.IsHost() {
1568
-			return runconfig.ErrConflictHostNetworkAndLinks
1568
+			return cerrdefs.ErrInvalidArgument.WithMessage("conflicting options: host type networking can't be used with links. This would result in undefined behavior")
1569 1569
 		}
1570 1570
 		if err := daemon.registerLink(container, child, alias); err != nil {
1571 1571
 			return err
... ...
@@ -19,7 +19,6 @@ import (
19 19
 	"github.com/docker/docker/daemon/server/httputils"
20 20
 	"github.com/docker/docker/errdefs"
21 21
 	"github.com/docker/docker/pkg/ioutils"
22
-	"github.com/docker/docker/runconfig"
23 22
 	"github.com/moby/moby/api/types"
24 23
 	"github.com/moby/moby/api/types/container"
25 24
 	"github.com/moby/moby/api/types/filters"
... ...
@@ -506,7 +505,7 @@ func (c *containerRouter) postContainersCreate(ctx context.Context, w http.Respo
506 506
 	}
507 507
 
508 508
 	if config == nil {
509
-		return errdefs.InvalidParameter(runconfig.ErrEmptyConfig)
509
+		return errdefs.InvalidParameter(errors.New("config cannot be empty in order to create a container"))
510 510
 	}
511 511
 	if hostConfig == nil {
512 512
 		hostConfig = &container.HostConfig{}
... ...
@@ -763,7 +762,7 @@ func handleMACAddressBC(config *container.Config, hostConfig *container.HostConf
763 763
 			return "", nil
764 764
 		}
765 765
 		if !hostConfig.NetworkMode.IsBridge() && !hostConfig.NetworkMode.IsUserDefined() {
766
-			return "", runconfig.ErrConflictContainerNetworkAndMac
766
+			return "", errdefs.InvalidParameter(errors.New("conflicting options: mac-address and the network mode"))
767 767
 		}
768 768
 
769 769
 		epConfig, err := epConfigForNetMode(version, hostConfig.NetworkMode, networkingConfig)
... ...
@@ -81,7 +81,8 @@ func decodeContainerConfig(src io.Reader, si *sysinfo.SysInfo) (*container.Confi
81 81
 func loadJSON(src io.Reader, out interface{}) error {
82 82
 	dec := json.NewDecoder(src)
83 83
 	if err := dec.Decode(&out); err != nil {
84
-		return invalidJSONError{Err: err}
84
+		// invalidJSONError allows unwrapping the error to detect io.EOF etc.
85
+		return invalidJSONError{error: err}
85 86
 	}
86 87
 	if dec.More() {
87 88
 		return validationError("unexpected content after JSON")
... ...
@@ -1,60 +1,19 @@
1 1
 package runconfig
2 2
 
3
-const (
4
-	// ErrConflictContainerNetworkAndLinks conflict between --net=container and links
5
-	ErrConflictContainerNetworkAndLinks validationError = "conflicting options: container type network can't be used with links. This would result in undefined behavior"
6
-	// ErrConflictSharedNetwork conflict between private and other networks
7
-	ErrConflictSharedNetwork validationError = "container sharing network namespace with another container or host cannot be connected to any other network"
8
-	// ErrConflictConnectToHostNetwork error when attempting to connect a container to host network when not in host network mode
9
-	ErrConflictConnectToHostNetwork validationError = "cannot connect container to host network - container must be created in host network mode"
10
-	// ErrConflictDisconnectFromHostNetwork error when attempting to disconnect a container from host network when in host network mode
11
-	ErrConflictDisconnectFromHostNetwork validationError = "cannot disconnect container from host network - container was created in host network mode"
12
-	// ErrConflictNoNetwork conflict between private and other networks
13
-	ErrConflictNoNetwork validationError = "container cannot be connected to multiple networks with one of the networks in private (none) mode"
14
-	// ErrConflictNetworkAndDNS conflict between --dns and the network mode
15
-	ErrConflictNetworkAndDNS validationError = "conflicting options: dns and the network mode"
16
-	// ErrConflictNetworkHostname conflict between the hostname and the network mode
17
-	ErrConflictNetworkHostname validationError = "conflicting options: hostname and the network mode"
18
-	// ErrConflictHostNetworkAndLinks conflict between --net=host and links
19
-	ErrConflictHostNetworkAndLinks validationError = "conflicting options: host type networking can't be used with links. This would result in undefined behavior"
20
-	// ErrConflictContainerNetworkAndMac conflict between the mac address and the network mode
21
-	ErrConflictContainerNetworkAndMac validationError = "conflicting options: mac-address and the network mode"
22
-	// ErrConflictNetworkHosts conflict between add-host and the network mode
23
-	ErrConflictNetworkHosts validationError = "conflicting options: custom host-to-IP mapping and the network mode"
24
-	// ErrConflictNetworkPublishPorts conflict between the publish options and the network mode
25
-	ErrConflictNetworkPublishPorts validationError = "conflicting options: port publishing and the container type network mode"
26
-	// ErrConflictNetworkExposePorts conflict between the expose option and the network mode
27
-	ErrConflictNetworkExposePorts validationError = "conflicting options: port exposing and the container type network mode"
28
-	// ErrUnsupportedNetworkAndIP conflict between network mode and requested ip address
29
-	ErrUnsupportedNetworkAndIP validationError = "user specified IP address is supported on user defined networks only"
30
-	// ErrUnsupportedNetworkNoSubnetAndIP conflict between network with no configured subnet and requested ip address
31
-	ErrUnsupportedNetworkNoSubnetAndIP validationError = "user specified IP address is supported only when connecting to networks with user configured subnets"
32
-	// ErrUnsupportedNetworkAndAlias conflict between network mode and alias
33
-	ErrUnsupportedNetworkAndAlias validationError = "network-scoped alias is supported only for containers in user defined networks"
34
-	// ErrConflictUTSHostname conflict between the hostname and the UTS mode
35
-	ErrConflictUTSHostname validationError = "conflicting options: hostname and the UTS mode"
36
-	// ErrEmptyConfig when container config is nil
37
-	ErrEmptyConfig validationError = "config cannot be empty in order to create a container"
38
-)
3
+import cerrdefs "github.com/containerd/errdefs"
39 4
 
40
-type validationError string
41
-
42
-func (e validationError) Error() string {
43
-	return string(e)
5
+func validationError(msg string) error {
6
+	return cerrdefs.ErrInvalidArgument.WithMessage(msg)
44 7
 }
45 8
 
46
-func (e validationError) InvalidParameter() {}
47
-
48
-type invalidJSONError struct {
49
-	Err error
50
-}
9
+type invalidJSONError struct{ error }
51 10
 
52 11
 func (e invalidJSONError) Error() string {
53
-	return "invalid JSON: " + e.Err.Error()
12
+	return "invalid JSON: " + e.error.Error()
54 13
 }
55 14
 
56 15
 func (e invalidJSONError) Unwrap() error {
57
-	return e.Err
16
+	return e.error
58 17
 }
59 18
 
60 19
 func (e invalidJSONError) InvalidParameter() {}
... ...
@@ -1,8 +1,6 @@
1 1
 package runconfig
2 2
 
3
-import (
4
-	"github.com/moby/moby/api/types/container"
5
-)
3
+import "github.com/moby/moby/api/types/container"
6 4
 
7 5
 // validateNetContainerMode ensures that the various combinations of requested
8 6
 // network settings wrt container mode are valid.
... ...
@@ -17,27 +15,27 @@ func validateNetContainerMode(c *container.Config, hc *container.HostConfig) err
17 17
 	}
18 18
 
19 19
 	if c.Hostname != "" {
20
-		return ErrConflictNetworkHostname
20
+		return validationError("conflicting options: hostname and the network mode")
21 21
 	}
22 22
 
23 23
 	if len(hc.Links) > 0 {
24
-		return ErrConflictContainerNetworkAndLinks
24
+		return validationError("conflicting options: container type network can't be used with links. This would result in undefined behavior")
25 25
 	}
26 26
 
27 27
 	if len(hc.DNS) > 0 {
28
-		return ErrConflictNetworkAndDNS
28
+		return validationError("conflicting options: dns and the network mode")
29 29
 	}
30 30
 
31 31
 	if len(hc.ExtraHosts) > 0 {
32
-		return ErrConflictNetworkHosts
32
+		return validationError("conflicting options: custom host-to-IP mapping and the network mode")
33 33
 	}
34 34
 
35 35
 	if len(hc.PortBindings) > 0 || hc.PublishAllPorts {
36
-		return ErrConflictNetworkPublishPorts
36
+		return validationError("conflicting options: port publishing and the container type network mode")
37 37
 	}
38 38
 
39 39
 	if len(c.ExposedPorts) > 0 {
40
-		return ErrConflictNetworkExposePorts
40
+		return validationError("conflicting options: port exposing and the container type network mode")
41 41
 	}
42 42
 	return nil
43 43
 }
... ...
@@ -18,10 +18,10 @@ func validateNetMode(c *container.Config, hc *container.HostConfig) error {
18 18
 		return err
19 19
 	}
20 20
 	if hc.UTSMode.IsHost() && c.Hostname != "" {
21
-		return ErrConflictUTSHostname
21
+		return validationError("conflicting options: hostname and the UTS mode")
22 22
 	}
23 23
 	if hc.NetworkMode.IsHost() && len(hc.Links) > 0 {
24
-		return ErrConflictHostNetworkAndLinks
24
+		return validationError("conflicting options: host type networking can't be used with links. This would result in undefined behavior")
25 25
 	}
26 26
 	return nil
27 27
 }