controller.go 31 KB

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