Merge pull request #8757 from dcantah/proto-api-conversions

Add From/ToProto helpers
This commit is contained in:
Kazuyoshi Kato
2023-07-03 10:59:08 -07:00
committed by GitHub
13 changed files with 200 additions and 374 deletions

View File

@@ -20,12 +20,11 @@ import (
"context"
diffapi "github.com/containerd/containerd/api/services/diff/v1"
"github.com/containerd/containerd/api/types"
"github.com/containerd/containerd/diff"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/oci"
"github.com/containerd/typeurl/v2"
"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)
@@ -49,8 +48,8 @@ func (s *service) Apply(ctx context.Context, er *diffapi.ApplyRequest) (*diffapi
var (
ocidesc ocispec.Descriptor
err error
desc = toDescriptor(er.Diff)
mounts = toMounts(er.Mounts)
desc = oci.DescriptorFromProto(er.Diff)
mounts = mount.FromProto(er.Mounts)
)
var opts []diff.ApplyOpt
@@ -68,7 +67,7 @@ func (s *service) Apply(ctx context.Context, er *diffapi.ApplyRequest) (*diffapi
}
return &diffapi.ApplyResponse{
Applied: fromDescriptor(ocidesc),
Applied: oci.DescriptorToProto(ocidesc),
}, nil
}
@@ -79,8 +78,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
@@ -104,37 +103,6 @@ func (s *service) Diff(ctx context.Context, dr *diffapi.DiffRequest) (*diffapi.D
}
return &diffapi.DiffResponse{
Diff: fromDescriptor(ocidesc),
Diff: oci.DescriptorToProto(ocidesc),
}, 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,
Digest: digest.Digest(d.Digest),
Size: d.Size,
Annotations: d.Annotations,
}
}
func fromDescriptor(d ocispec.Descriptor) *types.Descriptor {
return &types.Descriptor{
MediaType: d.MediaType,
Digest: d.Digest.String(),
Size: d.Size,
Annotations: d.Annotations,
}
}

View File

@@ -20,10 +20,8 @@ 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"
ptypes "github.com/containerd/containerd/protobuf/types"
"github.com/containerd/containerd/snapshots"
)
@@ -51,7 +49,7 @@ func (s service) Prepare(ctx context.Context, pr *snapshotsapi.PrepareSnapshotRe
}
return &snapshotsapi.PrepareSnapshotResponse{
Mounts: fromMounts(mounts),
Mounts: mount.ToProto(mounts),
}, nil
}
@@ -65,7 +63,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 +73,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
}
@@ -105,16 +103,16 @@ func (s service) Stat(ctx context.Context, sr *snapshotsapi.StatSnapshotRequest)
return nil, errdefs.ToGRPC(err)
}
return &snapshotsapi.StatSnapshotResponse{Info: fromInfo(info)}, nil
return &snapshotsapi.StatSnapshotResponse{Info: snapshots.InfoToProto(info)}, nil
}
func (s service) Update(ctx context.Context, sr *snapshotsapi.UpdateSnapshotRequest) (*snapshotsapi.UpdateSnapshotResponse, error) {
info, err := s.sn.Update(ctx, toInfo(sr.Info), sr.UpdateMask.GetPaths()...)
info, err := s.sn.Update(ctx, snapshots.InfoFromProto(sr.Info), sr.UpdateMask.GetPaths()...)
if err != nil {
return nil, errdefs.ToGRPC(err)
}
return &snapshotsapi.UpdateSnapshotResponse{Info: fromInfo(info)}, nil
return &snapshotsapi.UpdateSnapshotResponse{Info: snapshots.InfoToProto(info)}, nil
}
func (s service) List(sr *snapshotsapi.ListSnapshotsRequest, ss snapshotsapi.Snapshots_ListServer) error {
@@ -127,7 +125,7 @@ func (s service) List(sr *snapshotsapi.ListSnapshotsRequest, ss snapshotsapi.Sna
}
)
err := s.sn.Walk(ss.Context(), func(ctx context.Context, info snapshots.Info) error {
buffer = append(buffer, fromInfo(info))
buffer = append(buffer, snapshots.InfoToProto(info))
if len(buffer) >= 100 {
if err := sendBlock(buffer); err != nil {
@@ -177,58 +175,3 @@ func (s service) Cleanup(ctx context.Context, cr *snapshotsapi.CleanupRequest) (
return empty, nil
}
func fromKind(kind snapshots.Kind) snapshotsapi.Kind {
if kind == snapshots.KindActive {
return snapshotsapi.Kind_ACTIVE
}
if kind == snapshots.KindView {
return snapshotsapi.Kind_VIEW
}
return snapshotsapi.Kind_COMMITTED
}
func fromInfo(info snapshots.Info) *snapshotsapi.Info {
return &snapshotsapi.Info{
Name: info.Name,
Parent: info.Parent,
Kind: fromKind(info.Kind),
CreatedAt: protobuf.ToTimestamp(info.Created),
UpdatedAt: protobuf.ToTimestamp(info.Updated),
Labels: info.Labels,
}
}
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,
Parent: info.Parent,
Kind: toKind(info.Kind),
Created: protobuf.FromTimestamp(info.CreatedAt),
Updated: protobuf.FromTimestamp(info.UpdatedAt),
Labels: info.Labels,
}
}
func toKind(kind snapshotsapi.Kind) snapshots.Kind {
if kind == snapshotsapi.Kind_ACTIVE {
return snapshots.KindActive
}
if kind == snapshotsapi.Kind_VIEW {
return snapshots.KindView
}
return snapshots.KindCommitted
}