-
Notifications
You must be signed in to change notification settings - Fork 1
/
frontend.go
170 lines (153 loc) · 4.91 KB
/
frontend.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
package llblib
import (
"context"
"encoding/json"
"github.com/moby/buildkit/client/llb"
gateway "github.com/moby/buildkit/frontend/gateway/client"
"github.com/moby/buildkit/solver/pb"
specsv1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
)
// FrontendOption can be used to modify a Frontend request.
type FrontendOption interface {
SetFrontendOption(*frontendOptions)
}
type frontendOptionFunc func(*frontendOptions)
func (f frontendOptionFunc) SetFrontendOption(fo *frontendOptions) {
f(fo)
}
// FrontendInput will attach the provided llb.State with the given name to
// the Frontend request.
func FrontendInput(name string, st llb.State) FrontendOption {
return frontendOptionFunc(func(fo *frontendOptions) {
fo.Inputs[name] = st
})
}
// FrontendOpt will add the name/value pair to the Opts for the Frontend
// request.
func FrontendOpt(name, value string) FrontendOption {
return frontendOptionFunc(func(fo *frontendOptions) {
fo.Opts[name] = value
})
}
// WithCustomName allows using the provided text for the progress display when
// solving the Frontend request.
func WithCustomName(name string) FrontendOption {
return frontendOptionFunc(func(fo *frontendOptions) {
fo.ConstraintsOpts = append(fo.ConstraintsOpts, llb.WithCustomName(name))
})
}
type frontendOptions struct {
Inputs map[string]llb.State
Opts map[string]string
ConstraintsOpts []llb.ConstraintsOpt
}
type constraintsToOptions struct {
NullOption
source *llb.Constraints
opts []llb.ConstraintsOpt
}
func (o constraintsToOptions) SetConstraintsOption(c *llb.Constraints) {
if o.source != nil {
*c = *o.source
}
for _, opt := range o.opts {
opt.SetConstraintsOption(c)
}
}
// Frontend will create an llb.State that is created via a frontend Request.
// One common frontend is the `docker/dockerfile` frontend that is used
// by `docker buildx` commands. The `source` argument is the image ref
// that is run as the frontend. A Frontend request is the same as
// using the `#syntax` directive in a Dockerfile. For example:
//
// image := llblib.Frontend("docker/dockerfile",
// llblib.FrontendInput("context", context),
// llblib.FrontendInput("dockerfile", dockerfile),
// )
func Frontend(source string, opts ...FrontendOption) llb.State {
// "gateway.v0" is used to trampoline the solve to the source docker image
// however "dockerfile.v0" is a built-in frontend, so we dont need to
// trampoline and can directly use it.
frontend := "gateway.v0"
if source == "dockerfile.v0" {
frontend = source
}
return llb.Scratch().Async(func(ctx context.Context, st llb.State, constraints *llb.Constraints) (llb.State, error) {
fo := frontendOptions{
Inputs: map[string]llb.State{},
Opts: map[string]string{},
}
if source != "dockerfile.v0" {
fo.Opts["source"] = source
}
for _, opt := range opts {
opt.SetFrontendOption(&fo)
}
sess := LoadSession(ctx)
if sess == nil {
return llb.Scratch(), errors.Errorf("frontend solve request without active session")
}
var constrainOpt llb.ConstraintsOpt = constraintsToOptions{
source: constraints,
opts: fo.ConstraintsOpts,
}
var result llb.State
req := Request{
buildFunc: func(ctx context.Context, c gateway.Client) (*gateway.Result, error) {
inputs := map[string]*pb.Definition{}
for name, input := range fo.Inputs {
def, err := input.Marshal(ctx, constrainOpt)
if err != nil {
return nil, err
}
// only add inputs that are non-nil (ie dont add scratch)
// because otherwise we will get a solve error:
// cannot marshal empty definition op
if def.Def != nil {
inputs[name] = def.ToPB()
}
}
req := gateway.SolveRequest{
Frontend: frontend,
FrontendOpt: fo.Opts,
FrontendInputs: inputs,
}
res, err := c.Solve(ctx, req)
if err != nil {
return nil, errors.Wrap(err, "failed to solve frontend request")
}
ref, err := res.SingleRef()
if err != nil {
return nil, errors.Wrap(err, "failed to extract ref from frontend request")
}
if ref == nil {
result = llb.Scratch()
} else {
result, err = ref.ToState()
if err != nil {
return nil, errors.Wrap(err, "failed to convert ref to state")
}
if config, ok := res.Metadata["containerimage.config"]; ok {
result, err = result.WithImageConfig(config)
if err != nil {
return nil, errors.Wrap(err, "failed to apply image config from frontend request")
}
// we need to parse the document again bc WithImageConfig
// does not apply the USER config.
var img specsv1.Image
if err := json.Unmarshal(config, &img); err != nil {
return nil, errors.Wrap(err, "failed to parse config from frontend request")
}
if img.Config.User != "" {
result = result.User(img.Config.User)
}
}
}
return nil, nil
},
}
_, err := sess.Do(ctx, req)
return result, err
})
}