-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
produce progress events polling ctrd's content.Store #12
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package containerd | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"sync" | ||
"time" | ||
|
||
"github.com/containerd/containerd/content" | ||
"github.com/containerd/containerd/errdefs" | ||
"github.com/containerd/containerd/log" | ||
"github.com/containerd/containerd/remotes" | ||
"github.com/docker/docker/pkg/progress" | ||
"github.com/docker/docker/pkg/streamformatter" | ||
"github.com/docker/docker/pkg/stringid" | ||
"github.com/opencontainers/go-digest" | ||
"github.com/opencontainers/image-spec/specs-go/v1" | ||
) | ||
|
||
func showProgress(ctx context.Context, ongoing *jobs, cs content.Store, w io.Writer, stop chan struct{}) { | ||
var ( | ||
out = streamformatter.NewJSONProgressOutput(w, false) | ||
ticker = time.NewTicker(100 * time.Millisecond) | ||
start = time.Now() | ||
done bool | ||
) | ||
defer ticker.Stop() | ||
|
||
outer: | ||
for { | ||
select { | ||
case <-ticker.C: | ||
if !ongoing.IsResolved() { | ||
continue | ||
} | ||
|
||
pulling := map[string]content.Status{} | ||
if !done { | ||
actives, err := cs.ListStatuses(ctx, "") | ||
if err != nil { | ||
log.G(ctx).WithError(err).Error("status check failed") | ||
continue | ||
} | ||
// update status of status entries! | ||
for _, status := range actives { | ||
pulling[status.Ref] = status | ||
} | ||
} | ||
|
||
// update inactive jobs | ||
for _, j := range ongoing.Jobs() { | ||
key := remotes.MakeRefKey(ctx, j) | ||
if info, ok := pulling[key]; ok { | ||
out.WriteProgress(progress.Progress{ | ||
ID: stringid.TruncateID(j.Digest.Encoded()), | ||
Action: "Downloading", | ||
Current: info.Offset, | ||
Total: info.Total, | ||
}) | ||
continue | ||
} | ||
|
||
info, err := cs.Info(ctx, j.Digest) | ||
if err != nil { | ||
if !errdefs.IsNotFound(err) { | ||
log.G(ctx).WithError(err).Error("failed to get content info") | ||
continue outer | ||
} | ||
} 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) | ||
} else { | ||
out.WriteProgress(progress.Progress{ | ||
ID: stringid.TruncateID(j.Digest.Encoded()), | ||
Action: "Exists", | ||
HideCounts: true, | ||
LastUpdate: true, | ||
}) | ||
ongoing.Remove(j) | ||
} | ||
} | ||
if done { | ||
return | ||
} | ||
case <-stop: | ||
done = true // allow ui to update once more | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
} | ||
|
||
// jobs holds a list of layers being downloaded to pull reference set by name | ||
type jobs struct { | ||
name string | ||
resolved bool // resolved is set to true once remote image metadata has been downloaded from registry | ||
descs map[digest.Digest]v1.Descriptor | ||
mu sync.Mutex | ||
} | ||
|
||
// newJobs creates a new instance of the job status tracker | ||
func newJobs() *jobs { | ||
return &jobs{ | ||
descs: map[digest.Digest]v1.Descriptor{}, | ||
} | ||
} | ||
|
||
// IsResolved checks whether a descriptor has been resolved | ||
func (j *jobs) IsResolved() bool { | ||
j.mu.Lock() | ||
defer j.mu.Unlock() | ||
return j.resolved | ||
} | ||
|
||
// Add adds a descriptor to be tracked | ||
func (j *jobs) Add(desc v1.Descriptor) { | ||
j.mu.Lock() | ||
defer j.mu.Unlock() | ||
|
||
if _, ok := j.descs[desc.Digest]; ok { | ||
return | ||
} | ||
j.descs[desc.Digest] = desc | ||
j.resolved = true | ||
} | ||
|
||
// Remove removes a descriptor | ||
func (j *jobs) Remove(desc v1.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() []v1.Descriptor { | ||
j.mu.Lock() | ||
defer j.mu.Unlock() | ||
|
||
descs := make([]v1.Descriptor, 0, len(j.descs)) | ||
for _, d := range j.descs { | ||
descs = append(descs, d) | ||
} | ||
return descs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heads up: this code is removed in a later PR, so (perhaps?) can be squashed