controller.go 32 KB

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