Skip to content

Commit 526091c

Browse files
committed
Example implementation of the pool of additional layer store
Signed-off-by: Kohei Tokunaga <[email protected]>
1 parent 23240a5 commit 526091c

File tree

14 files changed

+1184
-32
lines changed

14 files changed

+1184
-32
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ VERSION=$(shell git describe --match 'v[0-9]*' --dirty='.m' --always --tags)
2323
REVISION=$(shell git rev-parse HEAD)$(shell if ! git diff --no-ext-diff --quiet --exit-code; then echo .m; fi)
2424
GO_LD_FLAGS=-ldflags '-s -w -X $(PKG)/version.Version=$(VERSION) -X $(PKG)/version.Revision=$(REVISION) $(GO_EXTRA_LDFLAGS)'
2525

26-
CMD=containerd-stargz-grpc ctr-remote
26+
CMD=containerd-stargz-grpc ctr-remote registry-storage
2727

2828
CMD_BINARIES=$(addprefix $(PREFIX),$(CMD))
2929

@@ -41,6 +41,9 @@ containerd-stargz-grpc: FORCE
4141
ctr-remote: FORCE
4242
GO111MODULE=$(GO111MODULE_VALUE) go build -o $(PREFIX)$@ $(GO_BUILD_FLAGS) $(GO_LD_FLAGS) -v ./cmd/ctr-remote
4343

44+
registry-storage: FORCE
45+
GO111MODULE=$(GO111MODULE_VALUE) go build -o $(PREFIX)$@ $(GO_BUILD_FLAGS) $(GO_LD_FLAGS) -v ./cmd/registry-storage
46+
4447
check:
4548
@echo "$@"
4649
@GO111MODULE=$(GO111MODULE_VALUE) golangci-lint run

