controller.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. /*
  2. Package libnetwork provides the basic functionality and extension points to
  3. create network namespaces and allocate interfaces for containers to use.
  4. networkType := "bridge"
  5. // Create a new controller instance
  6. driverOptions := options.Generic{}
  7. genericOption := make(map[string]interface{})
  8. genericOption[netlabel.GenericData] = driverOptions
  9. controller, err := libnetwork.New(config.OptionDriverConfig(networkType, genericOption))
  10. if err != nil {
  11. return
  12. }
  13. // Create a network for containers to join.
  14. // NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can make use of
  15. network, err := controller.NewNetwork(networkType, "network1", "")
  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")
  24. if err != nil {
  25. return
  26. }
  27. // Create the sandbox for the container.
  28. // NewSandbox accepts Variadic optional arguments which libnetwork can use.
  29. sbx, err := controller.NewSandbox("container1",
  30. libnetwork.OptionHostname("test"),
  31. libnetwork.OptionDomainname("docker.io"))
  32. // A sandbox can join the endpoint via the join api.
  33. err = ep.Join(sbx)
  34. if err != nil {
  35. return
  36. }
  37. */
  38. package libnetwork
  39. import (
  40. "container/heap"
  41. "fmt"
  42. "net"
  43. "strings"
  44. "sync"
  45. log "github.com/Sirupsen/logrus"
  46. "github.com/docker/docker/pkg/discovery"
  47. "github.com/docker/docker/pkg/plugins"
  48. "github.com/docker/docker/pkg/stringid"
  49. "github.com/docker/libnetwork/cluster"
  50. "github.com/docker/libnetwork/config"
  51. "github.com/docker/libnetwork/datastore"
  52. "github.com/docker/libnetwork/discoverapi"
  53. "github.com/docker/libnetwork/driverapi"
  54. "github.com/docker/libnetwork/drvregistry"
  55. "github.com/docker/libnetwork/hostdiscovery"
  56. "github.com/docker/libnetwork/ipamapi"
  57. "github.com/docker/libnetwork/netlabel"
  58. "github.com/docker/libnetwork/osl"
  59. "github.com/docker/libnetwork/types"
  60. )
  61. // NetworkController provides the interface for controller instance which manages
  62. // networks.
  63. type NetworkController interface {
  64. // ID provides a unique identity for the controller
  65. ID() string
  66. // Config method returns the bootup configuration for the controller
  67. Config() config.Config
  68. // Create a new network. The options parameter carries network specific options.
  69. NewNetwork(networkType, name string, id string, options ...NetworkOption) (Network, error)
  70. // Networks returns the list of Network(s) managed by this controller.
  71. Networks() []Network
  72. // WalkNetworks uses the provided function to walk the Network(s) managed by this controller.
  73. WalkNetworks(walker NetworkWalker)
  74. // NetworkByName returns the Network which has the passed name. If not found, the error ErrNoSuchNetwork is returned.
  75. NetworkByName(name string) (Network, error)
  76. // NetworkByID returns the Network which has the passed id. If not found, the error ErrNoSuchNetwork is returned.
  77. NetworkByID(id string) (Network, error)
  78. // NewSandbox creates a new network sandbox for the passed container id
  79. NewSandbox(containerID string, options ...SandboxOption) (Sandbox, error)
  80. // Sandboxes returns the list of Sandbox(s) managed by this controller.
  81. Sandboxes() []Sandbox
  82. // WlakSandboxes uses the provided function to walk the Sandbox(s) managed by this controller.
  83. WalkSandboxes(walker SandboxWalker)
  84. // SandboxByID returns the Sandbox which has the passed id. If not found, a types.NotFoundError is returned.
  85. SandboxByID(id string) (Sandbox, error)
  86. // SandboxDestroy destroys a sandbox given a container ID
  87. SandboxDestroy(id string) error
  88. // Stop network controller
  89. Stop()
  90. // ReloadCondfiguration updates the controller configuration
  91. ReloadConfiguration(cfgOptions ...config.Option) error
  92. // SetClusterProvider sets cluster provider
  93. SetClusterProvider(provider cluster.Provider)
  94. // Wait for agent initialization complete in libnetwork controller
  95. AgentInitWait()
  96. // SetKeys configures the encryption key for gossip and overlay data path
  97. SetKeys(keys []*types.EncryptionKey) error
  98. }
  99. // NetworkWalker is a client provided function which will be used to walk the Networks.
  100. // When the function returns true, the walk will stop.
  101. type NetworkWalker func(nw Network) bool
  102. // SandboxWalker is a client provided function which will be used to walk the Sandboxes.
  103. // When the function returns true, the walk will stop.
  104. type SandboxWalker func(sb Sandbox) bool
  105. type sandboxTable map[string]*sandbox
  106. type controller struct {
  107. id string
  108. drvRegistry *drvregistry.DrvRegistry
  109. sandboxes sandboxTable
  110. cfg *config.Config
  111. stores []datastore.DataStore
  112. discovery hostdiscovery.HostDiscovery
  113. extKeyListener net.Listener
  114. watchCh chan *endpoint
  115. unWatchCh chan *endpoint
  116. svcRecords map[string]svcInfo
  117. nmap map[string]*netWatch
  118. serviceBindings map[string]*service
  119. defOsSbox osl.Sandbox
  120. ingressSandbox *sandbox
  121. sboxOnce sync.Once
  122. agent *agent
  123. agentInitDone chan struct{}
  124. keys []*types.EncryptionKey
  125. clusterConfigAvailable bool
  126. sync.Mutex
  127. }
  128. type initializer struct {
  129. fn drvregistry.InitFunc
  130. ntype string
  131. }
  132. // New creates a new instance of network controller.
  133. func New(cfgOptions ...config.Option) (NetworkController, error) {
  134. c := &controller{
  135. id: stringid.GenerateRandomID(),
  136. cfg: config.ParseConfigOptions(cfgOptions...),
  137. sandboxes: sandboxTable{},
  138. svcRecords: make(map[string]svcInfo),
  139. serviceBindings: make(map[string]*service),
  140. agentInitDone: make(chan struct{}),
  141. }
  142. if err := c.initStores(); err != nil {
  143. return nil, err
  144. }
  145. drvRegistry, err := drvregistry.New(c.getStore(datastore.LocalScope), c.getStore(datastore.GlobalScope), c.RegisterDriver, nil)
  146. if err != nil {
  147. return nil, err
  148. }
  149. for _, i := range getInitializers() {
  150. var dcfg map[string]interface{}
  151. // External plugins don't need config passed through daemon. They can
  152. // bootstrap themselves
  153. if i.ntype != "remote" {
  154. dcfg = c.makeDriverConfig(i.ntype)
  155. }
  156. if err := drvRegistry.AddDriver(i.ntype, i.fn, dcfg); err != nil {
  157. return nil, err
  158. }
  159. }
  160. c.drvRegistry = drvRegistry
  161. if c.cfg != nil && c.cfg.Cluster.Watcher != nil {
  162. if err := c.initDiscovery(c.cfg.Cluster.Watcher); err != nil {
  163. // Failing to initialize discovery is a bad situation to be in.
  164. // But it cannot fail creating the Controller
  165. log.Errorf("Failed to Initialize Discovery : %v", err)
  166. }
  167. }
  168. // Reserve pools first before doing cleanup. Otherwise the
  169. // cleanups of endpoint/network and sandbox below will
  170. // generate many unnecessary warnings
  171. c.reservePools()
  172. // Cleanup resources
  173. c.sandboxCleanup(c.cfg.ActiveSandboxes)
  174. c.cleanupLocalEndpoints()
  175. c.networkCleanup()
  176. if err := c.startExternalKeyListener(); err != nil {
  177. return nil, err
  178. }
  179. return c, nil
  180. }
  181. func (c *controller) SetClusterProvider(provider cluster.Provider) {
  182. c.Lock()
  183. defer c.Unlock()
  184. c.cfg.Daemon.ClusterProvider = provider
  185. if provider != nil {
  186. go c.clusterAgentInit()
  187. } else {
  188. c.cfg.Daemon.DisableProvider <- struct{}{}
  189. }
  190. }
  191. func isValidClusteringIP(addr string) bool {
  192. return addr != "" && !net.ParseIP(addr).IsLoopback() && !net.ParseIP(addr).IsUnspecified()
  193. }
  194. // libnetwork side of agent depends on the keys. On the first receipt of
  195. // keys setup the agent. For subsequent key set handle the key change
  196. func (c *controller) SetKeys(keys []*types.EncryptionKey) error {
  197. c.Lock()
  198. existingKeys := c.keys
  199. clusterConfigAvailable := c.clusterConfigAvailable
  200. agent := c.agent
  201. c.Unlock()
  202. if len(existingKeys) == 0 {
  203. c.Lock()
  204. c.keys = keys
  205. c.Unlock()
  206. if agent != nil {
  207. return (fmt.Errorf("libnetwork agent setup without keys"))
  208. }
  209. if clusterConfigAvailable {
  210. return c.agentSetup()
  211. }
  212. log.Debugf("received encryption keys before cluster config")
  213. return nil
  214. }
  215. if agent == nil {
  216. c.Lock()
  217. c.keys = keys
  218. c.Unlock()
  219. return nil
  220. }
  221. return c.handleKeyChange(keys)
  222. }
  223. func (c *controller) clusterAgentInit() {
  224. clusterProvider := c.cfg.Daemon.ClusterProvider
  225. for {
  226. select {
  227. case <-clusterProvider.ListenClusterEvents():
  228. if !c.isDistributedControl() {
  229. c.Lock()
  230. c.clusterConfigAvailable = true
  231. keys := c.keys
  232. c.Unlock()
  233. // agent initialization needs encyrption keys and bind/remote IP which
  234. // comes from the daemon cluster events
  235. if len(keys) > 0 {
  236. c.agentSetup()
  237. }
  238. }
  239. case <-c.cfg.Daemon.DisableProvider:
  240. c.Lock()
  241. c.clusterConfigAvailable = false
  242. c.agentInitDone = make(chan struct{})
  243. c.Unlock()
  244. c.agentClose()
  245. return
  246. }
  247. }
  248. }
  249. // AgentInitWait waits for agent initialization to be completed in the
  250. // controller.
  251. func (c *controller) AgentInitWait() {
  252. <-c.agentInitDone
  253. }
  254. func (c *controller) makeDriverConfig(ntype string) map[string]interface{} {
  255. if c.cfg == nil {
  256. return nil
  257. }
  258. config := make(map[string]interface{})
  259. for _, label := range c.cfg.Daemon.Labels {
  260. if !strings.HasPrefix(netlabel.Key(label), netlabel.DriverPrefix+"."+ntype) {
  261. continue
  262. }
  263. config[netlabel.Key(label)] = netlabel.Value(label)
  264. }
  265. drvCfg, ok := c.cfg.Daemon.DriverCfg[ntype]
  266. if ok {
  267. for k, v := range drvCfg.(map[string]interface{}) {
  268. config[k] = v
  269. }
  270. }
  271. for k, v := range c.cfg.Scopes {
  272. if !v.IsValid() {
  273. continue
  274. }
  275. config[netlabel.MakeKVClient(k)] = discoverapi.DatastoreConfigData{
  276. Scope: k,
  277. Provider: v.Client.Provider,
  278. Address: v.Client.Address,
  279. Config: v.Client.Config,
  280. }
  281. }
  282. return config
  283. }
  284. var procReloadConfig = make(chan (bool), 1)
  285. func (c *controller) ReloadConfiguration(cfgOptions ...config.Option) error {
  286. procReloadConfig <- true
  287. defer func() { <-procReloadConfig }()
  288. // For now we accept the configuration reload only as a mean to provide a global store config after boot.
  289. // Refuse the configuration if it alters an existing datastore client configuration.
  290. update := false
  291. cfg := config.ParseConfigOptions(cfgOptions...)
  292. for s := range c.cfg.Scopes {
  293. if _, ok := cfg.Scopes[s]; !ok {
  294. return types.ForbiddenErrorf("cannot accept new configuration because it removes an existing datastore client")
  295. }
  296. }
  297. for s, nSCfg := range cfg.Scopes {
  298. if eSCfg, ok := c.cfg.Scopes[s]; ok {
  299. if eSCfg.Client.Provider != nSCfg.Client.Provider ||
  300. eSCfg.Client.Address != nSCfg.Client.Address {
  301. return types.ForbiddenErrorf("cannot accept new configuration because it modifies an existing datastore client")
  302. }
  303. } else {
  304. if err := c.initScopedStore(s, nSCfg); err != nil {
  305. return err
  306. }
  307. update = true
  308. }
  309. }
  310. if !update {
  311. return nil
  312. }
  313. var dsConfig *discoverapi.DatastoreConfigData
  314. for scope, sCfg := range cfg.Scopes {
  315. if scope == datastore.LocalScope || !sCfg.IsValid() {
  316. continue
  317. }
  318. dsConfig = &discoverapi.DatastoreConfigData{
  319. Scope: scope,
  320. Provider: sCfg.Client.Provider,
  321. Address: sCfg.Client.Address,
  322. Config: sCfg.Client.Config,
  323. }
  324. break
  325. }
  326. if dsConfig == nil {
  327. return nil
  328. }
  329. c.drvRegistry.WalkIPAMs(func(name string, driver ipamapi.Ipam, cap *ipamapi.Capability) bool {
  330. err := driver.DiscoverNew(discoverapi.DatastoreConfig, *dsConfig)
  331. if err != nil {
  332. log.Errorf("Failed to set datastore in driver %s: %v", name, err)
  333. }
  334. return false
  335. })
  336. c.drvRegistry.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool {
  337. err := driver.DiscoverNew(discoverapi.DatastoreConfig, *dsConfig)
  338. if err != nil {
  339. log.Errorf("Failed to set datastore in driver %s: %v", name, err)
  340. }
  341. return false
  342. })
  343. if c.discovery == nil && c.cfg.Cluster.Watcher != nil {
  344. if err := c.initDiscovery(c.cfg.Cluster.Watcher); err != nil {
  345. log.Errorf("Failed to Initialize Discovery after configuration update: %v", err)
  346. }
  347. }
  348. return nil
  349. }
  350. func (c *controller) ID() string {
  351. return c.id
  352. }
  353. func (c *controller) validateHostDiscoveryConfig() bool {
  354. if c.cfg == nil || c.cfg.Cluster.Discovery == "" || c.cfg.Cluster.Address == "" {
  355. return false
  356. }
  357. return true
  358. }
  359. func (c *controller) clusterHostID() string {
  360. c.Lock()
  361. defer c.Unlock()
  362. if c.cfg == nil || c.cfg.Cluster.Address == "" {
  363. return ""
  364. }
  365. addr := strings.Split(c.cfg.Cluster.Address, ":")
  366. return addr[0]
  367. }
  368. func (c *controller) isNodeAlive(node string) bool {
  369. if c.discovery == nil {
  370. return false
  371. }
  372. nodes := c.discovery.Fetch()
  373. for _, n := range nodes {
  374. if n.String() == node {
  375. return true
  376. }
  377. }
  378. return false
  379. }
  380. func (c *controller) initDiscovery(watcher discovery.Watcher) error {
  381. if c.cfg == nil {
  382. return fmt.Errorf("discovery initialization requires a valid configuration")
  383. }
  384. c.discovery = hostdiscovery.NewHostDiscovery(watcher)
  385. return c.discovery.Watch(c.activeCallback, c.hostJoinCallback, c.hostLeaveCallback)
  386. }
  387. func (c *controller) activeCallback() {
  388. ds := c.getStore(datastore.GlobalScope)
  389. if ds != nil && !ds.Active() {
  390. ds.RestartWatch()
  391. }
  392. }
  393. func (c *controller) hostJoinCallback(nodes []net.IP) {
  394. c.processNodeDiscovery(nodes, true)
  395. }
  396. func (c *controller) hostLeaveCallback(nodes []net.IP) {
  397. c.processNodeDiscovery(nodes, false)
  398. }
  399. func (c *controller) processNodeDiscovery(nodes []net.IP, add bool) {
  400. c.drvRegistry.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool {
  401. c.pushNodeDiscovery(driver, capability, nodes, add)
  402. return false
  403. })
  404. }
  405. func (c *controller) pushNodeDiscovery(d driverapi.Driver, cap driverapi.Capability, nodes []net.IP, add bool) {
  406. var self net.IP
  407. if c.cfg != nil {
  408. addr := strings.Split(c.cfg.Cluster.Address, ":")
  409. self = net.ParseIP(addr[0])
  410. }
  411. if d == nil || cap.DataScope != datastore.GlobalScope || nodes == nil {
  412. return
  413. }
  414. for _, node := range nodes {
  415. nodeData := discoverapi.NodeDiscoveryData{Address: node.String(), Self: node.Equal(self)}
  416. var err error
  417. if add {
  418. err = d.DiscoverNew(discoverapi.NodeDiscovery, nodeData)
  419. } else {
  420. err = d.DiscoverDelete(discoverapi.NodeDiscovery, nodeData)
  421. }
  422. if err != nil {
  423. log.Debugf("discovery notification error : %v", err)
  424. }
  425. }
  426. }
  427. func (c *controller) Config() config.Config {
  428. c.Lock()
  429. defer c.Unlock()
  430. if c.cfg == nil {
  431. return config.Config{}
  432. }
  433. return *c.cfg
  434. }
  435. func (c *controller) isManager() bool {
  436. if c.cfg == nil || c.cfg.Daemon.ClusterProvider == nil {
  437. return false
  438. }
  439. return c.cfg.Daemon.ClusterProvider.IsManager()
  440. }
  441. func (c *controller) isAgent() bool {
  442. if c.cfg == nil || c.cfg.Daemon.ClusterProvider == nil {
  443. return false
  444. }
  445. return c.cfg.Daemon.ClusterProvider.IsAgent()
  446. }
  447. func (c *controller) isDistributedControl() bool {
  448. return !c.isManager() && !c.isAgent()
  449. }
  450. func (c *controller) RegisterDriver(networkType string, driver driverapi.Driver, capability driverapi.Capability) error {
  451. c.Lock()
  452. hd := c.discovery
  453. c.Unlock()
  454. if hd != nil {
  455. c.pushNodeDiscovery(driver, capability, hd.Fetch(), true)
  456. }
  457. c.agentDriverNotify(driver)
  458. return nil
  459. }
  460. // NewNetwork creates a new network of the specified network type. The options
  461. // are network specific and modeled in a generic way.
  462. func (c *controller) NewNetwork(networkType, name string, id string, options ...NetworkOption) (Network, error) {
  463. if !config.IsValidName(name) {
  464. return nil, ErrInvalidName(name)
  465. }
  466. if id == "" {
  467. id = stringid.GenerateRandomID()
  468. }
  469. // Construct the network object
  470. network := &network{
  471. name: name,
  472. networkType: networkType,
  473. generic: map[string]interface{}{netlabel.GenericData: make(map[string]string)},
  474. ipamType: ipamapi.DefaultIPAM,
  475. id: id,
  476. ctrlr: c,
  477. persist: true,
  478. drvOnce: &sync.Once{},
  479. }
  480. network.processOptions(options...)
  481. _, cap, err := network.resolveDriver(networkType, true)
  482. if err != nil {
  483. return nil, err
  484. }
  485. if cap.DataScope == datastore.GlobalScope && !c.isDistributedControl() && !network.dynamic {
  486. if c.isManager() {
  487. // For non-distributed controlled environment, globalscoped non-dynamic networks are redirected to Manager
  488. return nil, ManagerRedirectError(name)
  489. }
  490. return nil, types.ForbiddenErrorf("Cannot create a multi-host network from a worker node. Please create the network from a manager node.")
  491. }
  492. // Make sure we have a driver available for this network type
  493. // before we allocate anything.
  494. if _, err := network.driver(true); err != nil {
  495. return nil, err
  496. }
  497. err = network.ipamAllocate()
  498. if err != nil {
  499. return nil, err
  500. }
  501. defer func() {
  502. if err != nil {
  503. network.ipamRelease()
  504. }
  505. }()
  506. err = c.addNetwork(network)
  507. if err != nil {
  508. return nil, err
  509. }
  510. defer func() {
  511. if err != nil {
  512. if e := network.deleteNetwork(); e != nil {
  513. log.Warnf("couldn't roll back driver network on network %s creation failure: %v", network.name, err)
  514. }
  515. }
  516. }()
  517. // First store the endpoint count, then the network. To avoid to
  518. // end up with a datastore containing a network and not an epCnt,
  519. // in case of an ungraceful shutdown during this function call.
  520. epCnt := &endpointCnt{n: network}
  521. if err = c.updateToStore(epCnt); err != nil {
  522. return nil, err
  523. }
  524. defer func() {
  525. if err != nil {
  526. if e := c.deleteFromStore(epCnt); e != nil {
  527. log.Warnf("couldnt rollback from store, epCnt %v on failure (%v): %v", epCnt, err, e)
  528. }
  529. }
  530. }()
  531. network.epCnt = epCnt
  532. if err = c.updateToStore(network); err != nil {
  533. return nil, err
  534. }
  535. if err = network.joinCluster(); err != nil {
  536. log.Errorf("Failed to join network %s into agent cluster: %v", name, err)
  537. }
  538. network.addDriverWatches()
  539. return network, nil
  540. }
  541. func (c *controller) reservePools() {
  542. networks, err := c.getNetworksForScope(datastore.LocalScope)
  543. if err != nil {
  544. log.Warnf("Could not retrieve networks from local store during ipam allocation for existing networks: %v", err)
  545. return
  546. }
  547. for _, n := range networks {
  548. if !doReplayPoolReserve(n) {
  549. continue
  550. }
  551. // Construct pseudo configs for the auto IP case
  552. autoIPv4 := (len(n.ipamV4Config) == 0 || (len(n.ipamV4Config) == 1 && n.ipamV4Config[0].PreferredPool == "")) && len(n.ipamV4Info) > 0
  553. autoIPv6 := (len(n.ipamV6Config) == 0 || (len(n.ipamV6Config) == 1 && n.ipamV6Config[0].PreferredPool == "")) && len(n.ipamV6Info) > 0
  554. if autoIPv4 {
  555. n.ipamV4Config = []*IpamConf{{PreferredPool: n.ipamV4Info[0].Pool.String()}}
  556. }
  557. if n.enableIPv6 && autoIPv6 {
  558. n.ipamV6Config = []*IpamConf{{PreferredPool: n.ipamV6Info[0].Pool.String()}}
  559. }
  560. // Account current network gateways
  561. for i, c := range n.ipamV4Config {
  562. if c.Gateway == "" && n.ipamV4Info[i].Gateway != nil {
  563. c.Gateway = n.ipamV4Info[i].Gateway.IP.String()
  564. }
  565. }
  566. for i, c := range n.ipamV6Config {
  567. if c.Gateway == "" && n.ipamV6Info[i].Gateway != nil {
  568. c.Gateway = n.ipamV6Info[i].Gateway.IP.String()
  569. }
  570. }
  571. // Reserve pools
  572. if err := n.ipamAllocate(); err != nil {
  573. log.Warnf("Failed to allocate ipam pool(s) for network %q (%s): %v", n.Name(), n.ID(), err)
  574. }
  575. // Reserve existing endpoints' addresses
  576. ipam, _, err := n.getController().getIPAMDriver(n.ipamType)
  577. if err != nil {
  578. log.Warnf("Failed to retrieve ipam driver for network %q (%s) during address reservation", n.Name(), n.ID())
  579. continue
  580. }
  581. epl, err := n.getEndpointsFromStore()
  582. if err != nil {
  583. log.Warnf("Failed to retrieve list of current endpoints on network %q (%s)", n.Name(), n.ID())
  584. continue
  585. }
  586. for _, ep := range epl {
  587. if err := ep.assignAddress(ipam, true, ep.Iface().AddressIPv6() != nil); err != nil {
  588. log.Warnf("Failed to reserve current adress for endpoint %q (%s) on network %q (%s)",
  589. ep.Name(), ep.ID(), n.Name(), n.ID())
  590. }
  591. }
  592. }
  593. }
  594. func doReplayPoolReserve(n *network) bool {
  595. _, caps, err := n.getController().getIPAMDriver(n.ipamType)
  596. if err != nil {
  597. log.Warnf("Failed to retrieve ipam driver for network %q (%s): %v", n.Name(), n.ID(), err)
  598. return false
  599. }
  600. return caps.RequiresRequestReplay
  601. }
  602. func (c *controller) addNetwork(n *network) error {
  603. d, err := n.driver(true)
  604. if err != nil {
  605. return err
  606. }
  607. // Create the network
  608. if err := d.CreateNetwork(n.id, n.generic, n, n.getIPData(4), n.getIPData(6)); err != nil {
  609. return err
  610. }
  611. return nil
  612. }
  613. func (c *controller) Networks() []Network {
  614. var list []Network
  615. networks, err := c.getNetworksFromStore()
  616. if err != nil {
  617. log.Error(err)
  618. }
  619. for _, n := range networks {
  620. if n.inDelete {
  621. continue
  622. }
  623. list = append(list, n)
  624. }
  625. return list
  626. }
  627. func (c *controller) WalkNetworks(walker NetworkWalker) {
  628. for _, n := range c.Networks() {
  629. if walker(n) {
  630. return
  631. }
  632. }
  633. }
  634. func (c *controller) NetworkByName(name string) (Network, error) {
  635. if name == "" {
  636. return nil, ErrInvalidName(name)
  637. }
  638. var n Network
  639. s := func(current Network) bool {
  640. if current.Name() == name {
  641. n = current
  642. return true
  643. }
  644. return false
  645. }
  646. c.WalkNetworks(s)
  647. if n == nil {
  648. return nil, ErrNoSuchNetwork(name)
  649. }
  650. return n, nil
  651. }
  652. func (c *controller) NetworkByID(id string) (Network, error) {
  653. if id == "" {
  654. return nil, ErrInvalidID(id)
  655. }
  656. n, err := c.getNetworkFromStore(id)
  657. if err != nil {
  658. return nil, ErrNoSuchNetwork(id)
  659. }
  660. return n, nil
  661. }
  662. // NewSandbox creates a new sandbox for the passed container id
  663. func (c *controller) NewSandbox(containerID string, options ...SandboxOption) (sBox Sandbox, err error) {
  664. if containerID == "" {
  665. return nil, types.BadRequestErrorf("invalid container ID")
  666. }
  667. var sb *sandbox
  668. c.Lock()
  669. for _, s := range c.sandboxes {
  670. if s.containerID == containerID {
  671. // If not a stub, then we already have a complete sandbox.
  672. if !s.isStub {
  673. c.Unlock()
  674. return nil, types.BadRequestErrorf("container %s is already present: %v", containerID, s)
  675. }
  676. // We already have a stub sandbox from the
  677. // store. Make use of it so that we don't lose
  678. // the endpoints from store but reset the
  679. // isStub flag.
  680. sb = s
  681. sb.isStub = false
  682. break
  683. }
  684. }
  685. c.Unlock()
  686. // Create sandbox and process options first. Key generation depends on an option
  687. if sb == nil {
  688. sb = &sandbox{
  689. id: stringid.GenerateRandomID(),
  690. containerID: containerID,
  691. endpoints: epHeap{},
  692. epPriority: map[string]int{},
  693. config: containerConfig{},
  694. controller: c,
  695. }
  696. }
  697. sBox = sb
  698. heap.Init(&sb.endpoints)
  699. sb.processOptions(options...)
  700. c.Lock()
  701. if sb.ingress && c.ingressSandbox != nil {
  702. c.Unlock()
  703. return nil, fmt.Errorf("ingress sandbox already present")
  704. }
  705. if sb.ingress {
  706. c.ingressSandbox = sb
  707. }
  708. c.Unlock()
  709. defer func() {
  710. if err != nil {
  711. c.Lock()
  712. if sb.ingress {
  713. c.ingressSandbox = nil
  714. }
  715. c.Unlock()
  716. }
  717. }()
  718. if err = sb.setupResolutionFiles(); err != nil {
  719. return nil, err
  720. }
  721. if sb.config.useDefaultSandBox {
  722. c.sboxOnce.Do(func() {
  723. c.defOsSbox, err = osl.NewSandbox(sb.Key(), false, false)
  724. })
  725. if err != nil {
  726. c.sboxOnce = sync.Once{}
  727. return nil, fmt.Errorf("failed to create default sandbox: %v", err)
  728. }
  729. sb.osSbox = c.defOsSbox
  730. }
  731. if sb.osSbox == nil && !sb.config.useExternalKey {
  732. if sb.osSbox, err = osl.NewSandbox(sb.Key(), !sb.config.useDefaultSandBox, false); err != nil {
  733. return nil, fmt.Errorf("failed to create new osl sandbox: %v", err)
  734. }
  735. }
  736. c.Lock()
  737. c.sandboxes[sb.id] = sb
  738. c.Unlock()
  739. defer func() {
  740. if err != nil {
  741. c.Lock()
  742. delete(c.sandboxes, sb.id)
  743. c.Unlock()
  744. }
  745. }()
  746. err = sb.storeUpdate()
  747. if err != nil {
  748. return nil, fmt.Errorf("updating the store state of sandbox failed: %v", err)
  749. }
  750. return sb, nil
  751. }
  752. func (c *controller) Sandboxes() []Sandbox {
  753. c.Lock()
  754. defer c.Unlock()
  755. list := make([]Sandbox, 0, len(c.sandboxes))
  756. for _, s := range c.sandboxes {
  757. // Hide stub sandboxes from libnetwork users
  758. if s.isStub {
  759. continue
  760. }
  761. list = append(list, s)
  762. }
  763. return list
  764. }
  765. func (c *controller) WalkSandboxes(walker SandboxWalker) {
  766. for _, sb := range c.Sandboxes() {
  767. if walker(sb) {
  768. return
  769. }
  770. }
  771. }
  772. func (c *controller) SandboxByID(id string) (Sandbox, error) {
  773. if id == "" {
  774. return nil, ErrInvalidID(id)
  775. }
  776. c.Lock()
  777. s, ok := c.sandboxes[id]
  778. c.Unlock()
  779. if !ok {
  780. return nil, types.NotFoundErrorf("sandbox %s not found", id)
  781. }
  782. return s, nil
  783. }
  784. // SandboxDestroy destroys a sandbox given a container ID
  785. func (c *controller) SandboxDestroy(id string) error {
  786. var sb *sandbox
  787. c.Lock()
  788. for _, s := range c.sandboxes {
  789. if s.containerID == id {
  790. sb = s
  791. break
  792. }
  793. }
  794. c.Unlock()
  795. // It is not an error if sandbox is not available
  796. if sb == nil {
  797. return nil
  798. }
  799. return sb.Delete()
  800. }
  801. // SandboxContainerWalker returns a Sandbox Walker function which looks for an existing Sandbox with the passed containerID
  802. func SandboxContainerWalker(out *Sandbox, containerID string) SandboxWalker {
  803. return func(sb Sandbox) bool {
  804. if sb.ContainerID() == containerID {
  805. *out = sb
  806. return true
  807. }
  808. return false
  809. }
  810. }
  811. // SandboxKeyWalker returns a Sandbox Walker function which looks for an existing Sandbox with the passed key
  812. func SandboxKeyWalker(out *Sandbox, key string) SandboxWalker {
  813. return func(sb Sandbox) bool {
  814. if sb.Key() == key {
  815. *out = sb
  816. return true
  817. }
  818. return false
  819. }
  820. }
  821. func (c *controller) loadDriver(networkType string) error {
  822. // Plugins pkg performs lazy loading of plugins that acts as remote drivers.
  823. // As per the design, this Get call will result in remote driver discovery if there is a corresponding plugin available.
  824. _, err := plugins.Get(networkType, driverapi.NetworkPluginEndpointType)
  825. if err != nil {
  826. if err == plugins.ErrNotFound {
  827. return types.NotFoundErrorf(err.Error())
  828. }
  829. return err
  830. }
  831. return nil
  832. }
  833. func (c *controller) loadIPAMDriver(name string) error {
  834. if _, err := plugins.Get(name, ipamapi.PluginEndpointType); err != nil {
  835. if err == plugins.ErrNotFound {
  836. return types.NotFoundErrorf(err.Error())
  837. }
  838. return err
  839. }
  840. return nil
  841. }
  842. func (c *controller) getIPAMDriver(name string) (ipamapi.Ipam, *ipamapi.Capability, error) {
  843. id, cap := c.drvRegistry.IPAM(name)
  844. if id == nil {
  845. // Might be a plugin name. Try loading it
  846. if err := c.loadIPAMDriver(name); err != nil {
  847. return nil, nil, err
  848. }
  849. // Now that we resolved the plugin, try again looking up the registry
  850. id, cap = c.drvRegistry.IPAM(name)
  851. if id == nil {
  852. return nil, nil, types.BadRequestErrorf("invalid ipam driver: %q", name)
  853. }
  854. }
  855. return id, cap, nil
  856. }
  857. func (c *controller) Stop() {
  858. c.closeStores()
  859. c.stopExternalKeyListener()
  860. osl.GC()
  861. }