controller.go 5.6 KB

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