controller.go 32 KB

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