-
Notifications
You must be signed in to change notification settings - Fork 1
/
create.go
358 lines (310 loc) · 11.3 KB
/
create.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package commands // import "github.com/SUSE/groot-btrfs/commands"
import (
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"regexp"
"code.cloudfoundry.org/commandrunner/linux_command_runner"
"code.cloudfoundry.org/lager"
"github.com/SUSE/groot-btrfs/base_image_puller"
unpackerpkg "github.com/SUSE/groot-btrfs/base_image_puller/unpacker"
"github.com/SUSE/groot-btrfs/commands/config"
"github.com/SUSE/groot-btrfs/fetcher/layer_fetcher"
"github.com/SUSE/groot-btrfs/fetcher/layer_fetcher/source"
"github.com/SUSE/groot-btrfs/fetcher/tar_fetcher"
"github.com/SUSE/groot-btrfs/groot"
"github.com/SUSE/groot-btrfs/metrics"
storepkg "github.com/SUSE/groot-btrfs/store"
"github.com/SUSE/groot-btrfs/store/dependency_manager"
"github.com/SUSE/groot-btrfs/store/filesystems/namespaced"
"github.com/SUSE/groot-btrfs/store/garbage_collector"
"github.com/SUSE/groot-btrfs/store/image_cloner"
locksmithpkg "github.com/SUSE/groot-btrfs/store/locksmith"
"github.com/SUSE/groot-btrfs/store/manager"
"github.com/containers/image/types"
"github.com/docker/distribution/registry/api/errcode"
specs "github.com/opencontainers/runtime-spec/specs-go"
errorspkg "github.com/pkg/errors"
"github.com/urfave/cli"
)
var CreateCommand = cli.Command{
Name: "create",
Usage: "create [options] <image> <id>",
Description: "Creates a root filesystem for the provided image.",
Flags: []cli.Flag{
cli.Int64Flag{
Name: "disk-limit-size-bytes",
Usage: "Inclusive disk limit (i.e: includes all layers in the filesystem)",
},
cli.StringSliceFlag{
Name: "insecure-registry",
Usage: "Whitelist a private registry",
},
cli.BoolFlag{
Name: "exclude-image-from-quota",
Usage: "Set disk limit to be exclusive (i.e.: excluding image layers)",
},
cli.BoolFlag{
Name: "skip-layer-validation",
Usage: "Do not validate checksums of image layers. (Can only be used with oci:/// protocol images.)",
},
cli.BoolFlag{
Name: "with-clean",
Usage: "Clean up unused layers before creating rootfs",
},
cli.BoolFlag{
Name: "without-clean",
Usage: "Do NOT clean up unused layers before creating rootfs",
},
cli.Int64Flag{
Name: "threshold-bytes",
Usage: "Disk usage of the store directory at which cleanup should trigger",
},
cli.BoolFlag{
Name: "with-mount",
Usage: "Mount the root filesystem after creation. This may require root privileges.",
},
cli.BoolFlag{
Name: "without-mount",
Usage: "Do not mount the root filesystem.",
},
cli.StringFlag{
Name: "username",
Usage: "Username to authenticate in image registry",
},
cli.StringFlag{
Name: "password",
Usage: "Password to authenticate in image registry",
},
},
Action: func(ctx *cli.Context) error {
logger := ctx.App.Metadata["logger"].(lager.Logger)
logger = logger.Session("create")
newExitError := newErrorHandler(logger, "create")
if ctx.NArg() != 2 {
logger.Error("parsing-command", errorspkg.New("invalid arguments"), lager.Data{"args": ctx.Args()})
return newExitError(fmt.Sprintf("invalid arguments - usage: %s", ctx.Command.Usage), 1)
}
configBuilder := ctx.App.Metadata["configBuilder"].(*config.Builder)
configBuilder.WithInsecureRegistries(ctx.StringSlice("insecure-registry")).
WithDiskLimitSizeBytes(ctx.Int64("disk-limit-size-bytes"),
ctx.IsSet("disk-limit-size-bytes")).
WithExcludeImageFromQuota(ctx.Bool("exclude-image-from-quota"),
ctx.IsSet("exclude-image-from-quota")).
WithSkipLayerValidation(ctx.Bool("skip-layer-validation"),
ctx.IsSet("skip-layer-validation")).
WithCleanThresholdBytes(ctx.Int64("threshold-bytes"), ctx.IsSet("threshold-bytes")).
WithClean(ctx.IsSet("with-clean"), ctx.IsSet("without-clean")).
WithMount(ctx.IsSet("with-mount"), ctx.IsSet("without-mount"))
cfg, err := configBuilder.Build()
logger.Debug("create-config", lager.Data{"currentConfig": cfg})
if err != nil {
logger.Error("config-builder-failed", err)
return newExitError(err.Error(), 1)
}
if err = validateOptions(ctx, cfg); err != nil {
return newExitError(err.Error(), 1)
}
storePath := cfg.StorePath
id := ctx.Args().Tail()[0]
baseImage := ctx.Args().First()
baseImageURL, err := url.Parse(baseImage)
if err != nil {
logger.Error("base-image-url-parsing-failed", err)
return newExitError(err.Error(), 1)
}
fsDriver, err := createFileSystemDriver(cfg)
if err != nil {
return newExitError(err.Error(), 1)
}
metricsEmitter := metrics.NewEmitter()
sharedLocksmith := locksmithpkg.NewSharedFileSystem(storePath, metricsEmitter)
exclusiveLocksmith := locksmithpkg.NewExclusiveFileSystem(storePath, metricsEmitter)
imageCloner := image_cloner.NewImageCloner(fsDriver, storePath)
storeNamespacer := groot.NewStoreNamespacer(storePath)
manager := manager.New(storePath, storeNamespacer, fsDriver, fsDriver, fsDriver)
if !manager.IsStoreInitialized(logger) {
logger.Error("store-verification-failed", errors.New("store is not initialized"))
return newExitError("Store path is not initialized. Please run init-store.", 1)
}
idMappings, err := storeNamespacer.Read()
if err != nil {
logger.Error("reading-namespace-file", err)
return newExitError(err.Error(), 1)
}
runner := linux_command_runner.New()
var unpacker base_image_puller.Unpacker
unpackerStrategy := unpackerpkg.UnpackStrategy{
Name: "btrfs",
WhiteoutDevicePath: filepath.Join(storePath, "whiteout_dev"),
}
var idMapper unpackerpkg.IDMapper
if os.Getuid() == 0 {
unpacker, err = unpackerpkg.NewTarUnpacker(unpackerStrategy)
if err != nil {
return newExitError(err.Error(), 1)
}
} else {
idMapper = unpackerpkg.NewIDMapper(cfg.NewuidmapBin, cfg.NewgidmapBin, runner)
unpacker = unpackerpkg.NewNSIdMapperUnpacker(runner, idMapper, unpackerStrategy)
}
dependencyManager := dependency_manager.NewDependencyManager(
filepath.Join(storePath, storepkg.MetaDirName, "dependencies"),
)
nsFsDriver := namespaced.New(fsDriver, idMappings, idMapper, runner)
systemContext := createSystemContext(baseImageURL, cfg.Create, ctx.String("username"), ctx.String("password"))
baseImagePuller := base_image_puller.NewBaseImagePuller(
createFetcher(baseImageURL, systemContext, cfg.Create),
unpacker,
nsFsDriver,
metricsEmitter,
exclusiveLocksmith,
)
sm := storepkg.NewStoreMeasurer(storePath, fsDriver)
gc := garbage_collector.NewGC(nsFsDriver, imageCloner, dependencyManager)
cleaner := groot.IamCleaner(exclusiveLocksmith, sm, gc, metricsEmitter)
defer func() {
unusedVols, err := gc.UnusedVolumes(logger, nil)
if err != nil {
logger.Error("getting-unused-layers-failed", err)
return
}
metricsEmitter.TryEmitUsage(logger, "UnusedLayersSize", sm.CacheUsage(logger, unusedVols), "bytes")
}()
creator := groot.IamCreator(
imageCloner, baseImagePuller, sharedLocksmith,
dependencyManager, metricsEmitter, cleaner,
)
createSpec := groot.CreateSpec{
ID: id,
Mount: !cfg.Create.WithoutMount,
BaseImageURL: baseImageURL,
DiskLimit: cfg.Create.DiskLimitSizeBytes,
ExcludeBaseImageFromQuota: cfg.Create.ExcludeImageFromQuota,
UIDMappings: idMappings.UIDMappings,
GIDMappings: idMappings.GIDMappings,
CleanOnCreate: cfg.Create.WithClean,
CleanOnCreateThresholdBytes: cfg.Clean.ThresholdBytes,
}
image, err := creator.Create(logger, createSpec)
if err != nil {
logger.Error("creating", err)
humanizedError := tryHumanize(err, createSpec)
return newExitError(humanizedError, 1)
}
containerSpec := specs.Spec{
Root: &specs.Root{
Path: image.Rootfs,
},
Process: &specs.Process{
Env: image.Image.Config.Env,
},
Mounts: []specs.Mount{},
}
for _, mount := range image.Mounts {
containerSpec.Mounts = append(containerSpec.Mounts, specs.Mount{
Destination: mount.Destination,
Type: mount.Type,
Source: mount.Source,
Options: mount.Options,
})
}
jsonBytes, err := json.Marshal(containerSpec)
if err != nil {
logger.Error("formatting output", err)
return newExitError(err.Error(), 1)
}
fmt.Println(string(jsonBytes))
usage, err := sm.Usage(logger)
if err != nil {
logger.Error("measuring-store", err)
return newExitError(err.Error(), 1)
}
metricsEmitter.TryIncrementRunCount("create", nil)
metricsEmitter.TryEmitUsage(logger, "StoreUsage", usage, "bytes")
return nil
},
}
func createFetcher(baseImageUrl *url.URL, systemContext types.SystemContext, createCfg config.Create) base_image_puller.Fetcher {
if baseImageUrl.Scheme == "" {
return tar_fetcher.NewTarFetcher()
}
skipOCIChecksumValidation := createCfg.SkipLayerValidation && baseImageUrl.Scheme == "oci"
layerSource := source.NewLayerSource(systemContext, skipOCIChecksumValidation)
return layer_fetcher.NewLayerFetcher(&layerSource)
}
func createSystemContext(baseImageURL *url.URL, createConfig config.Create, username, password string) types.SystemContext {
scheme := baseImageURL.Scheme
switch scheme {
case "docker":
return types.SystemContext{
DockerInsecureSkipTLSVerify: skipTLSValidation(baseImageURL, createConfig.InsecureRegistries),
DockerAuthConfig: &types.DockerAuthConfig{
Username: username,
Password: password,
},
}
case "oci":
return types.SystemContext{
OCICertPath: createConfig.RemoteLayerClientCertificatesPath,
}
default:
return types.SystemContext{}
}
}
func skipTLSValidation(baseImageURL *url.URL, trustedRegistries []string) bool {
for _, trustedRegistry := range trustedRegistries {
if baseImageURL.Host == trustedRegistry {
return true
}
}
return false
}
func containsDockerError(errorsList errcode.Errors, errCode errcode.ErrorCode) bool {
for _, err := range errorsList {
if e, ok := err.(errcode.Error); ok && e.ErrorCode() == errCode {
return true
}
}
return false
}
func tryHumanizeDockerErrorsList(err errcode.Errors, spec groot.CreateSpec) string {
if containsDockerError(err, errcode.ErrorCodeUnauthorized) {
return fmt.Sprintf("%s does not exist or you do not have permissions to see it.", spec.BaseImageURL.String())
}
return err.Error()
}
func tryParsingErrorMessage(err error) error {
newErr := errorspkg.Cause(err)
if newErr.Error() == "unable to retrieve auth token: 401 unauthorized" {
return errorspkg.New("authorization failed: username and password are invalid")
}
if newErr.Error() == "directory provided instead of a tar file" {
return errorspkg.New("invalid base image: " + newErr.Error())
}
if regexp.MustCompile("fetching image reference: .*: no such file or directory").MatchString(err.Error()) {
return errorspkg.New("Image source doesn't exist")
}
return err
}
func tryHumanize(err error, spec groot.CreateSpec) string {
switch e := errorspkg.Cause(err).(type) {
case *url.Error:
if _, ok := e.Err.(x509.UnknownAuthorityError); ok {
return "This registry is insecure. To pull images from this registry, please use the --insecure-registry option."
}
case errcode.Errors:
return tryHumanizeDockerErrorsList(e, spec)
}
return tryParsingErrorMessage(err).Error()
}
func validateOptions(ctx *cli.Context, cfg config.Config) error {
if ctx.IsSet("with-clean") && ctx.IsSet("without-clean") {
return errorspkg.New("with-clean and without-clean cannot be used together")
}
return nil
}