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:
parent
0a6b8f0ee0
commit
b3ab1f26c4
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,9 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
|
||||
"github.com/containerd/containerd/mount"
|
||||
"github.com/containerd/containerd/protobuf"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -98,6 +100,29 @@ func (k *Kind) UnmarshalJSON(b []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// KindToProto converts from [Kind] to the protobuf definition [snapshots.Kind].
|
||||
func KindToProto(kind Kind) snapshotsapi.Kind {
|
||||
if kind == KindActive {
|
||||
return snapshotsapi.Kind_ACTIVE
|
||||
}
|
||||
if kind == KindView {
|
||||
return snapshotsapi.Kind_VIEW
|
||||
}
|
||||
return snapshotsapi.Kind_COMMITTED
|
||||
}
|
||||
|
||||
// KindFromProto converts from the protobuf definition [snapshots.Kind] to
|
||||
// [Kind].
|
||||
func KindFromProto(kind snapshotsapi.Kind) Kind {
|
||||
if kind == snapshotsapi.Kind_ACTIVE {
|
||||
return KindActive
|
||||
}
|
||||
if kind == snapshotsapi.Kind_VIEW {
|
||||
return KindView
|
||||
}
|
||||
return KindCommitted
|
||||
}
|
||||
|
||||
// Info provides information about a particular snapshot.
|
||||
// JSON marshalling is supported for interacting with tools like ctr,
|
||||
type Info struct {
|
||||
@ -114,6 +139,31 @@ type Info struct {
|
||||
Updated time.Time `json:",omitempty"` // Last update time
|
||||
}
|
||||
|
||||
// InfoToProto converts from [Info] to the protobuf definition [snapshots.Info].
|
||||
func InfoToProto(info Info) *snapshotsapi.Info {
|
||||
return &snapshotsapi.Info{
|
||||
Name: info.Name,
|
||||
Parent: info.Parent,
|
||||
Kind: KindToProto(info.Kind),
|
||||
CreatedAt: protobuf.ToTimestamp(info.Created),
|
||||
UpdatedAt: protobuf.ToTimestamp(info.Updated),
|
||||
Labels: info.Labels,
|
||||
}
|
||||
}
|
||||
|
||||
// InfoFromProto converts from the protobuf definition [snapshots.Info] to
|
||||
// [Info].
|
||||
func InfoFromProto(info *snapshotsapi.Info) Info {
|
||||
return Info{
|
||||
Name: info.Name,
|
||||
Parent: info.Parent,
|
||||
Kind: KindFromProto(info.Kind),
|
||||
Created: protobuf.FromTimestamp(info.CreatedAt),
|
||||
Updated: protobuf.FromTimestamp(info.UpdatedAt),
|
||||
Labels: info.Labels,
|
||||
}
|
||||
}
|
||||
|
||||
// Usage defines statistics for disk resources consumed by the snapshot.
|
||||
//
|
||||
// These resources only include the resources consumed by the snapshot itself
|
||||
@ -133,6 +183,23 @@ func (u *Usage) Add(other Usage) {
|
||||
u.Inodes += other.Inodes
|
||||
}
|
||||
|
||||
// UsageFromProto converts from the protobuf definition [snapshots.Usage] to
|
||||
// [Usage].
|
||||
func UsageFromProto(resp *snapshotsapi.UsageResponse) Usage {
|
||||
return Usage{
|
||||
Inodes: resp.Inodes,
|
||||
Size: resp.Size,
|
||||
}
|
||||
}
|
||||
|
||||
// UsageToProto converts from [Usage] to the protobuf definition [snapshots.Usage].
|
||||
func UsageToProto(usage Usage) *snapshotsapi.UsageResponse {
|
||||
return &snapshotsapi.UsageResponse{
|
||||
Inodes: usage.Inodes,
|
||||
Size: usage.Size,
|
||||
}
|
||||
}
|
||||
|
||||
// WalkFunc defines the callback for a snapshot walk.
|
||||
type WalkFunc func(context.Context, Info) error
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user