cmd/registry-storage/fs.go

Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
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 main
18+
19+
import (
20+
"context"
21+
"encoding/base64"
22+
"strings"
23+
"syscall"
24+
25+
"github.com/containerd/containerd/images"
26+
"github.com/containerd/containerd/log"
27+
"github.com/containerd/containerd/reference"
28+
"github.com/containerd/stargz-snapshotter/estargz"
29+
fusefs "github.com/hanwen/go-fuse/v2/fs"
30+
"github.com/hanwen/go-fuse/v2/fuse"
31+
digest "github.com/opencontainers/go-digest"
32+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
33+
)
34+
35+
const (
36+
defaultLinkMode = syscall.S_IFLNK | 0400 // -r--------
37+
defaultDirMode = syscall.S_IFDIR | 0500 // dr-x------
38+
39+
chainLink = "chain"
40+
layerLink = "diff"
41+
debugManifestLink = "manifest"
42+
debugConfigLink = "config"
43+
layerInfoLink = "info"
44+
layerUseFile = "use"
45+
)
46+
47+
// node is a filesystem inode abstraction.
48+
type rootnode struct {
49+
fusefs.Inode
50+
pool *pool
51+
}
52+
53+
var _ = (fusefs.InodeEmbedder)((*rootnode)(nil))
54+
55+
var _ = (fusefs.NodeLookuper)((*rootnode)(nil))
56+
57+
// Lookup loads manifest and config of specified name (imgae reference)
58+
// and returns refnode of the specified name
59+
func (n *rootnode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fusefs.Inode, syscall.Errno) {
60+
refBytes, err := base64.StdEncoding.DecodeString(name)
61+
if err != nil {
62+
log.G(ctx).WithError(err).Debugf("failed to decode ref base64 %q", name)
63+
return nil, syscall.EINVAL
64+
}
65+
ref := string(refBytes)
66+
refspec, err := reference.Parse(ref)
67+
if err != nil {
68+
log.G(ctx).WithError(err).Warnf("invalid reference %q for %q", ref, name)
69+
return nil, syscall.EINVAL
70+
}
71+
manifest, mPath, config, cPath, err := n.pool.loadManifestAndConfig(ctx, refspec)
72+
if err != nil {
73+
log.G(ctx).WithError(err).
74+
Warnf("failed to fetch manifest and config of %q(%q)", ref, name)
75+
return nil, syscall.EIO
76+
}
77+
return n.NewInode(ctx, &refnode{
78+
pool: n.pool,
79+
ref: refspec,
80+
manifest: manifest,
81+
manifestPath: mPath,
82+
config: config,
83+
configPath: cPath,
84+
}, defaultDirAttr(&out.Attr)), 0
85+
}
86+
87+
// node is a filesystem inode abstraction.
88+
type refnode struct {
89+
fusefs.Inode
90+
pool *pool
91+
92+
ref reference.Spec
93+
manifest ocispec.Manifest
94+
manifestPath string
95+
config ocispec.Image
96+
configPath string
97+
}
98+
99+
var _ = (fusefs.InodeEmbedder)((*refnode)(nil))
100+
101+
var _ = (fusefs.NodeLookuper)((*refnode)(nil))
102+
103+
// Lookup returns layernode of the specified name
104+
func (n *refnode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fusefs.Inode, syscall.Errno) {
105+
switch name {
106+
case debugManifestLink:
107+
return n.NewInode(ctx,
108+
&linknode{linkname: n.manifestPath}, defaultLinkAttr(&out.Attr)), 0
109+
case debugConfigLink:
110+
return n.NewInode(ctx,
111+
&linknode{linkname: n.configPath}, defaultLinkAttr(&out.Attr)), 0
112+
}
113+
targetDigest, err := digest.Parse(name)
114+
if err != nil {
115+
log.G(ctx).WithError(err).Warnf("invalid digest for %q", name)
116+
return nil, syscall.EINVAL
117+
}
118+
var chain []ocispec.Descriptor
119+
var found bool
120+
for _, l := range n.manifest.Layers {
121+
chain = append(chain, l)
122+
if l.Digest == targetDigest {
123+
found = true
124+
break
125+
}
126+
}
127+
if !found {
128+
log.G(ctx).WithError(err).Warnf("invalid digest for %q: %q", name, targetDigest.String())
129+
return nil, syscall.EINVAL
130+
}
131+
return n.NewInode(ctx, &layernode{
132+
pool: n.pool,
133+
layer: chain[len(chain)-1],
134+
chain: chain,
135+
refnode: n,
136+
}, defaultDirAttr(&out.Attr)), 0
137+
}
138+
139+
var _ = (fusefs.NodeRmdirer)((*refnode)(nil))
140+
141+
// Rmdir marks this layer as "release".
142+
// We don't use layernode.Unlink because Unlink event doesn't reach here when "use" file isn't visible
143+
// to the filesystem client.
144+
func (n *refnode) Rmdir(ctx context.Context, name string) syscall.Errno {
145+
if name == debugManifestLink || name == debugConfigLink {
146+
return syscall.EROFS // nop
147+
}
148+
targetDigest, err := digest.Parse(name)
149+
if err != nil {
150+
log.G(ctx).WithError(err).Warnf("invalid digest for %q during release", name)
151+
return syscall.EINVAL
152+
}
153+
current, err := n.pool.release(n.ref, targetDigest)
154+
if err != nil {
155+
log.G(ctx).WithError(err).Warnf("failed to release layer %v / %v", n.ref, targetDigest)
156+
return syscall.EIO
157+
158+
}
159+
log.G(ctx).WithField("refcounter", current).Warnf("layer %v / %v is marked as RELEASE", n.ref, targetDigest)
160+
return syscall.EROFS
161+
}
162+
163+
// node is a filesystem inode abstraction.
164+
type layernode struct {
165+
fusefs.Inode
166+
pool *pool
167+
168+
layer ocispec.Descriptor
169+
chain []ocispec.Descriptor
170+
171+
refnode *refnode
172+
}
173+
174+
var _ = (fusefs.InodeEmbedder)((*layernode)(nil))
175+
176+
var _ = (fusefs.NodeCreater)((*layernode)(nil))
177+
178+
// Create marks this layer as "using".
179+
// We don't use refnode.Mkdir because Mkdir event doesn't reach here if layernode already exists.
180+
func (n *layernode) Create(ctx context.Context, name string, flags uint32, mode uint32, out *fuse.EntryOut) (node *fusefs.Inode, fh fusefs.FileHandle, fuseFlags uint32, errno syscall.Errno) {
181+
if name == layerUseFile {
182+
current := n.pool.use(n.refnode.ref, n.layer.Digest)
183+
log.G(ctx).WithField("refcounter", current).Warnf("layer %v / %v is marked as USING", n.refnode.ref, n.layer.Digest)
184+
}
185+
186+
// TODO: implement cleanup
187+
return nil, nil, 0, syscall.EROFS
188+
}
189+
190+
var _ = (fusefs.NodeLookuper)((*layernode)(nil))
191+
192+
// Lookup routes to the target file stored in the pool, based on the specified file name.
193+
func (n *layernode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fusefs.Inode, syscall.Errno) {
194+
195+
var linkpath string
196+
switch name {
197+
case layerInfoLink:
198+
var err error
199+
linkpath, err = n.pool.loadLayerInfo(ctx, n.refnode.ref, n.layer.Digest)
200+
if err != nil {
201+
log.G(ctx).WithError(err).
202+
Warnf("failed to get layer info for %q: %q", name, n.layer.Digest)
203+
return nil, syscall.EIO
204+
}
205+
case layerLink:
206+
var layers []string
207+
for _, l := range n.chain {
208+
if images.IsLayerType(l.MediaType) {
209+
layers = append(layers, l.Digest.String())
210+
}
211+
}
212+
labels := map[string]string{
213+
targetImageLayersLabel: strings.Join(layers, ","),
214+
}
215+
if n.layer.Annotations != nil {
216+
tocDigest, ok := n.layer.Annotations[estargz.TOCJSONDigestAnnotation]
217+
if ok {
218+
labels[estargz.TOCJSONDigestAnnotation] = tocDigest
219+
}
220+
}
221+
var err error
222+
linkpath, err = n.pool.loadLayer(ctx, n.refnode.ref, n.layer.Digest, labels)
223+
if err != nil {
224+
log.G(ctx).WithError(err).Warnf("failed to mount layer of %q: %q", name, n.layer.Digest)
225+
return nil, syscall.EIO
226+
}
227+
case chainLink:
228+
var err error
229+
linkpath, err = n.pool.loadChain(ctx, n.refnode.ref, n.chain)
230+
if err != nil {
231+
log.G(ctx).WithError(err).Warnf("failed to mount chain of %q: %q",
232+
name, n.layer.Digest)
233+
return nil, syscall.EIO
234+
}
235+
case layerUseFile:
236+
log.G(ctx).Debugf("\"use\" file is referred but return ENOENT for reference management")
237+
return nil, syscall.ENOENT
238+
default:
239+
log.G(ctx).Warnf("unknown filename %q", name)
240+
return nil, syscall.ENOENT
241+
}
242+
return n.NewInode(ctx, &linknode{linkname: linkpath}, defaultLinkAttr(&out.Attr)), 0
243+
}
244+
245+
type linknode struct {
246+
fusefs.Inode
247+
linkname string
248+
}
249+
250+
var _ = (fusefs.InodeEmbedder)((*linknode)(nil))
251+
252+
var _ = (fusefs.NodeReadlinker)((*linknode)(nil))
253+
254+
func (n *linknode) Readlink(ctx context.Context) ([]byte, syscall.Errno) {
255+
return []byte(n.linkname), 0 // TODO: linkname shouldn't statically embedded?
256+
}
257+
258+
func defaultDirAttr(out *fuse.Attr) fusefs.StableAttr {
259+
// out.Ino
260+
out.Size = 0
261+
// out.Blksize
262+
// out.Blocks
263+
// out.Nlink
264+
out.Mode = defaultDirMode
265+
out.Owner = fuse.Owner{Uid: 0, Gid: 0}
266+
// out.Mtime
267+
// out.Mtimensec
268+
// out.Rdev
269+
// out.Padding
270+
271+
return fusefs.StableAttr{
272+
Mode: out.Mode,
273+
}
274+
}
275+
276+
func defaultLinkAttr(out *fuse.Attr) fusefs.StableAttr {
277+
// out.Ino
278+
out.Size = 0
279+
// out.Blksize
280+
// out.Blocks
281+
// out.Nlink
282+
out.Mode = defaultLinkMode
283+
out.Owner = fuse.Owner{Uid: 0, Gid: 0}
284+
// out.Mtime
285+
// out.Mtimensec
286+
// out.Rdev
287+
// out.Padding
288+
289+
return fusefs.StableAttr{
290+
Mode: out.Mode,
291+
}
292+
}

0 commit comments

Comments
 (0)