Debian Codegraph @

cmd/ctr-remote/commands/rpull.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 commands
18
19 import (
20 "context"
21 "fmt"
22
23 "github.com/containerd/containerd"
24 "github.com/containerd/containerd/cmd/ctr/commands"
25 "github.com/containerd/containerd/cmd/ctr/commands/content"
26 "github.com/containerd/containerd/images"
27 "github.com/containerd/containerd/log"
28 ctdsnapshotters "github.com/containerd/containerd/pkg/snapshotters"
29 "github.com/containerd/containerd/snapshots"
30 fsconfig "github.com/containerd/stargz-snapshotter/fs/config"
31 "github.com/containerd/stargz-snapshotter/fs/source"
32 "github.com/containerd/stargz-snapshotter/ipfs"
33 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
34 "github.com/urfave/cli"
35 )
36
37 const (
38 remoteSnapshotterName = "stargz"
39 skipContentVerifyOpt = "skip-content-verify"
40 )
41
42 // RpullCommand is a subcommand to pull an image from a registry levaraging stargz snapshotter
43 var RpullCommand = cli.Command{
44 Name: "rpull",
45 Usage: "pull an image from a registry levaraging stargz snapshotter",
46 ArgsUsage: "[flags] <ref>",
47 Description: `Fetch and prepare an image for use in containerd levaraging stargz snapshotter.
48
49 After pulling an image, it should be ready to use the same reference in a run
50 command.
51 `,
52 Flags: append(append(commands.RegistryFlags, commands.LabelFlag,
53 cli.BoolFlag{
54 Name: skipContentVerifyOpt,
55 Usage: "Skip content verification for layers contained in this image.",
56 },
57 cli.BoolFlag{
58 Name: "ipfs",
59 Usage: "Pull image from IPFS. Specify an IPFS CID as a reference. (experimental)",
60 },
61 cli.BoolFlag{
62 Name: "use-containerd-labels",
63 Usage: "Use labels defined in containerd project",
64 },
65 ), commands.SnapshotterFlags...),
66 Action: func(context *cli.Context) error {
67 var (
68 ref = context.Args().First()
69 config = &rPullConfig{}
70 )
71 if ref == "" {
72 return fmt.Errorf("please provide an image reference to pull")
73 }
74
75 client, ctx, cancel, err := commands.NewClient(context)
76 if err != nil {
77 return err
78 }
79 defer cancel()
80
81 ctx, done, err := client.WithLease(ctx)
82 if err != nil {
83 return err
84 }
85 defer done(ctx)
86
87 fc, err := content.NewFetchConfig(ctx, context)
88 if err != nil {
89 return err
90 }
91 config.FetchConfig = fc
92 config.containerdLabels = context.Bool("use-containerd-labels")
93
94 if context.Bool(skipContentVerifyOpt) {
95 config.skipVerify = true
96 }
97
98 if context.Bool("ipfs") {
99 r, err := ipfs.NewResolver(ipfs.ResolverOptions{
100 Scheme: "ipfs",
101 })
102 if err != nil {
103 return err
104 }
105 config.Resolver = r
106 }
107 config.snapshotter = remoteSnapshotterName
108 if sn := context.String("snapshotter"); sn != "" {
109 config.snapshotter = sn
110 }
111
112 return pull(ctx, client, ref, config)
113 },
114 }
115
116 type rPullConfig struct {
117 *content.FetchConfig
118 skipVerify bool
119 snapshotter string
120 containerdLabels bool
121 }
122
123 func pull(ctx context.Context, client *containerd.Client, ref string, config *rPullConfig) error {
124 pCtx := ctx
125 h := images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
126 if desc.MediaType != images.MediaTypeDockerSchema1Manifest {
127 fmt.Printf("fetching %v... %v\n", desc.Digest.String()[:15], desc.MediaType)
128 }
129 return nil, nil
130 })
131
132 var snOpts []snapshots.Opt
133 if config.skipVerify {
134 log.G(pCtx).WithField("image", ref).Warn("content verification disabled")
135 snOpts = append(snOpts, snapshots.WithLabels(map[string]string{
136 fsconfig.TargetSkipVerifyLabel: "true",
137 }))
138 }
139
140 var labelHandler func(h images.Handler) images.Handler
141 prefetchSize := int64(10*1024*1024)
142 if config.containerdLabels {
143 labelHandler = source.AppendExtraLabelsHandler(prefetchSize, ctdsnapshotters.AppendInfoHandlerWrapper(ref))
144 } else {
145 labelHandler = source.AppendDefaultLabelsHandlerWrapper(ref, prefetchSize)
146 }
147
148 log.G(pCtx).WithField("image", ref).Debug("fetching")
149 labels := commands.LabelArgs(config.Labels)
150 if _, err := client.Pull(pCtx, ref, []containerd.RemoteOpt{
151 containerd.WithPullLabels(labels),
152 containerd.WithResolver(config.Resolver),
153 containerd.WithImageHandler(h),
154 containerd.WithSchema1Conversion,
155 containerd.WithPullUnpack,
156 containerd.WithPullSnapshotter(config.snapshotter, snOpts...),
157 containerd.WithImageHandlerWrapper(labelHandler),
158 }...); err != nil {
159 return err
160 }
161
162 return nil
163 }