sandbox.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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/etchosts"
  11. "github.com/docker/libnetwork/netlabel"
  12. "github.com/docker/libnetwork/netutils"
  13. "github.com/docker/libnetwork/osl"
  14. "github.com/docker/libnetwork/types"
  15. )
  16. // Sandbox provides the control over the network container entity. It is a one to one mapping with the container.
  17. type Sandbox interface {
  18. // ID returns the ID of the sandbox
  19. ID() string
  20. // Key returns the sandbox's key
  21. Key() string
  22. // ContainerID returns the container id associated to this sandbox
  23. ContainerID() string
  24. // Labels returns the sandbox's labels
  25. Labels() map[string]interface{}
  26. // Statistics retrieves the interfaces' statistics for the sandbox
  27. Statistics() (map[string]*types.InterfaceStatistics, error)
  28. // Refresh leaves all the endpoints, resets and re-apply the options,
  29. // re-joins all the endpoints without destroying the osl sandbox
  30. Refresh(options ...SandboxOption) error
  31. // SetKey updates the Sandbox Key
  32. SetKey(key string) error
  33. // Rename changes the name of all attached Endpoints
  34. Rename(name string) error
  35. // Delete destroys this container after detaching it from all connected endpoints.
  36. Delete() error
  37. // ResolveName resolves a service name to an IPv4 or IPv6 address by searching the
  38. // networks the sandbox is connected to.
  39. ResolveName(name string, iplen int) []net.IP
  40. // ResolveIP returns the service name for the passed in IP. IP is in reverse dotted
  41. // notation; the format used for DNS PTR records
  42. ResolveIP(name string) string
  43. // Endpoints returns all the endpoints connected to the sandbox
  44. Endpoints() []Endpoint
  45. }
  46. // SandboxOption is an option setter function type used to pass various options to
  47. // NewNetContainer method. The various setter functions of type SandboxOption are
  48. // provided by libnetwork, they look like ContainerOptionXXXX(...)
  49. type SandboxOption func(sb *sandbox)
  50. func (sb *sandbox) processOptions(options ...SandboxOption) {
  51. for _, opt := range options {
  52. if opt != nil {
  53. opt(sb)
  54. }
  55. }
  56. }
  57. type epHeap []*endpoint
  58. type sandbox struct {
  59. id string
  60. containerID string
  61. config containerConfig
  62. extDNS []string
  63. osSbox osl.Sandbox
  64. controller *controller
  65. resolver Resolver
  66. resolverOnce sync.Once
  67. refCnt int
  68. endpoints epHeap
  69. epPriority map[string]int
  70. joinLeaveDone chan struct{}
  71. dbIndex uint64
  72. dbExists bool
  73. isStub bool
  74. inDelete bool
  75. sync.Mutex
  76. }
  77. // These are the container configs used to customize container /etc/hosts file.
  78. type hostsPathConfig struct {
  79. hostName string
  80. domainName string
  81. hostsPath string
  82. originHostsPath string
  83. extraHosts []extraHost
  84. parentUpdates []parentUpdate
  85. }
  86. type parentUpdate struct {
  87. cid string
  88. name string
  89. ip string
  90. }
  91. type extraHost struct {
  92. name string
  93. IP string
  94. }
  95. // These are the container configs used to customize container /etc/resolv.conf file.
  96. type resolvConfPathConfig struct {
  97. resolvConfPath string
  98. originResolvConfPath string
  99. resolvConfHashFile string
  100. dnsList []string
  101. dnsSearchList []string
  102. dnsOptionsList []string
  103. }
  104. type containerConfig struct {
  105. hostsPathConfig
  106. resolvConfPathConfig
  107. generic map[string]interface{}
  108. useDefaultSandBox bool
  109. useExternalKey bool
  110. prio int // higher the value, more the priority
  111. exposedPorts []types.TransportPort
  112. }
  113. func (sb *sandbox) ID() string {
  114. return sb.id
  115. }
  116. func (sb *sandbox) ContainerID() string {
  117. return sb.containerID
  118. }
  119. func (sb *sandbox) Key() string {
  120. if sb.config.useDefaultSandBox {
  121. return osl.GenerateKey("default")
  122. }
  123. return osl.GenerateKey(sb.id)
  124. }
  125. func (sb *sandbox) Labels() map[string]interface{} {
  126. sb.Lock()
  127. sb.Unlock()
  128. opts := make(map[string]interface{}, len(sb.config.generic))
  129. for k, v := range sb.config.generic {
  130. opts[k] = v
  131. }
  132. return opts
  133. }
  134. func (sb *sandbox) Statistics() (map[string]*types.InterfaceStatistics, error) {
  135. m := make(map[string]*types.InterfaceStatistics)
  136. sb.Lock()
  137. osb := sb.osSbox
  138. sb.Unlock()
  139. if osb == nil {
  140. return m, nil
  141. }
  142. var err error
  143. for _, i := range osb.Info().Interfaces() {
  144. if m[i.DstName()], err = i.Statistics(); err != nil {
  145. return m, err
  146. }
  147. }
  148. return m, nil
  149. }
  150. func (sb *sandbox) Delete() error {
  151. return sb.delete(false)
  152. }
  153. func (sb *sandbox) delete(force bool) error {
  154. sb.Lock()
  155. if sb.inDelete {
  156. sb.Unlock()
  157. return types.ForbiddenErrorf("another sandbox delete in progress")
  158. }
  159. // Set the inDelete flag. This will ensure that we don't
  160. // update the store until we have completed all the endpoint
  161. // leaves and deletes. And when endpoint leaves and deletes
  162. // are completed then we can finally delete the sandbox object
  163. // altogether from the data store. If the daemon exits
  164. // ungracefully in the middle of a sandbox delete this way we
  165. // will have all the references to the endpoints in the
  166. // sandbox so that we can clean them up when we restart
  167. sb.inDelete = true
  168. sb.Unlock()
  169. c := sb.controller
  170. // Detach from all endpoints
  171. retain := false
  172. for _, ep := range sb.getConnectedEndpoints() {
  173. // Retain the sanbdox if we can't obtain the network from store.
  174. if _, err := c.getNetworkFromStore(ep.getNetwork().ID()); err != nil {
  175. retain = true
  176. log.Warnf("Failed getting network for ep %s during sandbox %s delete: %v", ep.ID(), sb.ID(), err)
  177. continue
  178. }
  179. if !force {
  180. if err := ep.Leave(sb); err != nil {
  181. log.Warnf("Failed detaching sandbox %s from endpoint %s: %v\n", sb.ID(), ep.ID(), err)
  182. }
  183. }
  184. if err := ep.Delete(force); err != nil {
  185. log.Warnf("Failed deleting endpoint %s: %v\n", ep.ID(), err)
  186. }
  187. }
  188. if retain {
  189. sb.Lock()
  190. sb.inDelete = false
  191. sb.Unlock()
  192. return fmt.Errorf("could not cleanup all the endpoints in container %s / sandbox %s", sb.containerID, sb.id)
  193. }
  194. // Container is going away. Path cache in etchosts is most
  195. // likely not required any more. Drop it.
  196. etchosts.Drop(sb.config.hostsPath)
  197. if sb.resolver != nil {
  198. sb.resolver.Stop()
  199. }
  200. if sb.osSbox != nil && !sb.config.useDefaultSandBox {
  201. sb.osSbox.Destroy()
  202. }
  203. if err := sb.storeDelete(); err != nil {
  204. log.Warnf("Failed to delete sandbox %s from store: %v", sb.ID(), err)
  205. }
  206. c.Lock()
  207. delete(c.sandboxes, sb.ID())
  208. c.Unlock()
  209. return nil
  210. }
  211. func (sb *sandbox) Rename(name string) error {
  212. var err error
  213. for _, ep := range sb.getConnectedEndpoints() {
  214. if ep.endpointInGWNetwork() {
  215. continue
  216. }
  217. oldName := ep.Name()
  218. lEp := ep
  219. if err = ep.rename(name); err != nil {
  220. break
  221. }
  222. defer func() {
  223. if err != nil {
  224. lEp.rename(oldName)
  225. }
  226. }()
  227. }
  228. return err
  229. }
  230. func (sb *sandbox) Refresh(options ...SandboxOption) error {
  231. // Store connected endpoints
  232. epList := sb.getConnectedEndpoints()
  233. // Detach from all endpoints
  234. for _, ep := range epList {
  235. if err := ep.Leave(sb); err != nil {
  236. log.Warnf("Failed detaching sandbox %s from endpoint %s: %v\n", sb.ID(), ep.ID(), err)
  237. }
  238. }
  239. // Re-apply options
  240. sb.config = containerConfig{}
  241. sb.processOptions(options...)
  242. // Setup discovery files
  243. if err := sb.setupResolutionFiles(); err != nil {
  244. return err
  245. }
  246. // Re -connect to all endpoints
  247. for _, ep := range epList {
  248. if err := ep.Join(sb); err != nil {
  249. log.Warnf("Failed attach sandbox %s to endpoint %s: %v\n", sb.ID(), ep.ID(), err)
  250. }
  251. }
  252. return nil
  253. }
  254. func (sb *sandbox) MarshalJSON() ([]byte, error) {
  255. sb.Lock()
  256. defer sb.Unlock()
  257. // We are just interested in the container ID. This can be expanded to include all of containerInfo if there is a need
  258. return json.Marshal(sb.id)
  259. }
  260. func (sb *sandbox) UnmarshalJSON(b []byte) (err error) {
  261. sb.Lock()
  262. defer sb.Unlock()
  263. var id string
  264. if err := json.Unmarshal(b, &id); err != nil {
  265. return err
  266. }
  267. sb.id = id
  268. return nil
  269. }
  270. func (sb *sandbox) Endpoints() []Endpoint {
  271. sb.Lock()
  272. defer sb.Unlock()
  273. endpoints := make([]Endpoint, len(sb.endpoints))
  274. for i, ep := range sb.endpoints {
  275. endpoints[i] = ep
  276. }
  277. return endpoints
  278. }
  279. func (sb *sandbox) getConnectedEndpoints() []*endpoint {
  280. sb.Lock()
  281. defer sb.Unlock()
  282. eps := make([]*endpoint, len(sb.endpoints))
  283. for i, ep := range sb.endpoints {
  284. eps[i] = ep
  285. }
  286. return eps
  287. }
  288. func (sb *sandbox) removeEndpoint(ep *endpoint) {
  289. sb.Lock()
  290. defer sb.Unlock()
  291. for i, e := range sb.endpoints {
  292. if e == ep {
  293. heap.Remove(&sb.endpoints, i)
  294. return
  295. }
  296. }
  297. }
  298. func (sb *sandbox) getEndpoint(id string) *endpoint {
  299. sb.Lock()
  300. defer sb.Unlock()
  301. for _, ep := range sb.endpoints {
  302. if ep.id == id {
  303. return ep
  304. }
  305. }
  306. return nil
  307. }
  308. func (sb *sandbox) updateGateway(ep *endpoint) error {
  309. sb.Lock()
  310. osSbox := sb.osSbox
  311. sb.Unlock()
  312. if osSbox == nil {
  313. return nil
  314. }
  315. osSbox.UnsetGateway()
  316. osSbox.UnsetGatewayIPv6()
  317. if ep == nil {
  318. return nil
  319. }
  320. ep.Lock()
  321. joinInfo := ep.joinInfo
  322. ep.Unlock()
  323. if err := osSbox.SetGateway(joinInfo.gw); err != nil {
  324. return fmt.Errorf("failed to set gateway while updating gateway: %v", err)
  325. }
  326. if err := osSbox.SetGatewayIPv6(joinInfo.gw6); err != nil {
  327. return fmt.Errorf("failed to set IPv6 gateway while updating gateway: %v", err)
  328. }
  329. return nil
  330. }
  331. func (sb *sandbox) ResolveIP(ip string) string {
  332. var svc string
  333. log.Debugf("IP To resolve %v", ip)
  334. for _, ep := range sb.getConnectedEndpoints() {
  335. n := ep.getNetwork()
  336. sr, ok := n.getController().svcDb[n.ID()]
  337. if !ok {
  338. continue
  339. }
  340. nwName := n.Name()
  341. n.Lock()
  342. svc, ok = sr.ipMap[ip]
  343. n.Unlock()
  344. if ok {
  345. return svc + "." + nwName
  346. }
  347. }
  348. return svc
  349. }
  350. func (sb *sandbox) execFunc(f func()) {
  351. sb.osSbox.InvokeFunc(f)
  352. }
  353. func (sb *sandbox) ResolveName(name string, ipType int) []net.IP {
  354. var ip []net.IP
  355. // Embedded server owns the docker network domain. Resolution should work
  356. // for both container_name and container_name.network_name
  357. // We allow '.' in service name and network name. For a name a.b.c.d the
  358. // following have to tried;
  359. // {a.b.c.d in the networks container is connected to}
  360. // {a.b.c in network d},
  361. // {a.b in network c.d},
  362. // {a in network b.c.d},
  363. name = strings.TrimSuffix(name, ".")
  364. reqName := []string{name}
  365. networkName := []string{""}
  366. if strings.Contains(name, ".") {
  367. var i int
  368. dup := name
  369. for {
  370. if i = strings.LastIndex(dup, "."); i == -1 {
  371. break
  372. }
  373. networkName = append(networkName, name[i+1:])
  374. reqName = append(reqName, name[:i])
  375. dup = dup[:i]
  376. }
  377. }
  378. epList := sb.getConnectedEndpoints()
  379. for i := 0; i < len(reqName); i++ {
  380. log.Debugf("To resolve: %v in %v", reqName[i], networkName[i])
  381. // First check for local container alias
  382. ip = sb.resolveName(reqName[i], networkName[i], epList, true, ipType)
  383. if ip != nil {
  384. return ip
  385. }
  386. // Resolve the actual container name
  387. ip = sb.resolveName(reqName[i], networkName[i], epList, false, ipType)
  388. if ip != nil {
  389. return ip
  390. }
  391. }
  392. return nil
  393. }
  394. func (sb *sandbox) resolveName(req string, networkName string, epList []*endpoint, alias bool, ipType int) []net.IP {
  395. for _, ep := range epList {
  396. name := req
  397. n := ep.getNetwork()
  398. if networkName != "" && networkName != n.Name() {
  399. continue
  400. }
  401. if alias {
  402. if ep.aliases == nil {
  403. continue
  404. }
  405. var ok bool
  406. ep.Lock()
  407. name, ok = ep.aliases[req]
  408. ep.Unlock()
  409. if !ok {
  410. continue
  411. }
  412. } else {
  413. // If it is a regular lookup and if the requested name is an alias
  414. // don't perform a svc lookup for this endpoint.
  415. ep.Lock()
  416. if _, ok := ep.aliases[req]; ok {
  417. ep.Unlock()
  418. continue
  419. }
  420. ep.Unlock()
  421. }
  422. sr, ok := n.getController().svcDb[n.ID()]
  423. if !ok {
  424. continue
  425. }
  426. var ip []net.IP
  427. n.Lock()
  428. if ipType == netutils.IPv6 {
  429. ip, ok = sr.svcIPv6Map[name]
  430. } else {
  431. ip, ok = sr.svcMap[name]
  432. }
  433. n.Unlock()
  434. if ok {
  435. return ip
  436. }
  437. }
  438. return nil
  439. }
  440. func (sb *sandbox) SetKey(basePath string) error {
  441. if basePath == "" {
  442. return types.BadRequestErrorf("invalid sandbox key")
  443. }
  444. sb.Lock()
  445. oldosSbox := sb.osSbox
  446. sb.Unlock()
  447. if oldosSbox != nil {
  448. // If we already have an OS sandbox, release the network resources from that
  449. // and destroy the OS snab. We are moving into a new home further down. Note that none
  450. // of the network resources gets destroyed during the move.
  451. sb.releaseOSSbox()
  452. }
  453. osSbox, err := osl.GetSandboxForExternalKey(basePath, sb.Key())
  454. if err != nil {
  455. return err
  456. }
  457. sb.Lock()
  458. sb.osSbox = osSbox
  459. sb.Unlock()
  460. defer func() {
  461. if err != nil {
  462. sb.Lock()
  463. sb.osSbox = nil
  464. sb.Unlock()
  465. }
  466. }()
  467. // If the resolver was setup before stop it and set it up in the
  468. // new osl sandbox.
  469. if oldosSbox != nil && sb.resolver != nil {
  470. sb.resolver.Stop()
  471. sb.osSbox.InvokeFunc(sb.resolver.SetupFunc())
  472. if err := sb.resolver.Start(); err != nil {
  473. log.Errorf("Resolver Setup/Start failed for container %s, %q", sb.ContainerID(), err)
  474. }
  475. }
  476. for _, ep := range sb.getConnectedEndpoints() {
  477. if err = sb.populateNetworkResources(ep); err != nil {
  478. return err
  479. }
  480. }
  481. return nil
  482. }
  483. func releaseOSSboxResources(osSbox osl.Sandbox, ep *endpoint) {
  484. for _, i := range osSbox.Info().Interfaces() {
  485. // Only remove the interfaces owned by this endpoint from the sandbox.
  486. if ep.hasInterface(i.SrcName()) {
  487. if err := i.Remove(); err != nil {
  488. log.Debugf("Remove interface %s failed: %v", i.SrcName(), err)
  489. }
  490. }
  491. }
  492. ep.Lock()
  493. joinInfo := ep.joinInfo
  494. ep.Unlock()
  495. if joinInfo == nil {
  496. return
  497. }
  498. // Remove non-interface routes.
  499. for _, r := range joinInfo.StaticRoutes {
  500. if err := osSbox.RemoveStaticRoute(r); err != nil {
  501. log.Debugf("Remove route failed: %v", err)
  502. }
  503. }
  504. }
  505. func (sb *sandbox) releaseOSSbox() {
  506. sb.Lock()
  507. osSbox := sb.osSbox
  508. sb.osSbox = nil
  509. sb.Unlock()
  510. if osSbox == nil {
  511. return
  512. }
  513. for _, ep := range sb.getConnectedEndpoints() {
  514. releaseOSSboxResources(osSbox, ep)
  515. }
  516. osSbox.Destroy()
  517. }
  518. func (sb *sandbox) populateNetworkResources(ep *endpoint) error {
  519. sb.Lock()
  520. if sb.osSbox == nil {
  521. sb.Unlock()
  522. return nil
  523. }
  524. inDelete := sb.inDelete
  525. sb.Unlock()
  526. ep.Lock()
  527. joinInfo := ep.joinInfo
  528. i := ep.iface
  529. ep.Unlock()
  530. if ep.needResolver() {
  531. sb.startResolver()
  532. }
  533. if i != nil && i.srcName != "" {
  534. var ifaceOptions []osl.IfaceOption
  535. ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().Address(i.addr), sb.osSbox.InterfaceOptions().Routes(i.routes))
  536. if i.addrv6 != nil && i.addrv6.IP.To16() != nil {
  537. ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().AddressIPv6(i.addrv6))
  538. }
  539. if i.mac != nil {
  540. ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().MacAddress(i.mac))
  541. }
  542. if err := sb.osSbox.AddInterface(i.srcName, i.dstPrefix, ifaceOptions...); err != nil {
  543. return fmt.Errorf("failed to add interface %s to sandbox: %v", i.srcName, err)
  544. }
  545. }
  546. if joinInfo != nil {
  547. // Set up non-interface routes.
  548. for _, r := range joinInfo.StaticRoutes {
  549. if err := sb.osSbox.AddStaticRoute(r); err != nil {
  550. return fmt.Errorf("failed to add static route %s: %v", r.Destination.String(), err)
  551. }
  552. }
  553. }
  554. if ep == sb.getGatewayEndpoint() {
  555. if err := sb.updateGateway(ep); err != nil {
  556. return err
  557. }
  558. }
  559. // Only update the store if we did not come here as part of
  560. // sandbox delete. If we came here as part of delete then do
  561. // not bother updating the store. The sandbox object will be
  562. // deleted anyway
  563. if !inDelete {
  564. return sb.storeUpdate()
  565. }
  566. return nil
  567. }
  568. func (sb *sandbox) clearNetworkResources(origEp *endpoint) error {
  569. ep := sb.getEndpoint(origEp.id)
  570. if ep == nil {
  571. return fmt.Errorf("could not find the sandbox endpoint data for endpoint %s",
  572. origEp.id)
  573. }
  574. sb.Lock()
  575. osSbox := sb.osSbox
  576. inDelete := sb.inDelete
  577. sb.Unlock()
  578. if osSbox != nil {
  579. releaseOSSboxResources(osSbox, ep)
  580. }
  581. sb.Lock()
  582. if len(sb.endpoints) == 0 {
  583. // sb.endpoints should never be empty and this is unexpected error condition
  584. // We log an error message to note this down for debugging purposes.
  585. log.Errorf("No endpoints in sandbox while trying to remove endpoint %s", ep.Name())
  586. sb.Unlock()
  587. return nil
  588. }
  589. var (
  590. gwepBefore, gwepAfter *endpoint
  591. index = -1
  592. )
  593. for i, e := range sb.endpoints {
  594. if e == ep {
  595. index = i
  596. }
  597. if len(e.Gateway()) > 0 && gwepBefore == nil {
  598. gwepBefore = e
  599. }
  600. if index != -1 && gwepBefore != nil {
  601. break
  602. }
  603. }
  604. heap.Remove(&sb.endpoints, index)
  605. for _, e := range sb.endpoints {
  606. if len(e.Gateway()) > 0 {
  607. gwepAfter = e
  608. break
  609. }
  610. }
  611. delete(sb.epPriority, ep.ID())
  612. sb.Unlock()
  613. if gwepAfter != nil && gwepBefore != gwepAfter {
  614. sb.updateGateway(gwepAfter)
  615. }
  616. // Only update the store if we did not come here as part of
  617. // sandbox delete. If we came here as part of delete then do
  618. // not bother updating the store. The sandbox object will be
  619. // deleted anyway
  620. if !inDelete {
  621. return sb.storeUpdate()
  622. }
  623. return nil
  624. }
  625. // joinLeaveStart waits to ensure there are no joins or leaves in progress and
  626. // marks this join/leave in progress without race
  627. func (sb *sandbox) joinLeaveStart() {
  628. sb.Lock()
  629. defer sb.Unlock()
  630. for sb.joinLeaveDone != nil {
  631. joinLeaveDone := sb.joinLeaveDone
  632. sb.Unlock()
  633. select {
  634. case <-joinLeaveDone:
  635. }
  636. sb.Lock()
  637. }
  638. sb.joinLeaveDone = make(chan struct{})
  639. }
  640. // joinLeaveEnd marks the end of this join/leave operation and
  641. // signals the same without race to other join and leave waiters
  642. func (sb *sandbox) joinLeaveEnd() {
  643. sb.Lock()
  644. defer sb.Unlock()
  645. if sb.joinLeaveDone != nil {
  646. close(sb.joinLeaveDone)
  647. sb.joinLeaveDone = nil
  648. }
  649. }
  650. func (sb *sandbox) hasPortConfigs() bool {
  651. opts := sb.Labels()
  652. _, hasExpPorts := opts[netlabel.ExposedPorts]
  653. _, hasPortMaps := opts[netlabel.PortMap]
  654. return hasExpPorts || hasPortMaps
  655. }
  656. // OptionHostname function returns an option setter for hostname option to
  657. // be passed to NewSandbox method.
  658. func OptionHostname(name string) SandboxOption {
  659. return func(sb *sandbox) {
  660. sb.config.hostName = name
  661. }
  662. }
  663. // OptionDomainname function returns an option setter for domainname option to
  664. // be passed to NewSandbox method.
  665. func OptionDomainname(name string) SandboxOption {
  666. return func(sb *sandbox) {
  667. sb.config.domainName = name
  668. }
  669. }
  670. // OptionHostsPath function returns an option setter for hostspath option to
  671. // be passed to NewSandbox method.
  672. func OptionHostsPath(path string) SandboxOption {
  673. return func(sb *sandbox) {
  674. sb.config.hostsPath = path
  675. }
  676. }
  677. // OptionOriginHostsPath function returns an option setter for origin hosts file path
  678. // tbeo passed to NewSandbox method.
  679. func OptionOriginHostsPath(path string) SandboxOption {
  680. return func(sb *sandbox) {
  681. sb.config.originHostsPath = path
  682. }
  683. }
  684. // OptionExtraHost function returns an option setter for extra /etc/hosts options
  685. // which is a name and IP as strings.
  686. func OptionExtraHost(name string, IP string) SandboxOption {
  687. return func(sb *sandbox) {
  688. sb.config.extraHosts = append(sb.config.extraHosts, extraHost{name: name, IP: IP})
  689. }
  690. }
  691. // OptionParentUpdate function returns an option setter for parent container
  692. // which needs to update the IP address for the linked container.
  693. func OptionParentUpdate(cid string, name, ip string) SandboxOption {
  694. return func(sb *sandbox) {
  695. sb.config.parentUpdates = append(sb.config.parentUpdates, parentUpdate{cid: cid, name: name, ip: ip})
  696. }
  697. }
  698. // OptionResolvConfPath function returns an option setter for resolvconfpath option to
  699. // be passed to net container methods.
  700. func OptionResolvConfPath(path string) SandboxOption {
  701. return func(sb *sandbox) {
  702. sb.config.resolvConfPath = path
  703. }
  704. }
  705. // OptionOriginResolvConfPath function returns an option setter to set the path to the
  706. // origin resolv.conf file to be passed to net container methods.
  707. func OptionOriginResolvConfPath(path string) SandboxOption {
  708. return func(sb *sandbox) {
  709. sb.config.originResolvConfPath = path
  710. }
  711. }
  712. // OptionDNS function returns an option setter for dns entry option to
  713. // be passed to container Create method.
  714. func OptionDNS(dns string) SandboxOption {
  715. return func(sb *sandbox) {
  716. sb.config.dnsList = append(sb.config.dnsList, dns)
  717. }
  718. }
  719. // OptionDNSSearch function returns an option setter for dns search entry option to
  720. // be passed to container Create method.
  721. func OptionDNSSearch(search string) SandboxOption {
  722. return func(sb *sandbox) {
  723. sb.config.dnsSearchList = append(sb.config.dnsSearchList, search)
  724. }
  725. }
  726. // OptionDNSOptions function returns an option setter for dns options entry option to
  727. // be passed to container Create method.
  728. func OptionDNSOptions(options string) SandboxOption {
  729. return func(sb *sandbox) {
  730. sb.config.dnsOptionsList = append(sb.config.dnsOptionsList, options)
  731. }
  732. }
  733. // OptionUseDefaultSandbox function returns an option setter for using default sandbox to
  734. // be passed to container Create method.
  735. func OptionUseDefaultSandbox() SandboxOption {
  736. return func(sb *sandbox) {
  737. sb.config.useDefaultSandBox = true
  738. }
  739. }
  740. // OptionUseExternalKey function returns an option setter for using provided namespace
  741. // instead of creating one.
  742. func OptionUseExternalKey() SandboxOption {
  743. return func(sb *sandbox) {
  744. sb.config.useExternalKey = true
  745. }
  746. }
  747. // OptionGeneric function returns an option setter for Generic configuration
  748. // that is not managed by libNetwork but can be used by the Drivers during the call to
  749. // net container creation method. Container Labels are a good example.
  750. func OptionGeneric(generic map[string]interface{}) SandboxOption {
  751. return func(sb *sandbox) {
  752. if sb.config.generic == nil {
  753. sb.config.generic = make(map[string]interface{}, len(generic))
  754. }
  755. for k, v := range generic {
  756. sb.config.generic[k] = v
  757. }
  758. }
  759. }
  760. // OptionExposedPorts function returns an option setter for the container exposed
  761. // ports option to be passed to container Create method.
  762. func OptionExposedPorts(exposedPorts []types.TransportPort) SandboxOption {
  763. return func(sb *sandbox) {
  764. if sb.config.generic == nil {
  765. sb.config.generic = make(map[string]interface{})
  766. }
  767. // Defensive copy
  768. eps := make([]types.TransportPort, len(exposedPorts))
  769. copy(eps, exposedPorts)
  770. // Store endpoint label and in generic because driver needs it
  771. sb.config.exposedPorts = eps
  772. sb.config.generic[netlabel.ExposedPorts] = eps
  773. }
  774. }
  775. // OptionPortMapping function returns an option setter for the mapping
  776. // ports option to be passed to container Create method.
  777. func OptionPortMapping(portBindings []types.PortBinding) SandboxOption {
  778. return func(sb *sandbox) {
  779. if sb.config.generic == nil {
  780. sb.config.generic = make(map[string]interface{})
  781. }
  782. // Store a copy of the bindings as generic data to pass to the driver
  783. pbs := make([]types.PortBinding, len(portBindings))
  784. copy(pbs, portBindings)
  785. sb.config.generic[netlabel.PortMap] = pbs
  786. }
  787. }
  788. func (eh epHeap) Len() int { return len(eh) }
  789. func (eh epHeap) Less(i, j int) bool {
  790. var (
  791. cip, cjp int
  792. ok bool
  793. )
  794. ci, _ := eh[i].getSandbox()
  795. cj, _ := eh[j].getSandbox()
  796. epi := eh[i]
  797. epj := eh[j]
  798. if epi.endpointInGWNetwork() {
  799. return false
  800. }
  801. if epj.endpointInGWNetwork() {
  802. return true
  803. }
  804. if ci != nil {
  805. cip, ok = ci.epPriority[eh[i].ID()]
  806. if !ok {
  807. cip = 0
  808. }
  809. }
  810. if cj != nil {
  811. cjp, ok = cj.epPriority[eh[j].ID()]
  812. if !ok {
  813. cjp = 0
  814. }
  815. }
  816. if cip == cjp {
  817. return eh[i].network.Name() < eh[j].network.Name()
  818. }
  819. return cip > cjp
  820. }
  821. func (eh epHeap) Swap(i, j int) { eh[i], eh[j] = eh[j], eh[i] }
  822. func (eh *epHeap) Push(x interface{}) {
  823. *eh = append(*eh, x.(*endpoint))
  824. }
  825. func (eh *epHeap) Pop() interface{} {
  826. old := *eh
  827. n := len(old)
  828. x := old[n-1]
  829. *eh = old[0 : n-1]
  830. return x
  831. }