controller.go 31 KB

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