Snapshots: Add From/ToProto helpers for types

Helpers to convert from snapshot types to their protobuf structures and
vice-versa appear three times. It seems sane to just expose this facility
in the snapshots pkg. From/ToKind weren't used anywhere but doesn't hurt to
round out the types by exposing them.

Signed-off-by: Danny Canter <danny@dcantah.dev>
This commit is contained in:
Danny Canter
2023-06-28 04:32:22 -07:00
parent 0a6b8f0ee0
commit b3ab1f26c4
4 changed files with 81 additions and 157 deletions

View File

@@ -25,7 +25,6 @@ import (
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/plugin"
"github.com/containerd/containerd/protobuf"
ptypes "github.com/containerd/containerd/protobuf/types"
"github.com/containerd/containerd/services"
"github.com/containerd/containerd/snapshots"
@@ -184,7 +183,7 @@ 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) {
@@ -194,12 +193,12 @@ func (s *service) Update(ctx context.Context, sr *snapshotsapi.UpdateSnapshotReq
return nil, err
}
info, err := sn.Update(ctx, toInfo(sr.Info), sr.UpdateMask.GetPaths()...)
info, err := 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 {
@@ -217,7 +216,7 @@ func (s *service) List(sr *snapshotsapi.ListSnapshotsRequest, ss snapshotsapi.Sn
}
)
err = 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 {
@@ -253,7 +252,7 @@ func (s *service) Usage(ctx context.Context, ur *snapshotsapi.UsageRequest) (*sn
return nil, errdefs.ToGRPC(err)
}
return fromUsage(usage), nil
return snapshots.UsageToProto(usage), nil
}
func (s *service) Cleanup(ctx context.Context, cr *snapshotsapi.CleanupRequest) (*ptypes.Empty, error) {
@@ -274,52 +273,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 fromUsage(usage snapshots.Usage) *snapshotsapi.UsageResponse {
return &snapshotsapi.UsageResponse{
Inodes: usage.Inodes,
Size: usage.Size,
}
}
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
}