-
Notifications
You must be signed in to change notification settings - Fork 9
/
binary.go
309 lines (252 loc) · 8.44 KB
/
binary.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// 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 cuefrontend
import (
"bytes"
"context"
"encoding/json"
"cuelang.org/go/cue"
"github.com/docker/go-units"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/frontend/fncue"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/internal/parsing/invariants"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
type cueBinary struct {
Name string `json:"name,omitempty"`
Config *schema.BinaryConfig `json:"config,omitempty"`
From *cueImageBuildPlan `json:"from,omitempty"`
BuildPlan *cueLayeredImageBuildPlan `json:"build_plan,omitempty"`
Labels map[string]string `json:"labels"`
}
type cueLayeredImageBuildPlan struct {
LayerBuildPlan []*cueImageBuildPlan
}
var _ json.Unmarshaler = &cueLayeredImageBuildPlan{}
type cueImageBuildPlan struct {
Prebuilt string `json:"prebuilt,omitempty"`
GoPackage string `json:"go_package,omitempty"`
GoBuild *schema.ImageBuildPlan_GoBuild `json:"go_build,omitempty"`
Dockerfile string `json:"dockerfile,omitempty"`
LlbPlan *cueImageBuildPlan_LLBPlan `json:"llb_plan,omitempty"`
NixFlake string `json:"nix_flake,omitempty"`
Deprecated_SnapshotFiles []string `json:"snapshot_files,omitempty"` // Use `files` instead.
Files []string `json:"files,omitempty"`
AlpineBuild *schema.ImageBuildPlan_AlpineBuild `json:"alpine_build,omitempty"`
NodejsBuild *schema.NodejsBuild `json:"nodejs_build,omitempty"`
Binary string `json:"binary,omitempty"`
FilesFrom *cueImageBuildPlan_FilesFrom `json:"files_from,omitempty"`
MakeSquashFS *cueImageBuildPlan_MakeSquashFS `json:"make_squashfs,omitempty"`
MakeFilesystemImage *cueImageBuildPlan_MakeFilesystemImage `json:"make_fs_image,omitempty"`
ImageID string `json:"image_id,omitempty"`
}
type cueImageBuildPlan_LLBPlan struct {
OutputOf cueBinary `json:"output_of,omitempty"`
}
type cueImageBuildPlan_FilesFrom struct {
From cueImageBuildPlan `json:"from"`
Files []string `json:"files"`
TargetDir string `json:"target_dir"`
}
type cueImageBuildPlan_MakeSquashFS struct {
From cueImageBuildPlan `json:"from"`
Target string `json:"target"`
}
type cueImageBuildPlan_MakeFilesystemImage struct {
From cueImageBuildPlan `json:"from"`
Target string `json:"target"`
Kind string `json:"kind"`
Size string `json:"size"`
}
func parseCueBinary(ctx context.Context, pl parsing.EarlyPackageLoader, loc pkggraph.Location, parent, v *fncue.CueV) (*schema.Binary, error) {
// Ensure all fields are bound.
if err := v.Val.Validate(cue.Concrete(true)); err != nil {
return nil, err
}
var srcBin cueBinary
if err := v.Val.Decode(&srcBin); err != nil {
return nil, err
}
bin, err := srcBin.ToSchema(ctx, pl, loc)
if err != nil {
return nil, err
}
return bin, nil
}
func (srcBin cueBinary) ToSchema(ctx context.Context, pl parsing.EarlyPackageLoader, loc pkggraph.Location) (*schema.Binary, error) {
bin := &schema.Binary{
Name: srcBin.Name,
Config: srcBin.Config,
}
if srcBin.BuildPlan != nil {
var err error
bin.BuildPlan, err = srcBin.BuildPlan.ToSchema(ctx, pl, loc)
if err != nil {
return nil, err
}
}
if srcBin.From != nil {
if srcBin.BuildPlan != nil {
return nil, fnerrors.NewWithLocation(loc, "from and build_plan are exclusive -- only one can be set")
}
parsed, err := srcBin.From.ToSchema(ctx, pl, loc)
if err != nil {
return nil, err
}
bin.BuildPlan = &schema.LayeredImageBuildPlan{
LayerBuildPlan: []*schema.ImageBuildPlan{parsed},
}
}
for k, v := range srcBin.Labels {
bin.Labels = append(bin.Labels, &schema.Label{Name: k, Value: v})
}
bin.Labels = sortLabels(bin.Labels)
return bin, nil
}
func (lbp *cueLayeredImageBuildPlan) UnmarshalJSON(data []byte) error {
dec := json.NewDecoder(bytes.NewReader(data))
tok, err := dec.Token()
if err != nil {
return err
}
switch tok {
case json.Delim('['):
return json.Unmarshal(data, &lbp.LayerBuildPlan)
case json.Delim('{'):
var x struct {
LayerBuildPlan []*cueImageBuildPlan `json:"layer_build_plan,omitempty"`
}
if err := json.Unmarshal(data, &x); err != nil {
return err
}
lbp.LayerBuildPlan = x.LayerBuildPlan
return nil
default:
return fnerrors.BadInputError("unexpected input, expected array or object")
}
}
func (lbp *cueLayeredImageBuildPlan) ToSchema(ctx context.Context, pl parsing.EarlyPackageLoader, loc pkggraph.Location) (*schema.LayeredImageBuildPlan, error) {
plan := &schema.LayeredImageBuildPlan{}
for _, def := range lbp.LayerBuildPlan {
parsed, err := def.ToSchema(ctx, pl, loc)
if err != nil {
return nil, err
}
plan.LayerBuildPlan = append(plan.LayerBuildPlan, parsed)
}
return plan, nil
}
func (bp cueImageBuildPlan) ToSchema(ctx context.Context, pl parsing.EarlyPackageLoader, loc pkggraph.Location) (*schema.ImageBuildPlan, error) {
plan := &schema.ImageBuildPlan{}
var set []string
if bp.Prebuilt != "" {
plan.ImageId = bp.Prebuilt
set = append(set, "prebuilt")
}
if bp.GoPackage != "" {
plan.GoPackage = bp.GoPackage
set = append(set, "go_package")
}
if bp.GoBuild != nil {
plan.GoBuild = bp.GoBuild
set = append(set, "go_build")
}
if bp.Dockerfile != "" {
plan.Dockerfile = bp.Dockerfile
set = append(set, "dockerfile")
}
if bp.NodejsBuild != nil {
plan.NodejsBuild = bp.NodejsBuild
set = append(set, "nodejs_build")
}
if bp.LlbPlan != nil {
p, err := bp.LlbPlan.OutputOf.ToSchema(ctx, pl, loc)
if err != nil {
return nil, err
}
plan.LlbPlan = &schema.ImageBuildPlan_LLBPlan{OutputOf: p}
set = append(set, "llb_plan")
}
if bp.NixFlake != "" {
plan.NixFlake = bp.NixFlake
set = append(set, "nix_flake")
}
if bp.Deprecated_SnapshotFiles != nil {
plan.SnapshotFiles = bp.Deprecated_SnapshotFiles
set = append(set, "snapshot_files")
}
if bp.Files != nil {
plan.SnapshotFiles = bp.Files
set = append(set, "files")
}
if bp.AlpineBuild != nil {
plan.AlpineBuild = bp.AlpineBuild
set = append(set, "alpine_build")
}
if bp.FilesFrom != nil {
from, err := bp.FilesFrom.From.ToSchema(ctx, pl, loc)
if err != nil {
return nil, err
}
plan.FilesFrom = &schema.ImageBuildPlan_FilesFrom{
From: from,
Files: bp.FilesFrom.Files,
TargetDir: bp.FilesFrom.TargetDir,
}
set = append(set, "files_from")
}
if bp.MakeSquashFS != nil {
from, err := bp.MakeSquashFS.From.ToSchema(ctx, pl, loc)
if err != nil {
return nil, err
}
plan.MakeSquashfs = &schema.ImageBuildPlan_MakeSquashFS{
From: from,
Target: bp.MakeSquashFS.Target,
}
set = append(set, "make_squashfs")
}
if bp.MakeFilesystemImage != nil {
from, err := bp.MakeFilesystemImage.From.ToSchema(ctx, pl, loc)
if err != nil {
return nil, err
}
plan.MakeFsImage = &schema.ImageBuildPlan_MakeFilesystemImage{
From: from,
Target: bp.MakeFilesystemImage.Target,
Kind: bp.MakeFilesystemImage.Kind,
}
if bp.MakeFilesystemImage.Size != "" {
sizeBytes, err := units.FromHumanSize(bp.MakeFilesystemImage.Size)
if err != nil {
return nil, err
}
plan.MakeFsImage.Size = sizeBytes
}
set = append(set, "make_fs_image")
}
if bp.Binary != "" {
ref, err := schema.StrictParsePackageRef(bp.Binary)
if err != nil {
return nil, err
}
if err := invariants.EnsurePackageLoaded(ctx, pl, loc.PackageName, ref); err != nil {
return nil, err
}
plan.Binary = ref
set = append(set, "binary")
}
if bp.ImageID != "" {
plan.ImageId = bp.ImageID
set = append(set, "image_id")
}
if len(set) == 0 {
return nil, fnerrors.NewWithLocation(loc, "plan is missing at least one instruction")
} else if len(set) > 1 {
return nil, fnerrors.NewWithLocation(loc, "build plan must include exactly one instruction, saw %v", set)
}
return plan, nil
}