endpoint.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. package libnetwork
  2. import (
  3. "container/heap"
  4. "encoding/json"
  5. "fmt"
  6. "net"
  7. "strings"
  8. "sync"
  9. log "github.com/Sirupsen/logrus"
  10. "github.com/docker/libnetwork/datastore"
  11. "github.com/docker/libnetwork/ipamapi"
  12. "github.com/docker/libnetwork/netlabel"
  13. "github.com/docker/libnetwork/options"
  14. "github.com/docker/libnetwork/types"
  15. )
  16. // Endpoint represents a logical connection between a network and a sandbox.
  17. type Endpoint interface {
  18. // A system generated id for this endpoint.
  19. ID() string
  20. // Name returns the name of this endpoint.
  21. Name() string
  22. // Network returns the name of the network to which this endpoint is attached.
  23. Network() string
  24. // Join joins the sandbox to the endpoint and populates into the sandbox
  25. // the network resources allocated for the endpoint.
  26. Join(sandbox Sandbox, options ...EndpointOption) error
  27. // Leave detaches the network resources populated in the sandbox.
  28. Leave(sandbox Sandbox, options ...EndpointOption) error
  29. // Return certain operational data belonging to this endpoint
  30. Info() EndpointInfo
  31. // DriverInfo returns a collection of driver operational data related to this endpoint retrieved from the driver
  32. DriverInfo() (map[string]interface{}, error)
  33. // Delete and detaches this endpoint from the network.
  34. Delete() error
  35. }
  36. // EndpointOption is a option setter function type used to pass varios options to Network
  37. // and Endpoint interfaces methods. The various setter functions of type EndpointOption are
  38. // provided by libnetwork, they look like <Create|Join|Leave>Option[...](...)
  39. type EndpointOption func(ep *endpoint)
  40. type endpoint struct {
  41. name string
  42. id string
  43. network *network
  44. iface *endpointInterface
  45. joinInfo *endpointJoinInfo
  46. sandboxID string
  47. exposedPorts []types.TransportPort
  48. anonymous bool
  49. generic map[string]interface{}
  50. joinLeaveDone chan struct{}
  51. dbIndex uint64
  52. dbExists bool
  53. sync.Mutex
  54. }
  55. func (ep *endpoint) MarshalJSON() ([]byte, error) {
  56. ep.Lock()
  57. defer ep.Unlock()
  58. epMap := make(map[string]interface{})
  59. epMap["name"] = ep.name
  60. epMap["id"] = ep.id
  61. epMap["ep_iface"] = ep.iface
  62. epMap["exposed_ports"] = ep.exposedPorts
  63. if ep.generic != nil {
  64. epMap["generic"] = ep.generic
  65. }
  66. epMap["sandbox"] = ep.sandboxID
  67. epMap["anonymous"] = ep.anonymous
  68. return json.Marshal(epMap)
  69. }
  70. func (ep *endpoint) UnmarshalJSON(b []byte) (err error) {
  71. ep.Lock()
  72. defer ep.Unlock()
  73. var epMap map[string]interface{}
  74. if err := json.Unmarshal(b, &epMap); err != nil {
  75. return err
  76. }
  77. ep.name = epMap["name"].(string)
  78. ep.id = epMap["id"].(string)
  79. ib, _ := json.Marshal(epMap["ep_iface"])
  80. json.Unmarshal(ib, &ep.iface)
  81. tb, _ := json.Marshal(epMap["exposed_ports"])
  82. var tPorts []types.TransportPort
  83. json.Unmarshal(tb, &tPorts)
  84. ep.exposedPorts = tPorts
  85. cb, _ := json.Marshal(epMap["sandbox"])
  86. json.Unmarshal(cb, &ep.sandboxID)
  87. if v, ok := epMap["generic"]; ok {
  88. ep.generic = v.(map[string]interface{})
  89. }
  90. if v, ok := epMap["anonymous"]; ok {
  91. ep.anonymous = v.(bool)
  92. }
  93. return nil
  94. }
  95. func (ep *endpoint) New() datastore.KVObject {
  96. return &endpoint{network: ep.getNetwork()}
  97. }
  98. func (ep *endpoint) CopyTo(o datastore.KVObject) error {
  99. ep.Lock()
  100. defer ep.Unlock()
  101. dstEp := o.(*endpoint)
  102. dstEp.name = ep.name
  103. dstEp.id = ep.id
  104. dstEp.sandboxID = ep.sandboxID
  105. dstEp.dbIndex = ep.dbIndex
  106. dstEp.dbExists = ep.dbExists
  107. dstEp.anonymous = ep.anonymous
  108. if ep.iface != nil {
  109. dstEp.iface = &endpointInterface{}
  110. ep.iface.CopyTo(dstEp.iface)
  111. }
  112. dstEp.exposedPorts = make([]types.TransportPort, len(ep.exposedPorts))
  113. copy(dstEp.exposedPorts, ep.exposedPorts)
  114. dstEp.generic = options.Generic{}
  115. for k, v := range ep.generic {
  116. dstEp.generic[k] = v
  117. }
  118. return nil
  119. }
  120. func (ep *endpoint) ID() string {
  121. ep.Lock()
  122. defer ep.Unlock()
  123. return ep.id
  124. }
  125. func (ep *endpoint) Name() string {
  126. ep.Lock()
  127. defer ep.Unlock()
  128. return ep.name
  129. }
  130. func (ep *endpoint) Network() string {
  131. if ep.network == nil {
  132. return ""
  133. }
  134. return ep.network.name
  135. }
  136. func (ep *endpoint) isAnonymous() bool {
  137. ep.Lock()
  138. defer ep.Unlock()
  139. return ep.anonymous
  140. }
  141. // endpoint Key structure : endpoint/network-id/endpoint-id
  142. func (ep *endpoint) Key() []string {
  143. if ep.network == nil {
  144. return nil
  145. }
  146. return []string{datastore.EndpointKeyPrefix, ep.network.id, ep.id}
  147. }
  148. func (ep *endpoint) KeyPrefix() []string {
  149. if ep.network == nil {
  150. return nil
  151. }
  152. return []string{datastore.EndpointKeyPrefix, ep.network.id}
  153. }
  154. func (ep *endpoint) networkIDFromKey(key string) (string, error) {
  155. // endpoint Key structure : docker/libnetwork/endpoint/${network-id}/${endpoint-id}
  156. // it's an invalid key if the key doesn't have all the 5 key elements above
  157. keyElements := strings.Split(key, "/")
  158. if !strings.HasPrefix(key, datastore.Key(datastore.EndpointKeyPrefix)) || len(keyElements) < 5 {
  159. return "", fmt.Errorf("invalid endpoint key : %v", key)
  160. }
  161. // network-id is placed at index=3. pls refer to endpoint.Key() method
  162. return strings.Split(key, "/")[3], nil
  163. }
  164. func (ep *endpoint) Value() []byte {
  165. b, err := json.Marshal(ep)
  166. if err != nil {
  167. return nil
  168. }
  169. return b
  170. }
  171. func (ep *endpoint) SetValue(value []byte) error {
  172. return json.Unmarshal(value, ep)
  173. }
  174. func (ep *endpoint) Index() uint64 {
  175. ep.Lock()
  176. defer ep.Unlock()
  177. return ep.dbIndex
  178. }
  179. func (ep *endpoint) SetIndex(index uint64) {
  180. ep.Lock()
  181. defer ep.Unlock()
  182. ep.dbIndex = index
  183. ep.dbExists = true
  184. }
  185. func (ep *endpoint) Exists() bool {
  186. ep.Lock()
  187. defer ep.Unlock()
  188. return ep.dbExists
  189. }
  190. func (ep *endpoint) Skip() bool {
  191. return ep.getNetwork().Skip()
  192. }
  193. func (ep *endpoint) processOptions(options ...EndpointOption) {
  194. ep.Lock()
  195. defer ep.Unlock()
  196. for _, opt := range options {
  197. if opt != nil {
  198. opt(ep)
  199. }
  200. }
  201. }
  202. func (ep *endpoint) getNetwork() *network {
  203. ep.Lock()
  204. defer ep.Unlock()
  205. return ep.network
  206. }
  207. func (ep *endpoint) getNetworkFromStore() (*network, error) {
  208. if ep.network == nil {
  209. return nil, fmt.Errorf("invalid network object in endpoint %s", ep.Name())
  210. }
  211. return ep.network.ctrlr.getNetworkFromStore(ep.network.id)
  212. }
  213. func (ep *endpoint) Join(sbox Sandbox, options ...EndpointOption) error {
  214. if sbox == nil {
  215. return types.BadRequestErrorf("endpoint cannot be joined by nil container")
  216. }
  217. sb, ok := sbox.(*sandbox)
  218. if !ok {
  219. return types.BadRequestErrorf("not a valid Sandbox interface")
  220. }
  221. sb.joinLeaveStart()
  222. defer sb.joinLeaveEnd()
  223. return ep.sbJoin(sbox, options...)
  224. }
  225. func (ep *endpoint) sbJoin(sbox Sandbox, options ...EndpointOption) error {
  226. var err error
  227. sb, ok := sbox.(*sandbox)
  228. if !ok {
  229. return types.BadRequestErrorf("not a valid Sandbox interface")
  230. }
  231. network, err := ep.getNetworkFromStore()
  232. if err != nil {
  233. return fmt.Errorf("failed to get network from store during join: %v", err)
  234. }
  235. ep, err = network.getEndpointFromStore(ep.ID())
  236. if err != nil {
  237. return fmt.Errorf("failed to get endpoint from store during join: %v", err)
  238. }
  239. ep.Lock()
  240. if ep.sandboxID != "" {
  241. ep.Unlock()
  242. return types.ForbiddenErrorf("another container is attached to the same network endpoint")
  243. }
  244. ep.Unlock()
  245. ep.Lock()
  246. ep.network = network
  247. ep.sandboxID = sbox.ID()
  248. ep.joinInfo = &endpointJoinInfo{}
  249. epid := ep.id
  250. ep.Unlock()
  251. defer func() {
  252. if err != nil {
  253. ep.Lock()
  254. ep.sandboxID = ""
  255. ep.Unlock()
  256. }
  257. }()
  258. network.Lock()
  259. nid := network.id
  260. network.Unlock()
  261. ep.processOptions(options...)
  262. driver, err := network.driver()
  263. if err != nil {
  264. return fmt.Errorf("failed to join endpoint: %v", err)
  265. }
  266. err = driver.Join(nid, epid, sbox.Key(), ep, sbox.Labels())
  267. if err != nil {
  268. return err
  269. }
  270. defer func() {
  271. if err != nil {
  272. // Do not alter global err variable, it's needed by the previous defer
  273. if err := driver.Leave(nid, epid); err != nil {
  274. log.Warnf("driver leave failed while rolling back join: %v", err)
  275. }
  276. }
  277. }()
  278. address := ""
  279. if ip := ep.getFirstInterfaceAddress(); ip != nil {
  280. address = ip.String()
  281. }
  282. if err = sb.updateHostsFile(address, network.getSvcRecords()); err != nil {
  283. return err
  284. }
  285. // Watch for service records
  286. network.getController().watchSvcRecord(ep)
  287. if err = sb.updateDNS(network.enableIPv6); err != nil {
  288. return err
  289. }
  290. if err = network.getController().updateToStore(ep); err != nil {
  291. return err
  292. }
  293. sb.Lock()
  294. heap.Push(&sb.endpoints, ep)
  295. sb.Unlock()
  296. defer func() {
  297. if err != nil {
  298. for i, e := range sb.getConnectedEndpoints() {
  299. if e == ep {
  300. sb.Lock()
  301. heap.Remove(&sb.endpoints, i)
  302. sb.Unlock()
  303. return
  304. }
  305. }
  306. }
  307. }()
  308. if err = sb.populateNetworkResources(ep); err != nil {
  309. return err
  310. }
  311. if sb.needDefaultGW() {
  312. return sb.setupDefaultGW(ep)
  313. }
  314. return sb.clearDefaultGW()
  315. }
  316. func (ep *endpoint) hasInterface(iName string) bool {
  317. ep.Lock()
  318. defer ep.Unlock()
  319. return ep.iface != nil && ep.iface.srcName == iName
  320. }
  321. func (ep *endpoint) Leave(sbox Sandbox, options ...EndpointOption) error {
  322. if sbox == nil || sbox.ID() == "" || sbox.Key() == "" {
  323. return types.BadRequestErrorf("invalid Sandbox passed to enpoint leave: %v", sbox)
  324. }
  325. sb, ok := sbox.(*sandbox)
  326. if !ok {
  327. return types.BadRequestErrorf("not a valid Sandbox interface")
  328. }
  329. sb.joinLeaveStart()
  330. defer sb.joinLeaveEnd()
  331. return ep.sbLeave(sbox, options...)
  332. }
  333. func (ep *endpoint) sbLeave(sbox Sandbox, options ...EndpointOption) error {
  334. sb, ok := sbox.(*sandbox)
  335. if !ok {
  336. return types.BadRequestErrorf("not a valid Sandbox interface")
  337. }
  338. n, err := ep.getNetworkFromStore()
  339. if err != nil {
  340. return fmt.Errorf("failed to get network from store during leave: %v", err)
  341. }
  342. ep, err = n.getEndpointFromStore(ep.ID())
  343. if err != nil {
  344. return fmt.Errorf("failed to get endpoint from store during leave: %v", err)
  345. }
  346. ep.Lock()
  347. sid := ep.sandboxID
  348. ep.Unlock()
  349. if sid == "" {
  350. return types.ForbiddenErrorf("cannot leave endpoint with no attached sandbox")
  351. }
  352. if sid != sbox.ID() {
  353. return types.ForbiddenErrorf("unexpected sandbox ID in leave request. Expected %s. Got %s", ep.sandboxID, sbox.ID())
  354. }
  355. ep.processOptions(options...)
  356. d, err := n.driver()
  357. if err != nil {
  358. return fmt.Errorf("failed to leave endpoint: %v", err)
  359. }
  360. ep.Lock()
  361. ep.sandboxID = ""
  362. ep.network = n
  363. ep.Unlock()
  364. if err := d.Leave(n.id, ep.id); err != nil {
  365. if _, ok := err.(types.MaskableError); !ok {
  366. log.Warnf("driver error disconnecting container %s : %v", ep.name, err)
  367. }
  368. }
  369. if err := sb.clearNetworkResources(ep); err != nil {
  370. log.Warnf("Could not cleanup network resources on container %s disconnect: %v", ep.name, err)
  371. }
  372. // Update the store about the sandbox detach only after we
  373. // have completed sb.clearNetworkresources above to avoid
  374. // spurious logs when cleaning up the sandbox when the daemon
  375. // ungracefully exits and restarts before completing sandbox
  376. // detach but after store has been updated.
  377. if err := n.getController().updateToStore(ep); err != nil {
  378. return err
  379. }
  380. // unwatch for service records
  381. n.getController().unWatchSvcRecord(ep)
  382. if sb.needDefaultGW() {
  383. ep := sb.getEPwithoutGateway()
  384. if ep == nil {
  385. return fmt.Errorf("endpoint without GW expected, but not found")
  386. }
  387. return sb.setupDefaultGW(ep)
  388. }
  389. return sb.clearDefaultGW()
  390. }
  391. func (ep *endpoint) Delete() error {
  392. var err error
  393. n, err := ep.getNetworkFromStore()
  394. if err != nil {
  395. return fmt.Errorf("failed to get network during Delete: %v", err)
  396. }
  397. ep, err = n.getEndpointFromStore(ep.ID())
  398. if err != nil {
  399. return fmt.Errorf("failed to get endpoint from store during Delete: %v", err)
  400. }
  401. ep.Lock()
  402. epid := ep.id
  403. name := ep.name
  404. if ep.sandboxID != "" {
  405. ep.Unlock()
  406. return &ActiveContainerError{name: name, id: epid}
  407. }
  408. ep.Unlock()
  409. if err = n.getEpCnt().DecEndpointCnt(); err != nil {
  410. return err
  411. }
  412. defer func() {
  413. if err != nil {
  414. if e := n.getEpCnt().IncEndpointCnt(); e != nil {
  415. log.Warnf("failed to update network %s : %v", n.name, e)
  416. }
  417. }
  418. }()
  419. if err = n.getController().deleteFromStore(ep); err != nil {
  420. return err
  421. }
  422. defer func() {
  423. if err != nil {
  424. ep.dbExists = false
  425. if e := n.getController().updateToStore(ep); e != nil {
  426. log.Warnf("failed to recreate endpoint in store %s : %v", name, e)
  427. }
  428. }
  429. }()
  430. if err = ep.deleteEndpoint(); err != nil {
  431. return err
  432. }
  433. ep.releaseAddress()
  434. return nil
  435. }
  436. func (ep *endpoint) deleteEndpoint() error {
  437. ep.Lock()
  438. n := ep.network
  439. name := ep.name
  440. epid := ep.id
  441. ep.Unlock()
  442. driver, err := n.driver()
  443. if err != nil {
  444. return fmt.Errorf("failed to delete endpoint: %v", err)
  445. }
  446. if err := driver.DeleteEndpoint(n.id, epid); err != nil {
  447. if _, ok := err.(types.ForbiddenError); ok {
  448. return err
  449. }
  450. if _, ok := err.(types.MaskableError); !ok {
  451. log.Warnf("driver error deleting endpoint %s : %v", name, err)
  452. }
  453. }
  454. return nil
  455. }
  456. func (ep *endpoint) getSandbox() (*sandbox, bool) {
  457. ep.Lock()
  458. c := ep.network.getController()
  459. sid := ep.sandboxID
  460. ep.Unlock()
  461. c.Lock()
  462. ps, ok := c.sandboxes[sid]
  463. c.Unlock()
  464. return ps, ok
  465. }
  466. func (ep *endpoint) getFirstInterfaceAddress() net.IP {
  467. ep.Lock()
  468. defer ep.Unlock()
  469. if ep.iface.addr != nil {
  470. return ep.iface.addr.IP
  471. }
  472. return nil
  473. }
  474. // EndpointOptionGeneric function returns an option setter for a Generic option defined
  475. // in a Dictionary of Key-Value pair
  476. func EndpointOptionGeneric(generic map[string]interface{}) EndpointOption {
  477. return func(ep *endpoint) {
  478. for k, v := range generic {
  479. ep.generic[k] = v
  480. }
  481. }
  482. }
  483. // CreateOptionExposedPorts function returns an option setter for the container exposed
  484. // ports option to be passed to network.CreateEndpoint() method.
  485. func CreateOptionExposedPorts(exposedPorts []types.TransportPort) EndpointOption {
  486. return func(ep *endpoint) {
  487. // Defensive copy
  488. eps := make([]types.TransportPort, len(exposedPorts))
  489. copy(eps, exposedPorts)
  490. // Store endpoint label and in generic because driver needs it
  491. ep.exposedPorts = eps
  492. ep.generic[netlabel.ExposedPorts] = eps
  493. }
  494. }
  495. // CreateOptionPortMapping function returns an option setter for the mapping
  496. // ports option to be passed to network.CreateEndpoint() method.
  497. func CreateOptionPortMapping(portBindings []types.PortBinding) EndpointOption {
  498. return func(ep *endpoint) {
  499. // Store a copy of the bindings as generic data to pass to the driver
  500. pbs := make([]types.PortBinding, len(portBindings))
  501. copy(pbs, portBindings)
  502. ep.generic[netlabel.PortMap] = pbs
  503. }
  504. }
  505. // CreateOptionAnonymous function returns an option setter for setting
  506. // this endpoint as anonymous
  507. func CreateOptionAnonymous() EndpointOption {
  508. return func(ep *endpoint) {
  509. ep.anonymous = true
  510. }
  511. }
  512. // JoinOptionPriority function returns an option setter for priority option to
  513. // be passed to the endpoint.Join() method.
  514. func JoinOptionPriority(ep Endpoint, prio int) EndpointOption {
  515. return func(ep *endpoint) {
  516. // ep lock already acquired
  517. c := ep.network.getController()
  518. c.Lock()
  519. sb, ok := c.sandboxes[ep.sandboxID]
  520. c.Unlock()
  521. if !ok {
  522. log.Errorf("Could not set endpoint priority value during Join to endpoint %s: No sandbox id present in endpoint", ep.id)
  523. return
  524. }
  525. sb.epPriority[ep.id] = prio
  526. }
  527. }
  528. func (ep *endpoint) DataScope() string {
  529. return ep.getNetwork().DataScope()
  530. }
  531. func (ep *endpoint) assignAddress() error {
  532. var (
  533. ipam ipamapi.Ipam
  534. err error
  535. )
  536. n := ep.getNetwork()
  537. if n.Type() == "host" || n.Type() == "null" {
  538. return nil
  539. }
  540. log.Debugf("Assigning addresses for endpoint %s's interface on network %s", ep.Name(), n.Name())
  541. ipam, err = n.getController().getIpamDriver(n.ipamType)
  542. if err != nil {
  543. return err
  544. }
  545. err = ep.assignAddressVersion(4, ipam)
  546. if err != nil {
  547. return err
  548. }
  549. return ep.assignAddressVersion(6, ipam)
  550. }
  551. func (ep *endpoint) assignAddressVersion(ipVer int, ipam ipamapi.Ipam) error {
  552. var (
  553. poolID *string
  554. address **net.IPNet
  555. )
  556. n := ep.getNetwork()
  557. switch ipVer {
  558. case 4:
  559. poolID = &ep.iface.v4PoolID
  560. address = &ep.iface.addr
  561. case 6:
  562. poolID = &ep.iface.v6PoolID
  563. address = &ep.iface.addrv6
  564. default:
  565. return types.InternalErrorf("incorrect ip version number passed: %d", ipVer)
  566. }
  567. ipInfo := n.getIPInfo(ipVer)
  568. // ipv6 address is not mandatory
  569. if len(ipInfo) == 0 && ipVer == 6 {
  570. return nil
  571. }
  572. for _, d := range ipInfo {
  573. addr, _, err := ipam.RequestAddress(d.PoolID, nil, nil)
  574. if err == nil {
  575. ep.Lock()
  576. *address = addr
  577. *poolID = d.PoolID
  578. ep.Unlock()
  579. return nil
  580. }
  581. if err != ipamapi.ErrNoAvailableIPs {
  582. return err
  583. }
  584. }
  585. return fmt.Errorf("no available IPv%d addresses on this network's address pools: %s (%s)", ipVer, n.Name(), n.ID())
  586. }
  587. func (ep *endpoint) releaseAddress() {
  588. n := ep.getNetwork()
  589. if n.Type() == "host" || n.Type() == "null" {
  590. return
  591. }
  592. log.Debugf("Releasing addresses for endpoint %s's interface on network %s", ep.Name(), n.Name())
  593. ipam, err := n.getController().getIpamDriver(n.ipamType)
  594. if err != nil {
  595. log.Warnf("Failed to retrieve ipam driver to release interface address on delete of endpoint %s (%s): %v", ep.Name(), ep.ID(), err)
  596. return
  597. }
  598. if err := ipam.ReleaseAddress(ep.iface.v4PoolID, ep.iface.addr.IP); err != nil {
  599. log.Warnf("Failed to release ip address %s on delete of endpoint %s (%s): %v", ep.iface.addr.IP, ep.Name(), ep.ID(), err)
  600. }
  601. if ep.iface.addrv6 != nil && ep.iface.addrv6.IP.IsGlobalUnicast() {
  602. if err := ipam.ReleaseAddress(ep.iface.v6PoolID, ep.iface.addrv6.IP); err != nil {
  603. log.Warnf("Failed to release ip address %s on delete of endpoint %s (%s): %v", ep.iface.addrv6.IP, ep.Name(), ep.ID(), err)
  604. }
  605. }
  606. }
  607. func (c *controller) cleanupLocalEndpoints() {
  608. nl, err := c.getNetworksForScope(datastore.LocalScope)
  609. if err != nil {
  610. log.Warnf("Could not get list of networks during endpoint cleanup: %v", err)
  611. return
  612. }
  613. for _, n := range nl {
  614. epl, err := n.getEndpointsFromStore()
  615. if err != nil {
  616. log.Warnf("Could not get list of endpoints in network %s during endpoint cleanup: %v", n.name, err)
  617. continue
  618. }
  619. for _, ep := range epl {
  620. if err := ep.Delete(); err != nil {
  621. log.Warnf("Could not delete local endpoint %s during endpoint cleanup: %v", ep.name, err)
  622. }
  623. }
  624. }
  625. }