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:
@@ -22,7 +22,6 @@ import (
|
||||
snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
|
||||
"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"
|
||||
)
|
||||
@@ -104,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 {
|
||||
@@ -126,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 {
|
||||
@@ -176,45 +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 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user