progress.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package containerd
  2. import (
  3. "context"
  4. "errors"
  5. "sync"
  6. "time"
  7. "github.com/containerd/containerd/content"
  8. cerrdefs "github.com/containerd/containerd/errdefs"
  9. "github.com/containerd/containerd/log"
  10. "github.com/containerd/containerd/remotes"
  11. "github.com/containerd/containerd/remotes/docker"
  12. "github.com/docker/docker/pkg/progress"
  13. "github.com/docker/docker/pkg/stringid"
  14. "github.com/opencontainers/go-digest"
  15. ocispec "github.com/opencontainers/image-spec/specs-go/v1"
  16. )
  17. type progressUpdater interface {
  18. UpdateProgress(context.Context, *jobs, progress.Output, time.Time) error
  19. }
  20. type jobs struct {
  21. descs map[digest.Digest]ocispec.Descriptor
  22. mu sync.Mutex
  23. }
  24. // newJobs creates a new instance of the job status tracker
  25. func newJobs() *jobs {
  26. return &jobs{
  27. descs: map[digest.Digest]ocispec.Descriptor{},
  28. }
  29. }
  30. func (j *jobs) showProgress(ctx context.Context, out progress.Output, updater progressUpdater) func() {
  31. ctx, cancelProgress := context.WithCancel(ctx)
  32. start := time.Now()
  33. lastUpdate := make(chan struct{})
  34. go func() {
  35. ticker := time.NewTicker(100 * time.Millisecond)
  36. defer ticker.Stop()
  37. for {
  38. select {
  39. case <-ticker.C:
  40. if err := updater.UpdateProgress(ctx, j, out, start); err != nil {
  41. if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
  42. log.G(ctx).WithError(err).Error("Updating progress failed")
  43. }
  44. }
  45. case <-ctx.Done():
  46. ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*500)
  47. defer cancel()
  48. updater.UpdateProgress(ctx, j, out, start)
  49. close(lastUpdate)
  50. return
  51. }
  52. }
  53. }()
  54. return func() {
  55. cancelProgress()
  56. // Wait for the last update to finish.
  57. // UpdateProgress may still write progress to output and we need
  58. // to keep the caller from closing it before we finish.
  59. <-lastUpdate
  60. }
  61. }
  62. // Add adds a descriptor to be tracked
  63. func (j *jobs) Add(desc ...ocispec.Descriptor) {
  64. j.mu.Lock()
  65. defer j.mu.Unlock()
  66. for _, d := range desc {
  67. if _, ok := j.descs[d.Digest]; ok {
  68. continue
  69. }
  70. j.descs[d.Digest] = d
  71. }
  72. }
  73. // Remove removes a descriptor
  74. func (j *jobs) Remove(desc ocispec.Descriptor) {
  75. j.mu.Lock()
  76. defer j.mu.Unlock()
  77. delete(j.descs, desc.Digest)
  78. }
  79. // Jobs returns a list of all tracked descriptors
  80. func (j *jobs) Jobs() []ocispec.Descriptor {
  81. j.mu.Lock()
  82. defer j.mu.Unlock()
  83. descs := make([]ocispec.Descriptor, 0, len(j.descs))
  84. for _, d := range j.descs {
  85. descs = append(descs, d)
  86. }
  87. return descs
  88. }
  89. type pullProgress struct {
  90. Store content.Store
  91. ShowExists bool
  92. }
  93. func (p pullProgress) UpdateProgress(ctx context.Context, ongoing *jobs, out progress.Output, start time.Time) error {
  94. actives, err := p.Store.ListStatuses(ctx, "")
  95. if err != nil {
  96. if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
  97. return err
  98. }
  99. log.G(ctx).WithError(err).Error("status check failed")
  100. return nil
  101. }
  102. pulling := make(map[string]content.Status, len(actives))
  103. // update status of status entries!
  104. for _, status := range actives {
  105. pulling[status.Ref] = status
  106. }
  107. for _, j := range ongoing.Jobs() {
  108. key := remotes.MakeRefKey(ctx, j)
  109. if info, ok := pulling[key]; ok {
  110. out.WriteProgress(progress.Progress{
  111. ID: stringid.TruncateID(j.Digest.Encoded()),
  112. Action: "Downloading",
  113. Current: info.Offset,
  114. Total: info.Total,
  115. })
  116. continue
  117. }
  118. info, err := p.Store.Info(ctx, j.Digest)
  119. if err != nil {
  120. if !cerrdefs.IsNotFound(err) {
  121. return err
  122. }
  123. } else if info.CreatedAt.After(start) {
  124. out.WriteProgress(progress.Progress{
  125. ID: stringid.TruncateID(j.Digest.Encoded()),
  126. Action: "Download complete",
  127. HideCounts: true,
  128. LastUpdate: true,
  129. })
  130. ongoing.Remove(j)
  131. } else if p.ShowExists {
  132. out.WriteProgress(progress.Progress{
  133. ID: stringid.TruncateID(j.Digest.Encoded()),
  134. Action: "Already exists",
  135. HideCounts: true,
  136. LastUpdate: true,
  137. })
  138. ongoing.Remove(j)
  139. }
  140. }
  141. return nil
  142. }
  143. type pushProgress struct {
  144. Tracker docker.StatusTracker
  145. mountable map[digest.Digest]struct{}
  146. mutex sync.Mutex
  147. }
  148. func (p *pushProgress) addMountable(dgst digest.Digest) {
  149. p.mutex.Lock()
  150. defer p.mutex.Unlock()
  151. if p.mountable == nil {
  152. p.mountable = map[digest.Digest]struct{}{}
  153. }
  154. p.mountable[dgst] = struct{}{}
  155. }
  156. func (p *pushProgress) isMountable(dgst digest.Digest) bool {
  157. p.mutex.Lock()
  158. defer p.mutex.Unlock()
  159. if p.mountable == nil {
  160. return false
  161. }
  162. _, has := p.mountable[dgst]
  163. return has
  164. }
  165. func (p *pushProgress) UpdateProgress(ctx context.Context, ongoing *jobs, out progress.Output, start time.Time) error {
  166. for _, j := range ongoing.Jobs() {
  167. key := remotes.MakeRefKey(ctx, j)
  168. id := stringid.TruncateID(j.Digest.Encoded())
  169. status, err := p.Tracker.GetStatus(key)
  170. if err != nil {
  171. if cerrdefs.IsNotFound(err) {
  172. progress.Update(out, id, "Waiting")
  173. continue
  174. }
  175. }
  176. if status.Committed && status.Offset >= status.Total {
  177. if p.isMountable(j.Digest) {
  178. progress.Update(out, id, "Mounted")
  179. } else {
  180. progress.Update(out, id, "Pushed")
  181. }
  182. ongoing.Remove(j)
  183. continue
  184. }
  185. out.WriteProgress(progress.Progress{
  186. ID: id,
  187. Action: "Pushing",
  188. Current: status.Offset,
  189. Total: status.Total,
  190. })
  191. }
  192. return nil
  193. }
  194. type combinedProgress []progressUpdater
  195. func (combined combinedProgress) UpdateProgress(ctx context.Context, ongoing *jobs, out progress.Output, start time.Time) error {
  196. for _, p := range combined {
  197. err := p.UpdateProgress(ctx, ongoing, out, start)
  198. if err != nil {
  199. return err
  200. }
  201. }
  202. return nil
  203. }