sandbox.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  1. package libnetwork
  2. import (
  3. "container/heap"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net"
  8. "os"
  9. "path"
  10. "path/filepath"
  11. "strings"
  12. "sync"
  13. log "github.com/Sirupsen/logrus"
  14. "github.com/docker/libnetwork/etchosts"
  15. "github.com/docker/libnetwork/netutils"
  16. "github.com/docker/libnetwork/osl"
  17. "github.com/docker/libnetwork/resolvconf"
  18. "github.com/docker/libnetwork/types"
  19. )
  20. // Sandbox provides the control over the network container entity. It is a one to one mapping with the container.
  21. type Sandbox interface {
  22. // ID returns the ID of the sandbox
  23. ID() string
  24. // Key returns the sandbox's key
  25. Key() string
  26. // ContainerID returns the container id associated to this sandbox
  27. ContainerID() string
  28. // Labels returns the sandbox's labels
  29. Labels() map[string]interface{}
  30. // Statistics retrieves the interfaces' statistics for the sandbox
  31. Statistics() (map[string]*types.InterfaceStatistics, error)
  32. // Refresh leaves all the endpoints, resets and re-apply the options,
  33. // re-joins all the endpoints without destroying the osl sandbox
  34. Refresh(options ...SandboxOption) error
  35. // SetKey updates the Sandbox Key
  36. SetKey(key string) error
  37. // Rename changes the name of all attached Endpoints
  38. Rename(name string) error
  39. // Delete destroys this container after detaching it from all connected endpoints.
  40. Delete() error
  41. // ResolveName searches for the service name in the networks to which the sandbox
  42. // is connected to.
  43. ResolveName(name string) net.IP
  44. // ResolveIP returns the service name for the passed in IP. IP is in reverse dotted
  45. // notation; the format used for DNS PTR records
  46. ResolveIP(name string) string
  47. }
  48. // SandboxOption is an option setter function type used to pass various options to
  49. // NewNetContainer method. The various setter functions of type SandboxOption are
  50. // provided by libnetwork, they look like ContainerOptionXXXX(...)
  51. type SandboxOption func(sb *sandbox)
  52. func (sb *sandbox) processOptions(options ...SandboxOption) {
  53. for _, opt := range options {
  54. if opt != nil {
  55. opt(sb)
  56. }
  57. }
  58. }
  59. type epHeap []*endpoint
  60. type sandbox struct {
  61. id string
  62. containerID string
  63. config containerConfig
  64. extDNS []string
  65. osSbox osl.Sandbox
  66. controller *controller
  67. resolver Resolver
  68. resolverOnce sync.Once
  69. refCnt int
  70. endpoints epHeap
  71. epPriority map[string]int
  72. joinLeaveDone chan struct{}
  73. dbIndex uint64
  74. dbExists bool
  75. isStub bool
  76. inDelete bool
  77. sync.Mutex
  78. }
  79. // These are the container configs used to customize container /etc/hosts file.
  80. type hostsPathConfig struct {
  81. hostName string
  82. domainName string
  83. hostsPath string
  84. originHostsPath string
  85. extraHosts []extraHost
  86. parentUpdates []parentUpdate
  87. }
  88. type parentUpdate struct {
  89. cid string
  90. name string
  91. ip string
  92. }
  93. type extraHost struct {
  94. name string
  95. IP string
  96. }
  97. // These are the container configs used to customize container /etc/resolv.conf file.
  98. type resolvConfPathConfig struct {
  99. resolvConfPath string
  100. originResolvConfPath string
  101. resolvConfHashFile string
  102. dnsList []string
  103. dnsSearchList []string
  104. dnsOptionsList []string
  105. }
  106. type containerConfig struct {
  107. hostsPathConfig
  108. resolvConfPathConfig
  109. generic map[string]interface{}
  110. useDefaultSandBox bool
  111. useExternalKey bool
  112. prio int // higher the value, more the priority
  113. }
  114. func (sb *sandbox) ID() string {
  115. return sb.id
  116. }
  117. func (sb *sandbox) ContainerID() string {
  118. return sb.containerID
  119. }
  120. func (sb *sandbox) Key() string {
  121. if sb.config.useDefaultSandBox {
  122. return osl.GenerateKey("default")
  123. }
  124. return osl.GenerateKey(sb.id)
  125. }
  126. func (sb *sandbox) Labels() map[string]interface{} {
  127. return sb.config.generic
  128. }
  129. func (sb *sandbox) Statistics() (map[string]*types.InterfaceStatistics, error) {
  130. m := make(map[string]*types.InterfaceStatistics)
  131. if sb.osSbox == nil {
  132. return m, nil
  133. }
  134. var err error
  135. for _, i := range sb.osSbox.Info().Interfaces() {
  136. if m[i.DstName()], err = i.Statistics(); err != nil {
  137. return m, err
  138. }
  139. }
  140. return m, nil
  141. }
  142. func (sb *sandbox) Delete() error {
  143. return sb.delete(false)
  144. }
  145. func (sb *sandbox) delete(force bool) error {
  146. sb.Lock()
  147. if sb.inDelete {
  148. sb.Unlock()
  149. return types.ForbiddenErrorf("another sandbox delete in progress")
  150. }
  151. // Set the inDelete flag. This will ensure that we don't
  152. // update the store until we have completed all the endpoint
  153. // leaves and deletes. And when endpoint leaves and deletes
  154. // are completed then we can finally delete the sandbox object
  155. // altogether from the data store. If the daemon exits
  156. // ungracefully in the middle of a sandbox delete this way we
  157. // will have all the references to the endpoints in the
  158. // sandbox so that we can clean them up when we restart
  159. sb.inDelete = true
  160. sb.Unlock()
  161. c := sb.controller
  162. // Detach from all endpoints
  163. retain := false
  164. for _, ep := range sb.getConnectedEndpoints() {
  165. // endpoint in the Gateway network will be cleaned up
  166. // when when sandbox no longer needs external connectivity
  167. if ep.endpointInGWNetwork() {
  168. continue
  169. }
  170. // Retain the sanbdox if we can't obtain the network from store.
  171. if _, err := c.getNetworkFromStore(ep.getNetwork().ID()); err != nil {
  172. retain = true
  173. log.Warnf("Failed getting network for ep %s during sandbox %s delete: %v", ep.ID(), sb.ID(), err)
  174. continue
  175. }
  176. if !force {
  177. if err := ep.Leave(sb); err != nil {
  178. log.Warnf("Failed detaching sandbox %s from endpoint %s: %v\n", sb.ID(), ep.ID(), err)
  179. }
  180. }
  181. if err := ep.Delete(force); err != nil {
  182. log.Warnf("Failed deleting endpoint %s: %v\n", ep.ID(), err)
  183. }
  184. }
  185. if retain {
  186. sb.Lock()
  187. sb.inDelete = false
  188. sb.Unlock()
  189. return fmt.Errorf("could not cleanup all the endpoints in container %s / sandbox %s", sb.containerID, sb.id)
  190. }
  191. // Container is going away. Path cache in etchosts is most
  192. // likely not required any more. Drop it.
  193. etchosts.Drop(sb.config.hostsPath)
  194. if sb.resolver != nil {
  195. sb.resolver.Stop()
  196. }
  197. if sb.osSbox != nil && !sb.config.useDefaultSandBox {
  198. sb.osSbox.Destroy()
  199. }
  200. if err := sb.storeDelete(); err != nil {
  201. log.Warnf("Failed to delete sandbox %s from store: %v", sb.ID(), err)
  202. }
  203. c.Lock()
  204. delete(c.sandboxes, sb.ID())
  205. c.Unlock()
  206. return nil
  207. }
  208. func (sb *sandbox) Rename(name string) error {
  209. var err error
  210. for _, ep := range sb.getConnectedEndpoints() {
  211. if ep.endpointInGWNetwork() {
  212. continue
  213. }
  214. oldName := ep.Name()
  215. lEp := ep
  216. if err = ep.rename(name); err != nil {
  217. break
  218. }
  219. defer func() {
  220. if err != nil {
  221. lEp.rename(oldName)
  222. }
  223. }()
  224. }
  225. return err
  226. }
  227. func (sb *sandbox) Refresh(options ...SandboxOption) error {
  228. // Store connected endpoints
  229. epList := sb.getConnectedEndpoints()
  230. // Detach from all endpoints
  231. for _, ep := range epList {
  232. if err := ep.Leave(sb); err != nil {
  233. log.Warnf("Failed detaching sandbox %s from endpoint %s: %v\n", sb.ID(), ep.ID(), err)
  234. }
  235. }
  236. // Re-apply options
  237. sb.config = containerConfig{}
  238. sb.processOptions(options...)
  239. // Setup discovery files
  240. if err := sb.setupResolutionFiles(); err != nil {
  241. return err
  242. }
  243. // Re -connect to all endpoints
  244. for _, ep := range epList {
  245. if err := ep.Join(sb); err != nil {
  246. log.Warnf("Failed attach sandbox %s to endpoint %s: %v\n", sb.ID(), ep.ID(), err)
  247. }
  248. }
  249. return nil
  250. }
  251. func (sb *sandbox) MarshalJSON() ([]byte, error) {
  252. sb.Lock()
  253. defer sb.Unlock()
  254. // We are just interested in the container ID. This can be expanded to include all of containerInfo if there is a need
  255. return json.Marshal(sb.id)
  256. }
  257. func (sb *sandbox) UnmarshalJSON(b []byte) (err error) {
  258. sb.Lock()
  259. defer sb.Unlock()
  260. var id string
  261. if err := json.Unmarshal(b, &id); err != nil {
  262. return err
  263. }
  264. sb.id = id
  265. return nil
  266. }
  267. func (sb *sandbox) startResolver() {
  268. sb.resolverOnce.Do(func() {
  269. var err error
  270. sb.resolver = NewResolver(sb)
  271. defer func() {
  272. if err != nil {
  273. sb.resolver = nil
  274. }
  275. }()
  276. err = sb.rebuildDNS()
  277. if err != nil {
  278. log.Errorf("Updating resolv.conf failed for container %s, %q", sb.ContainerID(), err)
  279. return
  280. }
  281. sb.resolver.SetExtServers(sb.extDNS)
  282. sb.osSbox.InvokeFunc(sb.resolver.SetupFunc())
  283. if err = sb.resolver.Start(); err != nil {
  284. log.Errorf("Resolver Setup/Start failed for container %s, %q", sb.ContainerID(), err)
  285. }
  286. })
  287. }
  288. func (sb *sandbox) setupResolutionFiles() error {
  289. if err := sb.buildHostsFile(); err != nil {
  290. return err
  291. }
  292. if err := sb.updateParentHosts(); err != nil {
  293. return err
  294. }
  295. if err := sb.setupDNS(); err != nil {
  296. return err
  297. }
  298. return nil
  299. }
  300. func (sb *sandbox) getConnectedEndpoints() []*endpoint {
  301. sb.Lock()
  302. defer sb.Unlock()
  303. eps := make([]*endpoint, len(sb.endpoints))
  304. for i, ep := range sb.endpoints {
  305. eps[i] = ep
  306. }
  307. return eps
  308. }
  309. func (sb *sandbox) getEndpoint(id string) *endpoint {
  310. sb.Lock()
  311. defer sb.Unlock()
  312. for _, ep := range sb.endpoints {
  313. if ep.id == id {
  314. return ep
  315. }
  316. }
  317. return nil
  318. }
  319. func (sb *sandbox) updateGateway(ep *endpoint) error {
  320. sb.Lock()
  321. osSbox := sb.osSbox
  322. sb.Unlock()
  323. if osSbox == nil {
  324. return nil
  325. }
  326. osSbox.UnsetGateway()
  327. osSbox.UnsetGatewayIPv6()
  328. if ep == nil {
  329. return nil
  330. }
  331. ep.Lock()
  332. joinInfo := ep.joinInfo
  333. ep.Unlock()
  334. if err := osSbox.SetGateway(joinInfo.gw); err != nil {
  335. return fmt.Errorf("failed to set gateway while updating gateway: %v", err)
  336. }
  337. if err := osSbox.SetGatewayIPv6(joinInfo.gw6); err != nil {
  338. return fmt.Errorf("failed to set IPv6 gateway while updating gateway: %v", err)
  339. }
  340. return nil
  341. }
  342. func (sb *sandbox) ResolveIP(ip string) string {
  343. var svc string
  344. log.Debugf("IP To resolve %v", ip)
  345. for _, ep := range sb.getConnectedEndpoints() {
  346. n := ep.getNetwork()
  347. sr, ok := n.getController().svcDb[n.ID()]
  348. if !ok {
  349. continue
  350. }
  351. nwName := n.Name()
  352. n.Lock()
  353. svc, ok = sr.ipMap[ip]
  354. n.Unlock()
  355. if ok {
  356. return svc + "." + nwName
  357. }
  358. }
  359. return svc
  360. }
  361. func (sb *sandbox) ResolveName(name string) net.IP {
  362. var ip net.IP
  363. // Embedded server owns the docker network domain. Resolution should work
  364. // for both container_name and container_name.network_name
  365. // We allow '.' in service name and network name. For a name a.b.c.d the
  366. // following have to tried;
  367. // {a.b.c.d in the networks container is connected to}
  368. // {a.b.c in network d},
  369. // {a.b in network c.d},
  370. // {a in network b.c.d},
  371. name = strings.TrimSuffix(name, ".")
  372. reqName := []string{name}
  373. networkName := []string{""}
  374. if strings.Contains(name, ".") {
  375. var i int
  376. dup := name
  377. for {
  378. if i = strings.LastIndex(dup, "."); i == -1 {
  379. break
  380. }
  381. networkName = append(networkName, name[i+1:])
  382. reqName = append(reqName, name[:i])
  383. dup = dup[:i]
  384. }
  385. }
  386. epList := sb.getConnectedEndpoints()
  387. for i := 0; i < len(reqName); i++ {
  388. log.Debugf("To resolve: %v in %v", reqName[i], networkName[i])
  389. // First check for local container alias
  390. ip = sb.resolveName(reqName[i], networkName[i], epList, true)
  391. if ip != nil {
  392. return ip
  393. }
  394. // Resolve the actual container name
  395. ip = sb.resolveName(reqName[i], networkName[i], epList, false)
  396. if ip != nil {
  397. return ip
  398. }
  399. }
  400. return nil
  401. }
  402. func (sb *sandbox) resolveName(req string, networkName string, epList []*endpoint, alias bool) net.IP {
  403. for _, ep := range epList {
  404. name := req
  405. n := ep.getNetwork()
  406. if networkName != "" && networkName != n.Name() {
  407. continue
  408. }
  409. if alias {
  410. if ep.aliases == nil {
  411. continue
  412. }
  413. var ok bool
  414. ep.Lock()
  415. name, ok = ep.aliases[req]
  416. ep.Unlock()
  417. if !ok {
  418. continue
  419. }
  420. } else {
  421. // If it is a regular lookup and if the requested name is an alias
  422. // dont perform a svc lookup for this endpoint.
  423. ep.Lock()
  424. if _, ok := ep.aliases[req]; ok {
  425. ep.Unlock()
  426. continue
  427. }
  428. ep.Unlock()
  429. }
  430. sr, ok := n.getController().svcDb[n.ID()]
  431. if !ok {
  432. continue
  433. }
  434. n.Lock()
  435. ip, ok := sr.svcMap[name]
  436. n.Unlock()
  437. if ok {
  438. return ip[0]
  439. }
  440. }
  441. return nil
  442. }
  443. func (sb *sandbox) SetKey(basePath string) error {
  444. if basePath == "" {
  445. return types.BadRequestErrorf("invalid sandbox key")
  446. }
  447. sb.Lock()
  448. oldosSbox := sb.osSbox
  449. sb.Unlock()
  450. if oldosSbox != nil {
  451. // If we already have an OS sandbox, release the network resources from that
  452. // and destroy the OS snab. We are moving into a new home further down. Note that none
  453. // of the network resources gets destroyed during the move.
  454. sb.releaseOSSbox()
  455. }
  456. osSbox, err := osl.GetSandboxForExternalKey(basePath, sb.Key())
  457. if err != nil {
  458. return err
  459. }
  460. sb.Lock()
  461. sb.osSbox = osSbox
  462. sb.Unlock()
  463. defer func() {
  464. if err != nil {
  465. sb.Lock()
  466. sb.osSbox = nil
  467. sb.Unlock()
  468. }
  469. }()
  470. // If the resolver was setup before stop it and set it up in the
  471. // new osl sandbox.
  472. if oldosSbox != nil && sb.resolver != nil {
  473. sb.resolver.Stop()
  474. sb.osSbox.InvokeFunc(sb.resolver.SetupFunc())
  475. if err := sb.resolver.Start(); err != nil {
  476. log.Errorf("Resolver Setup/Start failed for container %s, %q", sb.ContainerID(), err)
  477. }
  478. }
  479. for _, ep := range sb.getConnectedEndpoints() {
  480. if err = sb.populateNetworkResources(ep); err != nil {
  481. return err
  482. }
  483. }
  484. return nil
  485. }
  486. func releaseOSSboxResources(osSbox osl.Sandbox, ep *endpoint) {
  487. for _, i := range osSbox.Info().Interfaces() {
  488. // Only remove the interfaces owned by this endpoint from the sandbox.
  489. if ep.hasInterface(i.SrcName()) {
  490. if err := i.Remove(); err != nil {
  491. log.Debugf("Remove interface failed: %v", err)
  492. }
  493. }
  494. }
  495. ep.Lock()
  496. joinInfo := ep.joinInfo
  497. ep.Unlock()
  498. if joinInfo == nil {
  499. return
  500. }
  501. // Remove non-interface routes.
  502. for _, r := range joinInfo.StaticRoutes {
  503. if err := osSbox.RemoveStaticRoute(r); err != nil {
  504. log.Debugf("Remove route failed: %v", err)
  505. }
  506. }
  507. }
  508. func (sb *sandbox) releaseOSSbox() {
  509. sb.Lock()
  510. osSbox := sb.osSbox
  511. sb.osSbox = nil
  512. sb.Unlock()
  513. if osSbox == nil {
  514. return
  515. }
  516. for _, ep := range sb.getConnectedEndpoints() {
  517. releaseOSSboxResources(osSbox, ep)
  518. }
  519. osSbox.Destroy()
  520. }
  521. func (sb *sandbox) populateNetworkResources(ep *endpoint) error {
  522. sb.Lock()
  523. if sb.osSbox == nil {
  524. sb.Unlock()
  525. return nil
  526. }
  527. inDelete := sb.inDelete
  528. sb.Unlock()
  529. ep.Lock()
  530. joinInfo := ep.joinInfo
  531. i := ep.iface
  532. ep.Unlock()
  533. if ep.needResolver() {
  534. sb.startResolver()
  535. }
  536. if i != nil && i.srcName != "" {
  537. var ifaceOptions []osl.IfaceOption
  538. ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().Address(i.addr), sb.osSbox.InterfaceOptions().Routes(i.routes))
  539. if i.addrv6 != nil && i.addrv6.IP.To16() != nil {
  540. ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().AddressIPv6(i.addrv6))
  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. for _, gwep := range sb.getConnectedEndpoints() {
  555. if len(gwep.Gateway()) > 0 {
  556. if gwep != ep {
  557. break
  558. }
  559. if err := sb.updateGateway(gwep); err != nil {
  560. return err
  561. }
  562. }
  563. }
  564. // Only update the store if we did not come here as part of
  565. // sandbox delete. If we came here as part of delete then do
  566. // not bother updating the store. The sandbox object will be
  567. // deleted anyway
  568. if !inDelete {
  569. return sb.storeUpdate()
  570. }
  571. return nil
  572. }
  573. func (sb *sandbox) clearNetworkResources(origEp *endpoint) error {
  574. ep := sb.getEndpoint(origEp.id)
  575. if ep == nil {
  576. return fmt.Errorf("could not find the sandbox endpoint data for endpoint %s",
  577. ep.name)
  578. }
  579. sb.Lock()
  580. osSbox := sb.osSbox
  581. inDelete := sb.inDelete
  582. sb.Unlock()
  583. if osSbox != nil {
  584. releaseOSSboxResources(osSbox, ep)
  585. }
  586. sb.Lock()
  587. if len(sb.endpoints) == 0 {
  588. // sb.endpoints should never be empty and this is unexpected error condition
  589. // We log an error message to note this down for debugging purposes.
  590. log.Errorf("No endpoints in sandbox while trying to remove endpoint %s", ep.Name())
  591. sb.Unlock()
  592. return nil
  593. }
  594. var (
  595. gwepBefore, gwepAfter *endpoint
  596. index = -1
  597. )
  598. for i, e := range sb.endpoints {
  599. if e == ep {
  600. index = i
  601. }
  602. if len(e.Gateway()) > 0 && gwepBefore == nil {
  603. gwepBefore = e
  604. }
  605. if index != -1 && gwepBefore != nil {
  606. break
  607. }
  608. }
  609. heap.Remove(&sb.endpoints, index)
  610. for _, e := range sb.endpoints {
  611. if len(e.Gateway()) > 0 {
  612. gwepAfter = e
  613. break
  614. }
  615. }
  616. delete(sb.epPriority, ep.ID())
  617. sb.Unlock()
  618. if gwepAfter != nil && gwepBefore != gwepAfter {
  619. sb.updateGateway(gwepAfter)
  620. }
  621. // Only update the store if we did not come here as part of
  622. // sandbox delete. If we came here as part of delete then do
  623. // not bother updating the store. The sandbox object will be
  624. // deleted anyway
  625. if !inDelete {
  626. return sb.storeUpdate()
  627. }
  628. return nil
  629. }
  630. const (
  631. defaultPrefix = "/var/lib/docker/network/files"
  632. dirPerm = 0755
  633. filePerm = 0644
  634. )
  635. func (sb *sandbox) buildHostsFile() error {
  636. if sb.config.hostsPath == "" {
  637. sb.config.hostsPath = defaultPrefix + "/" + sb.id + "/hosts"
  638. }
  639. dir, _ := filepath.Split(sb.config.hostsPath)
  640. if err := createBasePath(dir); err != nil {
  641. return err
  642. }
  643. // This is for the host mode networking
  644. if sb.config.originHostsPath != "" {
  645. if err := copyFile(sb.config.originHostsPath, sb.config.hostsPath); err != nil && !os.IsNotExist(err) {
  646. return types.InternalErrorf("could not copy source hosts file %s to %s: %v", sb.config.originHostsPath, sb.config.hostsPath, err)
  647. }
  648. return nil
  649. }
  650. extraContent := make([]etchosts.Record, 0, len(sb.config.extraHosts))
  651. for _, extraHost := range sb.config.extraHosts {
  652. extraContent = append(extraContent, etchosts.Record{Hosts: extraHost.name, IP: extraHost.IP})
  653. }
  654. return etchosts.Build(sb.config.hostsPath, "", sb.config.hostName, sb.config.domainName, extraContent)
  655. }
  656. func (sb *sandbox) updateHostsFile(ifaceIP string) error {
  657. var mhost string
  658. if sb.config.originHostsPath != "" {
  659. return nil
  660. }
  661. if sb.config.domainName != "" {
  662. mhost = fmt.Sprintf("%s.%s %s", sb.config.hostName, sb.config.domainName,
  663. sb.config.hostName)
  664. } else {
  665. mhost = sb.config.hostName
  666. }
  667. extraContent := []etchosts.Record{{Hosts: mhost, IP: ifaceIP}}
  668. sb.addHostsEntries(extraContent)
  669. return nil
  670. }
  671. func (sb *sandbox) addHostsEntries(recs []etchosts.Record) {
  672. if err := etchosts.Add(sb.config.hostsPath, recs); err != nil {
  673. log.Warnf("Failed adding service host entries to the running container: %v", err)
  674. }
  675. }
  676. func (sb *sandbox) deleteHostsEntries(recs []etchosts.Record) {
  677. if err := etchosts.Delete(sb.config.hostsPath, recs); err != nil {
  678. log.Warnf("Failed deleting service host entries to the running container: %v", err)
  679. }
  680. }
  681. func (sb *sandbox) updateParentHosts() error {
  682. var pSb Sandbox
  683. for _, update := range sb.config.parentUpdates {
  684. sb.controller.WalkSandboxes(SandboxContainerWalker(&pSb, update.cid))
  685. if pSb == nil {
  686. continue
  687. }
  688. if err := etchosts.Update(pSb.(*sandbox).config.hostsPath, update.ip, update.name); err != nil {
  689. return err
  690. }
  691. }
  692. return nil
  693. }
  694. func (sb *sandbox) setupDNS() error {
  695. var newRC *resolvconf.File
  696. if sb.config.resolvConfPath == "" {
  697. sb.config.resolvConfPath = defaultPrefix + "/" + sb.id + "/resolv.conf"
  698. }
  699. sb.config.resolvConfHashFile = sb.config.resolvConfPath + ".hash"
  700. dir, _ := filepath.Split(sb.config.resolvConfPath)
  701. if err := createBasePath(dir); err != nil {
  702. return err
  703. }
  704. // This is for the host mode networking
  705. if sb.config.originResolvConfPath != "" {
  706. if err := copyFile(sb.config.originResolvConfPath, sb.config.resolvConfPath); err != nil {
  707. return fmt.Errorf("could not copy source resolv.conf file %s to %s: %v", sb.config.originResolvConfPath, sb.config.resolvConfPath, err)
  708. }
  709. return nil
  710. }
  711. currRC, err := resolvconf.Get()
  712. if err != nil {
  713. return err
  714. }
  715. if len(sb.config.dnsList) > 0 || len(sb.config.dnsSearchList) > 0 || len(sb.config.dnsOptionsList) > 0 {
  716. var (
  717. err error
  718. dnsList = resolvconf.GetNameservers(currRC.Content, netutils.IP)
  719. dnsSearchList = resolvconf.GetSearchDomains(currRC.Content)
  720. dnsOptionsList = resolvconf.GetOptions(currRC.Content)
  721. )
  722. if len(sb.config.dnsList) > 0 {
  723. dnsList = sb.config.dnsList
  724. }
  725. if len(sb.config.dnsSearchList) > 0 {
  726. dnsSearchList = sb.config.dnsSearchList
  727. }
  728. if len(sb.config.dnsOptionsList) > 0 {
  729. dnsOptionsList = sb.config.dnsOptionsList
  730. }
  731. newRC, err = resolvconf.Build(sb.config.resolvConfPath, dnsList, dnsSearchList, dnsOptionsList)
  732. if err != nil {
  733. return err
  734. }
  735. } else {
  736. // Replace any localhost/127.* (at this point we have no info about ipv6, pass it as true)
  737. if newRC, err = resolvconf.FilterResolvDNS(currRC.Content, true); err != nil {
  738. return err
  739. }
  740. // No contention on container resolv.conf file at sandbox creation
  741. if err := ioutil.WriteFile(sb.config.resolvConfPath, newRC.Content, filePerm); err != nil {
  742. return types.InternalErrorf("failed to write unhaltered resolv.conf file content when setting up dns for sandbox %s: %v", sb.ID(), err)
  743. }
  744. }
  745. // Write hash
  746. if err := ioutil.WriteFile(sb.config.resolvConfHashFile, []byte(newRC.Hash), filePerm); err != nil {
  747. return types.InternalErrorf("failed to write resolv.conf hash file when setting up dns for sandbox %s: %v", sb.ID(), err)
  748. }
  749. return nil
  750. }
  751. func (sb *sandbox) updateDNS(ipv6Enabled bool) error {
  752. var (
  753. currHash string
  754. hashFile = sb.config.resolvConfHashFile
  755. )
  756. // This is for the host mode networking
  757. if sb.config.originResolvConfPath != "" {
  758. return nil
  759. }
  760. if len(sb.config.dnsList) > 0 || len(sb.config.dnsSearchList) > 0 || len(sb.config.dnsOptionsList) > 0 {
  761. return nil
  762. }
  763. currRC, err := resolvconf.GetSpecific(sb.config.resolvConfPath)
  764. if err != nil {
  765. if !os.IsNotExist(err) {
  766. return err
  767. }
  768. } else {
  769. h, err := ioutil.ReadFile(hashFile)
  770. if err != nil {
  771. if !os.IsNotExist(err) {
  772. return err
  773. }
  774. } else {
  775. currHash = string(h)
  776. }
  777. }
  778. if currHash != "" && currHash != currRC.Hash {
  779. // Seems the user has changed the container resolv.conf since the last time
  780. // we checked so return without doing anything.
  781. log.Infof("Skipping update of resolv.conf file with ipv6Enabled: %t because file was touched by user", ipv6Enabled)
  782. return nil
  783. }
  784. // replace any localhost/127.* and remove IPv6 nameservers if IPv6 disabled.
  785. newRC, err := resolvconf.FilterResolvDNS(currRC.Content, ipv6Enabled)
  786. if err != nil {
  787. return err
  788. }
  789. err = ioutil.WriteFile(sb.config.resolvConfPath, newRC.Content, 0644)
  790. if err != nil {
  791. return err
  792. }
  793. // write the new hash in a temp file and rename it to make the update atomic
  794. dir := path.Dir(sb.config.resolvConfPath)
  795. tmpHashFile, err := ioutil.TempFile(dir, "hash")
  796. if err != nil {
  797. return err
  798. }
  799. if err = ioutil.WriteFile(tmpHashFile.Name(), []byte(newRC.Hash), filePerm); err != nil {
  800. return err
  801. }
  802. return os.Rename(tmpHashFile.Name(), hashFile)
  803. }
  804. // Embedded DNS server has to be enabled for this sandbox. Rebuild the container's
  805. // resolv.conf by doing the follwing
  806. // - Save the external name servers in resolv.conf in the sandbox
  807. // - Add only the embedded server's IP to container's resolv.conf
  808. // - If the embedded server needs any resolv.conf options add it to the current list
  809. func (sb *sandbox) rebuildDNS() error {
  810. currRC, err := resolvconf.GetSpecific(sb.config.resolvConfPath)
  811. if err != nil {
  812. return err
  813. }
  814. // localhost entries have already been filtered out from the list
  815. // retain only the v4 servers in sb for forwarding the DNS queries
  816. sb.extDNS = resolvconf.GetNameservers(currRC.Content, netutils.IPv4)
  817. var (
  818. dnsList = []string{sb.resolver.NameServer()}
  819. dnsOptionsList = resolvconf.GetOptions(currRC.Content)
  820. dnsSearchList = resolvconf.GetSearchDomains(currRC.Content)
  821. )
  822. // external v6 DNS servers has to be listed in resolv.conf
  823. dnsList = append(dnsList, resolvconf.GetNameservers(currRC.Content, netutils.IPv6)...)
  824. // Resolver returns the options in the format resolv.conf expects
  825. dnsOptionsList = append(dnsOptionsList, sb.resolver.ResolverOptions()...)
  826. _, err = resolvconf.Build(sb.config.resolvConfPath, dnsList, dnsSearchList, dnsOptionsList)
  827. return err
  828. }
  829. // joinLeaveStart waits to ensure there are no joins or leaves in progress and
  830. // marks this join/leave in progress without race
  831. func (sb *sandbox) joinLeaveStart() {
  832. sb.Lock()
  833. defer sb.Unlock()
  834. for sb.joinLeaveDone != nil {
  835. joinLeaveDone := sb.joinLeaveDone
  836. sb.Unlock()
  837. select {
  838. case <-joinLeaveDone:
  839. }
  840. sb.Lock()
  841. }
  842. sb.joinLeaveDone = make(chan struct{})
  843. }
  844. // joinLeaveEnd marks the end of this join/leave operation and
  845. // signals the same without race to other join and leave waiters
  846. func (sb *sandbox) joinLeaveEnd() {
  847. sb.Lock()
  848. defer sb.Unlock()
  849. if sb.joinLeaveDone != nil {
  850. close(sb.joinLeaveDone)
  851. sb.joinLeaveDone = nil
  852. }
  853. }
  854. // OptionHostname function returns an option setter for hostname option to
  855. // be passed to NewSandbox method.
  856. func OptionHostname(name string) SandboxOption {
  857. return func(sb *sandbox) {
  858. sb.config.hostName = name
  859. }
  860. }
  861. // OptionDomainname function returns an option setter for domainname option to
  862. // be passed to NewSandbox method.
  863. func OptionDomainname(name string) SandboxOption {
  864. return func(sb *sandbox) {
  865. sb.config.domainName = name
  866. }
  867. }
  868. // OptionHostsPath function returns an option setter for hostspath option to
  869. // be passed to NewSandbox method.
  870. func OptionHostsPath(path string) SandboxOption {
  871. return func(sb *sandbox) {
  872. sb.config.hostsPath = path
  873. }
  874. }
  875. // OptionOriginHostsPath function returns an option setter for origin hosts file path
  876. // tbeo passed to NewSandbox method.
  877. func OptionOriginHostsPath(path string) SandboxOption {
  878. return func(sb *sandbox) {
  879. sb.config.originHostsPath = path
  880. }
  881. }
  882. // OptionExtraHost function returns an option setter for extra /etc/hosts options
  883. // which is a name and IP as strings.
  884. func OptionExtraHost(name string, IP string) SandboxOption {
  885. return func(sb *sandbox) {
  886. sb.config.extraHosts = append(sb.config.extraHosts, extraHost{name: name, IP: IP})
  887. }
  888. }
  889. // OptionParentUpdate function returns an option setter for parent container
  890. // which needs to update the IP address for the linked container.
  891. func OptionParentUpdate(cid string, name, ip string) SandboxOption {
  892. return func(sb *sandbox) {
  893. sb.config.parentUpdates = append(sb.config.parentUpdates, parentUpdate{cid: cid, name: name, ip: ip})
  894. }
  895. }
  896. // OptionResolvConfPath function returns an option setter for resolvconfpath option to
  897. // be passed to net container methods.
  898. func OptionResolvConfPath(path string) SandboxOption {
  899. return func(sb *sandbox) {
  900. sb.config.resolvConfPath = path
  901. }
  902. }
  903. // OptionOriginResolvConfPath function returns an option setter to set the path to the
  904. // origin resolv.conf file to be passed to net container methods.
  905. func OptionOriginResolvConfPath(path string) SandboxOption {
  906. return func(sb *sandbox) {
  907. sb.config.originResolvConfPath = path
  908. }
  909. }
  910. // OptionDNS function returns an option setter for dns entry option to
  911. // be passed to container Create method.
  912. func OptionDNS(dns string) SandboxOption {
  913. return func(sb *sandbox) {
  914. sb.config.dnsList = append(sb.config.dnsList, dns)
  915. }
  916. }
  917. // OptionDNSSearch function returns an option setter for dns search entry option to
  918. // be passed to container Create method.
  919. func OptionDNSSearch(search string) SandboxOption {
  920. return func(sb *sandbox) {
  921. sb.config.dnsSearchList = append(sb.config.dnsSearchList, search)
  922. }
  923. }
  924. // OptionDNSOptions function returns an option setter for dns options entry option to
  925. // be passed to container Create method.
  926. func OptionDNSOptions(options string) SandboxOption {
  927. return func(sb *sandbox) {
  928. sb.config.dnsOptionsList = append(sb.config.dnsOptionsList, options)
  929. }
  930. }
  931. // OptionUseDefaultSandbox function returns an option setter for using default sandbox to
  932. // be passed to container Create method.
  933. func OptionUseDefaultSandbox() SandboxOption {
  934. return func(sb *sandbox) {
  935. sb.config.useDefaultSandBox = true
  936. }
  937. }
  938. // OptionUseExternalKey function returns an option setter for using provided namespace
  939. // instead of creating one.
  940. func OptionUseExternalKey() SandboxOption {
  941. return func(sb *sandbox) {
  942. sb.config.useExternalKey = true
  943. }
  944. }
  945. // OptionGeneric function returns an option setter for Generic configuration
  946. // that is not managed by libNetwork but can be used by the Drivers during the call to
  947. // net container creation method. Container Labels are a good example.
  948. func OptionGeneric(generic map[string]interface{}) SandboxOption {
  949. return func(sb *sandbox) {
  950. sb.config.generic = generic
  951. }
  952. }
  953. func (eh epHeap) Len() int { return len(eh) }
  954. func (eh epHeap) Less(i, j int) bool {
  955. var (
  956. cip, cjp int
  957. ok bool
  958. )
  959. ci, _ := eh[i].getSandbox()
  960. cj, _ := eh[j].getSandbox()
  961. epi := eh[i]
  962. epj := eh[j]
  963. if epi.endpointInGWNetwork() {
  964. return false
  965. }
  966. if epj.endpointInGWNetwork() {
  967. return true
  968. }
  969. if ci != nil {
  970. cip, ok = ci.epPriority[eh[i].ID()]
  971. if !ok {
  972. cip = 0
  973. }
  974. }
  975. if cj != nil {
  976. cjp, ok = cj.epPriority[eh[j].ID()]
  977. if !ok {
  978. cjp = 0
  979. }
  980. }
  981. if cip == cjp {
  982. return eh[i].network.Name() < eh[j].network.Name()
  983. }
  984. return cip > cjp
  985. }
  986. func (eh epHeap) Swap(i, j int) { eh[i], eh[j] = eh[j], eh[i] }
  987. func (eh *epHeap) Push(x interface{}) {
  988. *eh = append(*eh, x.(*endpoint))
  989. }
  990. func (eh *epHeap) Pop() interface{} {
  991. old := *eh
  992. n := len(old)
  993. x := old[n-1]
  994. *eh = old[0 : n-1]
  995. return x
  996. }
  997. func createBasePath(dir string) error {
  998. return os.MkdirAll(dir, dirPerm)
  999. }
  1000. func createFile(path string) error {
  1001. var f *os.File
  1002. dir, _ := filepath.Split(path)
  1003. err := createBasePath(dir)
  1004. if err != nil {
  1005. return err
  1006. }
  1007. f, err = os.Create(path)
  1008. if err == nil {
  1009. f.Close()
  1010. }
  1011. return err
  1012. }
  1013. func copyFile(src, dst string) error {
  1014. sBytes, err := ioutil.ReadFile(src)
  1015. if err != nil {
  1016. return err
  1017. }
  1018. return ioutil.WriteFile(dst, sBytes, filePerm)
  1019. }