mount: Add From/ToProto helpers
Helpers to convert from containerd's [Mount] to its protobuf structure for [Mount] and vice-versa appear three times. It seems sane to just expose this facility in /mount. Signed-off-by: Danny Canter <danny@dcantah.dev>
This commit is contained in:
parent
9b4ed8acc2
commit
55a8102ec1
@ -50,7 +50,7 @@ func (s *service) Apply(ctx context.Context, er *diffapi.ApplyRequest) (*diffapi
|
|||||||
ocidesc ocispec.Descriptor
|
ocidesc ocispec.Descriptor
|
||||||
err error
|
err error
|
||||||
desc = toDescriptor(er.Diff)
|
desc = toDescriptor(er.Diff)
|
||||||
mounts = toMounts(er.Mounts)
|
mounts = mount.FromProto(er.Mounts)
|
||||||
)
|
)
|
||||||
|
|
||||||
var opts []diff.ApplyOpt
|
var opts []diff.ApplyOpt
|
||||||
@ -79,8 +79,8 @@ func (s *service) Diff(ctx context.Context, dr *diffapi.DiffRequest) (*diffapi.D
|
|||||||
var (
|
var (
|
||||||
ocidesc ocispec.Descriptor
|
ocidesc ocispec.Descriptor
|
||||||
err error
|
err error
|
||||||
aMounts = toMounts(dr.Left)
|
aMounts = mount.FromProto(dr.Left)
|
||||||
bMounts = toMounts(dr.Right)
|
bMounts = mount.FromProto(dr.Right)
|
||||||
)
|
)
|
||||||
|
|
||||||
var opts []diff.Opt
|
var opts []diff.Opt
|
||||||
@ -108,19 +108,6 @@ func (s *service) Diff(ctx context.Context, dr *diffapi.DiffRequest) (*diffapi.D
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func toMounts(apim []*types.Mount) []mount.Mount {
|
|
||||||
mounts := make([]mount.Mount, len(apim))
|
|
||||||
for i, m := range apim {
|
|
||||||
mounts[i] = mount.Mount{
|
|
||||||
Type: m.Type,
|
|
||||||
Source: m.Source,
|
|
||||||
Target: m.Target,
|
|
||||||
Options: m.Options,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return mounts
|
|
||||||
}
|
|
||||||
|
|
||||||
func toDescriptor(d *types.Descriptor) ocispec.Descriptor {
|
func toDescriptor(d *types.Descriptor) ocispec.Descriptor {
|
||||||
return ocispec.Descriptor{
|
return ocispec.Descriptor{
|
||||||
MediaType: d.MediaType,
|
MediaType: d.MediaType,
|
||||||
|
@ -20,7 +20,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
|
snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
|
||||||
"github.com/containerd/containerd/api/types"
|
|
||||||
"github.com/containerd/containerd/errdefs"
|
"github.com/containerd/containerd/errdefs"
|
||||||
"github.com/containerd/containerd/mount"
|
"github.com/containerd/containerd/mount"
|
||||||
"github.com/containerd/containerd/protobuf"
|
"github.com/containerd/containerd/protobuf"
|
||||||
@ -51,7 +50,7 @@ func (s service) Prepare(ctx context.Context, pr *snapshotsapi.PrepareSnapshotRe
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &snapshotsapi.PrepareSnapshotResponse{
|
return &snapshotsapi.PrepareSnapshotResponse{
|
||||||
Mounts: fromMounts(mounts),
|
Mounts: mount.ToProto(mounts),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,7 +64,7 @@ func (s service) View(ctx context.Context, pr *snapshotsapi.ViewSnapshotRequest)
|
|||||||
return nil, errdefs.ToGRPC(err)
|
return nil, errdefs.ToGRPC(err)
|
||||||
}
|
}
|
||||||
return &snapshotsapi.ViewSnapshotResponse{
|
return &snapshotsapi.ViewSnapshotResponse{
|
||||||
Mounts: fromMounts(mounts),
|
Mounts: mount.ToProto(mounts),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,7 +74,7 @@ func (s service) Mounts(ctx context.Context, mr *snapshotsapi.MountsRequest) (*s
|
|||||||
return nil, errdefs.ToGRPC(err)
|
return nil, errdefs.ToGRPC(err)
|
||||||
}
|
}
|
||||||
return &snapshotsapi.MountsResponse{
|
return &snapshotsapi.MountsResponse{
|
||||||
Mounts: fromMounts(mounts),
|
Mounts: mount.ToProto(mounts),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -199,19 +198,6 @@ func fromInfo(info snapshots.Info) *snapshotsapi.Info {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func fromMounts(mounts []mount.Mount) []*types.Mount {
|
|
||||||
out := make([]*types.Mount, len(mounts))
|
|
||||||
for i, m := range mounts {
|
|
||||||
out[i] = &types.Mount{
|
|
||||||
Type: m.Type,
|
|
||||||
Source: m.Source,
|
|
||||||
Target: m.Target,
|
|
||||||
Options: m.Options,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
func toInfo(info *snapshotsapi.Info) snapshots.Info {
|
func toInfo(info *snapshotsapi.Info) snapshots.Info {
|
||||||
return snapshots.Info{
|
return snapshots.Info{
|
||||||
Name: info.Name,
|
Name: info.Name,
|
||||||
|
@ -60,7 +60,7 @@ func (r *diffRemote) Apply(ctx context.Context, desc ocispec.Descriptor, mounts
|
|||||||
|
|
||||||
req := &diffapi.ApplyRequest{
|
req := &diffapi.ApplyRequest{
|
||||||
Diff: fromDescriptor(desc),
|
Diff: fromDescriptor(desc),
|
||||||
Mounts: fromMounts(mounts),
|
Mounts: mount.ToProto(mounts),
|
||||||
Payloads: payloads,
|
Payloads: payloads,
|
||||||
}
|
}
|
||||||
resp, err := r.client.Apply(ctx, req)
|
resp, err := r.client.Apply(ctx, req)
|
||||||
@ -85,8 +85,8 @@ func (r *diffRemote) Compare(ctx context.Context, a, b []mount.Mount, opts ...di
|
|||||||
sourceDateEpoch = timestamppb.New(*config.SourceDateEpoch)
|
sourceDateEpoch = timestamppb.New(*config.SourceDateEpoch)
|
||||||
}
|
}
|
||||||
req := &diffapi.DiffRequest{
|
req := &diffapi.DiffRequest{
|
||||||
Left: fromMounts(a),
|
Left: mount.ToProto(a),
|
||||||
Right: fromMounts(b),
|
Right: mount.ToProto(b),
|
||||||
MediaType: config.MediaType,
|
MediaType: config.MediaType,
|
||||||
Ref: config.Reference,
|
Ref: config.Reference,
|
||||||
Labels: config.Labels,
|
Labels: config.Labels,
|
||||||
@ -119,16 +119,3 @@ func fromDescriptor(d ocispec.Descriptor) *types.Descriptor {
|
|||||||
Annotations: d.Annotations,
|
Annotations: d.Annotations,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func fromMounts(mounts []mount.Mount) []*types.Mount {
|
|
||||||
apiMounts := make([]*types.Mount, len(mounts))
|
|
||||||
for i, m := range mounts {
|
|
||||||
apiMounts[i] = &types.Mount{
|
|
||||||
Type: m.Type,
|
|
||||||
Source: m.Source,
|
|
||||||
Target: m.Target,
|
|
||||||
Options: m.Options,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return apiMounts
|
|
||||||
}
|
|
||||||
|
@ -20,6 +20,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/containerd/containerd/api/types"
|
||||||
"github.com/containerd/continuity/fs"
|
"github.com/containerd/continuity/fs"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -130,3 +131,33 @@ func readonlyOverlay(opt []string) []string {
|
|||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToProto converts from [Mount] to the containerd
|
||||||
|
// APIs protobuf definition of a Mount.
|
||||||
|
func ToProto(mounts []Mount) []*types.Mount {
|
||||||
|
apiMounts := make([]*types.Mount, len(mounts))
|
||||||
|
for i, m := range mounts {
|
||||||
|
apiMounts[i] = &types.Mount{
|
||||||
|
Type: m.Type,
|
||||||
|
Source: m.Source,
|
||||||
|
Target: m.Target,
|
||||||
|
Options: m.Options,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return apiMounts
|
||||||
|
}
|
||||||
|
|
||||||
|
// FromProto converts from the protobuf definition [types.Mount] to
|
||||||
|
// [Mount].
|
||||||
|
func FromProto(mm []*types.Mount) []Mount {
|
||||||
|
mounts := make([]Mount, len(mm))
|
||||||
|
for i, m := range mm {
|
||||||
|
mounts[i] = Mount{
|
||||||
|
Type: m.Type,
|
||||||
|
Source: m.Source,
|
||||||
|
Target: m.Target,
|
||||||
|
Options: m.Options,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mounts
|
||||||
|
}
|
||||||
|
@ -98,7 +98,7 @@ func (l *local) Apply(ctx context.Context, er *diffapi.ApplyRequest, _ ...grpc.C
|
|||||||
ocidesc ocispec.Descriptor
|
ocidesc ocispec.Descriptor
|
||||||
err error
|
err error
|
||||||
desc = toDescriptor(er.Diff)
|
desc = toDescriptor(er.Diff)
|
||||||
mounts = toMounts(er.Mounts)
|
mounts = mount.FromProto(er.Mounts)
|
||||||
)
|
)
|
||||||
|
|
||||||
var opts []diff.ApplyOpt
|
var opts []diff.ApplyOpt
|
||||||
@ -131,8 +131,8 @@ func (l *local) Diff(ctx context.Context, dr *diffapi.DiffRequest, _ ...grpc.Cal
|
|||||||
var (
|
var (
|
||||||
ocidesc ocispec.Descriptor
|
ocidesc ocispec.Descriptor
|
||||||
err error
|
err error
|
||||||
aMounts = toMounts(dr.Left)
|
aMounts = mount.FromProto(dr.Left)
|
||||||
bMounts = toMounts(dr.Right)
|
bMounts = mount.FromProto(dr.Right)
|
||||||
)
|
)
|
||||||
|
|
||||||
var opts []diff.Opt
|
var opts []diff.Opt
|
||||||
@ -165,19 +165,6 @@ func (l *local) Diff(ctx context.Context, dr *diffapi.DiffRequest, _ ...grpc.Cal
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func toMounts(apim []*types.Mount) []mount.Mount {
|
|
||||||
mounts := make([]mount.Mount, len(apim))
|
|
||||||
for i, m := range apim {
|
|
||||||
mounts[i] = mount.Mount{
|
|
||||||
Type: m.Type,
|
|
||||||
Source: m.Source,
|
|
||||||
Target: m.Target,
|
|
||||||
Options: m.Options,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return mounts
|
|
||||||
}
|
|
||||||
|
|
||||||
func toDescriptor(d *types.Descriptor) ocispec.Descriptor {
|
func toDescriptor(d *types.Descriptor) ocispec.Descriptor {
|
||||||
return ocispec.Descriptor{
|
return ocispec.Descriptor{
|
||||||
MediaType: d.MediaType,
|
MediaType: d.MediaType,
|
||||||
|
@ -21,7 +21,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
|
snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
|
||||||
"github.com/containerd/containerd/api/types"
|
|
||||||
"github.com/containerd/containerd/errdefs"
|
"github.com/containerd/containerd/errdefs"
|
||||||
"github.com/containerd/containerd/log"
|
"github.com/containerd/containerd/log"
|
||||||
"github.com/containerd/containerd/mount"
|
"github.com/containerd/containerd/mount"
|
||||||
@ -102,7 +101,7 @@ func (s *service) Prepare(ctx context.Context, pr *snapshotsapi.PrepareSnapshotR
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &snapshotsapi.PrepareSnapshotResponse{
|
return &snapshotsapi.PrepareSnapshotResponse{
|
||||||
Mounts: fromMounts(mounts),
|
Mounts: mount.ToProto(mounts),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,7 +120,7 @@ func (s *service) View(ctx context.Context, pr *snapshotsapi.ViewSnapshotRequest
|
|||||||
return nil, errdefs.ToGRPC(err)
|
return nil, errdefs.ToGRPC(err)
|
||||||
}
|
}
|
||||||
return &snapshotsapi.ViewSnapshotResponse{
|
return &snapshotsapi.ViewSnapshotResponse{
|
||||||
Mounts: fromMounts(mounts),
|
Mounts: mount.ToProto(mounts),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,7 +136,7 @@ func (s *service) Mounts(ctx context.Context, mr *snapshotsapi.MountsRequest) (*
|
|||||||
return nil, errdefs.ToGRPC(err)
|
return nil, errdefs.ToGRPC(err)
|
||||||
}
|
}
|
||||||
return &snapshotsapi.MountsResponse{
|
return &snapshotsapi.MountsResponse{
|
||||||
Mounts: fromMounts(mounts),
|
Mounts: mount.ToProto(mounts),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -304,19 +303,6 @@ func fromUsage(usage snapshots.Usage) *snapshotsapi.UsageResponse {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func fromMounts(mounts []mount.Mount) []*types.Mount {
|
|
||||||
out := make([]*types.Mount, len(mounts))
|
|
||||||
for i, m := range mounts {
|
|
||||||
out[i] = &types.Mount{
|
|
||||||
Type: m.Type,
|
|
||||||
Source: m.Source,
|
|
||||||
Target: m.Target,
|
|
||||||
Options: m.Options,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
func toInfo(info *snapshotsapi.Info) snapshots.Info {
|
func toInfo(info *snapshotsapi.Info) snapshots.Info {
|
||||||
return snapshots.Info{
|
return snapshots.Info{
|
||||||
Name: info.Name,
|
Name: info.Name,
|
||||||
|
@ -21,7 +21,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
|
|
||||||
snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
|
snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
|
||||||
"github.com/containerd/containerd/api/types"
|
|
||||||
"github.com/containerd/containerd/errdefs"
|
"github.com/containerd/containerd/errdefs"
|
||||||
"github.com/containerd/containerd/mount"
|
"github.com/containerd/containerd/mount"
|
||||||
"github.com/containerd/containerd/protobuf"
|
"github.com/containerd/containerd/protobuf"
|
||||||
@ -89,7 +88,7 @@ func (p *proxySnapshotter) Mounts(ctx context.Context, key string) ([]mount.Moun
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errdefs.FromGRPC(err)
|
return nil, errdefs.FromGRPC(err)
|
||||||
}
|
}
|
||||||
return toMounts(resp.Mounts), nil
|
return mount.FromProto(resp.Mounts), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *proxySnapshotter) Prepare(ctx context.Context, key, parent string, opts ...snapshots.Opt) ([]mount.Mount, error) {
|
func (p *proxySnapshotter) Prepare(ctx context.Context, key, parent string, opts ...snapshots.Opt) ([]mount.Mount, error) {
|
||||||
@ -108,7 +107,7 @@ func (p *proxySnapshotter) Prepare(ctx context.Context, key, parent string, opts
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errdefs.FromGRPC(err)
|
return nil, errdefs.FromGRPC(err)
|
||||||
}
|
}
|
||||||
return toMounts(resp.Mounts), nil
|
return mount.FromProto(resp.Mounts), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *proxySnapshotter) View(ctx context.Context, key, parent string, opts ...snapshots.Opt) ([]mount.Mount, error) {
|
func (p *proxySnapshotter) View(ctx context.Context, key, parent string, opts ...snapshots.Opt) ([]mount.Mount, error) {
|
||||||
@ -127,7 +126,7 @@ func (p *proxySnapshotter) View(ctx context.Context, key, parent string, opts ..
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errdefs.FromGRPC(err)
|
return nil, errdefs.FromGRPC(err)
|
||||||
}
|
}
|
||||||
return toMounts(resp.Mounts), nil
|
return mount.FromProto(resp.Mounts), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *proxySnapshotter) Commit(ctx context.Context, name, key string, opts ...snapshots.Opt) error {
|
func (p *proxySnapshotter) Commit(ctx context.Context, name, key string, opts ...snapshots.Opt) error {
|
||||||
@ -220,19 +219,6 @@ func toUsage(resp *snapshotsapi.UsageResponse) snapshots.Usage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func toMounts(mm []*types.Mount) []mount.Mount {
|
|
||||||
mounts := make([]mount.Mount, len(mm))
|
|
||||||
for i, m := range mm {
|
|
||||||
mounts[i] = mount.Mount{
|
|
||||||
Type: m.Type,
|
|
||||||
Source: m.Source,
|
|
||||||
Target: m.Target,
|
|
||||||
Options: m.Options,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return mounts
|
|
||||||
}
|
|
||||||
|
|
||||||
func fromKind(kind snapshots.Kind) snapshotsapi.Kind {
|
func fromKind(kind snapshots.Kind) snapshotsapi.Kind {
|
||||||
if kind == snapshots.KindActive {
|
if kind == snapshots.KindActive {
|
||||||
return snapshotsapi.Kind_ACTIVE
|
return snapshotsapi.Kind_ACTIVE
|
||||||
|
Loading…
Reference in New Issue
Block a user