Debian Codegraph @

buildkit/cache/remotecache/local/local.go

raw
1 package local
2
3 import (
4 "context"
5 "strconv"
6 "time"
7
8 "github.com/containerd/containerd/v2/core/content"
9 "github.com/moby/buildkit/cache/remotecache"
10 "github.com/moby/buildkit/session"
11 sessioncontent "github.com/moby/buildkit/session/content"
12 "github.com/moby/buildkit/util/compression"
13 digest "github.com/opencontainers/go-digest"
14 ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
15 "github.com/pkg/errors"
16 )
17
18 const (
19 attrDigest = "digest"
20 attrSrc = "src"
21 attrDest = "dest"
22 attrImageManifest = "image-manifest"
23 attrOCIMediatypes = "oci-mediatypes"
24 contentStoreIDPrefix = "local:"
25 )
26
27 type exporter struct {
28 remotecache.Exporter
29 }
30
31 func (*exporter) Name() string {
32 return "exporting cache to client directory"
33 }
34
35 // ResolveCacheExporterFunc for "local" cache exporter.
36 func ResolveCacheExporterFunc(sm *session.Manager) remotecache.ResolveCacheExporterFunc {
37 return func(ctx context.Context, g session.Group, attrs map[string]string) (remotecache.Exporter, error) {
38 store := attrs[attrDest]
39 if store == "" {
40 return nil, errors.New("local cache exporter requires dest")
41 }
42 compressionConfig, err := compression.ParseAttributes(attrs)
43 if err != nil {
44 return nil, err
45 }
46 ociMediatypes := true
47 if v, ok := attrs[attrOCIMediatypes]; ok {
48 b, err := strconv.ParseBool(v)
49 if err != nil {
50 return nil, errors.Wrapf(err, "failed to parse %s", attrOCIMediatypes)
51 }
52 ociMediatypes = b
53 }
54 imageManifest := true
55 if v, ok := attrs[attrImageManifest]; ok {
56 b, err := strconv.ParseBool(v)
57 if err != nil {
58 return nil, errors.Wrapf(err, "failed to parse %s", attrImageManifest)
59 }
60 imageManifest = b
61 } else if !ociMediatypes {
62 imageManifest = false
63 }
64
65 csID := contentStoreIDPrefix + store
66 cs, err := getContentStore(ctx, sm, g, csID)
67 if err != nil {
68 return nil, err
69 }
70 return &exporter{remotecache.NewExporter(cs, "", ociMediatypes, imageManifest, compressionConfig)}, nil
71 }
72 }
73
74 // ResolveCacheImporterFunc for "local" cache importer.
75 func ResolveCacheImporterFunc(sm *session.Manager) remotecache.ResolveCacheImporterFunc {
76 return func(ctx context.Context, g session.Group, attrs map[string]string) (remotecache.Importer, ocispecs.Descriptor, error) {
77 dgstStr := attrs[attrDigest]
78 if dgstStr == "" {
79 return nil, ocispecs.Descriptor{}, errors.New("local cache importer requires explicit digest")
80 }
81 dgst := digest.Digest(dgstStr)
82 store := attrs[attrSrc]
83 if store == "" {
84 return nil, ocispecs.Descriptor{}, errors.New("local cache importer requires src")
85 }
86 csID := contentStoreIDPrefix + store
87 cs, err := getContentStore(ctx, sm, g, csID)
88 if err != nil {
89 return nil, ocispecs.Descriptor{}, err
90 }
91 info, err := cs.Info(ctx, dgst)
92 if err != nil {
93 return nil, ocispecs.Descriptor{}, err
94 }
95 desc := ocispecs.Descriptor{
96 // MediaType is typically MediaTypeDockerSchema2ManifestList,
97 // but we leave it empty until we get correct support for local index.json
98 Digest: dgst,
99 Size: info.Size,
100 }
101 return remotecache.NewImporter(cs), desc, nil
102 }
103 }
104
105 func getContentStore(ctx context.Context, sm *session.Manager, g session.Group, storeID string) (content.Store, error) {
106 // TODO: to ensure correct session is detected, new api for finding if storeID is supported is needed
107 sessionID := g.SessionIterator().NextSession()
108 if sessionID == "" {
109 return nil, errors.New("local cache exporter/importer requires session")
110 }
111 timeoutCtx, cancel := context.WithCancelCause(ctx)
112 timeoutCtx, _ = context.WithTimeoutCause(timeoutCtx, 5*time.Second, errors.WithStack(context.DeadlineExceeded)) //nolint:govet
113 defer func() { cancel(errors.WithStack(context.Canceled)) }()
114
115 caller, err := sm.Get(timeoutCtx, sessionID, false)
116 if err != nil {
117 return nil, err
118 }
119 return &unlazyProvider{sessioncontent.NewCallerStore(caller, storeID), g}, nil
120 }
121
122 type unlazyProvider struct {
123 content.Store
124 s session.Group
125 }
126
127 func (p *unlazyProvider) UnlazySession(desc ocispecs.Descriptor) session.Group {
128 return p.s
129 }