Debian Codegraph @

nativeconverter/estargz/externaltoc/fetcher.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 externaltoc
18
19 import (
20 "context"
21 "fmt"
22 "io"
23
24 "github.com/containerd/containerd/platforms"
25 "github.com/containerd/containerd/reference"
26 "github.com/containerd/containerd/remotes"
27 "github.com/containerd/containerd/remotes/docker"
28 esgzexternaltoc "github.com/containerd/stargz-snapshotter/estargz/externaltoc"
29 "github.com/containerd/stargz-snapshotter/fs/source"
30 "github.com/containerd/stargz-snapshotter/util/containerdutil"
31 "github.com/opencontainers/go-digest"
32 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
33 )
34
35 func NewRemoteDecompressor(ctx context.Context, hosts source.RegistryHosts, refspec reference.Spec, desc ocispec.Descriptor) *esgzexternaltoc.GzipDecompressor {
36 return esgzexternaltoc.NewGzipDecompressor(func() ([]byte, error) {
37 resolver := docker.NewResolver(docker.ResolverOptions{
38 Hosts: func(host string) ([]docker.RegistryHost, error) {
39 if host != refspec.Hostname() {
40 return nil, fmt.Errorf("unexpected host %q for image ref %q", host, refspec.String())
41 }
42 return hosts(refspec)
43 },
44 })
45 return fetchTOCBlob(ctx, resolver, refspec, desc.Digest)
46 })
47 }
48
49 func fetchTOCBlob(ctx context.Context, resolver remotes.Resolver, refspec reference.Spec, dgst digest.Digest) ([]byte, error) {
50 // TODO: support custom location of TOC manifest and TOCs using annotations, etc.
51 tocImgRef, err := getTOCReference(refspec.String())
52 if err != nil {
53 return nil, err
54 }
55 _, img, err := resolver.Resolve(ctx, tocImgRef)
56 if err != nil {
57 return nil, err
58 }
59 fetcher, err := resolver.Fetcher(ctx, tocImgRef)
60 if err != nil {
61 return nil, err
62 }
63 // TODO: cache this manifest
64 manifest, err := containerdutil.FetchManifestPlatform(ctx, fetcher, img, platforms.DefaultSpec())
65 if err != nil {
66 return nil, err
67 }
68 return fetchTOCBlobFromManifest(ctx, fetcher, manifest, dgst)
69 }
70
71 func fetchTOCBlobFromManifest(ctx context.Context, fetcher remotes.Fetcher, manifest ocispec.Manifest, layerDigest digest.Digest) ([]byte, error) {
72 for _, l := range manifest.Layers {
73 if len(l.Annotations) == 0 {
74 continue
75 }
76 ldgst, ok := l.Annotations["containerd.io/snapshot/stargz/layer.digest"]
77 if !ok {
78 continue
79 }
80 if ldgst == layerDigest.String() {
81 r, err := fetcher.Fetch(ctx, l)
82 if err != nil {
83 return nil, err
84 }
85 defer r.Close()
86 return io.ReadAll(r)
87 }
88 }
89 return nil, fmt.Errorf("TOC not found")
90 }