-
Notifications
You must be signed in to change notification settings - Fork 9
/
exportimage.go
144 lines (115 loc) · 3.54 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// 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/google/go-containerregistry/pkg/v1/layout"
"github.com/moby/buildkit/client"
"namespacelabs.dev/foundation/internal/artifacts/oci"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/fnfs"
"namespacelabs.dev/foundation/internal/workspace/dirs"
"namespacelabs.dev/foundation/std/tasks"
)
const KeySourceDateEpoch = "source-date-epoch"
type exporter[V any] interface {
Kind() string
Prepare(context.Context) error
Exports() []client.ExportEntry
Provide(context.Context, *client.SolveResponse, builtkitOpts) (V, error)
}
func exportToImage(opts builtkitOpts) exporter[oci.Image] {
if opts.SupportsCanonicalBuilds {
return &exportOCILayout{}
}
return &exportImage{}
}
type exportImage struct {
output *os.File
}
func (e *exportImage) Kind() string { return "image" }
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.
KeySourceDateEpoch: "0",
},
Output: func(_ map[string]string) (io.WriteCloser, error) {
return e.output, nil
},
}}
}
func (e *exportImage) Provide(ctx context.Context, _ *client.SolveResponse, opts builtkitOpts) (oci.Image, error) {
image, err := oci.IngestFromFS(ctx, fnfs.Local(filepath.Dir(e.output.Name())), filepath.Base(e.output.Name()), false)
if err != nil {
return nil, err
}
if opts.SupportsCanonicalBuilds {
return image, nil
}
return oci.Canonical(ctx, image)
}
type exportOCILayout struct {
output string
}
func (e *exportOCILayout) Kind() string { return "oci-layout" }
func (e *exportOCILayout) Prepare(ctx context.Context) error {
f, err := dirs.CreateUserTempDir("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("dir", f), func(ctx context.Context) error {
return os.RemoveAll(f)
})
return nil
}
func (e *exportOCILayout) Exports() []client.ExportEntry {
return []client.ExportEntry{{
Type: client.ExporterOCI,
Attrs: map[string]string{
"buildinfo": "false", // Remove build info to keep reproducibility.
"tar": "false",
KeySourceDateEpoch: "0",
},
OutputDir: e.output,
}}
}
func (e *exportOCILayout) Provide(ctx context.Context, _ *client.SolveResponse, _ builtkitOpts) (oci.Image, error) {
index, err := layout.ImageIndexFromPath(e.output)
if err != nil {
return nil, err
}
idx, err := index.IndexManifest()
if err != nil {
return nil, err
}
if len(idx.Manifests) != 1 {
return nil, fnerrors.InternalError("buildkit: expected a single image, saw %d", len(idx.Manifests))
}
img, err := index.Image(idx.Manifests[0].Digest)
if err != nil {
return nil, err
}
oci.AttachDigestToAction(ctx, img)
return img, nil
}