controller.go 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  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("example.com"))
  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. "fmt"
  41. "net"
  42. "path/filepath"
  43. "runtime"
  44. "strings"
  45. "sync"
  46. "time"
  47. "github.com/docker/docker/libnetwork/cluster"
  48. "github.com/docker/docker/libnetwork/config"
  49. "github.com/docker/docker/libnetwork/datastore"
  50. "github.com/docker/docker/libnetwork/diagnostic"
  51. "github.com/docker/docker/libnetwork/discoverapi"
  52. "github.com/docker/docker/libnetwork/driverapi"
  53. remotedriver "github.com/docker/docker/libnetwork/drivers/remote"
  54. "github.com/docker/docker/libnetwork/drvregistry"
  55. "github.com/docker/docker/libnetwork/ipamapi"
  56. "github.com/docker/docker/libnetwork/netlabel"
  57. "github.com/docker/docker/libnetwork/options"
  58. "github.com/docker/docker/libnetwork/osl"
  59. "github.com/docker/docker/libnetwork/types"
  60. "github.com/docker/docker/pkg/plugingetter"
  61. "github.com/docker/docker/pkg/plugins"
  62. "github.com/docker/docker/pkg/stringid"
  63. "github.com/moby/locker"
  64. "github.com/pkg/errors"
  65. "github.com/sirupsen/logrus"
  66. )
  67. // NetworkWalker is a client provided function which will be used to walk the Networks.
  68. // When the function returns true, the walk will stop.
  69. type NetworkWalker func(nw Network) bool
  70. // SandboxWalker is a client provided function which will be used to walk the Sandboxes.
  71. // When the function returns true, the walk will stop.
  72. type SandboxWalker func(sb *Sandbox) bool
  73. type sandboxTable map[string]*Sandbox
  74. // Controller manages networks.
  75. type Controller struct {
  76. id string
  77. drvRegistry drvregistry.Networks
  78. ipamRegistry drvregistry.IPAMs
  79. sandboxes sandboxTable
  80. cfg *config.Config
  81. store datastore.DataStore
  82. extKeyListener net.Listener
  83. watchCh chan *Endpoint
  84. unWatchCh chan *Endpoint
  85. svcRecords map[string]svcInfo
  86. nmap map[string]*netWatch
  87. serviceBindings map[serviceKey]*service
  88. defOsSbox osl.Sandbox
  89. ingressSandbox *Sandbox
  90. sboxOnce sync.Once
  91. agent *agent
  92. networkLocker *locker.Locker
  93. agentInitDone chan struct{}
  94. agentStopDone chan struct{}
  95. keys []*types.EncryptionKey
  96. DiagnosticServer *diagnostic.Server
  97. mu sync.Mutex
  98. }
  99. type initializer struct {
  100. fn func(driverapi.Registerer, map[string]interface{}) error
  101. ntype string
  102. }
  103. // New creates a new instance of network controller.
  104. func New(cfgOptions ...config.Option) (*Controller, error) {
  105. c := &Controller{
  106. id: stringid.GenerateRandomID(),
  107. cfg: config.New(cfgOptions...),
  108. sandboxes: sandboxTable{},
  109. svcRecords: make(map[string]svcInfo),
  110. serviceBindings: make(map[serviceKey]*service),
  111. agentInitDone: make(chan struct{}),
  112. networkLocker: locker.New(),
  113. DiagnosticServer: diagnostic.New(),
  114. }
  115. c.DiagnosticServer.Init()
  116. if err := c.initStores(); err != nil {
  117. return nil, err
  118. }
  119. c.drvRegistry.Notify = c.RegisterDriver
  120. // External plugins don't need config passed through daemon. They can
  121. // bootstrap themselves.
  122. if err := remotedriver.Register(&c.drvRegistry, c.cfg.PluginGetter); err != nil {
  123. return nil, err
  124. }
  125. for _, i := range getInitializers() {
  126. if err := i.fn(&c.drvRegistry, c.makeDriverConfig(i.ntype)); err != nil {
  127. return nil, err
  128. }
  129. }
  130. if err := initIPAMDrivers(&c.ipamRegistry, c.cfg.PluginGetter, c.cfg.DefaultAddressPool); err != nil {
  131. return nil, err
  132. }
  133. c.WalkNetworks(populateSpecial)
  134. // Reserve pools first before doing cleanup. Otherwise the
  135. // cleanups of endpoint/network and sandbox below will
  136. // generate many unnecessary warnings
  137. c.reservePools()
  138. // Cleanup resources
  139. c.sandboxCleanup(c.cfg.ActiveSandboxes)
  140. c.cleanupLocalEndpoints()
  141. c.networkCleanup()
  142. if err := c.startExternalKeyListener(); err != nil {
  143. return nil, err
  144. }
  145. setupArrangeUserFilterRule(c)
  146. return c, nil
  147. }
  148. // SetClusterProvider sets the cluster provider.
  149. func (c *Controller) SetClusterProvider(provider cluster.Provider) {
  150. var sameProvider bool
  151. c.mu.Lock()
  152. // Avoids to spawn multiple goroutine for the same cluster provider
  153. if c.cfg.ClusterProvider == provider {
  154. // If the cluster provider is already set, there is already a go routine spawned
  155. // that is listening for events, so nothing to do here
  156. sameProvider = true
  157. } else {
  158. c.cfg.ClusterProvider = provider
  159. }
  160. c.mu.Unlock()
  161. if provider == nil || sameProvider {
  162. return
  163. }
  164. // We don't want to spawn a new go routine if the previous one did not exit yet
  165. c.AgentStopWait()
  166. go c.clusterAgentInit()
  167. }
  168. // SetKeys configures the encryption key for gossip and overlay data path.
  169. func (c *Controller) SetKeys(keys []*types.EncryptionKey) error {
  170. // libnetwork side of agent depends on the keys. On the first receipt of
  171. // keys setup the agent. For subsequent key set handle the key change
  172. subsysKeys := make(map[string]int)
  173. for _, key := range keys {
  174. if key.Subsystem != subsysGossip &&
  175. key.Subsystem != subsysIPSec {
  176. return fmt.Errorf("key received for unrecognized subsystem")
  177. }
  178. subsysKeys[key.Subsystem]++
  179. }
  180. for s, count := range subsysKeys {
  181. if count != keyringSize {
  182. return fmt.Errorf("incorrect number of keys for subsystem %v", s)
  183. }
  184. }
  185. if c.getAgent() == nil {
  186. c.mu.Lock()
  187. c.keys = keys
  188. c.mu.Unlock()
  189. return nil
  190. }
  191. return c.handleKeyChange(keys)
  192. }
  193. func (c *Controller) getAgent() *agent {
  194. c.mu.Lock()
  195. defer c.mu.Unlock()
  196. return c.agent
  197. }
  198. func (c *Controller) clusterAgentInit() {
  199. clusterProvider := c.cfg.ClusterProvider
  200. var keysAvailable bool
  201. for {
  202. eventType := <-clusterProvider.ListenClusterEvents()
  203. // The events: EventSocketChange, EventNodeReady and EventNetworkKeysAvailable are not ordered
  204. // when all the condition for the agent initialization are met then proceed with it
  205. switch eventType {
  206. case cluster.EventNetworkKeysAvailable:
  207. // Validates that the keys are actually available before starting the initialization
  208. // This will handle old spurious messages left on the channel
  209. c.mu.Lock()
  210. keysAvailable = c.keys != nil
  211. c.mu.Unlock()
  212. fallthrough
  213. case cluster.EventSocketChange, cluster.EventNodeReady:
  214. if keysAvailable && !c.isDistributedControl() {
  215. c.agentOperationStart()
  216. if err := c.agentSetup(clusterProvider); err != nil {
  217. c.agentStopComplete()
  218. } else {
  219. c.agentInitComplete()
  220. }
  221. }
  222. case cluster.EventNodeLeave:
  223. c.agentOperationStart()
  224. c.mu.Lock()
  225. c.keys = nil
  226. c.mu.Unlock()
  227. // We are leaving the cluster. Make sure we
  228. // close the gossip so that we stop all
  229. // incoming gossip updates before cleaning up
  230. // any remaining service bindings. But before
  231. // deleting the networks since the networks
  232. // should still be present when cleaning up
  233. // service bindings
  234. c.agentClose()
  235. c.cleanupServiceDiscovery("")
  236. c.cleanupServiceBindings("")
  237. c.agentStopComplete()
  238. return
  239. }
  240. }
  241. }
  242. // AgentInitWait waits for agent initialization to be completed in the controller.
  243. func (c *Controller) AgentInitWait() {
  244. c.mu.Lock()
  245. agentInitDone := c.agentInitDone
  246. c.mu.Unlock()
  247. if agentInitDone != nil {
  248. <-agentInitDone
  249. }
  250. }
  251. // AgentStopWait waits for the Agent stop to be completed in the controller.
  252. func (c *Controller) AgentStopWait() {
  253. c.mu.Lock()
  254. agentStopDone := c.agentStopDone
  255. c.mu.Unlock()
  256. if agentStopDone != nil {
  257. <-agentStopDone
  258. }
  259. }
  260. // agentOperationStart marks the start of an Agent Init or Agent Stop
  261. func (c *Controller) agentOperationStart() {
  262. c.mu.Lock()
  263. if c.agentInitDone == nil {
  264. c.agentInitDone = make(chan struct{})
  265. }
  266. if c.agentStopDone == nil {
  267. c.agentStopDone = make(chan struct{})
  268. }
  269. c.mu.Unlock()
  270. }
  271. // agentInitComplete notifies the successful completion of the Agent initialization
  272. func (c *Controller) agentInitComplete() {
  273. c.mu.Lock()
  274. if c.agentInitDone != nil {
  275. close(c.agentInitDone)
  276. c.agentInitDone = nil
  277. }
  278. c.mu.Unlock()
  279. }
  280. // agentStopComplete notifies the successful completion of the Agent stop
  281. func (c *Controller) agentStopComplete() {
  282. c.mu.Lock()
  283. if c.agentStopDone != nil {
  284. close(c.agentStopDone)
  285. c.agentStopDone = nil
  286. }
  287. c.mu.Unlock()
  288. }
  289. func (c *Controller) makeDriverConfig(ntype string) map[string]interface{} {
  290. if c.cfg == nil {
  291. return nil
  292. }
  293. cfg := map[string]interface{}{}
  294. for _, label := range c.cfg.Labels {
  295. key, val, _ := strings.Cut(label, "=")
  296. if !strings.HasPrefix(key, netlabel.DriverPrefix+"."+ntype) {
  297. continue
  298. }
  299. cfg[key] = val
  300. }
  301. drvCfg, ok := c.cfg.DriverCfg[ntype]
  302. if ok {
  303. for k, v := range drvCfg.(map[string]interface{}) {
  304. cfg[k] = v
  305. }
  306. }
  307. if c.cfg.Scope.IsValid() {
  308. // FIXME: every driver instance constructs a new DataStore
  309. // instance against the same database. Yikes!
  310. cfg[netlabel.LocalKVClient] = discoverapi.DatastoreConfigData{
  311. Scope: datastore.LocalScope,
  312. Provider: c.cfg.Scope.Client.Provider,
  313. Address: c.cfg.Scope.Client.Address,
  314. Config: c.cfg.Scope.Client.Config,
  315. }
  316. }
  317. return cfg
  318. }
  319. // ID returns the controller's unique identity.
  320. func (c *Controller) ID() string {
  321. return c.id
  322. }
  323. // BuiltinDrivers returns the list of builtin network drivers.
  324. func (c *Controller) BuiltinDrivers() []string {
  325. drivers := []string{}
  326. c.drvRegistry.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool {
  327. if driver.IsBuiltIn() {
  328. drivers = append(drivers, name)
  329. }
  330. return false
  331. })
  332. return drivers
  333. }
  334. // BuiltinIPAMDrivers returns the list of builtin ipam drivers.
  335. func (c *Controller) BuiltinIPAMDrivers() []string {
  336. drivers := []string{}
  337. c.ipamRegistry.WalkIPAMs(func(name string, driver ipamapi.Ipam, cap *ipamapi.Capability) bool {
  338. if driver.IsBuiltIn() {
  339. drivers = append(drivers, name)
  340. }
  341. return false
  342. })
  343. return drivers
  344. }
  345. func (c *Controller) processNodeDiscovery(nodes []net.IP, add bool) {
  346. c.drvRegistry.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool {
  347. c.pushNodeDiscovery(driver, capability, nodes, add)
  348. return false
  349. })
  350. }
  351. func (c *Controller) pushNodeDiscovery(d driverapi.Driver, cap driverapi.Capability, nodes []net.IP, add bool) {
  352. var self net.IP
  353. // try swarm-mode config
  354. if agent := c.getAgent(); agent != nil {
  355. self = net.ParseIP(agent.advertiseAddr)
  356. }
  357. if d == nil || cap.ConnectivityScope != datastore.GlobalScope || nodes == nil {
  358. return
  359. }
  360. for _, node := range nodes {
  361. nodeData := discoverapi.NodeDiscoveryData{Address: node.String(), Self: node.Equal(self)}
  362. var err error
  363. if add {
  364. err = d.DiscoverNew(discoverapi.NodeDiscovery, nodeData)
  365. } else {
  366. err = d.DiscoverDelete(discoverapi.NodeDiscovery, nodeData)
  367. }
  368. if err != nil {
  369. logrus.Debugf("discovery notification error: %v", err)
  370. }
  371. }
  372. }
  373. // Config returns the bootup configuration for the controller.
  374. func (c *Controller) Config() config.Config {
  375. c.mu.Lock()
  376. defer c.mu.Unlock()
  377. if c.cfg == nil {
  378. return config.Config{}
  379. }
  380. return *c.cfg
  381. }
  382. func (c *Controller) isManager() bool {
  383. c.mu.Lock()
  384. defer c.mu.Unlock()
  385. if c.cfg == nil || c.cfg.ClusterProvider == nil {
  386. return false
  387. }
  388. return c.cfg.ClusterProvider.IsManager()
  389. }
  390. func (c *Controller) isAgent() bool {
  391. c.mu.Lock()
  392. defer c.mu.Unlock()
  393. if c.cfg == nil || c.cfg.ClusterProvider == nil {
  394. return false
  395. }
  396. return c.cfg.ClusterProvider.IsAgent()
  397. }
  398. func (c *Controller) isDistributedControl() bool {
  399. return !c.isManager() && !c.isAgent()
  400. }
  401. func (c *Controller) GetPluginGetter() plugingetter.PluginGetter {
  402. return c.cfg.PluginGetter
  403. }
  404. func (c *Controller) RegisterDriver(networkType string, driver driverapi.Driver, capability driverapi.Capability) error {
  405. c.agentDriverNotify(driver)
  406. return nil
  407. }
  408. // XXX This should be made driver agnostic. See comment below.
  409. const overlayDSROptionString = "dsr"
  410. // NewNetwork creates a new network of the specified network type. The options
  411. // are network specific and modeled in a generic way.
  412. func (c *Controller) NewNetwork(networkType, name string, id string, options ...NetworkOption) (Network, error) {
  413. var (
  414. caps driverapi.Capability
  415. err error
  416. t *network
  417. skipCfgEpCount bool
  418. )
  419. if id != "" {
  420. c.networkLocker.Lock(id)
  421. defer c.networkLocker.Unlock(id) //nolint:errcheck
  422. if _, err = c.NetworkByID(id); err == nil {
  423. return nil, NetworkNameError(id)
  424. }
  425. }
  426. if !config.IsValidName(name) {
  427. return nil, ErrInvalidName(name)
  428. }
  429. if id == "" {
  430. id = stringid.GenerateRandomID()
  431. }
  432. defaultIpam := defaultIpamForNetworkType(networkType)
  433. // Construct the network object
  434. nw := &network{
  435. name: name,
  436. networkType: networkType,
  437. generic: map[string]interface{}{netlabel.GenericData: make(map[string]string)},
  438. ipamType: defaultIpam,
  439. id: id,
  440. created: time.Now(),
  441. ctrlr: c,
  442. persist: true,
  443. drvOnce: &sync.Once{},
  444. loadBalancerMode: loadBalancerModeDefault,
  445. }
  446. nw.processOptions(options...)
  447. if err = nw.validateConfiguration(); err != nil {
  448. return nil, err
  449. }
  450. // Reset network types, force local scope and skip allocation and
  451. // plumbing for configuration networks. Reset of the config-only
  452. // network drivers is needed so that this special network is not
  453. // usable by old engine versions.
  454. if nw.configOnly {
  455. nw.scope = datastore.LocalScope
  456. nw.networkType = "null"
  457. goto addToStore
  458. }
  459. _, caps, err = nw.resolveDriver(nw.networkType, true)
  460. if err != nil {
  461. return nil, err
  462. }
  463. if nw.scope == datastore.LocalScope && caps.DataScope == datastore.GlobalScope {
  464. return nil, types.ForbiddenErrorf("cannot downgrade network scope for %s networks", networkType)
  465. }
  466. if nw.ingress && caps.DataScope != datastore.GlobalScope {
  467. return nil, types.ForbiddenErrorf("Ingress network can only be global scope network")
  468. }
  469. // At this point the network scope is still unknown if not set by user
  470. if (caps.DataScope == datastore.GlobalScope || nw.scope == datastore.SwarmScope) &&
  471. !c.isDistributedControl() && !nw.dynamic {
  472. if c.isManager() {
  473. // For non-distributed controlled environment, globalscoped non-dynamic networks are redirected to Manager
  474. return nil, ManagerRedirectError(name)
  475. }
  476. return nil, types.ForbiddenErrorf("Cannot create a multi-host network from a worker node. Please create the network from a manager node.")
  477. }
  478. if nw.scope == datastore.SwarmScope && c.isDistributedControl() {
  479. return nil, types.ForbiddenErrorf("cannot create a swarm scoped network when swarm is not active")
  480. }
  481. // Make sure we have a driver available for this network type
  482. // before we allocate anything.
  483. if _, err := nw.driver(true); err != nil {
  484. return nil, err
  485. }
  486. // From this point on, we need the network specific configuration,
  487. // which may come from a configuration-only network
  488. if nw.configFrom != "" {
  489. t, err = c.getConfigNetwork(nw.configFrom)
  490. if err != nil {
  491. return nil, types.NotFoundErrorf("configuration network %q does not exist", nw.configFrom)
  492. }
  493. if err = t.applyConfigurationTo(nw); err != nil {
  494. return nil, types.InternalErrorf("Failed to apply configuration: %v", err)
  495. }
  496. nw.generic[netlabel.Internal] = nw.internal
  497. defer func() {
  498. if err == nil && !skipCfgEpCount {
  499. if err := t.getEpCnt().IncEndpointCnt(); err != nil {
  500. logrus.Warnf("Failed to update reference count for configuration network %q on creation of network %q: %v",
  501. t.Name(), nw.Name(), err)
  502. }
  503. }
  504. }()
  505. }
  506. err = nw.ipamAllocate()
  507. if err != nil {
  508. return nil, err
  509. }
  510. defer func() {
  511. if err != nil {
  512. nw.ipamRelease()
  513. }
  514. }()
  515. err = c.addNetwork(nw)
  516. if err != nil {
  517. if _, ok := err.(types.MaskableError); ok { //nolint:gosimple
  518. // This error can be ignored and set this boolean
  519. // value to skip a refcount increment for configOnly networks
  520. skipCfgEpCount = true
  521. } else {
  522. return nil, err
  523. }
  524. }
  525. defer func() {
  526. if err != nil {
  527. if e := nw.deleteNetwork(); e != nil {
  528. logrus.Warnf("couldn't roll back driver network on network %s creation failure: %v", nw.name, err)
  529. }
  530. }
  531. }()
  532. // XXX If the driver type is "overlay" check the options for DSR
  533. // being set. If so, set the network's load balancing mode to DSR.
  534. // This should really be done in a network option, but due to
  535. // time pressure to get this in without adding changes to moby,
  536. // swarm and CLI, it is being implemented as a driver-specific
  537. // option. Unfortunately, drivers can't influence the core
  538. // "libnetwork.network" data type. Hence we need this hack code
  539. // to implement in this manner.
  540. if gval, ok := nw.generic[netlabel.GenericData]; ok && nw.networkType == "overlay" {
  541. optMap := gval.(map[string]string)
  542. if _, ok := optMap[overlayDSROptionString]; ok {
  543. nw.loadBalancerMode = loadBalancerModeDSR
  544. }
  545. }
  546. addToStore:
  547. // First store the endpoint count, then the network. To avoid to
  548. // end up with a datastore containing a network and not an epCnt,
  549. // in case of an ungraceful shutdown during this function call.
  550. epCnt := &endpointCnt{n: nw}
  551. if err = c.updateToStore(epCnt); err != nil {
  552. return nil, err
  553. }
  554. defer func() {
  555. if err != nil {
  556. if e := c.deleteFromStore(epCnt); e != nil {
  557. logrus.Warnf("could not rollback from store, epCnt %v on failure (%v): %v", epCnt, err, e)
  558. }
  559. }
  560. }()
  561. nw.epCnt = epCnt
  562. if err = c.updateToStore(nw); err != nil {
  563. return nil, err
  564. }
  565. defer func() {
  566. if err != nil {
  567. if e := c.deleteFromStore(nw); e != nil {
  568. logrus.Warnf("could not rollback from store, network %v on failure (%v): %v", nw, err, e)
  569. }
  570. }
  571. }()
  572. if nw.configOnly {
  573. return nw, nil
  574. }
  575. joinCluster(nw)
  576. defer func() {
  577. if err != nil {
  578. nw.cancelDriverWatches()
  579. if e := nw.leaveCluster(); e != nil {
  580. logrus.Warnf("Failed to leave agent cluster on network %s on failure (%v): %v", nw.name, err, e)
  581. }
  582. }
  583. }()
  584. if nw.hasLoadBalancerEndpoint() {
  585. if err = nw.createLoadBalancerSandbox(); err != nil {
  586. return nil, err
  587. }
  588. }
  589. if !c.isDistributedControl() {
  590. c.mu.Lock()
  591. arrangeIngressFilterRule()
  592. c.mu.Unlock()
  593. }
  594. arrangeUserFilterRule()
  595. return nw, nil
  596. }
  597. var joinCluster NetworkWalker = func(nw Network) bool {
  598. n := nw.(*network)
  599. if n.configOnly {
  600. return false
  601. }
  602. if err := n.joinCluster(); err != nil {
  603. logrus.Errorf("Failed to join network %s (%s) into agent cluster: %v", n.Name(), n.ID(), err)
  604. }
  605. n.addDriverWatches()
  606. return false
  607. }
  608. func (c *Controller) reservePools() {
  609. networks, err := c.getNetworks()
  610. if err != nil {
  611. logrus.Warnf("Could not retrieve networks from local store during ipam allocation for existing networks: %v", err)
  612. return
  613. }
  614. for _, n := range networks {
  615. if n.configOnly {
  616. continue
  617. }
  618. if !doReplayPoolReserve(n) {
  619. continue
  620. }
  621. // Construct pseudo configs for the auto IP case
  622. autoIPv4 := (len(n.ipamV4Config) == 0 || (len(n.ipamV4Config) == 1 && n.ipamV4Config[0].PreferredPool == "")) && len(n.ipamV4Info) > 0
  623. autoIPv6 := (len(n.ipamV6Config) == 0 || (len(n.ipamV6Config) == 1 && n.ipamV6Config[0].PreferredPool == "")) && len(n.ipamV6Info) > 0
  624. if autoIPv4 {
  625. n.ipamV4Config = []*IpamConf{{PreferredPool: n.ipamV4Info[0].Pool.String()}}
  626. }
  627. if n.enableIPv6 && autoIPv6 {
  628. n.ipamV6Config = []*IpamConf{{PreferredPool: n.ipamV6Info[0].Pool.String()}}
  629. }
  630. // Account current network gateways
  631. for i, cfg := range n.ipamV4Config {
  632. if cfg.Gateway == "" && n.ipamV4Info[i].Gateway != nil {
  633. cfg.Gateway = n.ipamV4Info[i].Gateway.IP.String()
  634. }
  635. }
  636. if n.enableIPv6 {
  637. for i, cfg := range n.ipamV6Config {
  638. if cfg.Gateway == "" && n.ipamV6Info[i].Gateway != nil {
  639. cfg.Gateway = n.ipamV6Info[i].Gateway.IP.String()
  640. }
  641. }
  642. }
  643. // Reserve pools
  644. if err := n.ipamAllocate(); err != nil {
  645. logrus.Warnf("Failed to allocate ipam pool(s) for network %q (%s): %v", n.Name(), n.ID(), err)
  646. }
  647. // Reserve existing endpoints' addresses
  648. ipam, _, err := n.getController().getIPAMDriver(n.ipamType)
  649. if err != nil {
  650. logrus.Warnf("Failed to retrieve ipam driver for network %q (%s) during address reservation", n.Name(), n.ID())
  651. continue
  652. }
  653. epl, err := n.getEndpointsFromStore()
  654. if err != nil {
  655. logrus.Warnf("Failed to retrieve list of current endpoints on network %q (%s)", n.Name(), n.ID())
  656. continue
  657. }
  658. for _, ep := range epl {
  659. if ep.Iface() == nil {
  660. logrus.Warnf("endpoint interface is empty for %q (%s)", ep.Name(), ep.ID())
  661. continue
  662. }
  663. if err := ep.assignAddress(ipam, true, ep.Iface().AddressIPv6() != nil); err != nil {
  664. logrus.Warnf("Failed to reserve current address for endpoint %q (%s) on network %q (%s)",
  665. ep.Name(), ep.ID(), n.Name(), n.ID())
  666. }
  667. }
  668. }
  669. }
  670. func doReplayPoolReserve(n *network) bool {
  671. _, caps, err := n.getController().getIPAMDriver(n.ipamType)
  672. if err != nil {
  673. logrus.Warnf("Failed to retrieve ipam driver for network %q (%s): %v", n.Name(), n.ID(), err)
  674. return false
  675. }
  676. return caps.RequiresRequestReplay
  677. }
  678. func (c *Controller) addNetwork(n *network) error {
  679. d, err := n.driver(true)
  680. if err != nil {
  681. return err
  682. }
  683. // Create the network
  684. if err := d.CreateNetwork(n.id, n.generic, n, n.getIPData(4), n.getIPData(6)); err != nil {
  685. return err
  686. }
  687. n.startResolver()
  688. return nil
  689. }
  690. // Networks returns the list of Network(s) managed by this controller.
  691. func (c *Controller) Networks() []Network {
  692. var list []Network
  693. for _, n := range c.getNetworksFromStore() {
  694. if n.inDelete {
  695. continue
  696. }
  697. list = append(list, n)
  698. }
  699. return list
  700. }
  701. // WalkNetworks uses the provided function to walk the Network(s) managed by this controller.
  702. func (c *Controller) WalkNetworks(walker NetworkWalker) {
  703. for _, n := range c.Networks() {
  704. if walker(n) {
  705. return
  706. }
  707. }
  708. }
  709. // NetworkByName returns the Network which has the passed name.
  710. // If not found, the error [ErrNoSuchNetwork] is returned.
  711. func (c *Controller) NetworkByName(name string) (Network, error) {
  712. if name == "" {
  713. return nil, ErrInvalidName(name)
  714. }
  715. var n Network
  716. s := func(current Network) bool {
  717. if current.Name() == name {
  718. n = current
  719. return true
  720. }
  721. return false
  722. }
  723. c.WalkNetworks(s)
  724. if n == nil {
  725. return nil, ErrNoSuchNetwork(name)
  726. }
  727. return n, nil
  728. }
  729. // NetworkByID returns the Network which has the passed id.
  730. // If not found, the error [ErrNoSuchNetwork] is returned.
  731. func (c *Controller) NetworkByID(id string) (Network, error) {
  732. if id == "" {
  733. return nil, ErrInvalidID(id)
  734. }
  735. n, err := c.getNetworkFromStore(id)
  736. if err != nil {
  737. return nil, ErrNoSuchNetwork(id)
  738. }
  739. return n, nil
  740. }
  741. // NewSandbox creates a new sandbox for containerID.
  742. func (c *Controller) NewSandbox(containerID string, options ...SandboxOption) (*Sandbox, error) {
  743. if containerID == "" {
  744. return nil, types.BadRequestErrorf("invalid container ID")
  745. }
  746. var sb *Sandbox
  747. c.mu.Lock()
  748. for _, s := range c.sandboxes {
  749. if s.containerID == containerID {
  750. // If not a stub, then we already have a complete sandbox.
  751. if !s.isStub {
  752. sbID := s.ID()
  753. c.mu.Unlock()
  754. return nil, types.ForbiddenErrorf("container %s is already present in sandbox %s", containerID, sbID)
  755. }
  756. // We already have a stub sandbox from the
  757. // store. Make use of it so that we don't lose
  758. // the endpoints from store but reset the
  759. // isStub flag.
  760. sb = s
  761. sb.isStub = false
  762. break
  763. }
  764. }
  765. c.mu.Unlock()
  766. sandboxID := stringid.GenerateRandomID()
  767. if runtime.GOOS == "windows" {
  768. sandboxID = containerID
  769. }
  770. // Create sandbox and process options first. Key generation depends on an option
  771. if sb == nil {
  772. sb = &Sandbox{
  773. id: sandboxID,
  774. containerID: containerID,
  775. endpoints: []*Endpoint{},
  776. epPriority: map[string]int{},
  777. populatedEndpoints: map[string]struct{}{},
  778. config: containerConfig{},
  779. controller: c,
  780. extDNS: []extDNSEntry{},
  781. }
  782. }
  783. sb.processOptions(options...)
  784. c.mu.Lock()
  785. if sb.ingress && c.ingressSandbox != nil {
  786. c.mu.Unlock()
  787. return nil, types.ForbiddenErrorf("ingress sandbox already present")
  788. }
  789. if sb.ingress {
  790. c.ingressSandbox = sb
  791. sb.config.hostsPath = filepath.Join(c.cfg.DataDir, "/network/files/hosts")
  792. sb.config.resolvConfPath = filepath.Join(c.cfg.DataDir, "/network/files/resolv.conf")
  793. sb.id = "ingress_sbox"
  794. } else if sb.loadBalancerNID != "" {
  795. sb.id = "lb_" + sb.loadBalancerNID
  796. }
  797. c.mu.Unlock()
  798. var err error
  799. defer func() {
  800. if err != nil {
  801. c.mu.Lock()
  802. if sb.ingress {
  803. c.ingressSandbox = nil
  804. }
  805. c.mu.Unlock()
  806. }
  807. }()
  808. if err = sb.setupResolutionFiles(); err != nil {
  809. return nil, err
  810. }
  811. if sb.config.useDefaultSandBox {
  812. c.sboxOnce.Do(func() {
  813. c.defOsSbox, err = osl.NewSandbox(sb.Key(), false, false)
  814. })
  815. if err != nil {
  816. c.sboxOnce = sync.Once{}
  817. return nil, fmt.Errorf("failed to create default sandbox: %v", err)
  818. }
  819. sb.osSbox = c.defOsSbox
  820. }
  821. if sb.osSbox == nil && !sb.config.useExternalKey {
  822. if sb.osSbox, err = osl.NewSandbox(sb.Key(), !sb.config.useDefaultSandBox, false); err != nil {
  823. return nil, fmt.Errorf("failed to create new osl sandbox: %v", err)
  824. }
  825. }
  826. if sb.osSbox != nil {
  827. // Apply operating specific knobs on the load balancer sandbox
  828. err := sb.osSbox.InvokeFunc(func() {
  829. sb.osSbox.ApplyOSTweaks(sb.oslTypes)
  830. })
  831. if err != nil {
  832. logrus.Errorf("Failed to apply performance tuning sysctls to the sandbox: %v", err)
  833. }
  834. // Keep this just so performance is not changed
  835. sb.osSbox.ApplyOSTweaks(sb.oslTypes)
  836. }
  837. c.mu.Lock()
  838. c.sandboxes[sb.id] = sb
  839. c.mu.Unlock()
  840. defer func() {
  841. if err != nil {
  842. c.mu.Lock()
  843. delete(c.sandboxes, sb.id)
  844. c.mu.Unlock()
  845. }
  846. }()
  847. err = sb.storeUpdate()
  848. if err != nil {
  849. return nil, fmt.Errorf("failed to update the store state of sandbox: %v", err)
  850. }
  851. return sb, nil
  852. }
  853. // Sandboxes returns the list of Sandbox(s) managed by this controller.
  854. func (c *Controller) Sandboxes() []*Sandbox {
  855. c.mu.Lock()
  856. defer c.mu.Unlock()
  857. list := make([]*Sandbox, 0, len(c.sandboxes))
  858. for _, s := range c.sandboxes {
  859. // Hide stub sandboxes from libnetwork users
  860. if s.isStub {
  861. continue
  862. }
  863. list = append(list, s)
  864. }
  865. return list
  866. }
  867. // WalkSandboxes uses the provided function to walk the Sandbox(s) managed by this controller.
  868. func (c *Controller) WalkSandboxes(walker SandboxWalker) {
  869. for _, sb := range c.Sandboxes() {
  870. if walker(sb) {
  871. return
  872. }
  873. }
  874. }
  875. // SandboxByID returns the Sandbox which has the passed id.
  876. // If not found, a [types.NotFoundError] is returned.
  877. func (c *Controller) SandboxByID(id string) (*Sandbox, error) {
  878. if id == "" {
  879. return nil, ErrInvalidID(id)
  880. }
  881. c.mu.Lock()
  882. s, ok := c.sandboxes[id]
  883. c.mu.Unlock()
  884. if !ok {
  885. return nil, types.NotFoundErrorf("sandbox %s not found", id)
  886. }
  887. return s, nil
  888. }
  889. // SandboxDestroy destroys a sandbox given a container ID.
  890. func (c *Controller) SandboxDestroy(id string) error {
  891. var sb *Sandbox
  892. c.mu.Lock()
  893. for _, s := range c.sandboxes {
  894. if s.containerID == id {
  895. sb = s
  896. break
  897. }
  898. }
  899. c.mu.Unlock()
  900. // It is not an error if sandbox is not available
  901. if sb == nil {
  902. return nil
  903. }
  904. return sb.Delete()
  905. }
  906. // SandboxContainerWalker returns a Sandbox Walker function which looks for an existing Sandbox with the passed containerID
  907. func SandboxContainerWalker(out **Sandbox, containerID string) SandboxWalker {
  908. return func(sb *Sandbox) bool {
  909. if sb.ContainerID() == containerID {
  910. *out = sb
  911. return true
  912. }
  913. return false
  914. }
  915. }
  916. // SandboxKeyWalker returns a Sandbox Walker function which looks for an existing Sandbox with the passed key
  917. func SandboxKeyWalker(out **Sandbox, key string) SandboxWalker {
  918. return func(sb *Sandbox) bool {
  919. if sb.Key() == key {
  920. *out = sb
  921. return true
  922. }
  923. return false
  924. }
  925. }
  926. func (c *Controller) loadDriver(networkType string) error {
  927. var err error
  928. if pg := c.GetPluginGetter(); pg != nil {
  929. _, err = pg.Get(networkType, driverapi.NetworkPluginEndpointType, plugingetter.Lookup)
  930. } else {
  931. _, err = plugins.Get(networkType, driverapi.NetworkPluginEndpointType)
  932. }
  933. if err != nil {
  934. if errors.Cause(err) == plugins.ErrNotFound {
  935. return types.NotFoundErrorf(err.Error())
  936. }
  937. return err
  938. }
  939. return nil
  940. }
  941. func (c *Controller) loadIPAMDriver(name string) error {
  942. var err error
  943. if pg := c.GetPluginGetter(); pg != nil {
  944. _, err = pg.Get(name, ipamapi.PluginEndpointType, plugingetter.Lookup)
  945. } else {
  946. _, err = plugins.Get(name, ipamapi.PluginEndpointType)
  947. }
  948. if err != nil {
  949. if errors.Cause(err) == plugins.ErrNotFound {
  950. return types.NotFoundErrorf(err.Error())
  951. }
  952. return err
  953. }
  954. return nil
  955. }
  956. func (c *Controller) getIPAMDriver(name string) (ipamapi.Ipam, *ipamapi.Capability, error) {
  957. id, cap := c.ipamRegistry.IPAM(name)
  958. if id == nil {
  959. // Might be a plugin name. Try loading it
  960. if err := c.loadIPAMDriver(name); err != nil {
  961. return nil, nil, err
  962. }
  963. // Now that we resolved the plugin, try again looking up the registry
  964. id, cap = c.ipamRegistry.IPAM(name)
  965. if id == nil {
  966. return nil, nil, types.BadRequestErrorf("invalid ipam driver: %q", name)
  967. }
  968. }
  969. return id, cap, nil
  970. }
  971. // Stop stops the network controller.
  972. func (c *Controller) Stop() {
  973. c.closeStores()
  974. c.stopExternalKeyListener()
  975. osl.GC()
  976. }
  977. // StartDiagnostic starts the network diagnostic server listening on port.
  978. func (c *Controller) StartDiagnostic(port int) {
  979. c.mu.Lock()
  980. if !c.DiagnosticServer.IsDiagnosticEnabled() {
  981. c.DiagnosticServer.EnableDiagnostic("127.0.0.1", port)
  982. }
  983. c.mu.Unlock()
  984. }
  985. // StopDiagnostic stops the network diagnostic server.
  986. func (c *Controller) StopDiagnostic() {
  987. c.mu.Lock()
  988. if c.DiagnosticServer.IsDiagnosticEnabled() {
  989. c.DiagnosticServer.DisableDiagnostic()
  990. }
  991. c.mu.Unlock()
  992. }
  993. // IsDiagnosticEnabled returns true if the diagnostic server is running.
  994. func (c *Controller) IsDiagnosticEnabled() bool {
  995. c.mu.Lock()
  996. defer c.mu.Unlock()
  997. return c.DiagnosticServer.IsDiagnosticEnabled()
  998. }
  999. func (c *Controller) iptablesEnabled() bool {
  1000. c.mu.Lock()
  1001. defer c.mu.Unlock()
  1002. if c.cfg == nil {
  1003. return false
  1004. }
  1005. // parse map cfg["bridge"]["generic"]["EnableIPTable"]
  1006. cfgBridge, ok := c.cfg.DriverCfg["bridge"].(map[string]interface{})
  1007. if !ok {
  1008. return false
  1009. }
  1010. cfgGeneric, ok := cfgBridge[netlabel.GenericData].(options.Generic)
  1011. if !ok {
  1012. return false
  1013. }
  1014. enabled, ok := cfgGeneric["EnableIPTables"].(bool)
  1015. if !ok {
  1016. // unless user explicitly stated, assume iptable is enabled
  1017. enabled = true
  1018. }
  1019. return enabled
  1020. }
  1021. func (c *Controller) ip6tablesEnabled() bool {
  1022. c.mu.Lock()
  1023. defer c.mu.Unlock()
  1024. if c.cfg == nil {
  1025. return false
  1026. }
  1027. // parse map cfg["bridge"]["generic"]["EnableIP6Table"]
  1028. cfgBridge, ok := c.cfg.DriverCfg["bridge"].(map[string]interface{})
  1029. if !ok {
  1030. return false
  1031. }
  1032. cfgGeneric, ok := cfgBridge[netlabel.GenericData].(options.Generic)
  1033. if !ok {
  1034. return false
  1035. }
  1036. enabled, _ := cfgGeneric["EnableIP6Tables"].(bool)
  1037. return enabled
  1038. }