2022-07-08 13:28:14 +00:00
|
|
|
package containerd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/containerd/containerd/content"
|
|
|
|
cerrdefs "github.com/containerd/containerd/errdefs"
|
2023-10-03 12:06:00 +00:00
|
|
|
"github.com/containerd/containerd/images"
|
2022-07-08 13:28:14 +00:00
|
|
|
"github.com/containerd/containerd/remotes"
|
2022-07-08 13:12:35 +00:00
|
|
|
"github.com/containerd/containerd/remotes/docker"
|
2023-09-13 15:41:45 +00:00
|
|
|
"github.com/containerd/log"
|
2023-10-03 12:06:00 +00:00
|
|
|
"github.com/distribution/reference"
|
2023-10-04 11:17:51 +00:00
|
|
|
"github.com/docker/docker/internal/compatcontext"
|
2022-07-08 13:28:14 +00:00
|
|
|
"github.com/docker/docker/pkg/progress"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
"github.com/opencontainers/go-digest"
|
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
type progressUpdater interface {
|
|
|
|
UpdateProgress(context.Context, *jobs, progress.Output, time.Time) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type jobs struct {
|
|
|
|
descs map[digest.Digest]ocispec.Descriptor
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// newJobs creates a new instance of the job status tracker
|
|
|
|
func newJobs() *jobs {
|
|
|
|
return &jobs{
|
|
|
|
descs: map[digest.Digest]ocispec.Descriptor{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *jobs) showProgress(ctx context.Context, out progress.Output, updater progressUpdater) func() {
|
|
|
|
ctx, cancelProgress := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
start := time.Now()
|
2023-01-17 14:14:51 +00:00
|
|
|
lastUpdate := make(chan struct{})
|
2022-07-08 13:28:14 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
ticker := time.NewTicker(100 * time.Millisecond)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
if err := updater.UpdateProgress(ctx, j, out, start); err != nil {
|
|
|
|
if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(ctx).WithError(err).Error("Updating progress failed")
|
2022-07-08 13:28:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
2023-10-04 11:17:51 +00:00
|
|
|
ctx, cancel := context.WithTimeout(compatcontext.WithoutCancel(ctx), time.Millisecond*500)
|
2023-01-17 14:14:51 +00:00
|
|
|
defer cancel()
|
|
|
|
updater.UpdateProgress(ctx, j, out, start)
|
|
|
|
close(lastUpdate)
|
2022-07-08 13:28:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-01-17 14:14:51 +00:00
|
|
|
return func() {
|
|
|
|
cancelProgress()
|
|
|
|
// Wait for the last update to finish.
|
|
|
|
// UpdateProgress may still write progress to output and we need
|
|
|
|
// to keep the caller from closing it before we finish.
|
|
|
|
<-lastUpdate
|
|
|
|
}
|
2022-07-08 13:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds a descriptor to be tracked
|
2023-03-30 22:21:24 +00:00
|
|
|
func (j *jobs) Add(desc ...ocispec.Descriptor) {
|
2022-07-08 13:28:14 +00:00
|
|
|
j.mu.Lock()
|
|
|
|
defer j.mu.Unlock()
|
|
|
|
|
2023-03-30 22:21:24 +00:00
|
|
|
for _, d := range desc {
|
|
|
|
if _, ok := j.descs[d.Digest]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
j.descs[d.Digest] = d
|
2022-07-08 13:28:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes a descriptor
|
|
|
|
func (j *jobs) Remove(desc ocispec.Descriptor) {
|
|
|
|
j.mu.Lock()
|
|
|
|
defer j.mu.Unlock()
|
|
|
|
|
|
|
|
delete(j.descs, desc.Digest)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Jobs returns a list of all tracked descriptors
|
|
|
|
func (j *jobs) Jobs() []ocispec.Descriptor {
|
|
|
|
j.mu.Lock()
|
|
|
|
defer j.mu.Unlock()
|
|
|
|
|
|
|
|
descs := make([]ocispec.Descriptor, 0, len(j.descs))
|
|
|
|
for _, d := range j.descs {
|
|
|
|
descs = append(descs, d)
|
|
|
|
}
|
|
|
|
return descs
|
|
|
|
}
|
|
|
|
|
|
|
|
type pullProgress struct {
|
2023-09-06 10:06:32 +00:00
|
|
|
store content.Store
|
|
|
|
showExists bool
|
|
|
|
hideLayers bool
|
2022-07-08 13:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p pullProgress) UpdateProgress(ctx context.Context, ongoing *jobs, out progress.Output, start time.Time) error {
|
2023-09-06 10:06:32 +00:00
|
|
|
actives, err := p.store.ListStatuses(ctx, "")
|
2022-07-08 13:28:14 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(ctx).WithError(err).Error("status check failed")
|
2022-07-08 13:28:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
pulling := make(map[string]content.Status, len(actives))
|
|
|
|
|
|
|
|
// update status of status entries!
|
|
|
|
for _, status := range actives {
|
|
|
|
pulling[status.Ref] = status
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, j := range ongoing.Jobs() {
|
2023-09-06 10:06:32 +00:00
|
|
|
if p.hideLayers {
|
|
|
|
ongoing.Remove(j)
|
|
|
|
continue
|
|
|
|
}
|
2022-07-08 13:28:14 +00:00
|
|
|
key := remotes.MakeRefKey(ctx, j)
|
|
|
|
if info, ok := pulling[key]; ok {
|
2024-02-22 17:02:22 +00:00
|
|
|
if info.Offset == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2022-07-08 13:28:14 +00:00
|
|
|
out.WriteProgress(progress.Progress{
|
|
|
|
ID: stringid.TruncateID(j.Digest.Encoded()),
|
|
|
|
Action: "Downloading",
|
|
|
|
Current: info.Offset,
|
|
|
|
Total: info.Total,
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:06:32 +00:00
|
|
|
info, err := p.store.Info(ctx, j.Digest)
|
2022-07-08 13:28:14 +00:00
|
|
|
if err != nil {
|
|
|
|
if !cerrdefs.IsNotFound(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if info.CreatedAt.After(start) {
|
|
|
|
out.WriteProgress(progress.Progress{
|
|
|
|
ID: stringid.TruncateID(j.Digest.Encoded()),
|
|
|
|
Action: "Download complete",
|
|
|
|
HideCounts: true,
|
|
|
|
LastUpdate: true,
|
|
|
|
})
|
|
|
|
ongoing.Remove(j)
|
2023-09-06 10:06:32 +00:00
|
|
|
} else if p.showExists {
|
2022-07-08 13:28:14 +00:00
|
|
|
out.WriteProgress(progress.Progress{
|
|
|
|
ID: stringid.TruncateID(j.Digest.Encoded()),
|
2023-05-24 09:16:13 +00:00
|
|
|
Action: "Already exists",
|
2022-07-08 13:28:14 +00:00
|
|
|
HideCounts: true,
|
|
|
|
LastUpdate: true,
|
|
|
|
})
|
|
|
|
ongoing.Remove(j)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-07-08 13:12:35 +00:00
|
|
|
|
|
|
|
type pushProgress struct {
|
2023-10-11 09:46:33 +00:00
|
|
|
Tracker docker.StatusTracker
|
2022-07-08 13:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pushProgress) UpdateProgress(ctx context.Context, ongoing *jobs, out progress.Output, start time.Time) error {
|
|
|
|
for _, j := range ongoing.Jobs() {
|
|
|
|
key := remotes.MakeRefKey(ctx, j)
|
|
|
|
id := stringid.TruncateID(j.Digest.Encoded())
|
|
|
|
|
|
|
|
status, err := p.Tracker.GetStatus(key)
|
|
|
|
if err != nil {
|
|
|
|
if cerrdefs.IsNotFound(err) {
|
|
|
|
progress.Update(out, id, "Waiting")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if status.Committed && status.Offset >= status.Total {
|
2023-10-03 12:06:00 +00:00
|
|
|
if status.MountedFrom != "" {
|
|
|
|
from := status.MountedFrom
|
|
|
|
if ref, err := reference.ParseNormalizedNamed(from); err == nil {
|
|
|
|
from = reference.Path(ref)
|
|
|
|
}
|
|
|
|
progress.Update(out, id, "Mounted from "+from)
|
|
|
|
} else if status.Exists {
|
|
|
|
if images.IsLayerType(j.MediaType) {
|
|
|
|
progress.Update(out, id, "Layer already exists")
|
|
|
|
} else {
|
|
|
|
progress.Update(out, id, "Already exists")
|
|
|
|
}
|
2022-07-08 13:12:35 +00:00
|
|
|
} else {
|
|
|
|
progress.Update(out, id, "Pushed")
|
|
|
|
}
|
|
|
|
ongoing.Remove(j)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
out.WriteProgress(progress.Progress{
|
|
|
|
ID: id,
|
|
|
|
Action: "Pushing",
|
|
|
|
Current: status.Offset,
|
|
|
|
Total: status.Total,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type combinedProgress []progressUpdater
|
|
|
|
|
|
|
|
func (combined combinedProgress) UpdateProgress(ctx context.Context, ongoing *jobs, out progress.Output, start time.Time) error {
|
|
|
|
for _, p := range combined {
|
|
|
|
err := p.UpdateProgress(ctx, ongoing, out, start)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|