Now that we have most of the services required for use with containerd, it was found that common patterns were used throughout services. By defining a central `errdefs` package, we ensure that services will map errors to and from grpc consistently and cleanly. One can decorate an error with as much context as necessary, using `pkg/errors` and still have the error mapped correctly via grpc. We make a few sacrifices. At this point, the common errors we use across the repository all map directly to grpc error codes. While this seems positively crazy, it actually works out quite well. The error conditions that were specific weren't super necessary and the ones that were necessary now simply have better context information. We lose the ability to add new codes, but this constraint may not be a bad thing. Effectively, as long as one uses the errors defined in `errdefs`, the error class will be mapped correctly across the grpc boundary and everything will be good. If you don't use those definitions, the error maps to "unknown" and the error message is preserved. Signed-off-by: Stephen J Day <stephen.day@docker.com>
166 lines
3.7 KiB
Go
166 lines
3.7 KiB
Go
package content
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
|
|
contentapi "github.com/containerd/containerd/api/services/content/v1"
|
|
"github.com/containerd/containerd/content"
|
|
"github.com/containerd/containerd/errdefs"
|
|
digest "github.com/opencontainers/go-digest"
|
|
)
|
|
|
|
type remoteStore struct {
|
|
client contentapi.ContentClient
|
|
}
|
|
|
|
func NewStoreFromClient(client contentapi.ContentClient) content.Store {
|
|
return &remoteStore{
|
|
client: client,
|
|
}
|
|
}
|
|
|
|
func (rs *remoteStore) Info(ctx context.Context, dgst digest.Digest) (content.Info, error) {
|
|
resp, err := rs.client.Info(ctx, &contentapi.InfoRequest{
|
|
Digest: dgst,
|
|
})
|
|
if err != nil {
|
|
return content.Info{}, errdefs.FromGRPC(err)
|
|
}
|
|
|
|
return content.Info{
|
|
Digest: resp.Info.Digest,
|
|
Size: resp.Info.Size_,
|
|
CommittedAt: resp.Info.CommittedAt,
|
|
}, nil
|
|
}
|
|
|
|
func (rs *remoteStore) Walk(ctx context.Context, fn content.WalkFunc) error {
|
|
session, err := rs.client.List(ctx, &contentapi.ListContentRequest{})
|
|
if err != nil {
|
|
return errdefs.FromGRPC(err)
|
|
}
|
|
|
|
for {
|
|
msg, err := session.Recv()
|
|
if err != nil {
|
|
if err != io.EOF {
|
|
return errdefs.FromGRPC(err)
|
|
}
|
|
|
|
break
|
|
}
|
|
|
|
for _, info := range msg.Info {
|
|
if err := fn(content.Info{
|
|
Digest: info.Digest,
|
|
Size: info.Size_,
|
|
CommittedAt: info.CommittedAt,
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (rs *remoteStore) Delete(ctx context.Context, dgst digest.Digest) error {
|
|
if _, err := rs.client.Delete(ctx, &contentapi.DeleteContentRequest{
|
|
Digest: dgst,
|
|
}); err != nil {
|
|
return errdefs.FromGRPC(err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (rs *remoteStore) Reader(ctx context.Context, dgst digest.Digest) (io.ReadCloser, error) {
|
|
client, err := rs.client.Read(ctx, &contentapi.ReadContentRequest{Digest: dgst})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &remoteReader{
|
|
client: client,
|
|
}, nil
|
|
}
|
|
|
|
func (rs *remoteStore) ReaderAt(ctx context.Context, dgst digest.Digest) (io.ReaderAt, error) {
|
|
return &remoteReaderAt{
|
|
ctx: ctx,
|
|
digest: dgst,
|
|
client: rs.client,
|
|
}, nil
|
|
}
|
|
|
|
func (rs *remoteStore) Status(ctx context.Context, filter string) ([]content.Status, error) {
|
|
resp, err := rs.client.Status(ctx, &contentapi.StatusRequest{
|
|
Filter: filter,
|
|
})
|
|
if err != nil {
|
|
return nil, errdefs.FromGRPC(err)
|
|
}
|
|
|
|
var statuses []content.Status
|
|
for _, status := range resp.Statuses {
|
|
statuses = append(statuses, content.Status{
|
|
Ref: status.Ref,
|
|
StartedAt: status.StartedAt,
|
|
UpdatedAt: status.UpdatedAt,
|
|
Offset: status.Offset,
|
|
Total: status.Total,
|
|
Expected: status.Expected,
|
|
})
|
|
}
|
|
|
|
return statuses, nil
|
|
}
|
|
|
|
func (rs *remoteStore) Writer(ctx context.Context, ref string, size int64, expected digest.Digest) (content.Writer, error) {
|
|
wrclient, offset, err := rs.negotiate(ctx, ref, size, expected)
|
|
if err != nil {
|
|
return nil, errdefs.FromGRPC(err)
|
|
}
|
|
|
|
return &remoteWriter{
|
|
ref: ref,
|
|
client: wrclient,
|
|
offset: offset,
|
|
}, nil
|
|
}
|
|
|
|
// Abort implements asynchronous abort. It starts a new write session on the ref l
|
|
func (rs *remoteStore) Abort(ctx context.Context, ref string) error {
|
|
if _, err := rs.client.Abort(ctx, &contentapi.AbortRequest{
|
|
Ref: ref,
|
|
}); err != nil {
|
|
return errdefs.FromGRPC(err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (rs *remoteStore) negotiate(ctx context.Context, ref string, size int64, expected digest.Digest) (contentapi.Content_WriteClient, int64, error) {
|
|
wrclient, err := rs.client.Write(ctx)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
if err := wrclient.Send(&contentapi.WriteContentRequest{
|
|
Action: contentapi.WriteActionStat,
|
|
Ref: ref,
|
|
Total: size,
|
|
Expected: expected,
|
|
}); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
resp, err := wrclient.Recv()
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return wrclient, resp.Offset, nil
|
|
}
|