sandbox.go 21 KB

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