network.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. package daemon
  2. import (
  3. "fmt"
  4. "net"
  5. "runtime"
  6. "sort"
  7. "strings"
  8. "sync"
  9. "github.com/docker/docker/api/types"
  10. "github.com/docker/docker/api/types/network"
  11. clustertypes "github.com/docker/docker/daemon/cluster/provider"
  12. "github.com/docker/docker/pkg/plugingetter"
  13. "github.com/docker/docker/runconfig"
  14. "github.com/docker/libnetwork"
  15. lncluster "github.com/docker/libnetwork/cluster"
  16. "github.com/docker/libnetwork/driverapi"
  17. "github.com/docker/libnetwork/ipamapi"
  18. networktypes "github.com/docker/libnetwork/types"
  19. "github.com/pkg/errors"
  20. "github.com/sirupsen/logrus"
  21. "golang.org/x/net/context"
  22. )
  23. // NetworkControllerEnabled checks if the networking stack is enabled.
  24. // This feature depends on OS primitives and it's disabled in systems like Windows.
  25. func (daemon *Daemon) NetworkControllerEnabled() bool {
  26. return daemon.netController != nil
  27. }
  28. // FindNetwork function finds a network for a given string that can represent network name or id
  29. func (daemon *Daemon) FindNetwork(idName string) (libnetwork.Network, error) {
  30. // Find by Name
  31. n, err := daemon.GetNetworkByName(idName)
  32. if err != nil && !isNoSuchNetworkError(err) {
  33. return nil, err
  34. }
  35. if n != nil {
  36. return n, nil
  37. }
  38. // Find by id
  39. return daemon.GetNetworkByID(idName)
  40. }
  41. func isNoSuchNetworkError(err error) bool {
  42. _, ok := err.(libnetwork.ErrNoSuchNetwork)
  43. return ok
  44. }
  45. // GetNetworkByID function returns a network whose ID begins with the given prefix.
  46. // It fails with an error if no matching, or more than one matching, networks are found.
  47. func (daemon *Daemon) GetNetworkByID(partialID string) (libnetwork.Network, error) {
  48. list := daemon.GetNetworksByID(partialID)
  49. if len(list) == 0 {
  50. return nil, errors.WithStack(networkNotFound(partialID))
  51. }
  52. if len(list) > 1 {
  53. return nil, errors.WithStack(invalidIdentifier(partialID))
  54. }
  55. return list[0], nil
  56. }
  57. // GetNetworkByName function returns a network for a given network name.
  58. // If no network name is given, the default network is returned.
  59. func (daemon *Daemon) GetNetworkByName(name string) (libnetwork.Network, error) {
  60. c := daemon.netController
  61. if c == nil {
  62. return nil, libnetwork.ErrNoSuchNetwork(name)
  63. }
  64. if name == "" {
  65. name = c.Config().Daemon.DefaultNetwork
  66. }
  67. return c.NetworkByName(name)
  68. }
  69. // GetNetworksByID returns a list of networks whose ID partially matches zero or more networks
  70. func (daemon *Daemon) GetNetworksByID(partialID string) []libnetwork.Network {
  71. c := daemon.netController
  72. if c == nil {
  73. return nil
  74. }
  75. list := []libnetwork.Network{}
  76. l := func(nw libnetwork.Network) bool {
  77. if strings.HasPrefix(nw.ID(), partialID) {
  78. list = append(list, nw)
  79. }
  80. return false
  81. }
  82. c.WalkNetworks(l)
  83. return list
  84. }
  85. // getAllNetworks returns a list containing all networks
  86. func (daemon *Daemon) getAllNetworks() []libnetwork.Network {
  87. return daemon.netController.Networks()
  88. }
  89. type ingressJob struct {
  90. create *clustertypes.NetworkCreateRequest
  91. ip net.IP
  92. jobDone chan struct{}
  93. }
  94. var (
  95. ingressWorkerOnce sync.Once
  96. ingressJobsChannel chan *ingressJob
  97. ingressID string
  98. )
  99. func (daemon *Daemon) startIngressWorker() {
  100. ingressJobsChannel = make(chan *ingressJob, 100)
  101. go func() {
  102. // nolint: gosimple
  103. for {
  104. select {
  105. case r := <-ingressJobsChannel:
  106. if r.create != nil {
  107. daemon.setupIngress(r.create, r.ip, ingressID)
  108. ingressID = r.create.ID
  109. } else {
  110. daemon.releaseIngress(ingressID)
  111. ingressID = ""
  112. }
  113. close(r.jobDone)
  114. }
  115. }
  116. }()
  117. }
  118. // enqueueIngressJob adds a ingress add/rm request to the worker queue.
  119. // It guarantees the worker is started.
  120. func (daemon *Daemon) enqueueIngressJob(job *ingressJob) {
  121. ingressWorkerOnce.Do(daemon.startIngressWorker)
  122. ingressJobsChannel <- job
  123. }
  124. // SetupIngress setups ingress networking.
  125. // The function returns a channel which will signal the caller when the programming is completed.
  126. func (daemon *Daemon) SetupIngress(create clustertypes.NetworkCreateRequest, nodeIP string) (<-chan struct{}, error) {
  127. ip, _, err := net.ParseCIDR(nodeIP)
  128. if err != nil {
  129. return nil, err
  130. }
  131. done := make(chan struct{})
  132. daemon.enqueueIngressJob(&ingressJob{&create, ip, done})
  133. return done, nil
  134. }
  135. // ReleaseIngress releases the ingress networking.
  136. // The function returns a channel which will signal the caller when the programming is completed.
  137. func (daemon *Daemon) ReleaseIngress() (<-chan struct{}, error) {
  138. done := make(chan struct{})
  139. daemon.enqueueIngressJob(&ingressJob{nil, nil, done})
  140. return done, nil
  141. }
  142. func (daemon *Daemon) setupIngress(create *clustertypes.NetworkCreateRequest, ip net.IP, staleID string) {
  143. controller := daemon.netController
  144. controller.AgentInitWait()
  145. if staleID != "" && staleID != create.ID {
  146. daemon.releaseIngress(staleID)
  147. }
  148. if _, err := daemon.createNetwork(create.NetworkCreateRequest, create.ID, true); err != nil {
  149. // If it is any other error other than already
  150. // exists error log error and return.
  151. if _, ok := err.(libnetwork.NetworkNameError); !ok {
  152. logrus.Errorf("Failed creating ingress network: %v", err)
  153. return
  154. }
  155. // Otherwise continue down the call to create or recreate sandbox.
  156. }
  157. n, err := daemon.GetNetworkByID(create.ID)
  158. if err != nil {
  159. logrus.Errorf("Failed getting ingress network by id after creating: %v", err)
  160. }
  161. if err = daemon.createLoadBalancerSandbox("ingress", create.ID, ip, n, libnetwork.OptionIngress()); err != nil {
  162. logrus.Errorf("Failed creating load balancer sandbox for ingress network: %v", err)
  163. }
  164. }
  165. func (daemon *Daemon) releaseIngress(id string) {
  166. controller := daemon.netController
  167. if err := controller.SandboxDestroy("ingress-sbox"); err != nil {
  168. logrus.Errorf("Failed to delete ingress sandbox: %v", err)
  169. }
  170. if id == "" {
  171. return
  172. }
  173. n, err := controller.NetworkByID(id)
  174. if err != nil {
  175. logrus.Errorf("failed to retrieve ingress network %s: %v", id, err)
  176. return
  177. }
  178. for _, ep := range n.Endpoints() {
  179. if err := ep.Delete(true); err != nil {
  180. logrus.Errorf("Failed to delete endpoint %s (%s): %v", ep.Name(), ep.ID(), err)
  181. return
  182. }
  183. }
  184. if err := n.Delete(); err != nil {
  185. logrus.Errorf("Failed to delete ingress network %s: %v", n.ID(), err)
  186. return
  187. }
  188. }
  189. // SetNetworkBootstrapKeys sets the bootstrap keys.
  190. func (daemon *Daemon) SetNetworkBootstrapKeys(keys []*networktypes.EncryptionKey) error {
  191. err := daemon.netController.SetKeys(keys)
  192. if err == nil {
  193. // Upon successful key setting dispatch the keys available event
  194. daemon.cluster.SendClusterEvent(lncluster.EventNetworkKeysAvailable)
  195. }
  196. return err
  197. }
  198. // UpdateAttachment notifies the attacher about the attachment config.
  199. func (daemon *Daemon) UpdateAttachment(networkName, networkID, containerID string, config *network.NetworkingConfig) error {
  200. if daemon.clusterProvider == nil {
  201. return fmt.Errorf("cluster provider is not initialized")
  202. }
  203. if err := daemon.clusterProvider.UpdateAttachment(networkName, containerID, config); err != nil {
  204. return daemon.clusterProvider.UpdateAttachment(networkID, containerID, config)
  205. }
  206. return nil
  207. }
  208. // WaitForDetachment makes the cluster manager wait for detachment of
  209. // the container from the network.
  210. func (daemon *Daemon) WaitForDetachment(ctx context.Context, networkName, networkID, taskID, containerID string) error {
  211. if daemon.clusterProvider == nil {
  212. return fmt.Errorf("cluster provider is not initialized")
  213. }
  214. return daemon.clusterProvider.WaitForDetachment(ctx, networkName, networkID, taskID, containerID)
  215. }
  216. // CreateManagedNetwork creates an agent network.
  217. func (daemon *Daemon) CreateManagedNetwork(create clustertypes.NetworkCreateRequest) error {
  218. _, err := daemon.createNetwork(create.NetworkCreateRequest, create.ID, true)
  219. return err
  220. }
  221. // CreateNetwork creates a network with the given name, driver and other optional parameters
  222. func (daemon *Daemon) CreateNetwork(create types.NetworkCreateRequest) (*types.NetworkCreateResponse, error) {
  223. resp, err := daemon.createNetwork(create, "", false)
  224. if err != nil {
  225. return nil, err
  226. }
  227. return resp, err
  228. }
  229. func (daemon *Daemon) createLoadBalancerSandbox(prefix, id string, ip net.IP, n libnetwork.Network, options ...libnetwork.SandboxOption) error {
  230. c := daemon.netController
  231. sandboxName := prefix + "-sbox"
  232. sb, err := c.NewSandbox(sandboxName, options...)
  233. if err != nil {
  234. if _, ok := err.(networktypes.ForbiddenError); !ok {
  235. return errors.Wrapf(err, "Failed creating %s sandbox", sandboxName)
  236. }
  237. return nil
  238. }
  239. endpointName := prefix + "-endpoint"
  240. ep, err := n.CreateEndpoint(endpointName, libnetwork.CreateOptionIpam(ip, nil, nil, nil), libnetwork.CreateOptionLoadBalancer())
  241. if err != nil {
  242. return errors.Wrapf(err, "Failed creating %s in sandbox %s", endpointName, sandboxName)
  243. }
  244. if err := ep.Join(sb, nil); err != nil {
  245. return errors.Wrapf(err, "Failed joining %s to sandbox %s", endpointName, sandboxName)
  246. }
  247. if err := sb.EnableService(); err != nil {
  248. return errors.Wrapf(err, "Failed enabling service in %s sandbox", sandboxName)
  249. }
  250. return nil
  251. }
  252. func (daemon *Daemon) createNetwork(create types.NetworkCreateRequest, id string, agent bool) (*types.NetworkCreateResponse, error) {
  253. if runconfig.IsPreDefinedNetwork(create.Name) && !agent {
  254. err := fmt.Errorf("%s is a pre-defined network and cannot be created", create.Name)
  255. return nil, notAllowedError{err}
  256. }
  257. var warning string
  258. nw, err := daemon.GetNetworkByName(create.Name)
  259. if err != nil {
  260. if _, ok := err.(libnetwork.ErrNoSuchNetwork); !ok {
  261. return nil, err
  262. }
  263. }
  264. if nw != nil {
  265. // check if user defined CheckDuplicate, if set true, return err
  266. // otherwise prepare a warning message
  267. if create.CheckDuplicate {
  268. return nil, libnetwork.NetworkNameError(create.Name)
  269. }
  270. warning = fmt.Sprintf("Network with name %s (id : %s) already exists", nw.Name(), nw.ID())
  271. }
  272. c := daemon.netController
  273. driver := create.Driver
  274. if driver == "" {
  275. driver = c.Config().Daemon.DefaultDriver
  276. }
  277. nwOptions := []libnetwork.NetworkOption{
  278. libnetwork.NetworkOptionEnableIPv6(create.EnableIPv6),
  279. libnetwork.NetworkOptionDriverOpts(create.Options),
  280. libnetwork.NetworkOptionLabels(create.Labels),
  281. libnetwork.NetworkOptionAttachable(create.Attachable),
  282. libnetwork.NetworkOptionIngress(create.Ingress),
  283. libnetwork.NetworkOptionScope(create.Scope),
  284. }
  285. if create.ConfigOnly {
  286. nwOptions = append(nwOptions, libnetwork.NetworkOptionConfigOnly())
  287. }
  288. if create.IPAM != nil {
  289. ipam := create.IPAM
  290. v4Conf, v6Conf, err := getIpamConfig(ipam.Config)
  291. if err != nil {
  292. return nil, err
  293. }
  294. nwOptions = append(nwOptions, libnetwork.NetworkOptionIpam(ipam.Driver, "", v4Conf, v6Conf, ipam.Options))
  295. }
  296. if create.Internal {
  297. nwOptions = append(nwOptions, libnetwork.NetworkOptionInternalNetwork())
  298. }
  299. if agent {
  300. nwOptions = append(nwOptions, libnetwork.NetworkOptionDynamic())
  301. nwOptions = append(nwOptions, libnetwork.NetworkOptionPersist(false))
  302. }
  303. if create.ConfigFrom != nil {
  304. nwOptions = append(nwOptions, libnetwork.NetworkOptionConfigFrom(create.ConfigFrom.Network))
  305. }
  306. n, err := c.NewNetwork(driver, create.Name, id, nwOptions...)
  307. if err != nil {
  308. if _, ok := err.(libnetwork.ErrDataStoreNotInitialized); ok {
  309. // nolint: golint
  310. return nil, errors.New("This node is not a swarm manager. Use \"docker swarm init\" or \"docker swarm join\" to connect this node to swarm and try again.")
  311. }
  312. return nil, err
  313. }
  314. daemon.pluginRefCount(driver, driverapi.NetworkPluginEndpointType, plugingetter.Acquire)
  315. if create.IPAM != nil {
  316. daemon.pluginRefCount(create.IPAM.Driver, ipamapi.PluginEndpointType, plugingetter.Acquire)
  317. }
  318. daemon.LogNetworkEvent(n, "create")
  319. if agent && !n.Info().Ingress() && n.Type() == "overlay" {
  320. nodeIP, exists := daemon.GetLBAttachmentStore().GetLBIPForNetwork(id)
  321. if !exists {
  322. return nil, fmt.Errorf("Failed to find a load balancer IP to use for network: %v", id)
  323. }
  324. if err := daemon.createLoadBalancerSandbox(create.Name, id, nodeIP, n); err != nil {
  325. return nil, err
  326. }
  327. }
  328. return &types.NetworkCreateResponse{
  329. ID: n.ID(),
  330. Warning: warning,
  331. }, nil
  332. }
  333. func (daemon *Daemon) pluginRefCount(driver, capability string, mode int) {
  334. var builtinDrivers []string
  335. if capability == driverapi.NetworkPluginEndpointType {
  336. builtinDrivers = daemon.netController.BuiltinDrivers()
  337. } else if capability == ipamapi.PluginEndpointType {
  338. builtinDrivers = daemon.netController.BuiltinIPAMDrivers()
  339. }
  340. for _, d := range builtinDrivers {
  341. if d == driver {
  342. return
  343. }
  344. }
  345. if daemon.PluginStore != nil {
  346. _, err := daemon.PluginStore.Get(driver, capability, mode)
  347. if err != nil {
  348. logrus.WithError(err).WithFields(logrus.Fields{"mode": mode, "driver": driver}).Error("Error handling plugin refcount operation")
  349. }
  350. }
  351. }
  352. func getIpamConfig(data []network.IPAMConfig) ([]*libnetwork.IpamConf, []*libnetwork.IpamConf, error) {
  353. ipamV4Cfg := []*libnetwork.IpamConf{}
  354. ipamV6Cfg := []*libnetwork.IpamConf{}
  355. for _, d := range data {
  356. iCfg := libnetwork.IpamConf{}
  357. iCfg.PreferredPool = d.Subnet
  358. iCfg.SubPool = d.IPRange
  359. iCfg.Gateway = d.Gateway
  360. iCfg.AuxAddresses = d.AuxAddress
  361. ip, _, err := net.ParseCIDR(d.Subnet)
  362. if err != nil {
  363. return nil, nil, fmt.Errorf("Invalid subnet %s : %v", d.Subnet, err)
  364. }
  365. if ip.To4() != nil {
  366. ipamV4Cfg = append(ipamV4Cfg, &iCfg)
  367. } else {
  368. ipamV6Cfg = append(ipamV6Cfg, &iCfg)
  369. }
  370. }
  371. return ipamV4Cfg, ipamV6Cfg, nil
  372. }
  373. // UpdateContainerServiceConfig updates a service configuration.
  374. func (daemon *Daemon) UpdateContainerServiceConfig(containerName string, serviceConfig *clustertypes.ServiceConfig) error {
  375. container, err := daemon.GetContainer(containerName)
  376. if err != nil {
  377. return err
  378. }
  379. container.NetworkSettings.Service = serviceConfig
  380. return nil
  381. }
  382. // ConnectContainerToNetwork connects the given container to the given
  383. // network. If either cannot be found, an err is returned. If the
  384. // network cannot be set up, an err is returned.
  385. func (daemon *Daemon) ConnectContainerToNetwork(containerName, networkName string, endpointConfig *network.EndpointSettings) error {
  386. if runtime.GOOS == "solaris" {
  387. return errors.New("docker network connect is unsupported on Solaris platform")
  388. }
  389. container, err := daemon.GetContainer(containerName)
  390. if err != nil {
  391. return err
  392. }
  393. return daemon.ConnectToNetwork(container, networkName, endpointConfig)
  394. }
  395. // DisconnectContainerFromNetwork disconnects the given container from
  396. // the given network. If either cannot be found, an err is returned.
  397. func (daemon *Daemon) DisconnectContainerFromNetwork(containerName string, networkName string, force bool) error {
  398. if runtime.GOOS == "solaris" {
  399. return errors.New("docker network disconnect is unsupported on Solaris platform")
  400. }
  401. container, err := daemon.GetContainer(containerName)
  402. if err != nil {
  403. if force {
  404. return daemon.ForceEndpointDelete(containerName, networkName)
  405. }
  406. return err
  407. }
  408. return daemon.DisconnectFromNetwork(container, networkName, force)
  409. }
  410. // GetNetworkDriverList returns the list of plugins drivers
  411. // registered for network.
  412. func (daemon *Daemon) GetNetworkDriverList() []string {
  413. if !daemon.NetworkControllerEnabled() {
  414. return nil
  415. }
  416. pluginList := daemon.netController.BuiltinDrivers()
  417. managedPlugins := daemon.PluginStore.GetAllManagedPluginsByCap(driverapi.NetworkPluginEndpointType)
  418. for _, plugin := range managedPlugins {
  419. pluginList = append(pluginList, plugin.Name())
  420. }
  421. pluginMap := make(map[string]bool)
  422. for _, plugin := range pluginList {
  423. pluginMap[plugin] = true
  424. }
  425. networks := daemon.netController.Networks()
  426. for _, network := range networks {
  427. if !pluginMap[network.Type()] {
  428. pluginList = append(pluginList, network.Type())
  429. pluginMap[network.Type()] = true
  430. }
  431. }
  432. sort.Strings(pluginList)
  433. return pluginList
  434. }
  435. // DeleteManagedNetwork deletes an agent network.
  436. func (daemon *Daemon) DeleteManagedNetwork(networkID string) error {
  437. return daemon.deleteNetwork(networkID, true)
  438. }
  439. // DeleteNetwork destroys a network unless it's one of docker's predefined networks.
  440. func (daemon *Daemon) DeleteNetwork(networkID string) error {
  441. return daemon.deleteNetwork(networkID, false)
  442. }
  443. func (daemon *Daemon) deleteLoadBalancerSandbox(n libnetwork.Network) {
  444. controller := daemon.netController
  445. //The only endpoint left should be the LB endpoint (nw.Name() + "-endpoint")
  446. endpoints := n.Endpoints()
  447. if len(endpoints) == 1 {
  448. sandboxName := n.Name() + "-sbox"
  449. if err := endpoints[0].Info().Sandbox().DisableService(); err != nil {
  450. logrus.Errorf("Failed to disable service on sandbox %s: %v", sandboxName, err)
  451. //Ignore error and attempt to delete the load balancer endpoint
  452. }
  453. if err := endpoints[0].Delete(true); err != nil {
  454. logrus.Errorf("Failed to delete endpoint %s (%s) in %s: %v", endpoints[0].Name(), endpoints[0].ID(), sandboxName, err)
  455. //Ignore error and attempt to delete the sandbox.
  456. }
  457. if err := controller.SandboxDestroy(sandboxName); err != nil {
  458. logrus.Errorf("Failed to delete %s sandbox: %v", sandboxName, err)
  459. //Ignore error and attempt to delete the network.
  460. }
  461. }
  462. }
  463. func (daemon *Daemon) deleteNetwork(networkID string, dynamic bool) error {
  464. nw, err := daemon.FindNetwork(networkID)
  465. if err != nil {
  466. return err
  467. }
  468. if runconfig.IsPreDefinedNetwork(nw.Name()) && !dynamic {
  469. err := fmt.Errorf("%s is a pre-defined network and cannot be removed", nw.Name())
  470. return notAllowedError{err}
  471. }
  472. if dynamic && !nw.Info().Dynamic() {
  473. if runconfig.IsPreDefinedNetwork(nw.Name()) {
  474. // Predefined networks now support swarm services. Make this
  475. // a no-op when cluster requests to remove the predefined network.
  476. return nil
  477. }
  478. err := fmt.Errorf("%s is not a dynamic network", nw.Name())
  479. return notAllowedError{err}
  480. }
  481. if !nw.Info().Ingress() && nw.Type() == "overlay" {
  482. daemon.deleteLoadBalancerSandbox(nw)
  483. }
  484. if err := nw.Delete(); err != nil {
  485. return err
  486. }
  487. // If this is not a configuration only network, we need to
  488. // update the corresponding remote drivers' reference counts
  489. if !nw.Info().ConfigOnly() {
  490. daemon.pluginRefCount(nw.Type(), driverapi.NetworkPluginEndpointType, plugingetter.Release)
  491. ipamType, _, _, _ := nw.Info().IpamConfig()
  492. daemon.pluginRefCount(ipamType, ipamapi.PluginEndpointType, plugingetter.Release)
  493. daemon.LogNetworkEvent(nw, "destroy")
  494. }
  495. return nil
  496. }
  497. // GetNetworks returns a list of all networks
  498. func (daemon *Daemon) GetNetworks() []libnetwork.Network {
  499. return daemon.getAllNetworks()
  500. }
  501. // clearAttachableNetworks removes the attachable networks
  502. // after disconnecting any connected container
  503. func (daemon *Daemon) clearAttachableNetworks() {
  504. for _, n := range daemon.GetNetworks() {
  505. if !n.Info().Attachable() {
  506. continue
  507. }
  508. for _, ep := range n.Endpoints() {
  509. epInfo := ep.Info()
  510. if epInfo == nil {
  511. continue
  512. }
  513. sb := epInfo.Sandbox()
  514. if sb == nil {
  515. continue
  516. }
  517. containerID := sb.ContainerID()
  518. if err := daemon.DisconnectContainerFromNetwork(containerID, n.ID(), true); err != nil {
  519. logrus.Warnf("Failed to disconnect container %s from swarm network %s on cluster leave: %v",
  520. containerID, n.Name(), err)
  521. }
  522. }
  523. if err := daemon.DeleteManagedNetwork(n.ID()); err != nil {
  524. logrus.Warnf("Failed to remove swarm network %s on cluster leave: %v", n.Name(), err)
  525. }
  526. }
  527. }