Skip to content

Commit 28ea830

Browse files
committed
feat(initrd): Mount project-scope directory for buildkit caching
GitHub-Fixes: #2302 Signed-off-by: Cezar Craciunoiu <[email protected]>
1 parent feae8b1 commit 28ea830

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

initrd/dockerfile.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"kraftkit.sh/config"
2626
"kraftkit.sh/cpio"
2727
"kraftkit.sh/log"
28+
"kraftkit.sh/unikraft"
2829

2930
sfile "github.com/anchore/stereoscope/pkg/file"
3031
soci "github.com/anchore/stereoscope/pkg/image/oci"
@@ -215,7 +216,20 @@ func (initrd *dockerfile) Name() string {
215216
return "Dockerfile"
216217
}
217218

218-
func startBuildkit(ctx context.Context, buildkitVersion string, port int, printf *testcontainersPrintf) (testcontainers.Container, error) {
219+
func startBuildkit(ctx context.Context, buildkitVersion, workdir string, port int, printf *testcontainersPrintf) (testcontainers.Container, error) {
220+
cachePath := filepath.Join(workdir, unikraft.VendorDir, "buildkit-cache")
221+
if !filepath.IsAbs(cachePath) {
222+
var err error
223+
cachePath, err = filepath.Abs(cachePath)
224+
if err != nil {
225+
return nil, fmt.Errorf("could not get absolute path for buildkit cache: %w", err)
226+
}
227+
}
228+
229+
if err := os.MkdirAll(cachePath, 0o755); err != nil {
230+
return nil, fmt.Errorf("could not create buildkit cache directory: %w", err)
231+
}
232+
219233
// Trap any panics that occur when instantiating BuildKit through the
220234
// testcontainers library. This is known happen if Docker is not installed.
221235
// For more information see:
@@ -242,8 +256,8 @@ func startBuildkit(ctx context.Context, buildkitVersion string, port int, printf
242256
Cmd: []string{"--addr", fmt.Sprintf("tcp://0.0.0.0:%d", port)},
243257
Mounts: testcontainers.ContainerMounts{
244258
{
245-
Source: testcontainers.GenericVolumeMountSource{
246-
Name: "kraftkit-buildkit-cache",
259+
Source: testcontainers.GenericBindMountSource{
260+
HostPath: cachePath,
247261
},
248262
Target: "/var/lib/buildkit",
249263
},
@@ -342,7 +356,7 @@ func (initrd *dockerfile) Build(ctx context.Context) (string, error) {
342356
port := l.Addr().(*net.TCPAddr).Port
343357
_ = l.Close()
344358

345-
buildkitd, err := startBuildkit(ctx, buildkitVersion, port, printf)
359+
buildkitd, err := startBuildkit(ctx, buildkitVersion, initrd.opts.workdir, port, printf)
346360
if err != nil {
347361
return "", fmt.Errorf("creating buildkit container: %w", err)
348362
}

0 commit comments

Comments
 (0)