Skip to content

Commit 64d29eb

Browse files
committed
snapshots: core: Remove dependency on api types
Core should not have a dependency on API types. This was causing a transative dependency on grpc when importing the core snapshots package. Signed-off-by: Brian Goff <[email protected]>
1 parent 11ffba3 commit 64d29eb

File tree

5 files changed

+106
-83
lines changed

5 files changed

+106
-83
lines changed

contrib/snapshotservice/service.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
2323
"github.com/containerd/containerd/v2/core/mount"
2424
"github.com/containerd/containerd/v2/core/snapshots"
25+
"github.com/containerd/containerd/v2/core/snapshots/proxy"
2526
ptypes "github.com/containerd/containerd/v2/pkg/protobuf/types"
2627
"github.com/containerd/errdefs"
2728
)
@@ -103,16 +104,16 @@ func (s service) Stat(ctx context.Context, sr *snapshotsapi.StatSnapshotRequest)
103104
return nil, errdefs.ToGRPC(err)
104105
}
105106

106-
return &snapshotsapi.StatSnapshotResponse{Info: snapshots.InfoToProto(info)}, nil
107+
return &snapshotsapi.StatSnapshotResponse{Info: proxy.InfoToProto(info)}, nil
107108
}
108109

109110
func (s service) Update(ctx context.Context, sr *snapshotsapi.UpdateSnapshotRequest) (*snapshotsapi.UpdateSnapshotResponse, error) {
110-
info, err := s.sn.Update(ctx, snapshots.InfoFromProto(sr.Info), sr.UpdateMask.GetPaths()...)
111+
info, err := s.sn.Update(ctx, proxy.InfoFromProto(sr.Info), sr.UpdateMask.GetPaths()...)
111112
if err != nil {
112113
return nil, errdefs.ToGRPC(err)
113114
}
114115

115-
return &snapshotsapi.UpdateSnapshotResponse{Info: snapshots.InfoToProto(info)}, nil
116+
return &snapshotsapi.UpdateSnapshotResponse{Info: proxy.InfoToProto(info)}, nil
116117
}
117118

