-
Notifications
You must be signed in to change notification settings - Fork 23
/
load.go
709 lines (603 loc) · 17.5 KB
/
load.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
package dalec
import (
goerrors "errors"
"fmt"
"os"
"path"
"strings"
"github.com/goccy/go-yaml"
"github.com/moby/buildkit/frontend/dockerfile/shell"
"github.com/moby/buildkit/frontend/dockerui"
"github.com/pkg/errors"
"golang.org/x/exp/maps"
)
func knownArg(key string) bool {
switch key {
case "BUILDKIT_SYNTAX":
return true
case "DALEC_DISABLE_DIFF_MERGE":
return true
case "DALEC_SKIP_SIGNING":
return true
case "DALEC_SIGNING_CONFIG_CONTEXT_NAME":
return true
case "DALEC_SIGNING_CONFIG_PATH":
return true
case "SOURCE_DATE_EPOCH":
return true
case "DALEC_SKIP_TESTS":
return true
}
return platformArg(key)
}
func platformArg(key string) bool {
switch key {
case "TARGETOS", "TARGETARCH", "TARGETPLATFORM", "TARGETVARIANT",
"BUILDOS", "BUILDARCH", "BUILDPLATFORM", "BUILDVARIANT":
return true
default:
return false
}
}
const DefaultPatchStrip int = 1
type envGetterMap map[string]string
func (m envGetterMap) Get(key string) (string, bool) {
v, ok := m[key]
return v, ok
}
func (m envGetterMap) Keys() []string {
return maps.Keys(m)
}
func expandArgs(lex *shell.Lex, s string, args map[string]string) (string, error) {
result, err := lex.ProcessWordWithMatches(s, envGetterMap(args))
if err != nil {
return "", err
}
var errs []error
for m := range result.Unmatched {
if !knownArg(m) {
errs = append(errs, fmt.Errorf(`build arg "%s" not declared`, m))
continue
}
if platformArg(m) {
errs = append(errs, fmt.Errorf(`opt-in arg "%s" not present in args`, m))
}
}
return result.Result, goerrors.Join(errs...)
}
func (s *Source) substituteBuildArgs(args map[string]string) error {
lex := shell.NewLex('\\')
// force the shell lexer to skip unresolved env vars so they aren't
// replaced with ""
lex.SkipUnsetEnv = true
var errs []error
appendErr := func(err error) {
errs = append(errs, err)
}
switch {
case s.DockerImage != nil:
updated, err := expandArgs(lex, s.DockerImage.Ref, args)
if err != nil {
appendErr(fmt.Errorf("error performing shell expansion on docker image ref: %w", err))
}
s.DockerImage.Ref = updated
if s.DockerImage.Cmd != nil {
for _, mnt := range s.DockerImage.Cmd.Mounts {
err := mnt.Spec.substituteBuildArgs(args)
if err != nil {
appendErr(fmt.Errorf("error performing shell expansion on docker image mount: %w", err))
}
}
}
case s.Git != nil:
updated, err := expandArgs(lex, s.Git.URL, args)
s.Git.URL = updated
if err != nil {
appendErr(err)
}
updated, err = expandArgs(lex, s.Git.Commit, args)
s.Git.Commit = updated
if err != nil {
appendErr(err)
}
case s.HTTP != nil:
updated, err := expandArgs(lex, s.HTTP.URL, args)
if err != nil {
appendErr(err)
}
s.HTTP.URL = updated
case s.Context != nil:
updated, err := expandArgs(lex, s.Context.Name, args)
s.Context.Name = updated
if err != nil {
appendErr(err)
}
case s.Build != nil:
err := s.Build.Source.substituteBuildArgs(args)
if err != nil {
appendErr(err)
}
updated, err := expandArgs(lex, s.Build.DockerfilePath, args)
if err != nil {
appendErr(err)
}
s.Build.DockerfilePath = updated
updated, err = expandArgs(lex, s.Build.Target, args)
if err != nil {
appendErr(err)
}
s.Build.Target = updated
}
return goerrors.Join(errs...)
}
func fillDefaults(s *Source) {
switch {
case s.DockerImage != nil:
if s.DockerImage.Cmd != nil {
for _, mnt := range s.DockerImage.Cmd.Mounts {
fillDefaults(&mnt.Spec)
}
}
case s.Git != nil:
case s.HTTP != nil:
case s.Context != nil:
if s.Context.Name == "" {
s.Context.Name = dockerui.DefaultLocalNameContext
}
case s.Build != nil:
fillDefaults(&s.Build.Source)
case s.Inline != nil:
}
}
func (s *Source) validate(failContext ...string) (retErr error) {
count := 0
defer func() {
if retErr != nil && failContext != nil {
retErr = errors.Wrap(retErr, strings.Join(failContext, " "))
}
}()
for _, g := range s.Generate {
if err := g.Validate(); err != nil {
retErr = goerrors.Join(retErr, err)
}
}
if s.DockerImage != nil {
if s.DockerImage.Ref == "" {
retErr = goerrors.Join(retErr, fmt.Errorf("docker image source variant must have a ref"))
}
if s.DockerImage.Cmd != nil {
// If someone *really* wants to extract the entire rootfs, they need to say so explicitly.
// We won't fill this in for them, particularly because this is almost certainly not the user's intent.
if s.Path == "" {
retErr = goerrors.Join(retErr, errors.Errorf("source path cannot be empty"))
}
for _, mnt := range s.DockerImage.Cmd.Mounts {
if err := mnt.validate(s.Path); err != nil {
retErr = goerrors.Join(retErr, err)
}
if err := mnt.Spec.validate("docker image source with ref", "'"+s.DockerImage.Ref+"'"); err != nil {
retErr = goerrors.Join(retErr, err)
}
}
}
count++
}
if s.Git != nil {
count++
}
if s.HTTP != nil {
if err := s.HTTP.validate(); err != nil {
retErr = goerrors.Join(retErr, err)
}
count++
}
if s.Context != nil {
count++
}
if s.Build != nil {
c := s.Build.DockerfilePath
if err := s.Build.validate("build source with dockerfile", "`"+c+"`"); err != nil {
retErr = goerrors.Join(retErr, err)
}
count++
}
if s.Inline != nil {
if err := s.Inline.validate(s.Path); err != nil {
retErr = goerrors.Join(retErr, err)
}
count++
}
switch count {
case 0:
retErr = goerrors.Join(retErr, fmt.Errorf("no non-nil source variant"))
case 1:
return retErr
default:
retErr = goerrors.Join(retErr, fmt.Errorf("more than one source variant defined"))
}
return retErr
}
var errUnknownArg = errors.New("unknown arg")
func (s *Spec) SubstituteArgs(env map[string]string) error {
lex := shell.NewLex('\\')
// force the shell lexer to skip unresolved env vars so they aren't
// replaced with ""
lex.SkipUnsetEnv = true
var errs []error
appendErr := func(err error) {
errs = append(errs, err)
}
args := make(map[string]string)
for k, v := range s.Args {
args[k] = v
}
for k, v := range env {
if _, ok := args[k]; !ok {
if !knownArg(k) {
appendErr(fmt.Errorf("%w: %q", errUnknownArg, k))
}
// if the build arg isn't present in args by opt-in, skip
// and don't automatically inject a value
continue
}
args[k] = v
}
for name, src := range s.Sources {
if err := src.substituteBuildArgs(args); err != nil {
appendErr(fmt.Errorf("error performing shell expansion on source %q: %w", name, err))
}
if src.DockerImage != nil {
if err := src.DockerImage.Cmd.processBuildArgs(lex, args, name); err != nil {
appendErr(fmt.Errorf("error performing shell expansion on source %q: %w", name, err))
}
}
s.Sources[name] = src
}
updated, err := expandArgs(lex, s.Version, args)
if err != nil {
appendErr(fmt.Errorf("error performing shell expansion on version: %w", err))
}
s.Version = updated
updated, err = expandArgs(lex, s.Revision, args)
if err != nil {
appendErr(fmt.Errorf("error performing shell expansion on revision: %w", err))
}
s.Revision = updated
for k, v := range s.Build.Env {
updated, err := expandArgs(lex, v, args)
if err != nil {
appendErr(fmt.Errorf("error performing shell expansion on env var %q: %w", k, err))
}
s.Build.Env[k] = updated
}
if s.Build.NetworkMode != "" {
updated, err := expandArgs(lex, s.Build.NetworkMode, args)
if err != nil {
appendErr(fmt.Errorf("error performing shell expansion on build network mode: %s: %w", s.Build.NetworkMode, err))
}
s.Build.NetworkMode = updated
}
for i, step := range s.Build.Steps {
bs := &step
if err := bs.processBuildArgs(lex, args, i); err != nil {
appendErr(fmt.Errorf("error performing shell expansion on build step %d: %w", i, err))
}
s.Build.Steps[i] = *bs
}
for _, t := range s.Tests {
if err := t.processBuildArgs(lex, args, t.Name); err != nil {
appendErr(fmt.Errorf("error performing shell expansion on test %q: %w", t.Name, err))
}
}
for name, t := range s.Targets {
if err := t.processBuildArgs(name, lex, args); err != nil {
appendErr(fmt.Errorf("error processing build args for target %q: %w", name, err))
}
}
if s.PackageConfig != nil {
if err := s.PackageConfig.processBuildArgs(lex, args); err != nil {
appendErr(fmt.Errorf("could not process build args for base spec package config: %w", err))
}
}
return goerrors.Join(errs...)
}
// LoadSpec loads a spec from the given data.
func LoadSpec(dt []byte) (*Spec, error) {
var spec Spec
dt, err := stripXFields(dt)
if err != nil {
return nil, fmt.Errorf("error stripping x-fields: %w", err)
}
if err := yaml.UnmarshalWithOptions(dt, &spec, yaml.Strict()); err != nil {
return nil, fmt.Errorf("error unmarshalling spec: %w", err)
}
if err := spec.Validate(); err != nil {
return nil, err
}
spec.FillDefaults()
return &spec, nil
}
func stripXFields(dt []byte) ([]byte, error) {
var obj map[string]interface{}
if err := yaml.Unmarshal(dt, &obj); err != nil {
return nil, fmt.Errorf("error unmarshalling spec: %w", err)
}
for k := range obj {
if strings.HasPrefix(k, "x-") || strings.HasPrefix(k, "X-") {
delete(obj, k)
}
}
return yaml.Marshal(obj)
}
func (s *BuildStep) processBuildArgs(lex *shell.Lex, args map[string]string, i int) error {
var errs []error
for k, v := range s.Env {
updated, err := expandArgs(lex, v, args)
if err != nil {
errs = append(errs, fmt.Errorf("error performing shell expansion on env var %q for step %d: %w", k, i, err))
}
s.Env[k] = updated
}
return goerrors.Join(errs...)
}
func (c *Command) processBuildArgs(lex *shell.Lex, args map[string]string, name string) error {
if c == nil {
return nil
}
var errs []error
appendErr := func(err error) {
errs = append(errs, err)
}
for _, s := range c.Mounts {
if err := s.Spec.substituteBuildArgs(args); err != nil {
appendErr(fmt.Errorf("error performing shell expansion on source ref %q: %w", name, err))
}
}
for k, v := range c.Env {
updated, err := expandArgs(lex, v, args)
if err != nil {
appendErr(fmt.Errorf("error performing shell expansion on env var %q for source %q: %w", k, name, err))
}
c.Env[k] = updated
}
for i, step := range c.Steps {
for k, v := range step.Env {
updated, err := expandArgs(lex, v, args)
if err != nil {
appendErr(fmt.Errorf("error performing shell expansion on env var %q for source %q: %w", k, name, err))
}
step.Env[k] = updated
c.Steps[i] = step
}
}
return goerrors.Join(errs...)
}
func (s *Spec) FillDefaults() {
for name, src := range s.Sources {
fillDefaults(&src)
s.Sources[name] = src
}
for k, patches := range s.Patches {
for i, ps := range patches {
if ps.Strip != nil {
continue
}
strip := DefaultPatchStrip
s.Patches[k][i].Strip = &strip
}
}
if s.Dependencies != nil {
for i := range len(s.Dependencies.ExtraRepos) {
fillExtraRepoDefaults(&s.Dependencies.ExtraRepos[i])
}
}
}
func fillExtraRepoDefaults(extraRepo *PackageRepositoryConfig) {
if len(extraRepo.Envs) == 0 {
// default to all stages for the extra repo if unspecified
extraRepo.Envs = []string{"build", "install", "test"}
}
for configName := range extraRepo.Config {
configSource := extraRepo.Config[configName]
fillDefaults(&configSource)
extraRepo.Config[configName] = configSource
}
for keyName := range extraRepo.Keys {
keySource := extraRepo.Keys[keyName]
fillDefaults(&keySource)
// Default to 0644 permissions for gpg keys. This is because apt will will only import
// keys with a particular permission set.
if keySource.HTTP != nil {
keySource.HTTP.Permissions = 0644
}
extraRepo.Keys[keyName] = keySource
}
for _, mount := range extraRepo.Data {
fillDefaults(&mount.Spec)
}
}
func (s Spec) Validate() error {
var outErr error
for name, src := range s.Sources {
if strings.ContainsRune(name, os.PathSeparator) {
outErr = goerrors.Join(outErr, &InvalidSourceError{Name: name, Err: sourceNamePathSeparatorError})
}
if err := src.validate(); err != nil {
outErr = goerrors.Join(&InvalidSourceError{Name: name, Err: fmt.Errorf("error validating source ref %q: %w", name, err)})
}
if src.DockerImage != nil && src.DockerImage.Cmd != nil {
for p, cfg := range src.DockerImage.Cmd.CacheDirs {
if _, err := sharingMode(cfg.Mode); err != nil {
outErr = goerrors.Join(&InvalidSourceError{Name: name, Err: errors.Wrapf(err, "invalid sharing mode for source %q with cache mount at path %q", name, p)})
}
}
}
}
for _, t := range s.Tests {
for p, cfg := range t.CacheDirs {
if _, err := sharingMode(cfg.Mode); err != nil {
outErr = goerrors.Join(errors.Wrapf(err, "invalid sharing mode for test %q with cache mount at path %q", t.Name, p))
}
}
}
for src, patches := range s.Patches {
for _, patch := range patches {
patchSrc, ok := s.Sources[patch.Source]
if !ok {
outErr = goerrors.Join(outErr, &InvalidPatchError{Source: src, PatchSpec: &patch, Err: errMissingSource})
continue
}
if err := validatePatch(patch, patchSrc); err != nil {
outErr = goerrors.Join(outErr, &InvalidPatchError{Source: src, PatchSpec: &patch, Err: err})
}
}
}
switch s.Build.NetworkMode {
case "", netModeNone, netModeSandbox:
default:
outErr = goerrors.Join(outErr, fmt.Errorf("invalid network mode: %q: valid values %s", s.Build.NetworkMode, []string{netModeNone, netModeSandbox}))
}
return outErr
}
func validatePatch(patch PatchSpec, patchSrc Source) error {
if SourceIsDir(patchSrc) {
// Patch sources that use directory-backed sources require a subpath in the
// patch spec.
if isRoot(patch.Path) {
return errPatchRequiresSubpath
}
return nil
}
// File backed sources with a subpath in the patch spec is invalid since it is
// already a file, not a directory.
if !isRoot(patch.Path) {
return errPatchFileNoSubpath
}
return nil
}
func (c *CheckOutput) processBuildArgs(lex *shell.Lex, args map[string]string) error {
for i, contains := range c.Contains {
updated, err := expandArgs(lex, contains, args)
if err != nil {
return errors.Wrap(err, "error performing shell expansion on contains")
}
c.Contains[i] = updated
}
updated, err := expandArgs(lex, c.EndsWith, args)
if err != nil {
return errors.Wrap(err, "error performing shell expansion on endsWith")
}
c.EndsWith = updated
updated, err = expandArgs(lex, c.Matches, args)
if err != nil {
return errors.Wrap(err, "error performing shell expansion on matches")
}
c.Matches = updated
updated, err = expandArgs(lex, c.Equals, args)
if err != nil {
return errors.Wrap(err, "error performing shell expansion on equals")
}
c.Equals = updated
updated, err = expandArgs(lex, c.StartsWith, args)
if err != nil {
return errors.Wrap(err, "error performing shell expansion on startsWith")
}
c.StartsWith = updated
return nil
}
func (c *TestSpec) processBuildArgs(lex *shell.Lex, args map[string]string, name string) error {
var errs []error
appendErr := func(err error) {
errs = append(errs, err)
}
for _, s := range c.Mounts {
err := s.Spec.substituteBuildArgs(args)
if err != nil {
appendErr(fmt.Errorf("error performing shell expansion on source ref %q: %w", name, err))
}
}
for k, v := range c.Env {
updated, err := expandArgs(lex, v, args)
if err != nil {
appendErr(fmt.Errorf("error performing shell expansion on env var %q for source %q: %w", k, name, err))
}
c.Env[k] = updated
}
for i, step := range c.Steps {
for k, v := range step.Env {
updated, err := expandArgs(lex, v, args)
if err != nil {
appendErr(fmt.Errorf("error performing shell expansion on env var %q for source %q: %w", k, name, err))
}
step.Env[k] = updated
c.Steps[i] = step
}
}
for i, step := range c.Steps {
stdout := step.Stdout
if err := stdout.processBuildArgs(lex, args); err != nil {
appendErr(err)
}
step.Stdout = stdout
stderr := step.Stderr
if err := stderr.processBuildArgs(lex, args); err != nil {
appendErr(err)
}
step.Stderr = stderr
c.Steps[i] = step
}
for name, f := range c.Files {
if err := f.processBuildArgs(lex, args); err != nil {
appendErr(fmt.Errorf("error performing shell expansion to check output of file %s: %w", name, err))
}
c.Files[name] = f
}
return goerrors.Join(errs...)
}
func (c *FileCheckOutput) processBuildArgs(lex *shell.Lex, args map[string]string) error {
check := c.CheckOutput
if err := check.processBuildArgs(lex, args); err != nil {
return err
}
c.CheckOutput = check
return nil
}
func (g *SourceGenerator) Validate() error {
if g.Gomod == nil {
// Gomod is the only valid generator type
// An empty generator is invalid
return fmt.Errorf("no generator type specified")
}
return nil
}
func (s *PackageSigner) processBuildArgs(lex *shell.Lex, args map[string]string) error {
for k, v := range s.Args {
updated, err := expandArgs(lex, v, args)
if err != nil {
return fmt.Errorf("error performing shell expansion on env var %q: %w", k, err)
}
s.Args[k] = updated
}
return nil
}
func (t *Target) processBuildArgs(name string, lex *shell.Lex, args map[string]string) error {
for _, tt := range t.Tests {
if err := tt.processBuildArgs(lex, args, path.Join(name, tt.Name)); err != nil {
return err
}
}
if t.PackageConfig != nil {
if err := t.PackageConfig.processBuildArgs(lex, args); err != nil {
return fmt.Errorf("error processing package config build args: %w", err)
}
}
return nil
}
func (cfg *PackageConfig) processBuildArgs(lex *shell.Lex, args map[string]string) error {
if cfg.Signer != nil {
if err := cfg.Signer.processBuildArgs(lex, args); err != nil {
return fmt.Errorf("could not process build args for signer config: %w", err)
}
}
return nil
}