-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathsnapshotter.go
291 lines (252 loc) · 9.57 KB
/
snapshotter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package demux
import (
"context"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/snapshots"
"github.com/sirupsen/logrus"
"github.com/firecracker-microvm/firecracker-containerd/internal/vm"
"github.com/firecracker-microvm/firecracker-containerd/snapshotter/demux/cache"
mountutil "github.com/firecracker-microvm/firecracker-containerd/snapshotter/internal/mount"
)
const proxiedFunctionCallErrorString = "Proxied function call failed"
// Snapshotter routes snapshotter requests to their destined
// remote snapshotter via their snapshotter namespace.
//
// Proxy snapshotters are cached for subsequent snapshotter requests.
type Snapshotter struct {
cache cache.Cache
fetchSnapshotter cache.SnapshotterProvider
}
// NewSnapshotter creates instance of Snapshotter with cache and
// proxy snapshotter creation function.
func NewSnapshotter(cache cache.Cache, fetchSnapshotter cache.SnapshotterProvider) snapshots.Snapshotter {
return &Snapshotter{cache, fetchSnapshotter}
}
// Stat proxies remote snapshotter stat request.
//
// See https://github.com/containerd/containerd/blob/main/snapshots/snapshotter.go
func (s *Snapshotter) Stat(ctx context.Context, key string) (snapshots.Info, error) {
contextLogger := log.G(ctx).WithField("function", "Stat")
namespace, err := getNamespaceFromContext(ctx, contextLogger)
if err != nil {
return snapshots.Info{}, err
}
logger := contextLogger.WithField("namespace", namespace)
snapshotter, err := s.getSnapshotterFromCache(ctx, namespace, logger)
if err != nil {
return snapshots.Info{}, err
}
info, err := snapshotter.Stat(ctx, key)
if err != nil {
contextLogger.WithError(err).Error(proxiedFunctionCallErrorString)
return snapshots.Info{}, err
}
return info, nil
}
// Update proxies remote snapshotter update request.
//
// See https://github.com/containerd/containerd/blob/main/snapshots/snapshotter.go
func (s *Snapshotter) Update(ctx context.Context, info snapshots.Info, fieldpaths ...string) (snapshots.Info, error) {
contextLogger := log.G(ctx).WithField("function", "Update")
namespace, err := getNamespaceFromContext(ctx, contextLogger)
if err != nil {
return snapshots.Info{}, err
}
logger := contextLogger.WithField("namespace", namespace)
snapshotter, err := s.getSnapshotterFromCache(ctx, namespace, logger)
if err != nil {
return snapshots.Info{}, err
}
updatedInfo, err := snapshotter.Update(ctx, info, fieldpaths...)
if err != nil {
contextLogger.WithError(err).Error(proxiedFunctionCallErrorString)
return snapshots.Info{}, err
}
return updatedInfo, nil
}
// Usage proxies remote snapshotter usage request.
//
// See https://github.com/containerd/containerd/blob/main/snapshots/snapshotter.go
func (s *Snapshotter) Usage(ctx context.Context, key string) (snapshots.Usage, error) {
contextLogger := log.G(ctx).WithField("function", "Usage")
namespace, err := getNamespaceFromContext(ctx, contextLogger)
if err != nil {
return snapshots.Usage{}, err
}
logger := contextLogger.WithField("namespace", namespace)
snapshotter, err := s.getSnapshotterFromCache(ctx, namespace, logger)
if err != nil {
return snapshots.Usage{}, err
}
usage, err := snapshotter.Usage(ctx, key)
if err != nil {
contextLogger.WithError(err).Error(proxiedFunctionCallErrorString)
return snapshots.Usage{}, err
}
return usage, nil
}
// Mounts proxies remote snapshotter mounts request.
//
// See https://github.com/containerd/containerd/blob/main/snapshots/snapshotter.go
func (s *Snapshotter) Mounts(ctx context.Context, key string) ([]mount.Mount, error) {
contextLogger := log.G(ctx).WithField("function", "Mounts")
namespace, err := getNamespaceFromContext(ctx, contextLogger)
if err != nil {
return []mount.Mount{}, err
}
logger := contextLogger.WithField("namespace", namespace)
snapshotter, err := s.getSnapshotterFromCache(ctx, namespace, logger)
if err != nil {
return []mount.Mount{}, err
}
mounts, err := snapshotter.Mounts(ctx, key)
if err != nil {
contextLogger.WithError(err).Error(proxiedFunctionCallErrorString)
return []mount.Mount{}, err
}
return mountutil.Map(mounts, vm.AddLocalMountIdentifier), nil
}
// Prepare proxies remote snapshotter prepare request.
//
// See https://github.com/containerd/containerd/blob/main/snapshots/snapshotter.go
func (s *Snapshotter) Prepare(ctx context.Context, key string, parent string, opts ...snapshots.Opt) ([]mount.Mount, error) {
contextLogger := log.G(ctx).WithField("function", "Prepare")
namespace, err := getNamespaceFromContext(ctx, contextLogger)
if err != nil {
return []mount.Mount{}, err
}
logger := contextLogger.WithField("namespace", namespace)
snapshotter, err := s.getSnapshotterFromCache(ctx, namespace, logger)
if err != nil {
return []mount.Mount{}, err
}
mounts, err := snapshotter.Prepare(ctx, key, parent, opts...)
if err != nil {
contextLogger.WithError(err).Error(proxiedFunctionCallErrorString)
return []mount.Mount{}, err
}
return mountutil.Map(mounts, vm.AddLocalMountIdentifier), nil
}
// View proxies remote snapshotter view request.
//
// See https://github.com/containerd/containerd/blob/main/snapshots/snapshotter.go
func (s *Snapshotter) View(ctx context.Context, key string, parent string, opts ...snapshots.Opt) ([]mount.Mount, error) {
contextLogger := log.G(ctx).WithField("function", "View")
namespace, err := getNamespaceFromContext(ctx, contextLogger)
if err != nil {
return []mount.Mount{}, err
}
logger := contextLogger.WithField("namespace", namespace)
snapshotter, err := s.getSnapshotterFromCache(ctx, namespace, logger)
if err != nil {
return []mount.Mount{}, err
}
mounts, err := snapshotter.View(ctx, key, parent, opts...)
if err != nil {
contextLogger.WithError(err).Error(proxiedFunctionCallErrorString)
return []mount.Mount{}, err
}
return mountutil.Map(mounts, vm.AddLocalMountIdentifier), nil
}
// Commit proxies remote snapshotter commit request.
//
// See https://github.com/containerd/containerd/blob/main/snapshots/snapshotter.go
func (s *Snapshotter) Commit(ctx context.Context, name string, key string, opts ...snapshots.Opt) error {
contextLogger := log.G(ctx).WithField("function", "Commit")
namespace, err := getNamespaceFromContext(ctx, contextLogger)
if err != nil {
return err
}
logger := contextLogger.WithField("namespace", namespace)
snapshotter, err := s.getSnapshotterFromCache(ctx, namespace, logger)
if err != nil {
return err
}
err = snapshotter.Commit(ctx, name, key, opts...)
if err != nil {
contextLogger.WithError(err).Error(proxiedFunctionCallErrorString)
return err
}
return nil
}
// Remove proxies remote snapshotter remove request.
//
// See https://github.com/containerd/containerd/blob/main/snapshots/snapshotter.go
func (s *Snapshotter) Remove(ctx context.Context, key string) error {
contextLogger := log.G(ctx).WithField("function", "Remove")
namespace, err := getNamespaceFromContext(ctx, contextLogger)
if err != nil {
return err
}
logger := contextLogger.WithField("namespace", namespace)
snapshotter, err := s.getSnapshotterFromCache(ctx, namespace, logger)
if err != nil {
return err
}
err = snapshotter.Remove(ctx, key)
if err != nil {
contextLogger.WithError(err).Error(proxiedFunctionCallErrorString)
return err
}
return nil
}
// Walk proxies remote snapshotter walk request.
//
// See https://github.com/containerd/containerd/blob/main/snapshots/snapshotter.go
func (s *Snapshotter) Walk(ctx context.Context, fn snapshots.WalkFunc, filters ...string) error {
contextLogger := log.G(ctx).WithField("function", "Walk")
namespace, err := getNamespaceFromContext(ctx, contextLogger)
if err != nil {
contextLogger.Debug("no namespace found, proxying walk function to all cached snapshotters")
return s.cache.WalkAll(ctx, fn, filters...)
}
logger := contextLogger.WithField("namespace", namespace)
snapshotter, err := s.getSnapshotterFromCache(ctx, namespace, logger)
if err != nil {
return err
}
err = snapshotter.Walk(ctx, fn, filters...)
if err != nil {
contextLogger.WithError(err).Error(proxiedFunctionCallErrorString)
return err
}
return nil
}
// Close calls close on all cached remote snapshotters.
//
// See https://github.com/containerd/containerd/blob/main/snapshots/snapshotter.go
func (s *Snapshotter) Close() error {
return s.cache.Close()
}
const snapshotterNotFoundErrorString = "Snapshotter not found in cache"
func (s *Snapshotter) getSnapshotterFromCache(ctx context.Context, namespace string, log *logrus.Entry) (snapshots.Snapshotter, error) {
snapshotter, err := s.cache.Get(ctx, namespace, s.fetchSnapshotter)
if err != nil {
log.WithError(err).Error(snapshotterNotFoundErrorString)
return nil, err
}
return snapshotter, nil
}
const missingNamespaceErrorString = "Function called without namespaced context"
func getNamespaceFromContext(ctx context.Context, log *logrus.Entry) (string, error) {
namespace, err := namespaces.NamespaceRequired(ctx)
if err != nil {
log.WithError(err).Error(missingNamespaceErrorString)
return "", err
}
return namespace, nil
}