sandbox.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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) ResolveName(name string) net.IP {
  332. var ip net.IP
  333. // Embedded server owns the docker network domain. Resolution should work
  334. // for both container_name and container_name.network_name
  335. // We allow '.' in service name and network name. For a name a.b.c.d the
  336. // following have to tried;
  337. // {a.b.c.d in the networks container is connected to}
  338. // {a.b.c in network d},
  339. // {a.b in network c.d},
  340. // {a in network b.c.d},
  341. name = strings.TrimSuffix(name, ".")
  342. reqName := []string{name}
  343. networkName := []string{""}
  344. if strings.Contains(name, ".") {
  345. var i int
  346. dup := name
  347. for {
  348. if i = strings.LastIndex(dup, "."); i == -1 {
  349. break
  350. }
  351. networkName = append(networkName, name[i+1:])
  352. reqName = append(reqName, name[:i])
  353. dup = dup[:i]
  354. }
  355. }
  356. epList := sb.getConnectedEndpoints()
  357. for i := 0; i < len(reqName); i++ {
  358. log.Debugf("To resolve: %v in %v", reqName[i], networkName[i])
  359. // First check for local container alias
  360. ip = sb.resolveName(reqName[i], networkName[i], epList, true)
  361. if ip != nil {
  362. return ip
  363. }
  364. // Resolve the actual container name
  365. ip = sb.resolveName(reqName[i], networkName[i], epList, false)
  366. if ip != nil {
  367. return ip
  368. }
  369. }
  370. return nil
  371. }
  372. func (sb *sandbox) resolveName(req string, networkName string, epList []*endpoint, alias bool) net.IP {
  373. for _, ep := range epList {
  374. name := req
  375. n := ep.getNetwork()
  376. if networkName != "" && networkName != n.Name() {
  377. continue
  378. }
  379. if alias {
  380. if ep.aliases == nil {
  381. continue
  382. }
  383. var ok bool
  384. ep.Lock()
  385. name, ok = ep.aliases[req]
  386. ep.Unlock()
  387. if !ok {
  388. continue
  389. }
  390. } else {
  391. // If it is a regular lookup and if the requested name is an alias
  392. // dont perform a svc lookup for this endpoint.
  393. ep.Lock()
  394. if _, ok := ep.aliases[req]; ok {
  395. ep.Unlock()
  396. continue
  397. }
  398. ep.Unlock()
  399. }
  400. sr, ok := n.getController().svcDb[n.ID()]
  401. if !ok {
  402. continue
  403. }
  404. n.Lock()
  405. ip, ok := sr.svcMap[name]
  406. n.Unlock()
  407. if ok {
  408. return ip[0]
  409. }
  410. }
  411. return nil
  412. }
  413. func (sb *sandbox) SetKey(basePath string) error {
  414. if basePath == "" {
  415. return types.BadRequestErrorf("invalid sandbox key")
  416. }
  417. sb.Lock()
  418. oldosSbox := sb.osSbox
  419. sb.Unlock()
  420. if oldosSbox != nil {
  421. // If we already have an OS sandbox, release the network resources from that
  422. // and destroy the OS snab. We are moving into a new home further down. Note that none
  423. // of the network resources gets destroyed during the move.
  424. sb.releaseOSSbox()
  425. }
  426. osSbox, err := osl.GetSandboxForExternalKey(basePath, sb.Key())
  427. if err != nil {
  428. return err
  429. }
  430. sb.Lock()
  431. sb.osSbox = osSbox
  432. sb.Unlock()
  433. defer func() {
  434. if err != nil {
  435. sb.Lock()
  436. sb.osSbox = nil
  437. sb.Unlock()
  438. }
  439. }()
  440. // If the resolver was setup before stop it and set it up in the
  441. // new osl sandbox.
  442. if oldosSbox != nil && sb.resolver != nil {
  443. sb.resolver.Stop()
  444. sb.osSbox.InvokeFunc(sb.resolver.SetupFunc())
  445. if err := sb.resolver.Start(); err != nil {
  446. log.Errorf("Resolver Setup/Start failed for container %s, %q", sb.ContainerID(), err)
  447. }
  448. }
  449. for _, ep := range sb.getConnectedEndpoints() {
  450. if err = sb.populateNetworkResources(ep); err != nil {
  451. return err
  452. }
  453. }
  454. return nil
  455. }
  456. func releaseOSSboxResources(osSbox osl.Sandbox, ep *endpoint) {
  457. for _, i := range osSbox.Info().Interfaces() {
  458. // Only remove the interfaces owned by this endpoint from the sandbox.
  459. if ep.hasInterface(i.SrcName()) {
  460. if err := i.Remove(); err != nil {
  461. log.Debugf("Remove interface %s failed: %v", i.SrcName(), err)
  462. }
  463. }
  464. }
  465. ep.Lock()
  466. joinInfo := ep.joinInfo
  467. ep.Unlock()
  468. if joinInfo == nil {
  469. return
  470. }
  471. // Remove non-interface routes.
  472. for _, r := range joinInfo.StaticRoutes {
  473. if err := osSbox.RemoveStaticRoute(r); err != nil {
  474. log.Debugf("Remove route failed: %v", err)
  475. }
  476. }
  477. }
  478. func (sb *sandbox) releaseOSSbox() {
  479. sb.Lock()
  480. osSbox := sb.osSbox
  481. sb.osSbox = nil
  482. sb.Unlock()
  483. if osSbox == nil {
  484. return
  485. }
  486. for _, ep := range sb.getConnectedEndpoints() {
  487. releaseOSSboxResources(osSbox, ep)
  488. }
  489. osSbox.Destroy()
  490. }
  491. func (sb *sandbox) populateNetworkResources(ep *endpoint) error {
  492. sb.Lock()
  493. if sb.osSbox == nil {
  494. sb.Unlock()
  495. return nil
  496. }
  497. inDelete := sb.inDelete
  498. sb.Unlock()
  499. ep.Lock()
  500. joinInfo := ep.joinInfo
  501. i := ep.iface
  502. ep.Unlock()
  503. if ep.needResolver() {
  504. sb.startResolver()
  505. }
  506. if i != nil && i.srcName != "" {
  507. var ifaceOptions []osl.IfaceOption
  508. ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().Address(i.addr), sb.osSbox.InterfaceOptions().Routes(i.routes))
  509. if i.addrv6 != nil && i.addrv6.IP.To16() != nil {
  510. ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().AddressIPv6(i.addrv6))
  511. }
  512. if err := sb.osSbox.AddInterface(i.srcName, i.dstPrefix, ifaceOptions...); err != nil {
  513. return fmt.Errorf("failed to add interface %s to sandbox: %v", i.srcName, err)
  514. }
  515. }
  516. if joinInfo != nil {
  517. // Set up non-interface routes.
  518. for _, r := range joinInfo.StaticRoutes {
  519. if err := sb.osSbox.AddStaticRoute(r); err != nil {
  520. return fmt.Errorf("failed to add static route %s: %v", r.Destination.String(), err)
  521. }
  522. }
  523. }
  524. for _, gwep := range sb.getConnectedEndpoints() {
  525. if len(gwep.Gateway()) > 0 {
  526. if gwep != ep {
  527. break
  528. }
  529. if err := sb.updateGateway(gwep); err != nil {
  530. return err
  531. }
  532. }
  533. }
  534. // Only update the store if we did not come here as part of
  535. // sandbox delete. If we came here as part of delete then do
  536. // not bother updating the store. The sandbox object will be
  537. // deleted anyway
  538. if !inDelete {
  539. return sb.storeUpdate()
  540. }
  541. return nil
  542. }
  543. func (sb *sandbox) clearNetworkResources(origEp *endpoint) error {
  544. ep := sb.getEndpoint(origEp.id)
  545. if ep == nil {
  546. return fmt.Errorf("could not find the sandbox endpoint data for endpoint %s",
  547. ep.name)
  548. }
  549. sb.Lock()
  550. osSbox := sb.osSbox
  551. inDelete := sb.inDelete
  552. sb.Unlock()
  553. if osSbox != nil {
  554. releaseOSSboxResources(osSbox, ep)
  555. }
  556. sb.Lock()
  557. if len(sb.endpoints) == 0 {
  558. // sb.endpoints should never be empty and this is unexpected error condition
  559. // We log an error message to note this down for debugging purposes.
  560. log.Errorf("No endpoints in sandbox while trying to remove endpoint %s", ep.Name())
  561. sb.Unlock()
  562. return nil
  563. }
  564. var (
  565. gwepBefore, gwepAfter *endpoint
  566. index = -1
  567. )
  568. for i, e := range sb.endpoints {
  569. if e == ep {
  570. index = i
  571. }
  572. if len(e.Gateway()) > 0 && gwepBefore == nil {
  573. gwepBefore = e
  574. }
  575. if index != -1 && gwepBefore != nil {
  576. break
  577. }
  578. }
  579. heap.Remove(&sb.endpoints, index)
  580. for _, e := range sb.endpoints {
  581. if len(e.Gateway()) > 0 {
  582. gwepAfter = e
  583. break
  584. }
  585. }
  586. delete(sb.epPriority, ep.ID())
  587. sb.Unlock()
  588. if gwepAfter != nil && gwepBefore != gwepAfter {
  589. sb.updateGateway(gwepAfter)
  590. }
  591. // Only update the store if we did not come here as part of
  592. // sandbox delete. If we came here as part of delete then do
  593. // not bother updating the store. The sandbox object will be
  594. // deleted anyway
  595. if !inDelete {
  596. return sb.storeUpdate()
  597. }
  598. return nil
  599. }
  600. // joinLeaveStart waits to ensure there are no joins or leaves in progress and
  601. // marks this join/leave in progress without race
  602. func (sb *sandbox) joinLeaveStart() {
  603. sb.Lock()
  604. defer sb.Unlock()
  605. for sb.joinLeaveDone != nil {
  606. joinLeaveDone := sb.joinLeaveDone
  607. sb.Unlock()
  608. select {
  609. case <-joinLeaveDone:
  610. }
  611. sb.Lock()
  612. }
  613. sb.joinLeaveDone = make(chan struct{})
  614. }
  615. // joinLeaveEnd marks the end of this join/leave operation and
  616. // signals the same without race to other join and leave waiters
  617. func (sb *sandbox) joinLeaveEnd() {
  618. sb.Lock()
  619. defer sb.Unlock()
  620. if sb.joinLeaveDone != nil {
  621. close(sb.joinLeaveDone)
  622. sb.joinLeaveDone = nil
  623. }
  624. }
  625. // OptionHostname function returns an option setter for hostname option to
  626. // be passed to NewSandbox method.
  627. func OptionHostname(name string) SandboxOption {
  628. return func(sb *sandbox) {
  629. sb.config.hostName = name
  630. }
  631. }
  632. // OptionDomainname function returns an option setter for domainname option to
  633. // be passed to NewSandbox method.
  634. func OptionDomainname(name string) SandboxOption {
  635. return func(sb *sandbox) {
  636. sb.config.domainName = name
  637. }
  638. }
  639. // OptionHostsPath function returns an option setter for hostspath option to
  640. // be passed to NewSandbox method.
  641. func OptionHostsPath(path string) SandboxOption {
  642. return func(sb *sandbox) {
  643. sb.config.hostsPath = path
  644. }
  645. }
  646. // OptionOriginHostsPath function returns an option setter for origin hosts file path
  647. // tbeo passed to NewSandbox method.
  648. func OptionOriginHostsPath(path string) SandboxOption {
  649. return func(sb *sandbox) {
  650. sb.config.originHostsPath = path
  651. }
  652. }
  653. // OptionExtraHost function returns an option setter for extra /etc/hosts options
  654. // which is a name and IP as strings.
  655. func OptionExtraHost(name string, IP string) SandboxOption {
  656. return func(sb *sandbox) {
  657. sb.config.extraHosts = append(sb.config.extraHosts, extraHost{name: name, IP: IP})
  658. }
  659. }
  660. // OptionParentUpdate function returns an option setter for parent container
  661. // which needs to update the IP address for the linked container.
  662. func OptionParentUpdate(cid string, name, ip string) SandboxOption {
  663. return func(sb *sandbox) {
  664. sb.config.parentUpdates = append(sb.config.parentUpdates, parentUpdate{cid: cid, name: name, ip: ip})
  665. }
  666. }
  667. // OptionResolvConfPath function returns an option setter for resolvconfpath option to
  668. // be passed to net container methods.
  669. func OptionResolvConfPath(path string) SandboxOption {
  670. return func(sb *sandbox) {
  671. sb.config.resolvConfPath = path
  672. }
  673. }
  674. // OptionOriginResolvConfPath function returns an option setter to set the path to the
  675. // origin resolv.conf file to be passed to net container methods.
  676. func OptionOriginResolvConfPath(path string) SandboxOption {
  677. return func(sb *sandbox) {
  678. sb.config.originResolvConfPath = path
  679. }
  680. }
  681. // OptionDNS function returns an option setter for dns entry option to
  682. // be passed to container Create method.
  683. func OptionDNS(dns string) SandboxOption {
  684. return func(sb *sandbox) {
  685. sb.config.dnsList = append(sb.config.dnsList, dns)
  686. }
  687. }
  688. // OptionDNSSearch function returns an option setter for dns search entry option to
  689. // be passed to container Create method.
  690. func OptionDNSSearch(search string) SandboxOption {
  691. return func(sb *sandbox) {
  692. sb.config.dnsSearchList = append(sb.config.dnsSearchList, search)
  693. }
  694. }
  695. // OptionDNSOptions function returns an option setter for dns options entry option to
  696. // be passed to container Create method.
  697. func OptionDNSOptions(options string) SandboxOption {
  698. return func(sb *sandbox) {
  699. sb.config.dnsOptionsList = append(sb.config.dnsOptionsList, options)
  700. }
  701. }
  702. // OptionUseDefaultSandbox function returns an option setter for using default sandbox to
  703. // be passed to container Create method.
  704. func OptionUseDefaultSandbox() SandboxOption {
  705. return func(sb *sandbox) {
  706. sb.config.useDefaultSandBox = true
  707. }
  708. }
  709. // OptionUseExternalKey function returns an option setter for using provided namespace
  710. // instead of creating one.
  711. func OptionUseExternalKey() SandboxOption {
  712. return func(sb *sandbox) {
  713. sb.config.useExternalKey = true
  714. }
  715. }
  716. // OptionGeneric function returns an option setter for Generic configuration
  717. // that is not managed by libNetwork but can be used by the Drivers during the call to
  718. // net container creation method. Container Labels are a good example.
  719. func OptionGeneric(generic map[string]interface{}) SandboxOption {
  720. return func(sb *sandbox) {
  721. sb.config.generic = generic
  722. }
  723. }
  724. func (eh epHeap) Len() int { return len(eh) }
  725. func (eh epHeap) Less(i, j int) bool {
  726. var (
  727. cip, cjp int
  728. ok bool
  729. )
  730. ci, _ := eh[i].getSandbox()
  731. cj, _ := eh[j].getSandbox()
  732. epi := eh[i]
  733. epj := eh[j]
  734. if epi.endpointInGWNetwork() {
  735. return false
  736. }
  737. if epj.endpointInGWNetwork() {
  738. return true
  739. }
  740. if ci != nil {
  741. cip, ok = ci.epPriority[eh[i].ID()]
  742. if !ok {
  743. cip = 0
  744. }
  745. }
  746. if cj != nil {
  747. cjp, ok = cj.epPriority[eh[j].ID()]
  748. if !ok {
  749. cjp = 0
  750. }
  751. }
  752. if cip == cjp {
  753. return eh[i].network.Name() < eh[j].network.Name()
  754. }
  755. return cip > cjp
  756. }
  757. func (eh epHeap) Swap(i, j int) { eh[i], eh[j] = eh[j], eh[i] }
  758. func (eh *epHeap) Push(x interface{}) {
  759. *eh = append(*eh, x.(*endpoint))
  760. }
  761. func (eh *epHeap) Pop() interface{} {
  762. old := *eh
  763. n := len(old)
  764. x := old[n-1]
  765. *eh = old[0 : n-1]
  766. return x
  767. }