Skip to content

Commit

Permalink
Merge pull request moby#49349 from thaJeztah/bridge_clean_errors
Browse files Browse the repository at this point in the history
libnetwork/drivers/bridge: remove, or internalize errors
  • Loading branch information
thaJeztah authored Jan 28, 2025
2 parents 47dc8d5 + bbaa8af commit 04f9d3e
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 308 deletions.
30 changes: 15 additions & 15 deletions libnetwork/drivers/bridge/bridge_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func ValidateFixedCIDRV6(val string) error {
// Whatever can be assessed a priori before attempting any programming.
func (c *networkConfiguration) Validate() error {
if c.Mtu < 0 {
return ErrInvalidMtu(c.Mtu)
return errdefs.InvalidParameter(fmt.Errorf("invalid MTU number: %d", c.Mtu))
}

if c.EnableIPv4 {
Expand All @@ -246,7 +246,7 @@ func (c *networkConfiguration) Validate() error {
// If default gw is specified, it must be part of bridge subnet
if c.DefaultGatewayIPv4 != nil {
if !c.AddressIPv4.Contains(c.DefaultGatewayIPv4) {
return &ErrInvalidGateway{}
return errInvalidGateway
}
}
}
Expand All @@ -266,7 +266,7 @@ func (c *networkConfiguration) Validate() error {
}
// If a default gw is specified, it must belong to AddressIPv6's subnet
if c.DefaultGatewayIPv6 != nil && !c.AddressIPv6.Contains(c.DefaultGatewayIPv6) {
return &ErrInvalidGateway{}
return errInvalidGateway
}
}

Expand Down Expand Up @@ -447,7 +447,7 @@ func (n *bridgeNetwork) getPortDriverClient() portDriverClient {

func (n *bridgeNetwork) getEndpoint(eid string) (*bridgeEndpoint, error) {
if eid == "" {
return nil, InvalidEndpointIDError(eid)
return nil, invalidEndpointIDError(eid)
}

n.Lock()
Expand Down Expand Up @@ -499,7 +499,7 @@ func (d *driver) configure(option map[string]interface{}) error {
case nil:
// No GenericData option set. Use defaults.
default:
return &ErrInvalidDriverConfig{}
return errdefs.InvalidParameter(fmt.Errorf("invalid configuration type (%T) passed", opt))
}

if config.EnableIPTables {
Expand Down Expand Up @@ -1116,7 +1116,7 @@ func (d *driver) CreateEndpoint(ctx context.Context, nid, eid string, ifInfo dri
n.Lock()
if n.id != nid {
n.Unlock()
return InvalidNetworkIDError(nid)
return invalidNetworkIDError(nid)
}
n.Unlock()

Expand Down Expand Up @@ -1329,7 +1329,7 @@ func (d *driver) DeleteEndpoint(nid, eid string) error {
n.Lock()
if n.id != nid {
n.Unlock()
return InvalidNetworkIDError(nid)
return invalidNetworkIDError(nid)
}
n.Unlock()

Expand All @@ -1339,7 +1339,7 @@ func (d *driver) DeleteEndpoint(nid, eid string) error {
return err
}
if ep == nil {
return EndpointNotFoundError(eid)
return endpointNotFoundError(eid)
}

// Remove it
Expand Down Expand Up @@ -1390,7 +1390,7 @@ func (d *driver) EndpointOperInfo(nid, eid string) (map[string]interface{}, erro
n.Lock()
if n.id != nid {
n.Unlock()
return nil, InvalidNetworkIDError(nid)
return nil, invalidNetworkIDError(nid)
}
n.Unlock()

Expand Down Expand Up @@ -1449,7 +1449,7 @@ func (d *driver) Join(ctx context.Context, nid, eid string, sboxKey string, jinf
}

if endpoint == nil {
return EndpointNotFoundError(eid)
return endpointNotFoundError(eid)
}

endpoint.containerConfig, err = parseContainerOptions(options)
Expand Down Expand Up @@ -1491,7 +1491,7 @@ func (d *driver) Leave(nid, eid string) error {
}

if endpoint == nil {
return EndpointNotFoundError(eid)
return endpointNotFoundError(eid)
}

if !network.config.EnableICC {
Expand Down Expand Up @@ -1520,7 +1520,7 @@ func (d *driver) ProgramExternalConnectivity(ctx context.Context, nid, eid strin
}

if endpoint == nil {
return EndpointNotFoundError(eid)
return endpointNotFoundError(eid)
}

endpoint.extConnConfig, err = parseConnectivityOptions(options)
Expand Down Expand Up @@ -1580,7 +1580,7 @@ func (d *driver) RevokeExternalConnectivity(nid, eid string) error {
}

if endpoint == nil {
return EndpointNotFoundError(eid)
return endpointNotFoundError(eid)
}

err = network.releasePorts(endpoint)
Expand Down Expand Up @@ -1638,7 +1638,7 @@ func (d *driver) link(network *bridgeNetwork, endpoint *bridgeEndpoint, enable b
return err
}
if parentEndpoint == nil {
return InvalidEndpointIDError(p)
return invalidEndpointIDError(p)
}

l, err := newLink(parentEndpoint.addr.IP, endpoint.addr.IP, ec.ExposedPorts, network.config.BridgeName)
Expand All @@ -1662,7 +1662,7 @@ func (d *driver) link(network *bridgeNetwork, endpoint *bridgeEndpoint, enable b
return err
}
if childEndpoint == nil {
return InvalidEndpointIDError(c)
return invalidEndpointIDError(c)
}
if childEndpoint.extConnConfig == nil || childEndpoint.extConnConfig.ExposedPorts == nil {
continue
Expand Down
184 changes: 20 additions & 164 deletions libnetwork/drivers/bridge/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,188 +3,44 @@
package bridge

import (
"errors"
"fmt"
"net"
)

// ErrConfigExists error is returned when driver already has a config applied.
type ErrConfigExists struct{}

func (ece *ErrConfigExists) Error() string {
return "configuration already exists, bridge configuration can be applied only once"
}

// Forbidden denotes the type of this error
func (ece *ErrConfigExists) Forbidden() {}

// ErrInvalidDriverConfig error is returned when Bridge Driver is passed an invalid config
type ErrInvalidDriverConfig struct{}

func (eidc *ErrInvalidDriverConfig) Error() string {
return "Invalid configuration passed to Bridge Driver"
}

// ErrInvalidNetworkConfig error is returned when a network is created on a driver without valid config.
type ErrInvalidNetworkConfig struct{}

func (einc *ErrInvalidNetworkConfig) Error() string {
return "trying to create a network on a driver without valid config"
}

// Forbidden denotes the type of this error
func (einc *ErrInvalidNetworkConfig) Forbidden() {}

// ErrInvalidEndpointConfig error is returned when an endpoint create is attempted with an invalid endpoint configuration.
type ErrInvalidEndpointConfig struct{}

func (eiec *ErrInvalidEndpointConfig) Error() string {
return "trying to create an endpoint with an invalid endpoint configuration"
}

// InvalidParameter denotes the type of this error
func (eiec *ErrInvalidEndpointConfig) InvalidParameter() {}

// ErrNetworkExists error is returned when a network already exists and another network is created.
type ErrNetworkExists struct{}

func (ene *ErrNetworkExists) Error() string {
return "network already exists, bridge can only have one network"
}

// Forbidden denotes the type of this error
func (ene *ErrNetworkExists) Forbidden() {}

// ErrIfaceName error is returned when a new name could not be generated.
type ErrIfaceName struct{}

func (ein *ErrIfaceName) Error() string {
return "failed to find name for new interface"
}

// InternalError denotes the type of this error
func (ein *ErrIfaceName) InternalError() {}

// ErrNoIPAddr error is returned when bridge has no IPv4 address configured.
type ErrNoIPAddr struct{}

func (enip *ErrNoIPAddr) Error() string {
return "bridge has no IPv4 address configured"
}

// InternalError denotes the type of this error
func (enip *ErrNoIPAddr) InternalError() {}

// ErrInvalidGateway is returned when the user provided default gateway (v4/v6) is not valid.
type ErrInvalidGateway struct{}

func (eig *ErrInvalidGateway) Error() string {
return "default gateway ip must be part of the network"
}

// InvalidParameter denotes the type of this error
func (eig *ErrInvalidGateway) InvalidParameter() {}

// ErrInvalidMtu is returned when the user provided MTU is not valid.
type ErrInvalidMtu int

func (eim ErrInvalidMtu) Error() string {
return fmt.Sprintf("invalid MTU number: %d", int(eim))
}

// InvalidParameter denotes the type of this error
func (eim ErrInvalidMtu) InvalidParameter() {}

// ErrUnsupportedAddressType is returned when the specified address type is not supported.
type ErrUnsupportedAddressType string

func (uat ErrUnsupportedAddressType) Error() string {
return fmt.Sprintf("unsupported address type: %s", string(uat))
}
"github.com/docker/docker/errdefs"
)

// InvalidParameter denotes the type of this error
func (uat ErrUnsupportedAddressType) InvalidParameter() {}
// errInvalidGateway is returned when the user provided default gateway (v4/v6) is not valid.
var errInvalidGateway = errdefs.InvalidParameter(errors.New("default gateway ip must be part of the network"))

// InvalidNetworkIDError is returned when the passed
// invalidNetworkIDError is returned when the passed
// network id for an existing network is not a known id.
type InvalidNetworkIDError string
type invalidNetworkIDError string

func (inie InvalidNetworkIDError) Error() string {
return fmt.Sprintf("invalid network id %s", string(inie))
func (e invalidNetworkIDError) Error() string {
return fmt.Sprintf("invalid network id %s", string(e))
}

// NotFound denotes the type of this error
func (inie InvalidNetworkIDError) NotFound() {}
func (e invalidNetworkIDError) NotFound() {}

// InvalidEndpointIDError is returned when the passed
// invalidEndpointIDError is returned when the passed
// endpoint id is not valid.
type InvalidEndpointIDError string
type invalidEndpointIDError string

func (ieie InvalidEndpointIDError) Error() string {
return fmt.Sprintf("invalid endpoint id: %s", string(ieie))
func (e invalidEndpointIDError) Error() string {
return fmt.Sprintf("invalid endpoint id: %s", string(e))
}

// InvalidParameter denotes the type of this error
func (ieie InvalidEndpointIDError) InvalidParameter() {}
func (e invalidEndpointIDError) InvalidParameter() {}

// EndpointNotFoundError is returned when the no endpoint
// endpointNotFoundError is returned when the no endpoint
// with the passed endpoint id is found.
type EndpointNotFoundError string
type endpointNotFoundError string

func (enfe EndpointNotFoundError) Error() string {
return fmt.Sprintf("endpoint not found: %s", string(enfe))
func (e endpointNotFoundError) Error() string {
return fmt.Sprintf("endpoint not found: %s", string(e))
}

// NotFound denotes the type of this error
func (enfe EndpointNotFoundError) NotFound() {}

// NonDefaultBridgeExistError is returned when a non-default
// bridge config is passed but it does not already exist.
type NonDefaultBridgeExistError string

func (ndbee NonDefaultBridgeExistError) Error() string {
return fmt.Sprintf("bridge device with non default name %s must be created manually", string(ndbee))
}

// Forbidden denotes the type of this error
func (ndbee NonDefaultBridgeExistError) Forbidden() {}

// NonDefaultBridgeNeedsIPError is returned when a non-default
// bridge config is passed but it has no ip configured
type NonDefaultBridgeNeedsIPError string

func (ndbee NonDefaultBridgeNeedsIPError) Error() string {
return fmt.Sprintf("bridge device with non default name %s must have a valid IP address", string(ndbee))
}

// Forbidden denotes the type of this error
func (ndbee NonDefaultBridgeNeedsIPError) Forbidden() {}

// IPv4AddrAddError is returned when IPv4 address could not be added to the bridge.
type IPv4AddrAddError struct {
IP *net.IPNet
Err error
}

func (ipv4 *IPv4AddrAddError) Error() string {
return fmt.Sprintf("failed to add IPv4 address %s to bridge: %v", ipv4.IP, ipv4.Err)
}

// InternalError denotes the type of this error
func (ipv4 *IPv4AddrAddError) InternalError() {}

// IPv4AddrNoMatchError is returned when the bridge's IPv4 address does not match configured.
type IPv4AddrNoMatchError struct {
IP net.IP
CfgIP net.IP
}

func (ipv4 *IPv4AddrNoMatchError) Error() string {
return fmt.Sprintf("bridge IPv4 (%s) does not match requested configuration %s", ipv4.IP, ipv4.CfgIP)
}

// IPv6AddrNoMatchError is returned when the bridge's IPv6 address does not match configured.
type IPv6AddrNoMatchError net.IPNet

func (ipv6 *IPv6AddrNoMatchError) Error() string {
return fmt.Sprintf("bridge IPv6 addresses do not match the expected bridge configuration %s", (*net.IPNet)(ipv6).String())
}
func (e endpointNotFoundError) NotFound() {}
Loading

0 comments on commit 04f9d3e

Please sign in to comment.