driverapi.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package driverapi
  2. import (
  3. "errors"
  4. "fmt"
  5. "net"
  6. "github.com/docker/libnetwork/types"
  7. )
  8. var (
  9. // ErrEndpointExists is returned if more than one endpoint is added to the network
  10. ErrEndpointExists = errors.New("Endpoint already exists (Only one endpoint allowed)")
  11. // ErrNoNetwork is returned if no network with the specified id exists
  12. ErrNoNetwork = errors.New("No network exists")
  13. // ErrNoEndpoint is returned if no endpoint with the specified id exists
  14. ErrNoEndpoint = errors.New("No endpoint exists")
  15. // ErrNotImplemented is returned when a Driver has not implemented an API yet
  16. ErrNotImplemented = errors.New("The API is not implemented yet")
  17. )
  18. // NetworkPluginEndpointType represents the Endpoint Type used by Plugin system
  19. const NetworkPluginEndpointType = "NetworkDriver"
  20. // Driver is an interface that every plugin driver needs to implement.
  21. type Driver interface {
  22. // Push driver specific config to the driver
  23. Config(options map[string]interface{}) error
  24. // CreateNetwork invokes the driver method to create a network passing
  25. // the network id and network specific config. The config mechanism will
  26. // eventually be replaced with labels which are yet to be introduced.
  27. CreateNetwork(nid types.UUID, options map[string]interface{}) error
  28. // DeleteNetwork invokes the driver method to delete network passing
  29. // the network id.
  30. DeleteNetwork(nid types.UUID) error
  31. // CreateEndpoint invokes the driver method to create an endpoint
  32. // passing the network id, endpoint id endpoint information and driver
  33. // specific config. The endpoint information can be either consumed by
  34. // the driver or populated by the driver. The config mechanism will
  35. // eventually be replaced with labels which are yet to be introduced.
  36. CreateEndpoint(nid, eid types.UUID, epInfo EndpointInfo, options map[string]interface{}) error
  37. // DeleteEndpoint invokes the driver method to delete an endpoint
  38. // passing the network id and endpoint id.
  39. DeleteEndpoint(nid, eid types.UUID) error
  40. // EndpointOperInfo retrieves from the driver the operational data related to the specified endpoint
  41. EndpointOperInfo(nid, eid types.UUID) (map[string]interface{}, error)
  42. // Join method is invoked when a Sandbox is attached to an endpoint.
  43. Join(nid, eid types.UUID, sboxKey string, jinfo JoinInfo, options map[string]interface{}) error
  44. // Leave method is invoked when a Sandbox detaches from an endpoint.
  45. Leave(nid, eid types.UUID) error
  46. // Type returns the the type of this driver, the network type this driver manages
  47. Type() string
  48. }
  49. // EndpointInfo provides a go interface to fetch or populate endpoint assigned network resources.
  50. type EndpointInfo interface {
  51. // Interfaces returns a list of interfaces bound to the endpoint.
  52. // If the list is not empty the driver is only expected to consume the interfaces.
  53. // It is an error to try to add interfaces to a non-empty list.
  54. // If the list is empty the driver is expected to populate with 0 or more interfaces.
  55. Interfaces() []InterfaceInfo
  56. // AddInterface is used by the driver to add an interface to the interface list.
  57. // This method will return an error if the driver attempts to add interfaces
  58. // if the Interfaces() method returned a non-empty list.
  59. // ID field need only have significance within the endpoint so it can be a simple
  60. // monotonically increasing number
  61. AddInterface(ID int, mac net.HardwareAddr, ipv4 net.IPNet, ipv6 net.IPNet) error
  62. }
  63. // InterfaceInfo provides a go interface for drivers to retrive
  64. // network information to interface resources.
  65. type InterfaceInfo interface {
  66. // MacAddress returns the MAC address.
  67. MacAddress() net.HardwareAddr
  68. // Address returns the IPv4 address.
  69. Address() net.IPNet
  70. // AddressIPv6 returns the IPv6 address.
  71. AddressIPv6() net.IPNet
  72. // ID returns the numerical id of the interface and has significance only within
  73. // the endpoint.
  74. ID() int
  75. }
  76. // InterfaceNameInfo provides a go interface for the drivers to assign names
  77. // to interfaces.
  78. type InterfaceNameInfo interface {
  79. // SetNames method assigns the srcName and dstName for the interface.
  80. SetNames(srcName, dstName string) error
  81. // ID returns the numerical id that was assigned to the interface by the driver
  82. // CreateEndpoint.
  83. ID() int
  84. }
  85. // JoinInfo represents a set of resources that the driver has the ability to provide during
  86. // join time.
  87. type JoinInfo interface {
  88. // InterfaceNames returns a list of InterfaceNameInfo go interface to facilitate
  89. // setting the names for the interfaces.
  90. InterfaceNames() []InterfaceNameInfo
  91. // SetGateway sets the default IPv4 gateway when a container joins the endpoint.
  92. SetGateway(net.IP) error
  93. // SetGatewayIPv6 sets the default IPv6 gateway when a container joins the endpoint.
  94. SetGatewayIPv6(net.IP) error
  95. // SetHostsPath sets the overriding /etc/hosts path to use for the container.
  96. SetHostsPath(string) error
  97. // SetResolvConfPath sets the overriding /etc/resolv.conf path to use for the container.
  98. SetResolvConfPath(string) error
  99. }
  100. // ErrActiveRegistration represents an error when a driver is registered to a networkType that is previously registered
  101. type ErrActiveRegistration string
  102. // Error interface for ErrActiveRegistration
  103. func (ar ErrActiveRegistration) Error() string {
  104. return fmt.Sprintf("Driver already registered for type %q", string(ar))
  105. }
  106. // DriverCallback provides a Callback interface for Drivers into LibNetwork
  107. type DriverCallback interface {
  108. // RegisterDriver provides a way for Remote drivers to dynamically register new NetworkType and associate with a driver instance
  109. RegisterDriver(name string, driver Driver) error
  110. }