controller.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. Package libnetwork provides the basic functionality and extension points to
  3. create network namespaces and allocate interfaces for containers to use.
  4. // Create a new controller instance
  5. controller := libnetwork.New()
  6. // Select and configure the network driver
  7. networkType := "bridge"
  8. driverOptions := options.Generic{}
  9. genericOption := make(map[string]interface{})
  10. genericOption[netlabel.GenericData] = driverOptions
  11. err := controller.ConfigureNetworkDriver(networkType, genericOption)
  12. if err != nil {
  13. return
  14. }
  15. // Create a network for containers to join.
  16. // NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can make of
  17. network, err := controller.NewNetwork(networkType, "network1")
  18. if err != nil {
  19. return
  20. }
  21. // For each new container: allocate IP and interfaces. The returned network
  22. // settings will be used for container infos (inspect and such), as well as
  23. // iptables rules for port publishing. This info is contained or accessible
  24. // from the returned endpoint.
  25. ep, err := network.CreateEndpoint("Endpoint1")
  26. if err != nil {
  27. return
  28. }
  29. // A container can join the endpoint by providing the container ID to the join
  30. // api which returns the sandbox key which can be used to access the sandbox
  31. // created for the container during join.
  32. // Join acceps Variadic arguments which will be made use of by libnetwork and Drivers
  33. _, err = ep.Join("container1",
  34. libnetwork.JoinOptionHostname("test"),
  35. libnetwork.JoinOptionDomainname("docker.io"))
  36. if err != nil {
  37. return
  38. }
  39. */
  40. package libnetwork
  41. import (
  42. "sync"
  43. "github.com/docker/docker/pkg/stringid"
  44. "github.com/docker/libnetwork/driverapi"
  45. "github.com/docker/libnetwork/sandbox"
  46. "github.com/docker/libnetwork/types"
  47. )
  48. // NetworkController provides the interface for controller instance which manages
  49. // networks.
  50. type NetworkController interface {
  51. // ConfigureNetworkDriver applies the passed options to the driver instance for the specified network type
  52. ConfigureNetworkDriver(networkType string, options map[string]interface{}) error
  53. // Create a new network. The options parameter carries network specific options.
  54. // Labels support will be added in the near future.
  55. NewNetwork(networkType, name string, options ...NetworkOption) (Network, error)
  56. // Networks returns the list of Network(s) managed by this controller.
  57. Networks() []Network
  58. // WalkNetworks uses the provided function to walk the Network(s) managed by this controller.
  59. WalkNetworks(walker NetworkWalker)
  60. // NetworkByName returns the Network which has the passed name, if it exists otherwise nil is returned
  61. NetworkByName(name string) (Network, error)
  62. // NetworkByID returns the Network which has the passed id, if it exists otherwise nil is returned
  63. NetworkByID(id string) (Network, error)
  64. }
  65. // NetworkWalker is a client provided function which will be used to walk the Networks.
  66. // When the function returns true, the walk will stop.
  67. type NetworkWalker func(nw Network) bool
  68. type sandboxData struct {
  69. sandbox sandbox.Sandbox
  70. refCnt int
  71. }
  72. type networkTable map[types.UUID]*network
  73. type endpointTable map[types.UUID]*endpoint
  74. type sandboxTable map[string]sandboxData
  75. type controller struct {
  76. networks networkTable
  77. drivers driverTable
  78. sandboxes sandboxTable
  79. sync.Mutex
  80. }
  81. // New creates a new instance of network controller.
  82. func New() NetworkController {
  83. c := &controller{networks: networkTable{}, sandboxes: sandboxTable{}}
  84. c.drivers = enumerateDrivers(c)
  85. return c
  86. }
  87. func (c *controller) ConfigureNetworkDriver(networkType string, options map[string]interface{}) error {
  88. c.Lock()
  89. d, ok := c.drivers[networkType]
  90. c.Unlock()
  91. if !ok {
  92. return NetworkTypeError(networkType)
  93. }
  94. return d.Config(options)
  95. }
  96. func (c *controller) RegisterDriver(networkType string, driver driverapi.Driver) error {
  97. c.Lock()
  98. defer c.Unlock()
  99. if _, ok := c.drivers[networkType]; ok {
  100. return driverapi.ErrActiveRegistration(networkType)
  101. }
  102. c.drivers[networkType] = driver
  103. return nil
  104. }
  105. // NewNetwork creates a new network of the specified network type. The options
  106. // are network specific and modeled in a generic way.
  107. func (c *controller) NewNetwork(networkType, name string, options ...NetworkOption) (Network, error) {
  108. if name == "" {
  109. return nil, ErrInvalidName
  110. }
  111. // Check if a driver for the specified network type is available
  112. c.Lock()
  113. d, ok := c.drivers[networkType]
  114. c.Unlock()
  115. if !ok {
  116. return nil, ErrInvalidNetworkDriver
  117. }
  118. // Check if a network already exists with the specified network name
  119. c.Lock()
  120. for _, n := range c.networks {
  121. if n.name == name {
  122. c.Unlock()
  123. return nil, NetworkNameError(name)
  124. }
  125. }
  126. c.Unlock()
  127. // Construct the network object
  128. network := &network{
  129. name: name,
  130. id: types.UUID(stringid.GenerateRandomID()),
  131. ctrlr: c,
  132. driver: d,
  133. endpoints: endpointTable{},
  134. }
  135. network.processOptions(options...)
  136. // Create the network
  137. if err := d.CreateNetwork(network.id, network.generic); err != nil {
  138. return nil, err
  139. }
  140. // Store the network handler in controller
  141. c.Lock()
  142. c.networks[network.id] = network
  143. c.Unlock()
  144. return network, nil
  145. }
  146. func (c *controller) Networks() []Network {
  147. c.Lock()
  148. defer c.Unlock()
  149. list := make([]Network, 0, len(c.networks))
  150. for _, n := range c.networks {
  151. list = append(list, n)
  152. }
  153. return list
  154. }
  155. func (c *controller) WalkNetworks(walker NetworkWalker) {
  156. for _, n := range c.Networks() {
  157. if walker(n) {
  158. return
  159. }
  160. }
  161. }
  162. func (c *controller) NetworkByName(name string) (Network, error) {
  163. if name == "" {
  164. return nil, ErrInvalidName
  165. }
  166. var n Network
  167. s := func(current Network) bool {
  168. if current.Name() == name {
  169. n = current
  170. return true
  171. }
  172. return false
  173. }
  174. c.WalkNetworks(s)
  175. return n, nil
  176. }
  177. func (c *controller) NetworkByID(id string) (Network, error) {
  178. if id == "" {
  179. return nil, ErrInvalidID
  180. }
  181. c.Lock()
  182. defer c.Unlock()
  183. if n, ok := c.networks[types.UUID(id)]; ok {
  184. return n, nil
  185. }
  186. return nil, nil
  187. }
  188. func (c *controller) sandboxAdd(key string, create bool) (sandbox.Sandbox, error) {
  189. c.Lock()
  190. defer c.Unlock()
  191. sData, ok := c.sandboxes[key]
  192. if !ok {
  193. sb, err := sandbox.NewSandbox(key, create)
  194. if err != nil {
  195. return nil, err
  196. }
  197. sData = sandboxData{sandbox: sb, refCnt: 1}
  198. c.sandboxes[key] = sData
  199. return sData.sandbox, nil
  200. }
  201. sData.refCnt++
  202. return sData.sandbox, nil
  203. }
  204. func (c *controller) sandboxRm(key string) {
  205. c.Lock()
  206. defer c.Unlock()
  207. sData := c.sandboxes[key]
  208. sData.refCnt--
  209. if sData.refCnt == 0 {
  210. sData.sandbox.Destroy()
  211. delete(c.sandboxes, key)
  212. }
  213. }
  214. func (c *controller) sandboxGet(key string) sandbox.Sandbox {
  215. c.Lock()
  216. defer c.Unlock()
  217. sData, ok := c.sandboxes[key]
  218. if !ok {
  219. return nil
  220. }
  221. return sData.sandbox
  222. }