errors.go 11 KB

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