controller.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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, _err := 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, error) {
  83. c := &controller{
  84. networks: networkTable{},
  85. sandboxes: sandboxTable{},
  86. drivers: driverTable{}}
  87. if err := initDrivers(c); err != nil {
  88. return nil, err
  89. }
  90. return c, nil
  91. }
  92. func (c *controller) ConfigureNetworkDriver(networkType string, options map[string]interface{}) error {
  93. c.Lock()
  94. d, ok := c.drivers[networkType]
  95. c.Unlock()
  96. if !ok {
  97. return NetworkTypeError(networkType)
  98. }
  99. return d.Config(options)
  100. }
  101. func (c *controller) RegisterDriver(networkType string, driver driverapi.Driver) error {
  102. c.Lock()
  103. defer c.Unlock()
  104. if _, ok := c.drivers[networkType]; ok {
  105. return driverapi.ErrActiveRegistration(networkType)
  106. }
  107. c.drivers[networkType] = driver
  108. return nil
  109. }
  110. // NewNetwork creates a new network of the specified network type. The options
  111. // are network specific and modeled in a generic way.
  112. func (c *controller) NewNetwork(networkType, name string, options ...NetworkOption) (Network, error) {
  113. if name == "" {
  114. return nil, ErrInvalidName
  115. }
  116. // Check if a driver for the specified network type is available
  117. c.Lock()
  118. d, ok := c.drivers[networkType]
  119. c.Unlock()
  120. if !ok {
  121. return nil, ErrInvalidNetworkDriver
  122. }
  123. // Check if a network already exists with the specified network name
  124. c.Lock()
  125. for _, n := range c.networks {
  126. if n.name == name {
  127. c.Unlock()
  128. return nil, NetworkNameError(name)
  129. }
  130. }
  131. c.Unlock()
  132. // Construct the network object
  133. network := &network{
  134. name: name,
  135. id: types.UUID(stringid.GenerateRandomID()),
  136. ctrlr: c,
  137. driver: d,
  138. endpoints: endpointTable{},
  139. }
  140. network.processOptions(options...)
  141. // Create the network
  142. if err := d.CreateNetwork(network.id, network.generic); err != nil {
  143. return nil, err
  144. }
  145. // Store the network handler in controller
  146. c.Lock()
  147. c.networks[network.id] = network
  148. c.Unlock()
  149. return network, nil
  150. }
  151. func (c *controller) Networks() []Network {
  152. c.Lock()
  153. defer c.Unlock()
  154. list := make([]Network, 0, len(c.networks))
  155. for _, n := range c.networks {
  156. list = append(list, n)
  157. }
  158. return list
  159. }
  160. func (c *controller) WalkNetworks(walker NetworkWalker) {
  161. for _, n := range c.Networks() {
  162. if walker(n) {
  163. return
  164. }
  165. }
  166. }
  167. func (c *controller) NetworkByName(name string) (Network, error) {
  168. if name == "" {
  169. return nil, ErrInvalidName
  170. }
  171. var n Network
  172. s := func(current Network) bool {
  173. if current.Name() == name {
  174. n = current
  175. return true
  176. }
  177. return false
  178. }
  179. c.WalkNetworks(s)
  180. return n, nil
  181. }
  182. func (c *controller) NetworkByID(id string) (Network, error) {
  183. if id == "" {
  184. return nil, ErrInvalidID
  185. }
  186. c.Lock()
  187. defer c.Unlock()
  188. if n, ok := c.networks[types.UUID(id)]; ok {
  189. return n, nil
  190. }
  191. return nil, nil
  192. }
  193. func (c *controller) sandboxAdd(key string, create bool) (sandbox.Sandbox, error) {
  194. c.Lock()
  195. defer c.Unlock()
  196. sData, ok := c.sandboxes[key]
  197. if !ok {
  198. sb, err := sandbox.NewSandbox(key, create)
  199. if err != nil {
  200. return nil, err
  201. }
  202. sData = sandboxData{sandbox: sb, refCnt: 1}
  203. c.sandboxes[key] = sData
  204. return sData.sandbox, nil
  205. }
  206. sData.refCnt++
  207. return sData.sandbox, nil
  208. }
  209. func (c *controller) sandboxRm(key string) {
  210. c.Lock()
  211. defer c.Unlock()
  212. sData := c.sandboxes[key]
  213. sData.refCnt--
  214. if sData.refCnt == 0 {
  215. sData.sandbox.Destroy()
  216. delete(c.sandboxes, key)
  217. }
  218. }
  219. func (c *controller) sandboxGet(key string) sandbox.Sandbox {
  220. c.Lock()
  221. defer c.Unlock()
  222. sData, ok := c.sandboxes[key]
  223. if !ok {
  224. return nil
  225. }
  226. return sData.sandbox
  227. }