sandbox_store.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package libnetwork
  2. import (
  3. "encoding/json"
  4. "sync"
  5. "github.com/docker/docker/libnetwork/datastore"
  6. "github.com/docker/docker/libnetwork/osl"
  7. "github.com/sirupsen/logrus"
  8. )
  9. const (
  10. sandboxPrefix = "sandbox"
  11. )
  12. type epState struct {
  13. Eid string
  14. Nid string
  15. }
  16. type sbState struct {
  17. ID string
  18. Cid string
  19. c *Controller
  20. dbIndex uint64
  21. dbExists bool
  22. Eps []epState
  23. EpPriority map[string]int
  24. // external servers have to be persisted so that on restart of a live-restore
  25. // enabled daemon we get the external servers for the running containers.
  26. // We have two versions of ExtDNS to support upgrade & downgrade of the daemon
  27. // between >=1.14 and <1.14 versions.
  28. ExtDNS []string
  29. ExtDNS2 []extDNSEntry
  30. }
  31. func (sbs *sbState) Key() []string {
  32. return []string{sandboxPrefix, sbs.ID}
  33. }
  34. func (sbs *sbState) KeyPrefix() []string {
  35. return []string{sandboxPrefix}
  36. }
  37. func (sbs *sbState) Value() []byte {
  38. b, err := json.Marshal(sbs)
  39. if err != nil {
  40. return nil
  41. }
  42. return b
  43. }
  44. func (sbs *sbState) SetValue(value []byte) error {
  45. return json.Unmarshal(value, sbs)
  46. }
  47. func (sbs *sbState) Index() uint64 {
  48. sb, err := sbs.c.SandboxByID(sbs.ID)
  49. if err != nil {
  50. return sbs.dbIndex
  51. }
  52. maxIndex := sb.dbIndex
  53. if sbs.dbIndex > maxIndex {
  54. maxIndex = sbs.dbIndex
  55. }
  56. return maxIndex
  57. }
  58. func (sbs *sbState) SetIndex(index uint64) {
  59. sbs.dbIndex = index
  60. sbs.dbExists = true
  61. sb, err := sbs.c.SandboxByID(sbs.ID)
  62. if err != nil {
  63. return
  64. }
  65. sb.dbIndex = index
  66. sb.dbExists = true
  67. }
  68. func (sbs *sbState) Exists() bool {
  69. if sbs.dbExists {
  70. return sbs.dbExists
  71. }
  72. sb, err := sbs.c.SandboxByID(sbs.ID)
  73. if err != nil {
  74. return false
  75. }
  76. return sb.dbExists
  77. }
  78. func (sbs *sbState) Skip() bool {
  79. return false
  80. }
  81. func (sbs *sbState) New() datastore.KVObject {
  82. return &sbState{c: sbs.c}
  83. }
  84. func (sbs *sbState) CopyTo(o datastore.KVObject) error {
  85. dstSbs := o.(*sbState)
  86. dstSbs.c = sbs.c
  87. dstSbs.ID = sbs.ID
  88. dstSbs.Cid = sbs.Cid
  89. dstSbs.dbIndex = sbs.dbIndex
  90. dstSbs.dbExists = sbs.dbExists
  91. dstSbs.EpPriority = sbs.EpPriority
  92. dstSbs.Eps = append(dstSbs.Eps, sbs.Eps...)
  93. if len(sbs.ExtDNS2) > 0 {
  94. for _, dns := range sbs.ExtDNS2 {
  95. dstSbs.ExtDNS2 = append(dstSbs.ExtDNS2, dns)
  96. dstSbs.ExtDNS = append(dstSbs.ExtDNS, dns.IPStr)
  97. }
  98. return nil
  99. }
  100. for _, dns := range sbs.ExtDNS {
  101. dstSbs.ExtDNS = append(dstSbs.ExtDNS, dns)
  102. dstSbs.ExtDNS2 = append(dstSbs.ExtDNS2, extDNSEntry{IPStr: dns})
  103. }
  104. return nil
  105. }
  106. func (sbs *sbState) DataScope() string {
  107. return datastore.LocalScope
  108. }
  109. func (sb *Sandbox) storeUpdate() error {
  110. sbs := &sbState{
  111. c: sb.controller,
  112. ID: sb.id,
  113. Cid: sb.containerID,
  114. EpPriority: sb.epPriority,
  115. ExtDNS2: sb.extDNS,
  116. }
  117. for _, ext := range sb.extDNS {
  118. sbs.ExtDNS = append(sbs.ExtDNS, ext.IPStr)
  119. }
  120. retry:
  121. sbs.Eps = nil
  122. for _, ep := range sb.Endpoints() {
  123. // If the endpoint is not persisted then do not add it to
  124. // the sandbox checkpoint
  125. if ep.Skip() {
  126. continue
  127. }
  128. eps := epState{
  129. Nid: ep.getNetwork().ID(),
  130. Eid: ep.ID(),
  131. }
  132. sbs.Eps = append(sbs.Eps, eps)
  133. }
  134. err := sb.controller.updateToStore(sbs)
  135. if err == datastore.ErrKeyModified {
  136. // When we get ErrKeyModified it is sufficient to just
  137. // go back and retry. No need to get the object from
  138. // the store because we always regenerate the store
  139. // state from in memory sandbox state
  140. goto retry
  141. }
  142. return err
  143. }
  144. func (sb *Sandbox) storeDelete() error {
  145. sbs := &sbState{
  146. c: sb.controller,
  147. ID: sb.id,
  148. Cid: sb.containerID,
  149. dbIndex: sb.dbIndex,
  150. dbExists: sb.dbExists,
  151. }
  152. return sb.controller.deleteFromStore(sbs)
  153. }
  154. func (c *Controller) sandboxCleanup(activeSandboxes map[string]interface{}) {
  155. store := c.getStore()
  156. if store == nil {
  157. logrus.Error("Could not find local scope store while trying to cleanup sandboxes")
  158. return
  159. }
  160. kvol, err := store.List(datastore.Key(sandboxPrefix), &sbState{c: c})
  161. if err != nil && err != datastore.ErrKeyNotFound {
  162. logrus.Errorf("failed to get sandboxes for scope %s: %v", store.Scope(), err)
  163. return
  164. }
  165. // It's normal for no sandboxes to be found. Just bail out.
  166. if err == datastore.ErrKeyNotFound {
  167. return
  168. }
  169. for _, kvo := range kvol {
  170. sbs := kvo.(*sbState)
  171. sb := &Sandbox{
  172. id: sbs.ID,
  173. controller: sbs.c,
  174. containerID: sbs.Cid,
  175. endpoints: []*Endpoint{},
  176. populatedEndpoints: map[string]struct{}{},
  177. dbIndex: sbs.dbIndex,
  178. isStub: true,
  179. dbExists: true,
  180. }
  181. // If we are restoring from a older version extDNSEntry won't have the
  182. // HostLoopback field
  183. if len(sbs.ExtDNS2) > 0 {
  184. sb.extDNS = sbs.ExtDNS2
  185. } else {
  186. for _, dns := range sbs.ExtDNS {
  187. sb.extDNS = append(sb.extDNS, extDNSEntry{IPStr: dns})
  188. }
  189. }
  190. msg := " for cleanup"
  191. create := true
  192. isRestore := false
  193. if val, ok := activeSandboxes[sb.ID()]; ok {
  194. msg = ""
  195. sb.isStub = false
  196. isRestore = true
  197. opts := val.([]SandboxOption)
  198. sb.processOptions(opts...)
  199. sb.restorePath()
  200. create = !sb.config.useDefaultSandBox
  201. }
  202. sb.osSbox, err = osl.NewSandbox(sb.Key(), create, isRestore)
  203. if err != nil {
  204. logrus.Errorf("failed to create osl sandbox while trying to restore sandbox %.7s%s: %v", sb.ID(), msg, err)
  205. continue
  206. }
  207. c.mu.Lock()
  208. c.sandboxes[sb.id] = sb
  209. c.mu.Unlock()
  210. for _, eps := range sbs.Eps {
  211. n, err := c.getNetworkFromStore(eps.Nid)
  212. var ep *Endpoint
  213. if err != nil {
  214. logrus.Errorf("getNetworkFromStore for nid %s failed while trying to build sandbox for cleanup: %v", eps.Nid, err)
  215. n = &network{id: eps.Nid, ctrlr: c, drvOnce: &sync.Once{}, persist: true}
  216. ep = &Endpoint{id: eps.Eid, network: n, sandboxID: sbs.ID}
  217. } else {
  218. ep, err = n.getEndpointFromStore(eps.Eid)
  219. if err != nil {
  220. logrus.Errorf("getEndpointFromStore for eid %s failed while trying to build sandbox for cleanup: %v", eps.Eid, err)
  221. ep = &Endpoint{id: eps.Eid, network: n, sandboxID: sbs.ID}
  222. }
  223. }
  224. if _, ok := activeSandboxes[sb.ID()]; ok && err != nil {
  225. logrus.Errorf("failed to restore endpoint %s in %s for container %s due to %v", eps.Eid, eps.Nid, sb.ContainerID(), err)
  226. continue
  227. }
  228. sb.addEndpoint(ep)
  229. }
  230. if _, ok := activeSandboxes[sb.ID()]; !ok {
  231. logrus.Infof("Removing stale sandbox %s (%s)", sb.id, sb.containerID)
  232. if err := sb.delete(true); err != nil {
  233. logrus.Errorf("Failed to delete sandbox %s while trying to cleanup: %v", sb.id, err)
  234. }
  235. continue
  236. }
  237. // reconstruct osl sandbox field
  238. if !sb.config.useDefaultSandBox {
  239. if err := sb.restoreOslSandbox(); err != nil {
  240. logrus.Errorf("failed to populate fields for osl sandbox %s", sb.ID())
  241. continue
  242. }
  243. } else {
  244. c.sboxOnce.Do(func() {
  245. c.defOsSbox = sb.osSbox
  246. })
  247. }
  248. for _, ep := range sb.endpoints {
  249. // Watch for service records
  250. if !c.isAgent() {
  251. c.watchSvcRecord(ep)
  252. }
  253. }
  254. }
  255. }