-
Notifications
You must be signed in to change notification settings - Fork 13
/
builld.go
194 lines (167 loc) 路 5.08 KB
/
builld.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package testenv
import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/exporter/containerimage/exptypes"
"github.com/moby/buildkit/frontend/dockerui"
gwclient "github.com/moby/buildkit/frontend/gateway/client"
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/solver/pb"
"github.com/moby/buildkit/util/progress/progressui"
"github.com/pkg/errors"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
)
func buildBaseFrontend(ctx context.Context, c gwclient.Client) (*gwclient.Result, error) {
dc, err := dockerui.NewClient(c)
if err != nil {
return nil, errors.Wrap(err, "error creating dockerui client")
}
buildCtx, err := dc.MainContext(ctx)
if err != nil {
return nil, errors.Wrap(err, "error getting main context")
}
def, err := buildCtx.Marshal(ctx)
if err != nil {
return nil, errors.Wrap(err, "error marshaling main context")
}
// Can't use the state from `MainContext` because it filters out
// whatever was in `.dockerignore`, which may include `Dockerfile`,
// which we need.
dockerfileDef, err := llb.Local(dockerui.DefaultLocalNameDockerfile, llb.IncludePatterns([]string{"Dockerfile"})).Marshal(ctx)
if err != nil {
return nil, errors.Wrap(err, "error marshaling Dockerfile context")
}
defPB := def.ToPB()
return c.Solve(ctx, gwclient.SolveRequest{
Frontend: "dockerfile.v0",
FrontendOpt: map[string]string{},
FrontendInputs: map[string]*pb.Definition{
dockerui.DefaultLocalNameContext: defPB,
dockerui.DefaultLocalNameDockerfile: dockerfileDef.ToPB(),
},
})
}
// InjectInput adds the neccessary options to a solve request to use the output of the provided build function as an input to the solve request.
func injectInput(ctx context.Context, res *gwclient.Result, id string, req *gwclient.SolveRequest) (retErr error) {
ctx, span := otel.Tracer("").Start(ctx, "build input "+id)
defer func() {
if retErr != nil {
span.SetStatus(codes.Error, retErr.Error())
}
span.End()
}()
ref, err := res.SingleRef()
if err != nil {
return err
}
st, err := ref.ToState()
if err != nil {
return err
}
dt := res.Metadata[exptypes.ExporterImageConfigKey]
if dt != nil {
st, err = st.WithImageConfig(dt)
if err != nil {
return err
}
}
def, err := st.Marshal(ctx)
if err != nil {
return err
}
if req.FrontendOpt == nil {
req.FrontendOpt = make(map[string]string)
}
req.FrontendOpt["context:"+id] = "input:" + id
if req.FrontendInputs == nil {
req.FrontendInputs = make(map[string]*pb.Definition)
}
req.FrontendInputs[id] = def.ToPB()
if dt != nil {
meta := map[string][]byte{
exptypes.ExporterImageConfigKey: dt,
}
metaDt, err := json.Marshal(meta)
if err != nil {
return errors.Wrap(err, "error marshaling local frontend metadata")
}
req.FrontendOpt["input-metadata:"+id] = string(metaDt)
}
return nil
}
// withDalecInput adds the neccessary options to a solve request to use
// the locally built frontend as an input to the solve request.
// This only works with buildkit >= 0.12
func withDalecInput(ctx context.Context, gwc gwclient.Client, opts *gwclient.SolveRequest) error {
id := identity.NewID()
res, err := buildBaseFrontend(ctx, gwc)
if err != nil {
return errors.Wrap(err, "error building local frontend")
}
if err := injectInput(ctx, res, id, opts); err != nil {
return errors.Wrap(err, "error adding local frontend as input")
}
opts.FrontendOpt["source"] = id
opts.Frontend = "gateway.v0"
return nil
}
func displaySolveStatus(ctx context.Context, t *testing.T) (chan *client.SolveStatus, <-chan struct{}) {
ch := make(chan *client.SolveStatus)
done := make(chan struct{})
display, err := progressui.NewDisplay(&testWriter{t}, progressui.AutoMode, progressui.WithPhase(t.Name()))
if err != nil {
t.Fatal(err)
}
go func() {
_, err := display.UpdateFrom(ctx, ch)
if err != nil {
t.Log(err)
}
close(done)
}()
return ch, done
}
type testWriter struct {
t *testing.T
}
func (t *testWriter) Write(p []byte) (n int, err error) {
t.t.Log(string(p))
return len(p), nil
}
// withProjectRoot adds the current project root as the build context for the solve request.
func withProjectRoot(t *testing.T, opts *client.SolveOpt) {
t.Helper()
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
projectRoot, err := lookupProjectRoot(cwd)
if err != nil {
t.Fatal(err)
}
if opts.LocalDirs == nil {
opts.LocalDirs = make(map[string]string)
}
opts.LocalDirs[dockerui.DefaultLocalNameContext] = projectRoot
opts.LocalDirs[dockerui.DefaultLocalNameDockerfile] = projectRoot
}
// lookupProjectRoot looks up the project root from the current working directory.
// This is needed so the test suite can be run from any directory within the project.
func lookupProjectRoot(cur string) (string, error) {
if _, err := os.Stat(filepath.Join(cur, "go.mod")); err != nil {
if cur == "/" || cur == "." {
return "", errors.Wrap(err, "could not find project root")
}
if os.IsNotExist(err) {
return lookupProjectRoot(filepath.Dir(cur))
}
return "", err
}
return cur, nil
}