118119
func (s service) List(sr *snapshotsapi.ListSnapshotsRequest, ss snapshotsapi.Snapshots_ListServer) error {
@@ -125,7 +126,7 @@ func (s service) List(sr *snapshotsapi.ListSnapshotsRequest, ss snapshotsapi.Sna
125126
}
126127
)
127128
err := s.sn.Walk(ss.Context(), func(ctx context.Context, info snapshots.Info) error {
128-
buffer = append(buffer, snapshots.InfoToProto(info))
129+
buffer = append(buffer, proxy.InfoToProto(info))
129130

130131
if len(buffer) >= 100 {
131132
if err := sendBlock(buffer); err != nil {

core/snapshots/proxy/convert.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package proxy
18+
19+
import (
20+
snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
21+
"github.com/containerd/containerd/v2/core/snapshots"
22+
"github.com/containerd/containerd/v2/pkg/protobuf"
23+
)
24+
25+
// KindToProto converts from [Kind] to the protobuf definition [snapshots.Kind].
26+
func KindToProto(kind snapshots.Kind) snapshotsapi.Kind {
27+
switch kind {
28+
case snapshots.KindActive:
29+
return snapshotsapi.Kind_ACTIVE
30+
case snapshots.KindView:
31+
return snapshotsapi.Kind_VIEW
32+
default:
33+
return snapshotsapi.Kind_COMMITTED
34+
}
35+
}
36+
37+
// KindFromProto converts from the protobuf definition [snapshots.Kind] to
38+
// [Kind].
39+
func KindFromProto(kind snapshotsapi.Kind) snapshots.Kind {
40+
switch kind {
41+
case snapshotsapi.Kind_ACTIVE:
42+
return snapshots.KindActive
43+
case snapshotsapi.Kind_VIEW:
44+
return snapshots.KindView
45+
default:
46+
return snapshots.KindCommitted
47+
}
48+
}
49+
50+
// InfoToProto converts from [Info] to the protobuf definition [snapshots.Info].
51+
func InfoToProto(info snapshots.Info) *snapshotsapi.Info {
52+
return &snapshotsapi.Info{
53+
Name: info.Name,
54+
Parent: info.Parent,
55+
Kind: KindToProto(info.Kind),
56+
CreatedAt: protobuf.ToTimestamp(info.Created),
57+
UpdatedAt: protobuf.ToTimestamp(info.Updated),
58+
Labels: info.Labels,
59+
}
60+
}
61+
62+
// InfoFromProto converts from the protobuf definition [snapshots.Info] to
63+
// [Info].
64+
func InfoFromProto(info *snapshotsapi.Info) snapshots.Info {
65+
return snapshots.Info{
66+
Name: info.Name,
67+
Parent: info.Parent,
68+
Kind: KindFromProto(info.Kind),
69+
Created: protobuf.FromTimestamp(info.CreatedAt),
70+
Updated: protobuf.FromTimestamp(info.UpdatedAt),
71+
Labels: info.Labels,
72+
}
73+
}
74+
75+
// UsageFromProto converts from the protobuf definition [snapshots.Usage] to
76+
// [Usage].
77+
func UsageFromProto(resp *snapshotsapi.UsageResponse) snapshots.Usage {
78+
return snapshots.Usage{
79+
Inodes: resp.Inodes,
80+
Size: resp.Size,
81+
}
82+
}
83+
84+
// UsageToProto converts from [Usage] to the protobuf definition [snapshots.Usage].
85+
func UsageToProto(usage snapshots.Usage) *snapshotsapi.UsageResponse {
86+
return &snapshotsapi.UsageResponse{
87+
Inodes: usage.Inodes,
88+
Size: usage.Size,
89+
}
90+
}

core/snapshots/proxy/proxy.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,22 @@ func (p *proxySnapshotter) Stat(ctx context.Context, key string) (snapshots.Info
5050
if err != nil {
5151
return snapshots.Info{}, errdefs.FromGRPC(err)
5252
}
53-
return snapshots.InfoFromProto(resp.Info), nil
53+
return InfoFromProto(resp.Info), nil
5454
}
5555

5656
func (p *proxySnapshotter) Update(ctx context.Context, info snapshots.Info, fieldpaths ...string) (snapshots.Info, error) {
5757
resp, err := p.client.Update(ctx,
5858
&snapshotsapi.UpdateSnapshotRequest{
5959
Snapshotter: p.snapshotterName,
60-
Info: snapshots.InfoToProto(info),
60+
Info: InfoToProto(info),
6161
UpdateMask: &protobuftypes.FieldMask{
6262
Paths: fieldpaths,
6363
},
6464
})
6565
if err != nil {
6666
return snapshots.Info{}, errdefs.FromGRPC(err)
6767
}
68-
return snapshots.InfoFromProto(resp.Info), nil
68+
return InfoFromProto(resp.Info), nil
6969
}
7070

7171
func (p *proxySnapshotter) Usage(ctx context.Context, key string) (snapshots.Usage, error) {
@@ -76,7 +76,7 @@ func (p *proxySnapshotter) Usage(ctx context.Context, key string) (snapshots.Usa
7676
if err != nil {
7777
return snapshots.Usage{}, errdefs.FromGRPC(err)
7878
}
79-
return snapshots.UsageFromProto(resp), nil
79+
return UsageFromProto(resp), nil
8080
}
8181

8282
func (p *proxySnapshotter) Mounts(ctx context.Context, key string) ([]mount.Mount, error) {
@@ -172,7 +172,7 @@ func (p *proxySnapshotter) Walk(ctx context.Context, fn snapshots.WalkFunc, fs .
172172
return nil
173173
}
174174
for _, info := range resp.Info {
175-
if err := fn(ctx, snapshots.InfoFromProto(info)); err != nil {
175+
if err := fn(ctx, InfoFromProto(info)); err != nil {
176176
return err
177177
}
178178
}

core/snapshots/snapshotter.go

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ import (
2222
"strings"
2323
"time"
2424

25-
snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
2625
"github.com/containerd/containerd/v2/core/mount"
27-
"github.com/containerd/containerd/v2/pkg/protobuf"
2826
)
2927

3028
const (
@@ -100,31 +98,6 @@ func (k *Kind) UnmarshalJSON(b []byte) error {
10098
return nil
10199
}
102100

103-
// KindToProto converts from [Kind] to the protobuf definition [snapshots.Kind].
104-
func KindToProto(kind Kind) snapshotsapi.Kind {
105-
switch kind {
106-
case KindActive:
107-
return snapshotsapi.Kind_ACTIVE
108-
case KindView:
109-
return snapshotsapi.Kind_VIEW
110-
default:
111-
return snapshotsapi.Kind_COMMITTED
112-
}
113-
}
114-
115-
// KindFromProto converts from the protobuf definition [snapshots.Kind] to
116-
// [Kind].
117-
func KindFromProto(kind snapshotsapi.Kind) Kind {
118-
switch kind {
119-
case snapshotsapi.Kind_ACTIVE:
120-
return KindActive
121-
case snapshotsapi.Kind_VIEW:
122-
return KindView
123-
default:
124-
return KindCommitted
125-
}
126-
}
127-
128101
// Info provides information about a particular snapshot.
129102
// JSON marshalling is supported for interacting with tools like ctr,
130103
type Info struct {
@@ -141,31 +114,6 @@ type Info struct {
141114
Updated time.Time `json:",omitempty"` // Last update time
142115
}
143116

144-
// InfoToProto converts from [Info] to the protobuf definition [snapshots.Info].
145-
func InfoToProto(info Info) *snapshotsapi.Info {
146-
return &snapshotsapi.Info{
147-
Name: info.Name,
148-
Parent: info.Parent,
149-
Kind: KindToProto(info.Kind),
150-
CreatedAt: protobuf.ToTimestamp(info.Created),
151-
UpdatedAt: protobuf.ToTimestamp(info.Updated),
152-
Labels: info.Labels,
153-
}
154-
}
155-
156-
// InfoFromProto converts from the protobuf definition [snapshots.Info] to
157-
// [Info].
158-
func InfoFromProto(info *snapshotsapi.Info) Info {
159-
return Info{
160-
Name: info.Name,
161-
Parent: info.Parent,
162-
Kind: KindFromProto(info.Kind),
163-
Created: protobuf.FromTimestamp(info.CreatedAt),
164-
Updated: protobuf.FromTimestamp(info.UpdatedAt),
165-
Labels: info.Labels,
166-
}
167-
}
168-
169117
// Usage defines statistics for disk resources consumed by the snapshot.
170118
//
171119
// These resources only include the resources consumed by the snapshot itself
@@ -185,23 +133,6 @@ func (u *Usage) Add(other Usage) {
185133
u.Inodes += other.Inodes
186134
}
187135

188-
// UsageFromProto converts from the protobuf definition [snapshots.Usage] to
189-
// [Usage].
190-
func UsageFromProto(resp *snapshotsapi.UsageResponse) Usage {
191-
return Usage{
192-
Inodes: resp.Inodes,
193-
Size: resp.Size,
194-
}
195-
}
196-
197-
// UsageToProto converts from [Usage] to the protobuf definition [snapshots.Usage].
198-
func UsageToProto(usage Usage) *snapshotsapi.UsageResponse {
199-
return &snapshotsapi.UsageResponse{
200-
Inodes: usage.Inodes,
201-
Size: usage.Size,
202-
}
203-
}
204-
205136
// WalkFunc defines the callback for a snapshot walk.
206137
type WalkFunc func(context.Context, Info) error
207138

plugins/services/snapshots/service.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
2323
"github.com/containerd/containerd/v2/core/mount"
2424
"github.com/containerd/containerd/v2/core/snapshots"
25+
"github.com/containerd/containerd/v2/core/snapshots/proxy"
2526
ptypes "github.com/containerd/containerd/v2/pkg/protobuf/types"
2627
"github.com/containerd/containerd/v2/plugins"
2728
"github.com/containerd/containerd/v2/plugins/services"
@@ -175,7 +176,7 @@ func (s *service) Stat(ctx context.Context, sr *snapshotsapi.StatSnapshotRequest
175176
return nil, errdefs.ToGRPC(err)
176177
}
177178

178-
return &snapshotsapi.StatSnapshotResponse{Info: snapshots.InfoToProto(info)}, nil
179+
return &snapshotsapi.StatSnapshotResponse{Info: proxy.InfoToProto(info)}, nil
179180
}
180181

181182
func (s *service) Update(ctx context.Context, sr *snapshotsapi.UpdateSnapshotRequest) (*snapshotsapi.UpdateSnapshotResponse, error) {
@@ -185,12 +186,12 @@ func (s *service) Update(ctx context.Context, sr *snapshotsapi.UpdateSnapshotReq
185186
return nil, err
186187
}
187188

188-
info, err := sn.Update(ctx, snapshots.InfoFromProto(sr.Info), sr.UpdateMask.GetPaths()...)
189+
info, err := sn.Update(ctx, proxy.InfoFromProto(sr.Info), sr.UpdateMask.GetPaths()...)
189190
if err != nil {
190191
return nil, errdefs.ToGRPC(err)
191192
}
192193

193-
return &snapshotsapi.UpdateSnapshotResponse{Info: snapshots.InfoToProto(info)}, nil
194+
return &snapshotsapi.UpdateSnapshotResponse{Info: proxy.InfoToProto(info)}, nil
194195
}
195196

196197
func (s *service) List(sr *snapshotsapi.ListSnapshotsRequest, ss snapshotsapi.Snapshots_ListServer) error {
@@ -208,7 +209,7 @@ func (s *service) List(sr *snapshotsapi.ListSnapshotsRequest, ss snapshotsapi.Sn
208209
}
209210
)
210211
err = sn.Walk(ss.Context(), func(ctx context.Context, info snapshots.Info) error {
211-
buffer = append(buffer, snapshots.InfoToProto(info))
212+
buffer = append(buffer, proxy.InfoToProto(info))
212213

213214
if len(buffer) >= 100 {
214215
if err := sendBlock(buffer); err != nil {
@@ -244,7 +245,7 @@ func (s *service) Usage(ctx context.Context, ur *snapshotsapi.UsageRequest) (*sn
244245
return nil, errdefs.ToGRPC(err)
245246
}
246247

247-
return snapshots.UsageToProto(usage), nil
248+
return proxy.UsageToProto(usage), nil
248249
}
249250

250251
func (s *service) Cleanup(ctx context.Context, cr *snapshotsapi.CleanupRequest) (*ptypes.Empty, error) {

0 commit comments

Comments
 (0)