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:
Danny Canter 2023-06-28 04:03:18 -07:00
parent 9b4ed8acc2
commit 55a8102ec1
7 changed files with 49 additions and 99 deletions

View File

@ -50,7 +50,7 @@ func (s *service) Apply(ctx context.Context, er *diffapi.ApplyRequest) (*diffapi
ocidesc ocispec.Descriptor
err error
desc = toDescriptor(er.Diff)
mounts = toMounts(er.Mounts)
mounts = mount.FromProto(er.Mounts)
)
var opts []diff.ApplyOpt
@ -79,8 +79,8 @@ func (s *service) Diff(ctx context.Context, dr *diffapi.DiffRequest) (*diffapi.D
var (
ocidesc ocispec.Descriptor
err error
aMounts = toMounts(dr.Left)
bMounts = toMounts(dr.Right)
aMounts = mount.FromProto(dr.Left)
bMounts = mount.FromProto(dr.Right)
)
var opts []diff.Opt
@ -108,19 +108,6 @@ func (s *service) Diff(ctx context.Context, dr *diffapi.DiffRequest) (*diffapi.D
}, 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 {
return ocispec.Descriptor{
MediaType: d.MediaType,

View File

@ -20,7 +20,6 @@ import (
"context"
snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
"github.com/containerd/containerd/api/types"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/protobuf"
@ -51,7 +50,7 @@ func (s service) Prepare(ctx context.Context, pr *snapshotsapi.PrepareSnapshotRe
}
return &snapshotsapi.PrepareSnapshotResponse{
Mounts: fromMounts(mounts),
Mounts: mount.ToProto(mounts),
}, nil
}
@ -65,7 +64,7 @@ func (s service) View(ctx context.Context, pr *snapshotsapi.ViewSnapshotRequest)
return nil, errdefs.ToGRPC(err)
}
return &snapshotsapi.ViewSnapshotResponse{
Mounts: fromMounts(mounts),
Mounts: mount.ToProto(mounts),
}, nil
}
@ -75,7 +74,7 @@ func (s service) Mounts(ctx context.Context, mr *snapshotsapi.MountsRequest) (*s
return nil, errdefs.ToGRPC(err)
}
return &snapshotsapi.MountsResponse{
Mounts: fromMounts(mounts),
Mounts: mount.ToProto(mounts),
}, 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 {
return snapshots.Info{
Name: info.Name,

View File

@ -60,7 +60,7 @@ func (r *diffRemote) Apply(ctx context.Context, desc ocispec.Descriptor, mounts
req := &diffapi.ApplyRequest{
Diff: fromDescriptor(desc),
Mounts: fromMounts(mounts),
Mounts: mount.ToProto(mounts),
Payloads: payloads,
}
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)
}
req := &diffapi.DiffRequest{
Left: fromMounts(a),
Right: fromMounts(b),
Left: mount.ToProto(a),
Right: mount.ToProto(b),
MediaType: config.MediaType,
Ref: config.Reference,
Labels: config.Labels,
@ -119,16 +119,3 @@ func fromDescriptor(d ocispec.Descriptor) *types.Descriptor {
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
}

View File

@ -20,6 +20,7 @@ import (
"fmt"
"strings"
"github.com/containerd/containerd/api/types"
"github.com/containerd/continuity/fs"
)
@ -130,3 +131,33 @@ func readonlyOverlay(opt []string) []string {
}
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
}

View File

@ -98,7 +98,7 @@ func (l *local) Apply(ctx context.Context, er *diffapi.ApplyRequest, _ ...grpc.C
ocidesc ocispec.Descriptor
err error
desc = toDescriptor(er.Diff)
mounts = toMounts(er.Mounts)
mounts = mount.FromProto(er.Mounts)
)
var opts []diff.ApplyOpt
@ -131,8 +131,8 @@ func (l *local) Diff(ctx context.Context, dr *diffapi.DiffRequest, _ ...grpc.Cal
var (
ocidesc ocispec.Descriptor
err error
aMounts = toMounts(dr.Left)
bMounts = toMounts(dr.Right)
aMounts = mount.FromProto(dr.Left)
bMounts = mount.FromProto(dr.Right)
)
var opts []diff.Opt
@ -165,19 +165,6 @@ func (l *local) Diff(ctx context.Context, dr *diffapi.DiffRequest, _ ...grpc.Cal
}, 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 {
return ocispec.Descriptor{
MediaType: d.MediaType,

View File

@ -21,7 +21,6 @@ import (
"errors"
snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
"github.com/containerd/containerd/api/types"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/mount"
@ -102,7 +101,7 @@ func (s *service) Prepare(ctx context.Context, pr *snapshotsapi.PrepareSnapshotR
}
return &snapshotsapi.PrepareSnapshotResponse{
Mounts: fromMounts(mounts),
Mounts: mount.ToProto(mounts),
}, nil
}
@ -121,7 +120,7 @@ func (s *service) View(ctx context.Context, pr *snapshotsapi.ViewSnapshotRequest
return nil, errdefs.ToGRPC(err)
}
return &snapshotsapi.ViewSnapshotResponse{
Mounts: fromMounts(mounts),
Mounts: mount.ToProto(mounts),
}, nil
}
@ -137,7 +136,7 @@ func (s *service) Mounts(ctx context.Context, mr *snapshotsapi.MountsRequest) (*
return nil, errdefs.ToGRPC(err)
}
return &snapshotsapi.MountsResponse{
Mounts: fromMounts(mounts),
Mounts: mount.ToProto(mounts),
}, 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 {
return snapshots.Info{
Name: info.Name,

View File

@ -21,7 +21,6 @@ import (
"io"
snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
"github.com/containerd/containerd/api/types"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/protobuf"
@ -89,7 +88,7 @@ func (p *proxySnapshotter) Mounts(ctx context.Context, key string) ([]mount.Moun
if err != nil {
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) {
@ -108,7 +107,7 @@ func (p *proxySnapshotter) Prepare(ctx context.Context, key, parent string, opts
if err != nil {
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) {
@ -127,7 +126,7 @@ func (p *proxySnapshotter) View(ctx context.Context, key, parent string, opts ..
if err != nil {
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 {
@ -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 {
if kind == snapshots.KindActive {
return snapshotsapi.Kind_ACTIVE