driverapi.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package driverapi
  2. import "net"
  3. // NetworkPluginEndpointType represents the Endpoint Type used by Plugin system
  4. const NetworkPluginEndpointType = "NetworkDriver"
  5. // Driver is an interface that every plugin driver needs to implement.
  6. type Driver interface {
  7. // NetworkAllocate invokes the driver method to allocate network
  8. // specific resources passing network id and network specific config.
  9. // It returns a key,value pair of network specific driver allocations
  10. // to the caller.
  11. NetworkAllocate(nid string, options map[string]string, ipV4Data, ipV6Data []IPAMData) (map[string]string, error)
  12. // NetworkFree invokes the driver method to free network specific resources
  13. // associated with a given network id.
  14. NetworkFree(nid string) error
  15. // CreateNetwork invokes the driver method to create a network
  16. // passing the network id and network specific config. The
  17. // config mechanism will eventually be replaced with labels
  18. // which are yet to be introduced. The driver can return a
  19. // list of table names for which it is interested in receiving
  20. // notification when a CRUD operation is performed on any
  21. // entry in that table. This will be ignored for local scope
  22. // drivers.
  23. CreateNetwork(nid string, options map[string]interface{}, nInfo NetworkInfo, ipV4Data, ipV6Data []IPAMData) error
  24. // DeleteNetwork invokes the driver method to delete network passing
  25. // the network id.
  26. DeleteNetwork(nid string) error
  27. // CreateEndpoint invokes the driver method to create an endpoint
  28. // passing the network id, endpoint id endpoint information and driver
  29. // specific config. The endpoint information can be either consumed by
  30. // the driver or populated by the driver. The config mechanism will
  31. // eventually be replaced with labels which are yet to be introduced.
  32. CreateEndpoint(nid, eid string, ifInfo InterfaceInfo, options map[string]interface{}) error
  33. // DeleteEndpoint invokes the driver method to delete an endpoint
  34. // passing the network id and endpoint id.
  35. DeleteEndpoint(nid, eid string) error
  36. // EndpointOperInfo retrieves from the driver the operational data related to the specified endpoint
  37. EndpointOperInfo(nid, eid string) (map[string]interface{}, error)
  38. // Join method is invoked when a Sandbox is attached to an endpoint.
  39. Join(nid, eid string, sboxKey string, jinfo JoinInfo, options map[string]interface{}) error
  40. // Leave method is invoked when a Sandbox detaches from an endpoint.
  41. Leave(nid, eid string) error
  42. // ProgramExternalConnectivity invokes the driver method which does the necessary
  43. // programming to allow the external connectivity dictated by the passed options
  44. ProgramExternalConnectivity(nid, eid string, options map[string]interface{}) error
  45. // RevokeExternalConnectivity asks the driver to remove any external connectivity
  46. // programming that was done so far
  47. RevokeExternalConnectivity(nid, eid string) error
  48. // EventNotify notifies the driver when a CRUD operation has
  49. // happened on a table of its interest as soon as this node
  50. // receives such an event in the gossip layer. This method is
  51. // only invoked for the global scope driver.
  52. EventNotify(event EventType, nid string, tableName string, key string, value []byte)
  53. // DecodeTableEntry passes the driver a key, value pair from table it registered
  54. // with libnetwork. Driver should return {object ID, map[string]string} tuple.
  55. // If DecodeTableEntry is called for a table associated with NetworkObject or
  56. // EndpointObject the return object ID should be the network id or endpoint id
  57. // associated with that entry. map should have information about the object that
  58. // can be presented to the user.
  59. // For example: overlay driver returns the VTEP IP of the host that has the endpoint
  60. // which is shown in 'network inspect --verbose'
  61. DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string)
  62. // Type returns the type of this driver, the network type this driver manages
  63. Type() string
  64. // IsBuiltIn returns true if it is a built-in driver
  65. IsBuiltIn() bool
  66. }
  67. // NetworkInfo provides a go interface for drivers to provide network
  68. // specific information to libnetwork.
  69. type NetworkInfo interface {
  70. // TableEventRegister registers driver interest in a given
  71. // table name.
  72. TableEventRegister(tableName string, objType ObjectType) error
  73. // UpdateIPamConfig updates the networks IPAM configuration
  74. // based on information from the driver. In windows, the OS (HNS) chooses
  75. // the IP address space if the user does not specify an address space.
  76. UpdateIpamConfig(ipV4Data []IPAMData)
  77. }
  78. // InterfaceInfo provides a go interface for drivers to retrieve
  79. // network information to interface resources.
  80. type InterfaceInfo interface {
  81. // SetMacAddress allows the driver to set the mac address to the endpoint interface
  82. // during the call to CreateEndpoint, if the mac address is not already set.
  83. SetMacAddress(mac net.HardwareAddr) error
  84. // SetIPAddress allows the driver to set the ip address to the endpoint interface
  85. // during the call to CreateEndpoint, if the address is not already set.
  86. // The API is to be used to assign both the IPv4 and IPv6 address types.
  87. SetIPAddress(ip *net.IPNet) error
  88. // MacAddress returns the MAC address.
  89. MacAddress() net.HardwareAddr
  90. // Address returns the IPv4 address.
  91. Address() *net.IPNet
  92. // AddressIPv6 returns the IPv6 address.
  93. AddressIPv6() *net.IPNet
  94. }
  95. // InterfaceNameInfo provides a go interface for the drivers to assign names
  96. // to interfaces.
  97. type InterfaceNameInfo interface {
  98. // SetNames method assigns the srcName and dstPrefix for the interface.
  99. SetNames(srcName, dstPrefix string) error
  100. }
  101. // JoinInfo represents a set of resources that the driver has the ability to provide during
  102. // join time.
  103. type JoinInfo interface {
  104. // InterfaceName returns an InterfaceNameInfo go interface to facilitate
  105. // setting the names for the interface.
  106. InterfaceName() InterfaceNameInfo
  107. // SetGateway sets the default IPv4 gateway when a container joins the endpoint.
  108. SetGateway(net.IP) error
  109. // SetGatewayIPv6 sets the default IPv6 gateway when a container joins the endpoint.
  110. SetGatewayIPv6(net.IP) error
  111. // AddStaticRoute adds a route to the sandbox.
  112. // It may be used in addition to or instead of a default gateway (as above).
  113. AddStaticRoute(destination *net.IPNet, routeType int, nextHop net.IP) error
  114. // DisableGatewayService tells libnetwork not to provide Default GW for the container
  115. DisableGatewayService()
  116. // AddTableEntry adds a table entry to the gossip layer
  117. // passing the table name, key and an opaque value.
  118. AddTableEntry(tableName string, key string, value []byte) error
  119. }
  120. // Registerer provides a way for network drivers to be dynamically registered.
  121. type Registerer interface {
  122. RegisterDriver(name string, driver Driver, capability Capability) error
  123. }
  124. // Capability represents the high level capabilities of the drivers which libnetwork can make use of
  125. type Capability struct {
  126. DataScope string
  127. ConnectivityScope string
  128. }
  129. // IPAMData represents the per-network ip related
  130. // operational information libnetwork will send
  131. // to the network driver during CreateNetwork()
  132. type IPAMData struct {
  133. AddressSpace string
  134. Pool *net.IPNet
  135. Gateway *net.IPNet
  136. AuxAddresses map[string]*net.IPNet
  137. }
  138. // EventType defines a type for the CRUD event
  139. type EventType uint8
  140. const (
  141. // Create event is generated when a table entry is created,
  142. Create EventType = 1 + iota
  143. // Update event is generated when a table entry is updated.
  144. Update
  145. // Delete event is generated when a table entry is deleted.
  146. Delete
  147. )
  148. // ObjectType represents the type of object driver wants to store in libnetwork's networkDB
  149. type ObjectType int
  150. const (
  151. // EndpointObject should be set for libnetwork endpoint object related data
  152. EndpointObject ObjectType = 1 + iota
  153. // NetworkObject should be set for libnetwork network object related data
  154. NetworkObject
  155. // OpaqueObject is for driver specific data with no corresponding libnetwork object
  156. OpaqueObject
  157. )
  158. // IsValidType validates the passed in type against the valid object types
  159. func IsValidType(objType ObjectType) bool {
  160. switch objType {
  161. case EndpointObject:
  162. fallthrough
  163. case NetworkObject:
  164. fallthrough
  165. case OpaqueObject:
  166. return true
  167. }
  168. return false
  169. }