Skip to content

Commit 3021836

Browse files
committed
local cache: add lazy option to skip eager layer extraction on import
Add a lazy=true option to the local cache importer (--import-cache type=local,src=...,lazy=true) that skips the unlazyProvider wrapping. In ephemeral/daemonless buildkitd setups, this avoids eagerly extracting all cached layers on every build. Signed-off-by: Jiří Moravčík <jiri.moravcik@gmail.com>
1 parent 5e3efd4 commit 3021836

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ The directory layout conforms to OCI Image Spec v1.0.
503503
* `src=<path>`: source directory for cache importer
504504
* `tag=<tag>`: specify custom tag of image to read from local index (default: `latest`)
505505
* `digest=sha256:<sha256digest>`: specify explicit digest of the manifest list to import
506+
* `lazy=<true|false>`: when `true`, imported cache layers are not eagerly extracted but loaded on demand (default: `false`). This is useful for ephemeral/daemonless `buildkitd` setups where each build uses its own daemon instance, avoiding unnecessary extraction overhead for cached builds.
506507

507508
#### GitHub Actions cache (experimental)
508509

cache/remotecache/local/local.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const (
1919
attrDigest = "digest"
2020
attrSrc = "src"
2121
attrDest = "dest"
22+
attrLazy = "lazy"
2223
attrImageManifest = "image-manifest"
2324
attrOCIMediatypes = "oci-mediatypes"
2425
contentStoreIDPrefix = "local:"
@@ -63,7 +64,7 @@ func ResolveCacheExporterFunc(sm *session.Manager) remotecache.ResolveCacheExpor
6364
}
6465

6566
csID := contentStoreIDPrefix + store
66-
cs, err := getContentStore(ctx, sm, g, csID)
67+
cs, err := getContentStore(ctx, sm, g, csID, false)
6768
if err != nil {
6869
return nil, err
6970
}
@@ -83,8 +84,16 @@ func ResolveCacheImporterFunc(sm *session.Manager) remotecache.ResolveCacheImpor
8384
if store == "" {
8485
return nil, ocispecs.Descriptor{}, errors.New("local cache importer requires src")
8586
}
87+
lazy := false
88+
if v, ok := attrs[attrLazy]; ok {
89+
b, err := strconv.ParseBool(v)
90+
if err != nil {
91+
return nil, ocispecs.Descriptor{}, errors.Wrapf(err, "failed to parse %s", attrLazy)
92+
}
93+
lazy = b
94+
}
8695
csID := contentStoreIDPrefix + store
87-
cs, err := getContentStore(ctx, sm, g, csID)
96+
cs, err := getContentStore(ctx, sm, g, csID, lazy)
8897
if err != nil {
8998
return nil, ocispecs.Descriptor{}, err
9099
}
@@ -102,7 +111,7 @@ func ResolveCacheImporterFunc(sm *session.Manager) remotecache.ResolveCacheImpor
102111
}
103112
}
104113

105-
func getContentStore(ctx context.Context, sm *session.Manager, g session.Group, storeID string) (content.Store, error) {
114+
func getContentStore(ctx context.Context, sm *session.Manager, g session.Group, storeID string, lazy bool) (content.Store, error) {
106115
// TODO: to ensure correct session is detected, new api for finding if storeID is supported is needed
107116
sessionID := g.SessionIterator().NextSession()
108117
if sessionID == "" {
@@ -116,7 +125,11 @@ func getContentStore(ctx context.Context, sm *session.Manager, g session.Group,
116125
if err != nil {
117126
return nil, err
118127
}
119-
return &unlazyProvider{sessioncontent.NewCallerStore(caller, storeID), g}, nil
128+
cs := sessioncontent.NewCallerStore(caller, storeID)
129+
if lazy {
130+
return cs, nil
131+
}
132+
return &unlazyProvider{cs, g}, nil
120133
}
121134

122135
type unlazyProvider struct {

docs/reference/buildctl.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,4 @@ For example:
253253

254254
* `--import-cache type=registry,ref=example.com/foo/bar` - import into the cache from an OCI image.
255255
* `--import-cache type=local,src=path/to/dir` - import into the cache from a directory local to where `buildctl` is running.
256+
* `--import-cache type=local,src=path/to/dir,lazy=true` - import with lazy layer loading (layers are not eagerly extracted; useful for ephemeral/daemonless buildkitd).

0 commit comments

Comments
 (0)