controller.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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. ingressSandbox *Sandbox
  86. agent *nwAgent
  87. networkLocker *locker.Locker
  88. agentInitDone chan struct{}
  89. agentStopDone chan struct{}
  90. keys []*types.EncryptionKey
  91. DiagnosticServer *diagnostic.Server
  92. mu sync.Mutex
  93. // FIXME(thaJeztah): defOsSbox is always nil on non-Linux: move these fields to Linux-only files.
  94. defOsSboxOnce sync.Once
  95. defOsSbox *osl.Namespace
  96. }
  97. // New creates a new instance of network controller.
  98. func New(cfgOptions ...config.Option) (*Controller, error) {
  99. c := &Controller{
  100. id: stringid.GenerateRandomID(),
  101. cfg: config.New(cfgOptions...),
  102. sandboxes: map[string]*Sandbox{},
  103. svcRecords: make(map[string]*svcInfo),
  104. serviceBindings: make(map[serviceKey]*service),
  105. agentInitDone: make(chan struct{}),
  106. networkLocker: locker.New(),
  107. DiagnosticServer: diagnostic.New(),
  108. }
  109. c.DiagnosticServer.Init()
  110. if err := c.initStores(); err != nil {
  111. return nil, err
  112. }
  113. c.drvRegistry.Notify = c
  114. // External plugins don't need config passed through daemon. They can
  115. // bootstrap themselves.
  116. if err := remotedriver.Register(&c.drvRegistry, c.cfg.PluginGetter); err != nil {
  117. return nil, err
  118. }
  119. if err := registerNetworkDrivers(&c.drvRegistry, c.makeDriverConfig); err != nil {
  120. return nil, err
  121. }
  122. if err := initIPAMDrivers(&c.ipamRegistry, c.cfg.PluginGetter, c.cfg.DefaultAddressPool); err != nil {
  123. return nil, err
  124. }
  125. c.WalkNetworks(populateSpecial)
  126. // Reserve pools first before doing cleanup. Otherwise the
  127. // cleanups of endpoint/network and sandbox below will
  128. // generate many unnecessary warnings
  129. c.reservePools()
  130. // Cleanup resources
  131. c.sandboxCleanup(c.cfg.ActiveSandboxes)
  132. c.cleanupLocalEndpoints()
  133. c.networkCleanup()
  134. if err := c.startExternalKeyListener(); err != nil {
  135. return nil, err
  136. }
  137. setupArrangeUserFilterRule(c)
  138. return c, nil
  139. }
  140. // SetClusterProvider sets the cluster provider.
  141. func (c *Controller) SetClusterProvider(provider cluster.Provider) {
  142. var sameProvider bool
  143. c.mu.Lock()
  144. // Avoids to spawn multiple goroutine for the same cluster provider
  145. if c.cfg.ClusterProvider == provider {
  146. // If the cluster provider is already set, there is already a go routine spawned
  147. // that is listening for events, so nothing to do here
  148. sameProvider = true
  149. } else {
  150. c.cfg.ClusterProvider = provider
  151. }
  152. c.mu.Unlock()
  153. if provider == nil || sameProvider {
  154. return
  155. }
  156. // We don't want to spawn a new go routine if the previous one did not exit yet
  157. c.AgentStopWait()
  158. go c.clusterAgentInit()
  159. }
  160. // SetKeys configures the encryption key for gossip and overlay data path.
  161. func (c *Controller) SetKeys(keys []*types.EncryptionKey) error {
  162. // libnetwork side of agent depends on the keys. On the first receipt of
  163. // keys setup the agent. For subsequent key set handle the key change
  164. subsysKeys := make(map[string]int)
  165. for _, key := range keys {
  166. if key.Subsystem != subsysGossip &&
  167. key.Subsystem != subsysIPSec {
  168. return fmt.Errorf("key received for unrecognized subsystem")
  169. }
  170. subsysKeys[key.Subsystem]++
  171. }
  172. for s, count := range subsysKeys {
  173. if count != keyringSize {
  174. return fmt.Errorf("incorrect number of keys for subsystem %v", s)
  175. }
  176. }
  177. if c.getAgent() == nil {
  178. c.mu.Lock()
  179. c.keys = keys
  180. c.mu.Unlock()
  181. return nil
  182. }
  183. return c.handleKeyChange(keys)
  184. }
  185. func (c *Controller) getAgent() *nwAgent {
  186. c.mu.Lock()
  187. defer c.mu.Unlock()
  188. return c.agent
  189. }
  190. func (c *Controller) clusterAgentInit() {
  191. clusterProvider := c.cfg.ClusterProvider
  192. var keysAvailable bool
  193. for {
  194. eventType := <-clusterProvider.ListenClusterEvents()
  195. // The events: EventSocketChange, EventNodeReady and EventNetworkKeysAvailable are not ordered
  196. // when all the condition for the agent initialization are met then proceed with it
  197. switch eventType {
  198. case cluster.EventNetworkKeysAvailable:
  199. // Validates that the keys are actually available before starting the initialization
  200. // This will handle old spurious messages left on the channel
  201. c.mu.Lock()
  202. keysAvailable = c.keys != nil
  203. c.mu.Unlock()
  204. fallthrough
  205. case cluster.EventSocketChange, cluster.EventNodeReady:
  206. if keysAvailable && !c.isDistributedControl() {
  207. c.agentOperationStart()
  208. if err := c.agentSetup(clusterProvider); err != nil {
  209. c.agentStopComplete()
  210. } else {
  211. c.agentInitComplete()
  212. }
  213. }
  214. case cluster.EventNodeLeave:
  215. c.agentOperationStart()
  216. c.mu.Lock()
  217. c.keys = nil
  218. c.mu.Unlock()
  219. // We are leaving the cluster. Make sure we
  220. // close the gossip so that we stop all
  221. // incoming gossip updates before cleaning up
  222. // any remaining service bindings. But before
  223. // deleting the networks since the networks
  224. // should still be present when cleaning up
  225. // service bindings
  226. c.agentClose()
  227. c.cleanupServiceDiscovery("")
  228. c.cleanupServiceBindings("")
  229. c.agentStopComplete()
  230. return
  231. }
  232. }
  233. }
  234. // AgentInitWait waits for agent initialization to be completed in the controller.
  235. func (c *Controller) AgentInitWait() {
  236. c.mu.Lock()
  237. agentInitDone := c.agentInitDone
  238. c.mu.Unlock()
  239. if agentInitDone != nil {
  240. <-agentInitDone
  241. }
  242. }
  243. // AgentStopWait waits for the Agent stop to be completed in the controller.
  244. func (c *Controller) AgentStopWait() {
  245. c.mu.Lock()
  246. agentStopDone := c.agentStopDone
  247. c.mu.Unlock()
  248. if agentStopDone != nil {
  249. <-agentStopDone
  250. }
  251. }
  252. // agentOperationStart marks the start of an Agent Init or Agent Stop
  253. func (c *Controller) agentOperationStart() {
  254. c.mu.Lock()
  255. if c.agentInitDone == nil {
  256. c.agentInitDone = make(chan struct{})
  257. }
  258. if c.agentStopDone == nil {
  259. c.agentStopDone = make(chan struct{})
  260. }
  261. c.mu.Unlock()
  262. }
  263. // agentInitComplete notifies the successful completion of the Agent initialization
  264. func (c *Controller) agentInitComplete() {
  265. c.mu.Lock()
  266. if c.agentInitDone != nil {
  267. close(c.agentInitDone)
  268. c.agentInitDone = nil
  269. }
  270. c.mu.Unlock()
  271. }
  272. // agentStopComplete notifies the successful completion of the Agent stop
  273. func (c *Controller) agentStopComplete() {
  274. c.mu.Lock()
  275. if c.agentStopDone != nil {
  276. close(c.agentStopDone)
  277. c.agentStopDone = nil
  278. }
  279. c.mu.Unlock()
  280. }
  281. func (c *Controller) makeDriverConfig(ntype string) map[string]interface{} {
  282. if c.cfg == nil {
  283. return nil
  284. }
  285. cfg := map[string]interface{}{}
  286. for _, label := range c.cfg.Labels {
  287. key, val, _ := strings.Cut(label, "=")
  288. if !strings.HasPrefix(key, netlabel.DriverPrefix+"."+ntype) {
  289. continue
  290. }
  291. cfg[key] = val
  292. }
  293. // Merge in the existing config for this driver.
  294. for k, v := range c.cfg.DriverConfig(ntype) {
  295. cfg[k] = v
  296. }
  297. if c.cfg.Scope.IsValid() {
  298. // FIXME: every driver instance constructs a new DataStore
  299. // instance against the same database. Yikes!
  300. cfg[netlabel.LocalKVClient] = discoverapi.DatastoreConfigData{
  301. Scope: scope.Local,
  302. Provider: c.cfg.Scope.Client.Provider,
  303. Address: c.cfg.Scope.Client.Address,
  304. Config: c.cfg.Scope.Client.Config,
  305. }
  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) isDistributedControl() 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.isDistributedControl() && !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.isDistributedControl() {
  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.isDistributedControl() {
  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() []*Network {
  722. var list []*Network
  723. for _, n := range c.getNetworksFromStore() {
  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() {
  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. n, err := c.getNetworkFromStore(id)
  765. if err != nil {
  766. return nil, ErrNoSuchNetwork(id)
  767. }
  768. return n, nil
  769. }
  770. // NewSandbox creates a new sandbox for containerID.
  771. func (c *Controller) NewSandbox(containerID string, options ...SandboxOption) (_ *Sandbox, retErr error) {
  772. if containerID == "" {
  773. return nil, types.InvalidParameterErrorf("invalid container ID")
  774. }
  775. var sb *Sandbox
  776. c.mu.Lock()
  777. for _, s := range c.sandboxes {
  778. if s.containerID == containerID {
  779. // If not a stub, then we already have a complete sandbox.
  780. if !s.isStub {
  781. sbID := s.ID()
  782. c.mu.Unlock()
  783. return nil, types.ForbiddenErrorf("container %s is already present in sandbox %s", containerID, sbID)
  784. }
  785. // We already have a stub sandbox from the
  786. // store. Make use of it so that we don't lose
  787. // the endpoints from store but reset the
  788. // isStub flag.
  789. sb = s
  790. sb.isStub = false
  791. break
  792. }
  793. }
  794. c.mu.Unlock()
  795. // Create sandbox and process options first. Key generation depends on an option
  796. if sb == nil {
  797. // 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?
  798. sandboxID := containerID
  799. if runtime.GOOS != "windows" {
  800. sandboxID = stringid.GenerateRandomID()
  801. }
  802. sb = &Sandbox{
  803. id: sandboxID,
  804. containerID: containerID,
  805. endpoints: []*Endpoint{},
  806. epPriority: map[string]int{},
  807. populatedEndpoints: map[string]struct{}{},
  808. config: containerConfig{},
  809. controller: c,
  810. extDNS: []extDNSEntry{},
  811. }
  812. }
  813. sb.processOptions(options...)
  814. c.mu.Lock()
  815. if sb.ingress && c.ingressSandbox != nil {
  816. c.mu.Unlock()
  817. return nil, types.ForbiddenErrorf("ingress sandbox already present")
  818. }
  819. if sb.ingress {
  820. c.ingressSandbox = sb
  821. sb.config.hostsPath = filepath.Join(c.cfg.DataDir, "/network/files/hosts")
  822. sb.config.resolvConfPath = filepath.Join(c.cfg.DataDir, "/network/files/resolv.conf")
  823. sb.id = "ingress_sbox"
  824. } else if sb.loadBalancerNID != "" {
  825. sb.id = "lb_" + sb.loadBalancerNID
  826. }
  827. c.mu.Unlock()
  828. defer func() {
  829. if retErr != nil {
  830. c.mu.Lock()
  831. if sb.ingress {
  832. c.ingressSandbox = nil
  833. }
  834. c.mu.Unlock()
  835. }
  836. }()
  837. if err := sb.setupResolutionFiles(); err != nil {
  838. return nil, err
  839. }
  840. if err := c.setupOSLSandbox(sb); err != nil {
  841. return nil, err
  842. }
  843. c.mu.Lock()
  844. c.sandboxes[sb.id] = sb
  845. c.mu.Unlock()
  846. defer func() {
  847. if retErr != nil {
  848. c.mu.Lock()
  849. delete(c.sandboxes, sb.id)
  850. c.mu.Unlock()
  851. }
  852. }()
  853. if err := sb.storeUpdate(); err != nil {
  854. return nil, fmt.Errorf("failed to update the store state of sandbox: %v", err)
  855. }
  856. return sb, nil
  857. }
  858. // GetSandbox returns the Sandbox which has the passed id.
  859. //
  860. // It returns an [ErrInvalidID] when passing an invalid ID, or an
  861. // [types.NotFoundError] if no Sandbox was found for the container.
  862. func (c *Controller) GetSandbox(containerID string) (*Sandbox, error) {
  863. if containerID == "" {
  864. return nil, ErrInvalidID("id is empty")
  865. }
  866. c.mu.Lock()
  867. defer c.mu.Unlock()
  868. if runtime.GOOS == "windows" {
  869. // fast-path for Windows, which uses the container ID as sandbox ID.
  870. if sb := c.sandboxes[containerID]; sb != nil && !sb.isStub {
  871. return sb, nil
  872. }
  873. } else {
  874. for _, sb := range c.sandboxes {
  875. if sb.containerID == containerID && !sb.isStub {
  876. return sb, nil
  877. }
  878. }
  879. }
  880. return nil, types.NotFoundErrorf("network sandbox for container %s not found", containerID)
  881. }
  882. // SandboxByID returns the Sandbox which has the passed id.
  883. // If not found, a [types.NotFoundError] is returned.
  884. func (c *Controller) SandboxByID(id string) (*Sandbox, error) {
  885. if id == "" {
  886. return nil, ErrInvalidID(id)
  887. }
  888. c.mu.Lock()
  889. s, ok := c.sandboxes[id]
  890. c.mu.Unlock()
  891. if !ok {
  892. return nil, types.NotFoundErrorf("sandbox %s not found", id)
  893. }
  894. return s, nil
  895. }
  896. // SandboxDestroy destroys a sandbox given a container ID.
  897. func (c *Controller) SandboxDestroy(id string) error {
  898. var sb *Sandbox
  899. c.mu.Lock()
  900. for _, s := range c.sandboxes {
  901. if s.containerID == id {
  902. sb = s
  903. break
  904. }
  905. }
  906. c.mu.Unlock()
  907. // It is not an error if sandbox is not available
  908. if sb == nil {
  909. return nil
  910. }
  911. return sb.Delete()
  912. }
  913. func (c *Controller) loadDriver(networkType string) error {
  914. var err error
  915. if pg := c.GetPluginGetter(); pg != nil {
  916. _, err = pg.Get(networkType, driverapi.NetworkPluginEndpointType, plugingetter.Lookup)
  917. } else {
  918. _, err = plugins.Get(networkType, driverapi.NetworkPluginEndpointType)
  919. }
  920. if err != nil {
  921. if errors.Cause(err) == plugins.ErrNotFound {
  922. return types.NotFoundErrorf(err.Error())
  923. }
  924. return err
  925. }
  926. return nil
  927. }
  928. func (c *Controller) loadIPAMDriver(name string) error {
  929. var err error
  930. if pg := c.GetPluginGetter(); pg != nil {
  931. _, err = pg.Get(name, ipamapi.PluginEndpointType, plugingetter.Lookup)
  932. } else {
  933. _, err = plugins.Get(name, ipamapi.PluginEndpointType)
  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) getIPAMDriver(name string) (ipamapi.Ipam, *ipamapi.Capability, error) {
  944. id, caps := c.ipamRegistry.IPAM(name)
  945. if id == nil {
  946. // Might be a plugin name. Try loading it
  947. if err := c.loadIPAMDriver(name); err != nil {
  948. return nil, nil, err
  949. }
  950. // Now that we resolved the plugin, try again looking up the registry
  951. id, caps = c.ipamRegistry.IPAM(name)
  952. if id == nil {
  953. return nil, nil, types.InvalidParameterErrorf("invalid ipam driver: %q", name)
  954. }
  955. }
  956. return id, caps, nil
  957. }
  958. // Stop stops the network controller.
  959. func (c *Controller) Stop() {
  960. c.closeStores()
  961. c.stopExternalKeyListener()
  962. osl.GC()
  963. }
  964. // StartDiagnostic starts the network diagnostic server listening on port.
  965. func (c *Controller) StartDiagnostic(port int) {
  966. c.mu.Lock()
  967. if !c.DiagnosticServer.IsDiagnosticEnabled() {
  968. c.DiagnosticServer.EnableDiagnostic("127.0.0.1", port)
  969. }
  970. c.mu.Unlock()
  971. }
  972. // StopDiagnostic stops the network diagnostic server.
  973. func (c *Controller) StopDiagnostic() {
  974. c.mu.Lock()
  975. if c.DiagnosticServer.IsDiagnosticEnabled() {
  976. c.DiagnosticServer.DisableDiagnostic()
  977. }
  978. c.mu.Unlock()
  979. }
  980. // IsDiagnosticEnabled returns true if the diagnostic server is running.
  981. func (c *Controller) IsDiagnosticEnabled() bool {
  982. c.mu.Lock()
  983. defer c.mu.Unlock()
  984. return c.DiagnosticServer.IsDiagnosticEnabled()
  985. }