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

@@ -23,7 +23,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"
protobuftypes "github.com/containerd/containerd/protobuf/types"
"github.com/containerd/containerd/snapshots"
)
@@ -51,14 +50,14 @@ func (p *proxySnapshotter) Stat(ctx context.Context, key string) (snapshots.Info
if err != nil {
return snapshots.Info{}, errdefs.FromGRPC(err)
}
return toInfo(resp.Info), nil
return snapshots.InfoFromProto(resp.Info), nil
}
func (p *proxySnapshotter) Update(ctx context.Context, info snapshots.Info, fieldpaths ...string) (snapshots.Info, error) {
resp, err := p.client.Update(ctx,
&snapshotsapi.UpdateSnapshotRequest{
Snapshotter: p.snapshotterName,
Info: fromInfo(info),
Info: snapshots.InfoToProto(info),
UpdateMask: &protobuftypes.FieldMask{
Paths: fieldpaths,
},
@@ -66,7 +65,7 @@ func (p *proxySnapshotter) Update(ctx context.Context, info snapshots.Info, fiel
if err != nil {
return snapshots.Info{}, errdefs.FromGRPC(err)
}
return toInfo(resp.Info), nil
return snapshots.InfoFromProto(resp.Info), nil
}
func (p *proxySnapshotter) Usage(ctx context.Context, key string) (snapshots.Usage, error) {
@@ -77,7 +76,7 @@ func (p *proxySnapshotter) Usage(ctx context.Context, key string) (snapshots.Usa
if err != nil {
return snapshots.Usage{}, errdefs.FromGRPC(err)
}
return toUsage(resp), nil
return snapshots.UsageFromProto(resp), nil
}
func (p *proxySnapshotter) Mounts(ctx context.Context, key string) ([]mount.Mount, error) {
@@ -173,7 +172,7 @@ func (p *proxySnapshotter) Walk(ctx context.Context, fn snapshots.WalkFunc, fs .
return nil
}
for _, info := range resp.Info {
if err := fn(ctx, toInfo(info)); err != nil {
if err := fn(ctx, snapshots.InfoFromProto(info)); err != nil {
return err
}
}
@@ -190,52 +189,3 @@ func (p *proxySnapshotter) Cleanup(ctx context.Context) error {
})
return errdefs.FromGRPC(err)
}
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
}
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 toUsage(resp *snapshotsapi.UsageResponse) snapshots.Usage {
return snapshots.Usage{
Inodes: resp.Inodes,
Size: resp.Size,
}
}
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,
}
}