Debian Codegraph @

service/service.go

raw
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 service
18
19 import (
20 "context"
21 "path/filepath"
22
23 "github.com/containerd/containerd/log"
24 "github.com/containerd/containerd/reference"
25 "github.com/containerd/containerd/snapshots"
26 "github.com/containerd/containerd/snapshots/overlay/overlayutils"
27 stargzfs "github.com/containerd/stargz-snapshotter/fs"
28 "github.com/containerd/stargz-snapshotter/fs/layer"
29 "github.com/containerd/stargz-snapshotter/fs/source"
30 "github.com/containerd/stargz-snapshotter/metadata"
31 esgzexternaltoc "github.com/containerd/stargz-snapshotter/nativeconverter/estargz/externaltoc"
32 "github.com/containerd/stargz-snapshotter/service/resolver"
33 snbase "github.com/containerd/stargz-snapshotter/snapshot"
34 "github.com/hashicorp/go-multierror"
35 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
36 )
37
38 type Option func(*options)
39
40 type options struct {
41 credsFuncs []resolver.Credential
42 registryHosts source.RegistryHosts
43 fsOpts []stargzfs.Option
44 }
45
46 // WithCredsFuncs specifies credsFuncs to be used for connecting to the registries.
47 func WithCredsFuncs(creds ...resolver.Credential) Option {
48 return func(o *options) {
49 o.credsFuncs = append(o.credsFuncs, creds...)
50 }
51 }
52
53 // WithCustomRegistryHosts is registry hosts to use instead.
54 func WithCustomRegistryHosts(hosts source.RegistryHosts) Option {
55 return func(o *options) {
56 o.registryHosts = hosts
57 }
58 }
59
60 // WithFilesystemOptions allowes to pass filesystem-related configuration.
61 func WithFilesystemOptions(opts ...stargzfs.Option) Option {
62 return func(o *options) {
63 o.fsOpts = opts
64 }
65 }
66
67 // NewStargzSnapshotterService returns stargz snapshotter.
68 func NewStargzSnapshotterService(ctx context.Context, root string, config *Config, opts ...Option) (snapshots.Snapshotter, error) {
69 var sOpts options
70 for _, o := range opts {
71 o(&sOpts)
72 }
73
74 hosts := sOpts.registryHosts
75 if hosts == nil {
76 // Use RegistryHosts based on ResolverConfig and keychain
77 hosts = resolver.RegistryHostsFromConfig(resolver.Config(config.ResolverConfig), sOpts.credsFuncs...)
78 }
79
80 userxattr, err := overlayutils.NeedsUserXAttr(snapshotterRoot(root))
81 if err != nil {
82 log.G(ctx).WithError(err).Warnf("cannot detect whether \"userxattr\" option needs to be used, assuming to be %v", userxattr)
83 }
84 opq := layer.OverlayOpaqueTrusted
85 if userxattr {
86 opq = layer.OverlayOpaqueUser
87 }
88 // Configure filesystem and snapshotter
89 fsOpts := append(sOpts.fsOpts, stargzfs.WithGetSources(sources(
90 sourceFromCRILabels(hosts), // provides source info based on CRI labels
91 source.FromDefaultLabels(hosts), // provides source info based on default labels
92 )),
93 stargzfs.WithOverlayOpaqueType(opq),
94 stargzfs.WithAdditionalDecompressors(func(ctx context.Context, hosts source.RegistryHosts, refspec reference.Spec, desc ocispec.Descriptor) []metadata.Decompressor {
95 return []metadata.Decompressor{esgzexternaltoc.NewRemoteDecompressor(ctx, hosts, refspec, desc)}
96 }),
97 )
98 fs, err := stargzfs.NewFilesystem(fsRoot(root), config.Config, fsOpts...)
99 if err != nil {
100 log.G(ctx).WithError(err).Fatalf("failed to configure filesystem")
101 }
102
103 var snapshotter snapshots.Snapshotter
104
105 snOpts := []snbase.Opt{snbase.AsynchronousRemove}
106 if config.SnapshotterConfig.AllowInvalidMountsOnRestart {
107 snOpts = append(snOpts, snbase.AllowInvalidMountsOnRestart)
108 }
109
110 snapshotter, err = snbase.NewSnapshotter(ctx, snapshotterRoot(root), fs, snOpts...)
111 if err != nil {
112 log.G(ctx).WithError(err).Fatalf("failed to create new snapshotter")
113 }
114
115 return snapshotter, err
116 }
117
118 func snapshotterRoot(root string) string {
119 return filepath.Join(root, "snapshotter")
120 }
121
122 func fsRoot(root string) string {
123 return filepath.Join(root, "stargz")
124 }
125
126 func sources(ps ...source.GetSources) source.GetSources {
127 return func(labels map[string]string) (source []source.Source, allErr error) {
128 for _, p := range ps {
129 src, err := p(labels)
130 if err == nil {
131 return src, nil
132 }
133 allErr = multierror.Append(allErr, err)
134 }
135 return
136 }
137 }
138
139 // Supported returns nil when the remote snapshotter is functional on the system with the root directory.
140 // Supported is not called during plugin initialization, but exposed for downstream projects which uses
141 // this snapshotter as a library.
142 func Supported(root string) error {
143 // Remote snapshotter is implemented based on overlayfs snapshotter.
144 return overlayutils.Supported(snapshotterRoot(root))
145 }