Debian Codegraph @

cmd/soci/commands/image/rpull.go

raw symbols SCIP
1 /*
2 Copyright The Soci Snapshotter 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 /*
18 Copyright The containerd Authors.
19
20 Licensed under the Apache License, Version 2.0 (the "License");
21 you may not use this file except in compliance with the License.
22 You may obtain a copy of the License at
23
24 http://www.apache.org/licenses/LICENSE-2.0
25
26 Unless required by applicable law or agreed to in writing, software
27 distributed under the License is distributed on an "AS IS" BASIS,
28 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 See the License for the specific language governing permissions and
30 limitations under the License.
31 */
32
33 package image
34
35 import (
36 "context"
37 "fmt"
38
39 "github.com/awslabs/soci-snapshotter/cmd/soci/commands/internal"
40 "github.com/awslabs/soci-snapshotter/fs/source"
41 "github.com/containerd/containerd"
42 "github.com/containerd/containerd/cmd/ctr/commands"
43 "github.com/containerd/containerd/cmd/ctr/commands/content"
44 "github.com/containerd/containerd/images"
45 "github.com/containerd/containerd/log"
46 ctdsnapshotters "github.com/containerd/containerd/pkg/snapshotters"
47 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
48 "github.com/urfave/cli"
49 )
50
51 const (
52 remoteSnapshotterName = "soci"
53 skipContentVerifyOpt = "skip-content-verify"
54 )
55
56 // rpullCommand is a subcommand to pull an image from a registry levaraging soci snapshotter
57 var rpullCommand = cli.Command{
58 Name: "rpull",
59 Usage: "pull an image from a registry levaraging soci snapshotter",
60 ArgsUsage: "[flags] <ref>",
61 Description: `Fetch and prepare an image for use in containerd levaraging soci snapshotter.
62
63 After pulling an image, it should be ready to use the same reference in a run
64 command.
65 `,
66 Flags: append(append(append(
67 commands.RegistryFlags,
68 commands.LabelFlag),
69 commands.SnapshotterFlags...),
70 cli.BoolFlag{
71 Name: skipContentVerifyOpt,
72 Usage: "Skip content verification for layers contained in this image.",
73 },
74 // This is a standin for the snapshotter receiving the index digest from container runtimes.
75 cli.StringFlag{
76 Name: "soci-index-digest",
77 Usage: "The SOCI index digest.",
78 },
79 cli.StringFlag{
80 Name: internal.PlatformFlagKey,
81 Usage: "The platform to pull.",
82 },
83 ),
84 Action: func(context *cli.Context) error {
85 var (
86 ref = context.Args().First()
87 config = &rPullConfig{}
88 )
89 if ref == "" {
90 return fmt.Errorf("please provide an image reference")
91 }
92
93 config.indexDigest = context.String("soci-index-digest")
94
95 client, ctx, cancel, err := commands.NewClient(context)
96 if err != nil {
97 return err
98 }
99 defer cancel()
100
101 ctx, done, err := client.WithLease(ctx)
102 if err != nil {
103 return err
104 }
105 defer done(ctx)
106
107 fc, err := content.NewFetchConfig(ctx, context)
108 if err != nil {
109 return err
110 }
111 config.FetchConfig = fc
112
113 if context.Bool(skipContentVerifyOpt) {
114 config.skipVerify = true
115 }
116
117 config.snapshotter = remoteSnapshotterName
118 if sn := context.String("snapshotter"); sn != "" {
119 config.snapshotter = sn
120 }
121
122 config.platform = context.String(internal.PlatformFlagKey)
123
124 return pull(ctx, client, ref, config)
125 },
126 }
127
128 type rPullConfig struct {
129 *content.FetchConfig
130 skipVerify bool
131 snapshotter string
132 indexDigest string
133 platform string
134 }
135
136 func pull(ctx context.Context, client *containerd.Client, ref string, config *rPullConfig) error {
137 pCtx := ctx
138 h := images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
139 if desc.MediaType != images.MediaTypeDockerSchema1Manifest {
140 fmt.Printf("fetching %v... %v\n", desc.Digest.String()[:15], desc.MediaType)
141 }
142 return nil, nil
143 })
144
145 log.G(pCtx).WithField("image", ref).Debug("fetching")
146 labels := commands.LabelArgs(config.Labels)
147 if _, err := client.Pull(pCtx, ref, []containerd.RemoteOpt{
148 containerd.WithPullLabels(labels),
149 containerd.WithResolver(config.Resolver),
150 containerd.WithImageHandler(h),
151 //nolint:staticcheck
152 containerd.WithSchema1Conversion, //lint:ignore SA1019
153 containerd.WithPullUnpack,
154 containerd.WithPlatform(config.platform),
155 containerd.WithPullSnapshotter(config.snapshotter),
156 containerd.WithImageHandlerWrapper(source.AppendDefaultLabelsHandlerWrapper(
157 config.indexDigest, ctdsnapshotters.AppendInfoHandlerWrapper(ref))),
158 }...); err != nil {
159 return err
160 }
161
162 return nil
163 }