-
Notifications
You must be signed in to change notification settings - Fork 8
/
exportimage.go
63 lines (51 loc) · 1.69 KB
/
exportimage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package buildkit
import (
"context"
"io"
"os"
"path/filepath"
"github.com/moby/buildkit/client"
"namespacelabs.dev/foundation/internal/artifacts/oci"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/fnfs"
"namespacelabs.dev/foundation/internal/workspace/dirs"
"namespacelabs.dev/foundation/std/tasks"
)
type exporter[V any] interface {
Prepare(context.Context) error
Exports() []client.ExportEntry
Provide(context.Context, *client.SolveResponse) (V, error)
}
func exportToImage() exporter[oci.Image] { return &exportImage{} }
type exportImage struct {
output *os.File
}
func (e *exportImage) Prepare(ctx context.Context) error {
f, err := dirs.CreateUserTemp("buildkit", "image")
if err != nil {
return err
}
// ExportEntry below takes care of closing f.
e.output = f
compute.On(ctx).Cleanup(tasks.Action("buildkit.build-image.cleanup").Arg("name", f.Name()), func(ctx context.Context) error {
return os.Remove(f.Name())
})
return nil
}
func (e *exportImage) Exports() []client.ExportEntry {
return []client.ExportEntry{{
Type: client.ExporterOCI,
Attrs: map[string]string{
"buildinfo": "false", // Remove build info to keep reproducibility.
},
Output: func(_ map[string]string) (io.WriteCloser, error) {
return e.output, nil
},
}}
}
func (e *exportImage) Provide(ctx context.Context, _ *client.SolveResponse) (oci.Image, error) {
return oci.IngestFromFS(ctx, fnfs.Local(filepath.Dir(e.output.Name())), filepath.Base(e.output.Name()), false)
}