Skip to content

Commit a362ae9

Browse files
authored
Merge pull request moby#50532 from thaJeztah/runconfig_rm_errors
runconfig: remove exported errors
2 parents bc6851e + 26fda34 commit a362ae9

8 files changed

Lines changed: 30 additions & 78 deletions

File tree

daemon/container_operations.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/docker/docker/daemon/network"
2929
"github.com/docker/docker/daemon/pkg/opts"
3030
"github.com/docker/docker/errdefs"
31-
"github.com/docker/docker/runconfig"
3231
"github.com/docker/go-connections/nat"
3332
containertypes "github.com/moby/moby/api/types/container"
3433
"github.com/moby/moby/api/types/events"
@@ -174,7 +173,7 @@ func (daemon *Daemon) updateNetworkSettings(ctr *container.Container, n *libnetw
174173
}
175174

176175
if !ctr.HostConfig.NetworkMode.IsHost() && containertypes.NetworkMode(n.Type()).IsHost() {
177-
return runconfig.ErrConflictConnectToHostNetwork
176+
return cerrdefs.ErrInvalidArgument.WithMessage("cannot connect container to host network - container must be created in host network mode")
178177
}
179178

180179
for s, v := range ctr.NetworkSettings.Networks {
@@ -194,13 +193,11 @@ func (daemon *Daemon) updateNetworkSettings(ctr *container.Container, n *libnetw
194193
// Avoid duplicate config
195194
return nil
196195
}
197-
if !containertypes.NetworkMode(sn.Type()).IsPrivate() ||
198-
!containertypes.NetworkMode(n.Type()).IsPrivate() {
199-
return runconfig.ErrConflictSharedNetwork
196+
if !containertypes.NetworkMode(sn.Type()).IsPrivate() || !containertypes.NetworkMode(n.Type()).IsPrivate() {
197+
return cerrdefs.ErrInvalidArgument.WithMessage("container sharing network namespace with another container or host cannot be connected to any other network")
200198
}
201-
if containertypes.NetworkMode(sn.Name()).IsNone() ||
202-
containertypes.NetworkMode(n.Name()).IsNone() {
203-
return runconfig.ErrConflictNoNetwork
199+
if containertypes.NetworkMode(sn.Name()).IsNone() || containertypes.NetworkMode(n.Name()).IsNone() {
200+
return cerrdefs.ErrInvalidArgument.WithMessage("container cannot be connected to multiple networks with one of the networks in private (none) mode")
204201
}
205202
}
206203

@@ -549,10 +546,10 @@ func validateEndpointSettings(nw *libnetwork.Network, nwName string, epConfig *n
549546
hasStaticAddresses := ipamConfig.IPv4Address != "" || ipamConfig.IPv6Address != ""
550547
// On Linux, user specified IP address is accepted only by networks with user specified subnets.
551548
if hasStaticAddresses && !enableIPOnPredefinedNetwork() {
552-
errs = append(errs, runconfig.ErrUnsupportedNetworkAndIP)
549+
errs = append(errs, cerrdefs.ErrInvalidArgument.WithMessage("user specified IP address is supported on user defined networks only"))
553550
}
554551
if len(epConfig.Aliases) > 0 && !serviceDiscoveryOnDefaultNetwork() {
555-
errs = append(errs, runconfig.ErrUnsupportedNetworkAndAlias)
552+
errs = append(errs, cerrdefs.ErrInvalidArgument.WithMessage("network-scoped alias is supported only for containers in user defined networks"))
556553
}
557554
}
558555

@@ -671,7 +668,7 @@ func (daemon *Daemon) connectToNetwork(ctx context.Context, cfg *config.Config,
671668
start := time.Now()
672669

673670
if ctr.HostConfig.NetworkMode.IsContainer() {
674-
return runconfig.ErrConflictSharedNetwork
671+
return cerrdefs.ErrInvalidArgument.WithMessage("container sharing network namespace with another container or host cannot be connected to any other network")
675672
}
676673
if cfg.DisableBridge && containertypes.NetworkMode(idOrName).IsBridge() {
677674
ctr.Config.NetworkDisabled = true
@@ -1046,7 +1043,7 @@ func (daemon *Daemon) DisconnectFromNetwork(ctx context.Context, ctr *container.
10461043
delete(ctr.NetworkSettings.Networks, networkName)
10471044
} else if err == nil {
10481045
if ctr.HostConfig.NetworkMode.IsHost() && containertypes.NetworkMode(n.Type()).IsHost() {
1049-
return runconfig.ErrConflictDisconnectFromHostNetwork
1046+
return cerrdefs.ErrInvalidArgument.WithMessage("cannot disconnect container from host network - container was created in host network mode")
10501047
}
10511048

10521049
if err := daemon.disconnectFromNetwork(ctx, ctr, n, false); err != nil {

daemon/create.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/docker/docker/daemon/internal/otelutil"
2424
"github.com/docker/docker/daemon/server/backend"
2525
"github.com/docker/docker/errdefs"
26-
"github.com/docker/docker/runconfig"
2726
containertypes "github.com/moby/moby/api/types/container"
2827
"github.com/moby/moby/api/types/events"
2928
networktypes "github.com/moby/moby/api/types/network"
@@ -77,7 +76,7 @@ func (daemon *Daemon) containerCreate(ctx context.Context, daemonCfg *configStor
7776

7877
start := time.Now()
7978
if opts.params.Config == nil {
80-
return containertypes.CreateResponse{}, errdefs.InvalidParameter(runconfig.ErrEmptyConfig)
79+
return containertypes.CreateResponse{}, errdefs.InvalidParameter(errors.New("config cannot be empty in order to create a container"))
8180
}
8281

8382
// Normalize some defaults. Doing this "ad-hoc" here for now, as there's

daemon/daemon_unix.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import (
4040
volumemounts "github.com/docker/docker/daemon/volume/mounts"
4141
"github.com/docker/docker/errdefs"
4242
"github.com/docker/docker/pkg/sysinfo"
43-
"github.com/docker/docker/runconfig"
4443
"github.com/moby/moby/api/types/blkiodev"
4544
containertypes "github.com/moby/moby/api/types/container"
4645
"github.com/moby/moby/api/types/network"
@@ -1565,7 +1564,7 @@ func (daemon *Daemon) registerLinks(container *container.Container, hostConfig *
15651564
}
15661565
}
15671566
if child.HostConfig.NetworkMode.IsHost() {
1568-
return runconfig.ErrConflictHostNetworkAndLinks
1567+
return cerrdefs.ErrInvalidArgument.WithMessage("conflicting options: host type networking can't be used with links. This would result in undefined behavior")
15691568
}
15701569
if err := daemon.registerLink(container, child, alias); err != nil {
15711570
return err

daemon/server/router/container/container_routes.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/docker/docker/daemon/server/httputils"
2020
"github.com/docker/docker/errdefs"
2121
"github.com/docker/docker/pkg/ioutils"
22-
"github.com/docker/docker/runconfig"
2322
"github.com/moby/moby/api/types"
2423
"github.com/moby/moby/api/types/container"
2524
"github.com/moby/moby/api/types/filters"
@@ -506,7 +505,7 @@ func (c *containerRouter) postContainersCreate(ctx context.Context, w http.Respo
506505
}
507506

508507
if config == nil {
509-
return errdefs.InvalidParameter(runconfig.ErrEmptyConfig)
508+
return errdefs.InvalidParameter(errors.New("config cannot be empty in order to create a container"))
510509
}
511510
if hostConfig == nil {
512511
hostConfig = &container.HostConfig{}
@@ -763,7 +762,7 @@ func handleMACAddressBC(config *container.Config, hostConfig *container.HostConf
763762
return "", nil
764763
}
765764
if !hostConfig.NetworkMode.IsBridge() && !hostConfig.NetworkMode.IsUserDefined() {
766-
return "", runconfig.ErrConflictContainerNetworkAndMac
765+
return "", errdefs.InvalidParameter(errors.New("conflicting options: mac-address and the network mode"))
767766
}
768767

769768
epConfig, err := epConfigForNetMode(version, hostConfig.NetworkMode, networkingConfig)

runconfig/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ func decodeContainerConfig(src io.Reader, si *sysinfo.SysInfo) (*container.Confi
8181
func loadJSON(src io.Reader, out interface{}) error {
8282
dec := json.NewDecoder(src)
8383
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}
8586
}
8687
if dec.More() {
8788
return validationError("unexpected content after JSON")

runconfig/errors.go

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,19 @@
11
package runconfig
22

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"
394

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)
447
}
458

46-
func (e validationError) InvalidParameter() {}
47-
48-
type invalidJSONError struct {
49-
Err error
50-
}
9+
type invalidJSONError struct{ error }
5110

5211
func (e invalidJSONError) Error() string {
53-
return "invalid JSON: " + e.Err.Error()
12+
return "invalid JSON: " + e.error.Error()
5413
}
5514

5615
func (e invalidJSONError) Unwrap() error {
57-
return e.Err
16+
return e.error
5817
}
5918

6019
func (e invalidJSONError) InvalidParameter() {}

runconfig/hostconfig.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package runconfig
22

3-
import (
4-
"github.com/moby/moby/api/types/container"
5-
)
3+
import "github.com/moby/moby/api/types/container"
64

75
// validateNetContainerMode ensures that the various combinations of requested
86
// network settings wrt container mode are valid.
@@ -17,27 +15,27 @@ func validateNetContainerMode(c *container.Config, hc *container.HostConfig) err
1715
}
1816

1917
if c.Hostname != "" {
20-
return ErrConflictNetworkHostname
18+
return validationError("conflicting options: hostname and the network mode")
2119
}
2220

2321
if len(hc.Links) > 0 {
24-
return ErrConflictContainerNetworkAndLinks
22+
return validationError("conflicting options: container type network can't be used with links. This would result in undefined behavior")
2523
}
2624

2725
if len(hc.DNS) > 0 {
28-
return ErrConflictNetworkAndDNS
26+
return validationError("conflicting options: dns and the network mode")
2927
}
3028

3129
if len(hc.ExtraHosts) > 0 {
32-
return ErrConflictNetworkHosts
30+
return validationError("conflicting options: custom host-to-IP mapping and the network mode")
3331
}
3432

3533
if len(hc.PortBindings) > 0 || hc.PublishAllPorts {
36-
return ErrConflictNetworkPublishPorts
34+
return validationError("conflicting options: port publishing and the container type network mode")
3735
}
3836

3937
if len(c.ExposedPorts) > 0 {
40-
return ErrConflictNetworkExposePorts
38+
return validationError("conflicting options: port exposing and the container type network mode")
4139
}
4240
return nil
4341
}

runconfig/hostconfig_unix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ func validateNetMode(c *container.Config, hc *container.HostConfig) error {
1818
return err
1919
}
2020
if hc.UTSMode.IsHost() && c.Hostname != "" {
21-
return ErrConflictUTSHostname
21+
return validationError("conflicting options: hostname and the UTS mode")
2222
}
2323
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")
2525
}
2626
return nil
2727
}

0 commit comments

Comments
 (0)