controller.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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/sandbox"
  45. "github.com/docker/libnetwork/types"
  46. )
  47. // NetworkController provides the interface for controller instance which manages
  48. // networks.
  49. type NetworkController interface {
  50. // ConfigureNetworkDriver applies the passed options to the driver instance for the specified network type
  51. ConfigureNetworkDriver(networkType string, options map[string]interface{}) error
  52. // Create a new network. The options parameter carries network specific options.
  53. // Labels support will be added in the near future.
  54. NewNetwork(networkType, name string, options ...NetworkOption) (Network, error)
  55. // Networks returns the list of Network(s) managed by this controller.
  56. Networks() []Network
  57. // WalkNetworks uses the provided function to walk the Network(s) managed by this controller.
  58. WalkNetworks(walker NetworkWalker)
  59. // NetworkByName returns the Network which has the passed name, if it exists otherwise nil is returned
  60. NetworkByName(name string) Network
  61. // NetworkByID returns the Network which has the passed id, if it exists otherwise nil is returned
  62. NetworkByID(id string) Network
  63. }
  64. // NetworkWalker is a client provided function which will be used to walk the Networks.
  65. // When the function returns true, the walk will stop.
  66. type NetworkWalker func(nw Network) bool
  67. type sandboxData struct {
  68. sandbox sandbox.Sandbox
  69. refCnt int
  70. }
  71. type networkTable map[types.UUID]*network
  72. type endpointTable map[types.UUID]*endpoint
  73. type sandboxTable map[string]sandboxData
  74. type controller struct {
  75. networks networkTable
  76. drivers driverTable
  77. sandboxes sandboxTable
  78. sync.Mutex
  79. }
  80. // New creates a new instance of network controller.
  81. func New() NetworkController {
  82. return &controller{networkTable{}, enumerateDrivers(), sandboxTable{}, sync.Mutex{}}
  83. }
  84. func (c *controller) ConfigureNetworkDriver(networkType string, options map[string]interface{}) error {
  85. d, ok := c.drivers[networkType]
  86. if !ok {
  87. return NetworkTypeError(networkType)
  88. }
  89. return d.Config(options)
  90. }
  91. // NewNetwork creates a new network of the specified network type. The options
  92. // are network specific and modeled in a generic way.
  93. func (c *controller) NewNetwork(networkType, name string, options ...NetworkOption) (Network, error) {
  94. // Check if a driver for the specified network type is available
  95. d, ok := c.drivers[networkType]
  96. if !ok {
  97. return nil, ErrInvalidNetworkDriver
  98. }
  99. // Check if a network already exists with the specified network name
  100. c.Lock()
  101. for _, n := range c.networks {
  102. if n.name == name {
  103. c.Unlock()
  104. return nil, NetworkNameError(name)
  105. }
  106. }
  107. c.Unlock()
  108. // Construct the network object
  109. network := &network{
  110. name: name,
  111. id: types.UUID(stringid.GenerateRandomID()),
  112. ctrlr: c,
  113. driver: d,
  114. endpoints: endpointTable{},
  115. }
  116. network.processOptions(options...)
  117. // Create the network
  118. if err := d.CreateNetwork(network.id, network.generic); err != nil {
  119. return nil, err
  120. }
  121. // Store the network handler in controller
  122. c.Lock()
  123. c.networks[network.id] = network
  124. c.Unlock()
  125. return network, nil
  126. }
  127. func (c *controller) Networks() []Network {
  128. c.Lock()
  129. defer c.Unlock()
  130. list := make([]Network, 0, len(c.networks))
  131. for _, n := range c.networks {
  132. list = append(list, n)
  133. }
  134. return list
  135. }
  136. func (c *controller) WalkNetworks(walker NetworkWalker) {
  137. for _, n := range c.Networks() {
  138. if walker(n) {
  139. return
  140. }
  141. }
  142. }
  143. func (c *controller) NetworkByName(name string) Network {
  144. var n Network
  145. if name != "" {
  146. s := func(current Network) bool {
  147. if current.Name() == name {
  148. n = current
  149. return true
  150. }
  151. return false
  152. }
  153. c.WalkNetworks(s)
  154. }
  155. return n
  156. }
  157. func (c *controller) NetworkByID(id string) Network {
  158. c.Lock()
  159. defer c.Unlock()
  160. if n, ok := c.networks[types.UUID(id)]; ok {
  161. return n
  162. }
  163. return nil
  164. }
  165. func (c *controller) sandboxAdd(key string, create bool) (sandbox.Sandbox, error) {
  166. c.Lock()
  167. defer c.Unlock()
  168. sData, ok := c.sandboxes[key]
  169. if !ok {
  170. sb, err := sandbox.NewSandbox(key, create)
  171. if err != nil {
  172. return nil, err
  173. }
  174. sData = sandboxData{sandbox: sb, refCnt: 1}
  175. c.sandboxes[key] = sData
  176. return sData.sandbox, nil
  177. }
  178. sData.refCnt++
  179. return sData.sandbox, nil
  180. }
  181. func (c *controller) sandboxRm(key string) {
  182. c.Lock()
  183. defer c.Unlock()
  184. sData := c.sandboxes[key]
  185. sData.refCnt--
  186. if sData.refCnt == 0 {
  187. sData.sandbox.Destroy()
  188. delete(c.sandboxes, key)
  189. }
  190. }
  191. func (c *controller) sandboxGet(key string) sandbox.Sandbox {
  192. c.Lock()
  193. defer c.Unlock()
  194. sData, ok := c.sandboxes[key]
  195. if !ok {
  196. return nil
  197. }
  198. return sData.sandbox
  199. }