errors.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. // ErrInvalidNetworkConfig error is returned when a network is created on a driver without valid config.
  20. type ErrInvalidNetworkConfig struct{}
  21. func (einc *ErrInvalidNetworkConfig) Error() string {
  22. return "trying to create a network on a driver without valid config"
  23. }
  24. // Forbidden denotes the type of this error
  25. func (einc *ErrInvalidNetworkConfig) Forbidden() {}
  26. // ErrInvalidEndpointConfig error is returned when an endpoint create is attempted with an invalid endpoint configuration.
  27. type ErrInvalidEndpointConfig struct{}
  28. func (eiec *ErrInvalidEndpointConfig) Error() string {
  29. return "trying to create an endpoint with an invalid endpoint configuration"
  30. }
  31. // InvalidParameter denotes the type of this error
  32. func (eiec *ErrInvalidEndpointConfig) InvalidParameter() {}
  33. // ErrNetworkExists error is returned when a network already exists and another network is created.
  34. type ErrNetworkExists struct{}
  35. func (ene *ErrNetworkExists) Error() string {
  36. return "network already exists, bridge can only have one network"
  37. }
  38. // Forbidden denotes the type of this error
  39. func (ene *ErrNetworkExists) Forbidden() {}
  40. // ErrIfaceName error is returned when a new name could not be generated.
  41. type ErrIfaceName struct{}
  42. func (ein *ErrIfaceName) Error() string {
  43. return "failed to find name for new interface"
  44. }
  45. // InternalError denotes the type of this error
  46. func (ein *ErrIfaceName) InternalError() {}
  47. // ErrNoIPAddr error is returned when bridge has no IPv4 address configured.
  48. type ErrNoIPAddr struct{}
  49. func (enip *ErrNoIPAddr) Error() string {
  50. return "bridge has no IPv4 address configured"
  51. }
  52. // InternalError denotes the type of this error
  53. func (enip *ErrNoIPAddr) InternalError() {}
  54. // ErrInvalidGateway is returned when the user provided default gateway (v4/v6) is not not valid.
  55. type ErrInvalidGateway struct{}
  56. func (eig *ErrInvalidGateway) Error() string {
  57. return "default gateway ip must be part of the network"
  58. }
  59. // InvalidParameter denotes the type of this error
  60. func (eig *ErrInvalidGateway) InvalidParameter() {}
  61. // ErrInvalidContainerSubnet is returned when the container subnet (FixedCIDR) is not valid.
  62. type ErrInvalidContainerSubnet struct{}
  63. func (eis *ErrInvalidContainerSubnet) Error() string {
  64. return "container subnet must be a subset of bridge network"
  65. }
  66. // InvalidParameter denotes the type of this error
  67. func (eis *ErrInvalidContainerSubnet) InvalidParameter() {}
  68. // ErrInvalidMtu is returned when the user provided MTU is not valid.
  69. type ErrInvalidMtu int
  70. func (eim ErrInvalidMtu) Error() string {
  71. return fmt.Sprintf("invalid MTU number: %d", int(eim))
  72. }
  73. // InvalidParameter denotes the type of this error
  74. func (eim ErrInvalidMtu) InvalidParameter() {}
  75. // ErrUnsupportedAddressType is returned when the specified address type is not supported.
  76. type ErrUnsupportedAddressType string
  77. func (uat ErrUnsupportedAddressType) Error() string {
  78. return fmt.Sprintf("unsupported address type: %s", string(uat))
  79. }
  80. // InvalidParameter denotes the type of this error
  81. func (uat ErrUnsupportedAddressType) InvalidParameter() {}
  82. // ActiveEndpointsError is returned when there are
  83. // still active endpoints in the network being deleted.
  84. type ActiveEndpointsError string
  85. func (aee ActiveEndpointsError) Error() string {
  86. return fmt.Sprintf("network %s has active endpoint", string(aee))
  87. }
  88. // Forbidden denotes the type of this error
  89. func (aee ActiveEndpointsError) Forbidden() {}
  90. // InvalidNetworkIDError is returned when the passed
  91. // network id for an existing network is not a known id.
  92. type InvalidNetworkIDError string
  93. func (inie InvalidNetworkIDError) Error() string {
  94. return fmt.Sprintf("invalid network id %s", string(inie))
  95. }
  96. // NotFound denotes the type of this error
  97. func (inie InvalidNetworkIDError) NotFound() {}
  98. // InvalidEndpointIDError is returned when the passed
  99. // endpoint id is not valid.
  100. type InvalidEndpointIDError string
  101. func (ieie InvalidEndpointIDError) Error() string {
  102. return fmt.Sprintf("invalid endpoint id: %s", string(ieie))
  103. }
  104. // InvalidParameter denotes the type of this error
  105. func (ieie InvalidEndpointIDError) InvalidParameter() {}
  106. // EndpointNotFoundError is returned when the no endpoint
  107. // with the passed endpoint id is found.
  108. type EndpointNotFoundError string
  109. func (enfe EndpointNotFoundError) Error() string {
  110. return fmt.Sprintf("endpoint not found: %s", string(enfe))
  111. }
  112. // NotFound denotes the type of this error
  113. func (enfe EndpointNotFoundError) NotFound() {}
  114. // NonDefaultBridgeExistError is returned when a non-default
  115. // bridge config is passed but it does not already exist.
  116. type NonDefaultBridgeExistError string
  117. func (ndbee NonDefaultBridgeExistError) Error() string {
  118. return fmt.Sprintf("bridge device with non default name %s must be created manually", string(ndbee))
  119. }
  120. // Forbidden denotes the type of this error
  121. func (ndbee NonDefaultBridgeExistError) Forbidden() {}
  122. // NonDefaultBridgeNeedsIPError is returned when a non-default
  123. // bridge config is passed but it has no ip configured
  124. type NonDefaultBridgeNeedsIPError string
  125. func (ndbee NonDefaultBridgeNeedsIPError) Error() string {
  126. return fmt.Sprintf("bridge device with non default name %s must have a valid IP address", string(ndbee))
  127. }
  128. // Forbidden denotes the type of this error
  129. func (ndbee NonDefaultBridgeNeedsIPError) Forbidden() {}
  130. // FixedCIDRv4Error is returned when fixed-cidrv4 configuration
  131. // failed.
  132. type FixedCIDRv4Error struct {
  133. Net *net.IPNet
  134. Subnet *net.IPNet
  135. Err error
  136. }
  137. func (fcv4 *FixedCIDRv4Error) Error() string {
  138. return fmt.Sprintf("setup FixedCIDRv4 failed for subnet %s in %s: %v", fcv4.Subnet, fcv4.Net, fcv4.Err)
  139. }
  140. // InternalError denotes the type of this error
  141. func (fcv4 *FixedCIDRv4Error) InternalError() {}
  142. // FixedCIDRv6Error is returned when fixed-cidrv6 configuration
  143. // failed.
  144. type FixedCIDRv6Error struct {
  145. Net *net.IPNet
  146. Err error
  147. }
  148. func (fcv6 *FixedCIDRv6Error) Error() string {
  149. return fmt.Sprintf("setup FixedCIDRv6 failed for subnet %s in %s: %v", fcv6.Net, fcv6.Net, fcv6.Err)
  150. }
  151. // InternalError denotes the type of this error
  152. func (fcv6 *FixedCIDRv6Error) InternalError() {}
  153. // IPv4AddrAddError is returned when IPv4 address could not be added to the bridge.
  154. type IPv4AddrAddError struct {
  155. IP *net.IPNet
  156. Err error
  157. }
  158. func (ipv4 *IPv4AddrAddError) Error() string {
  159. return fmt.Sprintf("failed to add IPv4 address %s to bridge: %v", ipv4.IP, ipv4.Err)
  160. }
  161. // InternalError denotes the type of this error
  162. func (ipv4 *IPv4AddrAddError) InternalError() {}
  163. // IPv4AddrNoMatchError is returned when the bridge's IPv4 address does not match configured.
  164. type IPv4AddrNoMatchError struct {
  165. IP net.IP
  166. CfgIP net.IP
  167. }
  168. func (ipv4 *IPv4AddrNoMatchError) Error() string {
  169. return fmt.Sprintf("bridge IPv4 (%s) does not match requested configuration %s", ipv4.IP, ipv4.CfgIP)
  170. }
  171. // IPv6AddrNoMatchError is returned when the bridge's IPv6 address does not match configured.
  172. type IPv6AddrNoMatchError net.IPNet
  173. func (ipv6 *IPv6AddrNoMatchError) Error() string {
  174. return fmt.Sprintf("bridge IPv6 addresses do not match the expected bridge configuration %s", (*net.IPNet)(ipv6).String())
  175. }