errors.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //go:build linux
  2. package bridge
  3. import (
  4. "fmt"
  5. "net"
  6. )
  7. // ErrConfigExists error is returned when driver already has a config applied.
  8. type ErrConfigExists struct{}
  9. func (ece *ErrConfigExists) Error() string {
  10. return "configuration already exists, bridge configuration can be applied only once"
  11. }
  12. // Forbidden denotes the type of this error
  13. func (ece *ErrConfigExists) Forbidden() {}
  14. // ErrInvalidDriverConfig error is returned when Bridge Driver is passed an invalid config
  15. type ErrInvalidDriverConfig struct{}
  16. func (eidc *ErrInvalidDriverConfig) Error() string {
  17. return "Invalid configuration passed to Bridge Driver"
  18. }
  19. // BadRequest denotes the type of this error
  20. func (eidc *ErrInvalidDriverConfig) BadRequest() {}
  21. // ErrInvalidNetworkConfig error is returned when a network is created on a driver without valid config.
  22. type ErrInvalidNetworkConfig struct{}
  23. func (einc *ErrInvalidNetworkConfig) Error() string {
  24. return "trying to create a network on a driver without valid config"
  25. }
  26. // Forbidden denotes the type of this error
  27. func (einc *ErrInvalidNetworkConfig) Forbidden() {}
  28. // ErrInvalidEndpointConfig error is returned when an endpoint create is attempted with an invalid endpoint configuration.
  29. type ErrInvalidEndpointConfig struct{}
  30. func (eiec *ErrInvalidEndpointConfig) Error() string {
  31. return "trying to create an endpoint with an invalid endpoint configuration"
  32. }
  33. // BadRequest denotes the type of this error
  34. func (eiec *ErrInvalidEndpointConfig) BadRequest() {}
  35. // ErrNetworkExists error is returned when a network already exists and another network is created.
  36. type ErrNetworkExists struct{}
  37. func (ene *ErrNetworkExists) Error() string {
  38. return "network already exists, bridge can only have one network"
  39. }
  40. // Forbidden denotes the type of this error
  41. func (ene *ErrNetworkExists) Forbidden() {}
  42. // ErrIfaceName error is returned when a new name could not be generated.
  43. type ErrIfaceName struct{}
  44. func (ein *ErrIfaceName) Error() string {
  45. return "failed to find name for new interface"
  46. }
  47. // InternalError denotes the type of this error
  48. func (ein *ErrIfaceName) InternalError() {}
  49. // ErrNoIPAddr error is returned when bridge has no IPv4 address configured.
  50. type ErrNoIPAddr struct{}
  51. func (enip *ErrNoIPAddr) Error() string {
  52. return "bridge has no IPv4 address configured"
  53. }
  54. // InternalError denotes the type of this error
  55. func (enip *ErrNoIPAddr) InternalError() {}
  56. // ErrInvalidGateway is returned when the user provided default gateway (v4/v6) is not not valid.
  57. type ErrInvalidGateway struct{}
  58. func (eig *ErrInvalidGateway) Error() string {
  59. return "default gateway ip must be part of the network"
  60. }
  61. // BadRequest denotes the type of this error
  62. func (eig *ErrInvalidGateway) BadRequest() {}
  63. // ErrInvalidContainerSubnet is returned when the container subnet (FixedCIDR) is not valid.
  64. type ErrInvalidContainerSubnet struct{}
  65. func (eis *ErrInvalidContainerSubnet) Error() string {
  66. return "container subnet must be a subset of bridge network"
  67. }
  68. // BadRequest denotes the type of this error
  69. func (eis *ErrInvalidContainerSubnet) BadRequest() {}
  70. // ErrInvalidMtu is returned when the user provided MTU is not valid.
  71. type ErrInvalidMtu int
  72. func (eim ErrInvalidMtu) Error() string {
  73. return fmt.Sprintf("invalid MTU number: %d", int(eim))
  74. }
  75. // BadRequest denotes the type of this error
  76. func (eim ErrInvalidMtu) BadRequest() {}
  77. // ErrUnsupportedAddressType is returned when the specified address type is not supported.
  78. type ErrUnsupportedAddressType string
  79. func (uat ErrUnsupportedAddressType) Error() string {
  80. return fmt.Sprintf("unsupported address type: %s", string(uat))
  81. }
  82. // BadRequest denotes the type of this error
  83. func (uat ErrUnsupportedAddressType) BadRequest() {}
  84. // ActiveEndpointsError is returned when there are
  85. // still active endpoints in the network being deleted.
  86. type ActiveEndpointsError string
  87. func (aee ActiveEndpointsError) Error() string {
  88. return fmt.Sprintf("network %s has active endpoint", string(aee))
  89. }
  90. // Forbidden denotes the type of this error
  91. func (aee ActiveEndpointsError) Forbidden() {}
  92. // InvalidNetworkIDError is returned when the passed
  93. // network id for an existing network is not a known id.
  94. type InvalidNetworkIDError string
  95. func (inie InvalidNetworkIDError) Error() string {
  96. return fmt.Sprintf("invalid network id %s", string(inie))
  97. }
  98. // NotFound denotes the type of this error
  99. func (inie InvalidNetworkIDError) NotFound() {}
  100. // InvalidEndpointIDError is returned when the passed
  101. // endpoint id is not valid.
  102. type InvalidEndpointIDError string
  103. func (ieie InvalidEndpointIDError) Error() string {
  104. return fmt.Sprintf("invalid endpoint id: %s", string(ieie))
  105. }
  106. // BadRequest denotes the type of this error
  107. func (ieie InvalidEndpointIDError) BadRequest() {}
  108. // EndpointNotFoundError is returned when the no endpoint
  109. // with the passed endpoint id is found.
  110. type EndpointNotFoundError string
  111. func (enfe EndpointNotFoundError) Error() string {
  112. return fmt.Sprintf("endpoint not found: %s", string(enfe))
  113. }
  114. // NotFound denotes the type of this error
  115. func (enfe EndpointNotFoundError) NotFound() {}
  116. // NonDefaultBridgeExistError is returned when a non-default
  117. // bridge config is passed but it does not already exist.
  118. type NonDefaultBridgeExistError string
  119. func (ndbee NonDefaultBridgeExistError) Error() string {
  120. return fmt.Sprintf("bridge device with non default name %s must be created manually", string(ndbee))
  121. }
  122. // Forbidden denotes the type of this error
  123. func (ndbee NonDefaultBridgeExistError) Forbidden() {}
  124. // NonDefaultBridgeNeedsIPError is returned when a non-default
  125. // bridge config is passed but it has no ip configured
  126. type NonDefaultBridgeNeedsIPError string
  127. func (ndbee NonDefaultBridgeNeedsIPError) Error() string {
  128. return fmt.Sprintf("bridge device with non default name %s must have a valid IP address", string(ndbee))
  129. }
  130. // Forbidden denotes the type of this error
  131. func (ndbee NonDefaultBridgeNeedsIPError) Forbidden() {}
  132. // FixedCIDRv4Error is returned when fixed-cidrv4 configuration
  133. // failed.
  134. type FixedCIDRv4Error struct {
  135. Net *net.IPNet
  136. Subnet *net.IPNet
  137. Err error
  138. }
  139. func (fcv4 *FixedCIDRv4Error) Error() string {
  140. return fmt.Sprintf("setup FixedCIDRv4 failed for subnet %s in %s: %v", fcv4.Subnet, fcv4.Net, fcv4.Err)
  141. }
  142. // InternalError denotes the type of this error
  143. func (fcv4 *FixedCIDRv4Error) InternalError() {}
  144. // FixedCIDRv6Error is returned when fixed-cidrv6 configuration
  145. // failed.
  146. type FixedCIDRv6Error struct {
  147. Net *net.IPNet
  148. Err error
  149. }
  150. func (fcv6 *FixedCIDRv6Error) Error() string {
  151. return fmt.Sprintf("setup FixedCIDRv6 failed for subnet %s in %s: %v", fcv6.Net, fcv6.Net, fcv6.Err)
  152. }
  153. // InternalError denotes the type of this error
  154. func (fcv6 *FixedCIDRv6Error) InternalError() {}
  155. // IPTableCfgError is returned when an unexpected ip tables configuration is entered
  156. type IPTableCfgError string
  157. func (name IPTableCfgError) Error() string {
  158. return fmt.Sprintf("unexpected request to set IP tables for interface: %s", string(name))
  159. }
  160. // BadRequest denotes the type of this error
  161. func (name IPTableCfgError) BadRequest() {}
  162. // InvalidIPTablesCfgError is returned when an invalid ip tables configuration is entered
  163. type InvalidIPTablesCfgError string
  164. func (action InvalidIPTablesCfgError) Error() string {
  165. return fmt.Sprintf("Invalid IPTables action '%s'", string(action))
  166. }
  167. // BadRequest denotes the type of this error
  168. func (action InvalidIPTablesCfgError) BadRequest() {}
  169. // IPv4AddrAddError is returned when IPv4 address could not be added to the bridge.
  170. type IPv4AddrAddError struct {
  171. IP *net.IPNet
  172. Err error
  173. }
  174. func (ipv4 *IPv4AddrAddError) Error() string {
  175. return fmt.Sprintf("failed to add IPv4 address %s to bridge: %v", ipv4.IP, ipv4.Err)
  176. }
  177. // InternalError denotes the type of this error
  178. func (ipv4 *IPv4AddrAddError) InternalError() {}
  179. // IPv6AddrAddError is returned when IPv6 address could not be added to the bridge.
  180. type IPv6AddrAddError struct {
  181. IP *net.IPNet
  182. Err error
  183. }
  184. func (ipv6 *IPv6AddrAddError) Error() string {
  185. return fmt.Sprintf("failed to add IPv6 address %s to bridge: %v", ipv6.IP, ipv6.Err)
  186. }
  187. // InternalError denotes the type of this error
  188. func (ipv6 *IPv6AddrAddError) InternalError() {}
  189. // IPv4AddrNoMatchError is returned when the bridge's IPv4 address does not match configured.
  190. type IPv4AddrNoMatchError struct {
  191. IP net.IP
  192. CfgIP net.IP
  193. }
  194. func (ipv4 *IPv4AddrNoMatchError) Error() string {
  195. return fmt.Sprintf("bridge IPv4 (%s) does not match requested configuration %s", ipv4.IP, ipv4.CfgIP)
  196. }
  197. // BadRequest denotes the type of this error
  198. func (ipv4 *IPv4AddrNoMatchError) BadRequest() {}
  199. // IPv6AddrNoMatchError is returned when the bridge's IPv6 address does not match configured.
  200. type IPv6AddrNoMatchError net.IPNet
  201. func (ipv6 *IPv6AddrNoMatchError) Error() string {
  202. return fmt.Sprintf("bridge IPv6 addresses do not match the expected bridge configuration %s", (*net.IPNet)(ipv6).String())
  203. }
  204. // BadRequest denotes the type of this error
  205. func (ipv6 *IPv6AddrNoMatchError) BadRequest() {}
  206. // InvalidLinkIPAddrError is returned when a link is configured to a container with an invalid ip address
  207. type InvalidLinkIPAddrError string
  208. func (address InvalidLinkIPAddrError) Error() string {
  209. return fmt.Sprintf("Cannot link to a container with Invalid IP Address '%s'", string(address))
  210. }
  211. // BadRequest denotes the type of this error
  212. func (address InvalidLinkIPAddrError) BadRequest() {}