controller.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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/plugins"
  44. "github.com/docker/docker/pkg/stringid"
  45. "github.com/docker/libnetwork/driverapi"
  46. "github.com/docker/libnetwork/sandbox"
  47. "github.com/docker/libnetwork/types"
  48. )
  49. // NetworkController provides the interface for controller instance which manages
  50. // networks.
  51. type NetworkController interface {
  52. // ConfigureNetworkDriver applies the passed options to the driver instance for the specified network type
  53. ConfigureNetworkDriver(networkType string, options map[string]interface{}) error
  54. // Create a new network. The options parameter carries network specific options.
  55. // Labels support will be added in the near future.
  56. NewNetwork(networkType, name string, options ...NetworkOption) (Network, error)
  57. // Networks returns the list of Network(s) managed by this controller.
  58. Networks() []Network
  59. // WalkNetworks uses the provided function to walk the Network(s) managed by this controller.
  60. WalkNetworks(walker NetworkWalker)
  61. // NetworkByName returns the Network which has the passed name. If not found, the error ErrNoSuchNetwork is returned.
  62. NetworkByName(name string) (Network, error)
  63. // NetworkByID returns the Network which has the passed id. If not found, the error ErrNoSuchNetwork is returned.
  64. NetworkByID(id string) (Network, error)
  65. }
  66. // NetworkWalker is a client provided function which will be used to walk the Networks.
  67. // When the function returns true, the walk will stop.
  68. type NetworkWalker func(nw Network) bool
  69. type sandboxData struct {
  70. sandbox sandbox.Sandbox
  71. refCnt int
  72. }
  73. type networkTable map[types.UUID]*network
  74. type endpointTable map[types.UUID]*endpoint
  75. type sandboxTable map[string]*sandboxData
  76. type controller struct {
  77. networks networkTable
  78. drivers driverTable
  79. sandboxes sandboxTable
  80. sync.Mutex
  81. }
  82. // New creates a new instance of network controller.
  83. func New() (NetworkController, error) {
  84. c := &controller{
  85. networks: networkTable{},
  86. sandboxes: sandboxTable{},
  87. drivers: driverTable{}}
  88. if err := initDrivers(c); err != nil {
  89. return nil, err
  90. }
  91. return c, nil
  92. }
  93. func (c *controller) ConfigureNetworkDriver(networkType string, options map[string]interface{}) error {
  94. c.Lock()
  95. d, ok := c.drivers[networkType]
  96. c.Unlock()
  97. if !ok {
  98. return NetworkTypeError(networkType)
  99. }
  100. return d.Config(options)
  101. }
  102. func (c *controller) RegisterDriver(networkType string, driver driverapi.Driver) error {
  103. c.Lock()
  104. defer c.Unlock()
  105. if _, ok := c.drivers[networkType]; ok {
  106. return driverapi.ErrActiveRegistration(networkType)
  107. }
  108. c.drivers[networkType] = driver
  109. return nil
  110. }
  111. // NewNetwork creates a new network of the specified network type. The options
  112. // are network specific and modeled in a generic way.
  113. func (c *controller) NewNetwork(networkType, name string, options ...NetworkOption) (Network, error) {
  114. if name == "" {
  115. return nil, ErrInvalidName(name)
  116. }
  117. // Check if a driver for the specified network type is available
  118. c.Lock()
  119. d, ok := c.drivers[networkType]
  120. c.Unlock()
  121. if !ok {
  122. var err error
  123. d, err = c.loadDriver(networkType)
  124. if err != nil {
  125. return nil, err
  126. }
  127. }
  128. // Check if a network already exists with the specified network name
  129. c.Lock()
  130. for _, n := range c.networks {
  131. if n.name == name {
  132. c.Unlock()
  133. return nil, NetworkNameError(name)
  134. }
  135. }
  136. c.Unlock()
  137. // Construct the network object
  138. network := &network{
  139. name: name,
  140. id: types.UUID(stringid.GenerateRandomID()),
  141. ctrlr: c,
  142. driver: d,
  143. endpoints: endpointTable{},
  144. }
  145. network.processOptions(options...)
  146. // Create the network
  147. if err := d.CreateNetwork(network.id, network.generic); err != nil {
  148. return nil, err
  149. }
  150. // Store the network handler in controller
  151. c.Lock()
  152. c.networks[network.id] = network
  153. c.Unlock()
  154. return network, nil
  155. }
  156. func (c *controller) Networks() []Network {
  157. c.Lock()
  158. defer c.Unlock()
  159. list := make([]Network, 0, len(c.networks))
  160. for _, n := range c.networks {
  161. list = append(list, n)
  162. }
  163. return list
  164. }
  165. func (c *controller) WalkNetworks(walker NetworkWalker) {
  166. for _, n := range c.Networks() {
  167. if walker(n) {
  168. return
  169. }
  170. }
  171. }
  172. func (c *controller) NetworkByName(name string) (Network, error) {
  173. if name == "" {
  174. return nil, ErrInvalidName(name)
  175. }
  176. var n Network
  177. s := func(current Network) bool {
  178. if current.Name() == name {
  179. n = current
  180. return true
  181. }
  182. return false
  183. }
  184. c.WalkNetworks(s)
  185. if n == nil {
  186. return nil, ErrNoSuchNetwork(name)
  187. }
  188. return n, nil
  189. }
  190. func (c *controller) NetworkByID(id string) (Network, error) {
  191. if id == "" {
  192. return nil, ErrInvalidID(id)
  193. }
  194. c.Lock()
  195. defer c.Unlock()
  196. if n, ok := c.networks[types.UUID(id)]; ok {
  197. return n, nil
  198. }
  199. return nil, ErrNoSuchNetwork(id)
  200. }
  201. func (c *controller) sandboxAdd(key string, create bool) (sandbox.Sandbox, error) {
  202. c.Lock()
  203. defer c.Unlock()
  204. sData, ok := c.sandboxes[key]
  205. if !ok {
  206. sb, err := sandbox.NewSandbox(key, create)
  207. if err != nil {
  208. return nil, err
  209. }
  210. sData = &sandboxData{sandbox: sb, refCnt: 1}
  211. c.sandboxes[key] = sData
  212. return sData.sandbox, nil
  213. }
  214. sData.refCnt++
  215. return sData.sandbox, nil
  216. }
  217. func (c *controller) sandboxRm(key string) {
  218. c.Lock()
  219. defer c.Unlock()
  220. sData := c.sandboxes[key]
  221. sData.refCnt--
  222. if sData.refCnt == 0 {
  223. sData.sandbox.Destroy()
  224. delete(c.sandboxes, key)
  225. }
  226. }
  227. func (c *controller) sandboxGet(key string) sandbox.Sandbox {
  228. c.Lock()
  229. defer c.Unlock()
  230. sData, ok := c.sandboxes[key]
  231. if !ok {
  232. return nil
  233. }
  234. return sData.sandbox
  235. }
  236. func (c *controller) loadDriver(networkType string) (driverapi.Driver, error) {
  237. // Plugins pkg performs lazy loading of plugins that acts as remote drivers.
  238. // As per the design, this Get call will result in remote driver discovery if there is a corresponding plugin available.
  239. _, err := plugins.Get(networkType, driverapi.NetworkPluginEndpointType)
  240. if err != nil {
  241. if err == plugins.ErrNotFound {
  242. return nil, types.NotFoundErrorf(err.Error())
  243. }
  244. return nil, err
  245. }
  246. c.Lock()
  247. defer c.Unlock()
  248. d, ok := c.drivers[networkType]
  249. if !ok {
  250. return nil, ErrInvalidNetworkDriver(networkType)
  251. }
  252. return d, nil
  253. }