error.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package bridge
  2. import (
  3. "errors"
  4. "fmt"
  5. "net"
  6. )
  7. var (
  8. // ErrConfigExists error is returned when driver already has a config applied.
  9. ErrConfigExists = errors.New("configuration already exists, bridge configuration can be applied only once")
  10. // ErrInvalidDriverConfig error is returned when Bridge Driver is passed an invalid config
  11. ErrInvalidDriverConfig = errors.New("Invalid configuration passed to Bridge Driver")
  12. // ErrInvalidNetworkConfig error is returned when a network is created on a driver without valid config.
  13. ErrInvalidNetworkConfig = errors.New("trying to create a network on a driver without valid config")
  14. // ErrInvalidContainerConfig error is returned when a endpoint create is attempted with an invalid configuration.
  15. ErrInvalidContainerConfig = errors.New("Error in joining a container due to invalid configuration")
  16. // ErrInvalidEndpointConfig error is returned when a endpoint create is attempted with an invalid endpoint configuration.
  17. ErrInvalidEndpointConfig = errors.New("trying to create an endpoint with an invalid endpoint configuration")
  18. // ErrNetworkExists error is returned when a network already exists and another network is created.
  19. ErrNetworkExists = errors.New("network already exists, bridge can only have one network")
  20. // ErrIfaceName error is returned when a new name could not be generated.
  21. ErrIfaceName = errors.New("failed to find name for new interface")
  22. // ErrNoIPAddr error is returned when bridge has no IPv4 address configured.
  23. ErrNoIPAddr = errors.New("bridge has no IPv4 address configured")
  24. // ErrInvalidGateway is returned when the user provided default gateway (v4/v6) is not not valid.
  25. ErrInvalidGateway = errors.New("default gateway ip must be part of the network")
  26. // ErrInvalidContainerSubnet is returned when the container subnet (FixedCIDR) is not valid.
  27. ErrInvalidContainerSubnet = errors.New("container subnet must be a subset of bridge network")
  28. // ErrInvalidMtu is returned when the user provided MTU is not valid.
  29. ErrInvalidMtu = errors.New("invalid MTU number")
  30. )
  31. // ErrInvalidPort is returned when the container or host port specified in the port binding is not valid.
  32. type ErrInvalidPort string
  33. func (ip ErrInvalidPort) Error() string {
  34. return fmt.Sprintf("invalid transport port: %s", string(ip))
  35. }
  36. // ErrUnsupportedAddressType is returned when the specified address type is not supported.
  37. type ErrUnsupportedAddressType string
  38. func (uat ErrUnsupportedAddressType) Error() string {
  39. return fmt.Sprintf("unsupported address type: %s", string(uat))
  40. }
  41. // ErrInvalidAddressBinding is returned when the host address specfied in the port binding is not valid.
  42. type ErrInvalidAddressBinding string
  43. func (iab ErrInvalidAddressBinding) Error() string {
  44. return fmt.Sprintf("invalid host address in port binding: %s", string(iab))
  45. }
  46. // ActiveEndpointsError is returned when there are
  47. // still active endpoints in the network being deleted.
  48. type ActiveEndpointsError string
  49. func (aee ActiveEndpointsError) Error() string {
  50. return fmt.Sprintf("network %s has active endpoint", string(aee))
  51. }
  52. // InvalidNetworkIDError is returned when the passed
  53. // network id for an existing network is not a known id.
  54. type InvalidNetworkIDError string
  55. func (inie InvalidNetworkIDError) Error() string {
  56. return fmt.Sprintf("invalid network id %s", string(inie))
  57. }
  58. // InvalidEndpointIDError is returned when the passed
  59. // endpoint id is not valid.
  60. type InvalidEndpointIDError string
  61. func (ieie InvalidEndpointIDError) Error() string {
  62. return fmt.Sprintf("invalid endpoint id: %s", string(ieie))
  63. }
  64. // InvalidSandboxIDError is returned when the passed
  65. // sandbox id valid.
  66. type InvalidSandboxIDError string
  67. func (isie InvalidSandboxIDError) Error() string {
  68. return fmt.Sprintf("invalid sanbox id: %s", string(isie))
  69. }
  70. // EndpointNotFoundError is returned when the no endpoint
  71. // with the passed endpoint id is found.
  72. type EndpointNotFoundError string
  73. func (enfe EndpointNotFoundError) Error() string {
  74. return fmt.Sprintf("endpoint not found: %s", string(enfe))
  75. }
  76. // NonDefaultBridgeExistError is returned when a non-default
  77. // bridge config is passed but it does not already exist.
  78. type NonDefaultBridgeExistError string
  79. func (ndbee NonDefaultBridgeExistError) Error() string {
  80. return fmt.Sprintf("bridge device with non default name %s must be created manually", string(ndbee))
  81. }
  82. // FixedCIDRv4Error is returned when fixed-cidrv4 configuration
  83. // failed.
  84. type FixedCIDRv4Error struct {
  85. net *net.IPNet
  86. subnet *net.IPNet
  87. err error
  88. }
  89. func (fcv4 *FixedCIDRv4Error) Error() string {
  90. return fmt.Sprintf("setup FixedCIDRv4 failed for subnet %s in %s: %v", fcv4.subnet, fcv4.net, fcv4.err)
  91. }
  92. // FixedCIDRv6Error is returned when fixed-cidrv6 configuration
  93. // failed.
  94. type FixedCIDRv6Error struct {
  95. net *net.IPNet
  96. err error
  97. }
  98. func (fcv6 *FixedCIDRv6Error) Error() string {
  99. return fmt.Sprintf("setup FixedCIDRv6 failed for subnet %s in %s: %v", fcv6.net, fcv6.net, fcv6.err)
  100. }
  101. type ipForwardCfgError bridgeInterface
  102. func (i *ipForwardCfgError) Error() string {
  103. return fmt.Sprintf("unexpected request to enable IP Forwarding for: %v", *i)
  104. }
  105. type ipTableCfgError string
  106. func (name ipTableCfgError) Error() string {
  107. return fmt.Sprintf("unexpected request to set IP tables for interface: %s", string(name))
  108. }
  109. // IPv4AddrRangeError is returned when a valid IP address range couldn't be found.
  110. type IPv4AddrRangeError string
  111. func (name IPv4AddrRangeError) Error() string {
  112. return fmt.Sprintf("can't find an address range for interface %q", string(name))
  113. }
  114. // IPv4AddrAddError is returned when IPv4 address could not be added to the bridge.
  115. type IPv4AddrAddError struct {
  116. ip *net.IPNet
  117. err error
  118. }
  119. func (ipv4 *IPv4AddrAddError) Error() string {
  120. return fmt.Sprintf("failed to add IPv4 address %s to bridge: %v", ipv4.ip, ipv4.err)
  121. }
  122. // IPv6AddrAddError is returned when IPv6 address could not be added to the bridge.
  123. type IPv6AddrAddError struct {
  124. ip *net.IPNet
  125. err error
  126. }
  127. func (ipv6 *IPv6AddrAddError) Error() string {
  128. return fmt.Sprintf("failed to add IPv6 address %s to bridge: %v", ipv6.ip, ipv6.err)
  129. }
  130. // IPv4AddrNoMatchError is returned when the bridge's IPv4 address does not match configured.
  131. type IPv4AddrNoMatchError struct {
  132. ip net.IP
  133. cfgIP net.IP
  134. }
  135. func (ipv4 *IPv4AddrNoMatchError) Error() string {
  136. return fmt.Sprintf("bridge IPv4 (%s) does not match requested configuration %s", ipv4.ip, ipv4.cfgIP)
  137. }
  138. // IPv6AddrNoMatchError is returned when the bridge's IPv6 address does not match configured.
  139. type IPv6AddrNoMatchError net.IPNet
  140. func (ipv6 *IPv6AddrNoMatchError) Error() string {
  141. return fmt.Sprintf("bridge IPv6 addresses do not match the expected bridge configuration %s", ipv6)
  142. }