-
Notifications
You must be signed in to change notification settings - Fork 25
/
source.go
680 lines (579 loc) · 19.2 KB
/
source.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
package dalec
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"path/filepath"
"strings"
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/util/gitutil"
"github.com/pkg/errors"
)
type FilterFunc = func(string, []string, []string, ...llb.ConstraintsOpt) llb.StateOption
var errNoSourceVariant = fmt.Errorf("no source variant found")
func (src Source) handlesOwnPath() bool {
// docker images handle their own path extraction if they have an attached command,
// and this information is needed in the case of mounts when we can do path
// extraction at mount time
return src.DockerImage != nil && src.DockerImage.Cmd != nil
}
func getFilter(src Source, forMount bool, opts ...llb.ConstraintsOpt) llb.StateOption {
var path = src.Path
if forMount {
// if we're using a mount for these sources, the mount will handle path extraction
path = "/"
}
switch {
case src.HTTP != nil,
src.Git != nil,
src.Build != nil,
src.Context != nil,
src.Inline != nil:
return filterState(path, src.Includes, src.Excludes, opts...)
case src.DockerImage != nil:
if src.DockerImage.Cmd != nil {
// if a docker image source has a command,
// the path extraction will be handled with a mount on the command
path = "/"
}
return filterState(path, src.Includes, src.Excludes)
}
return func(st llb.State) llb.State { return st }
}
func getSource(src Source, name string, sOpt SourceOpts, opts ...llb.ConstraintsOpt) (st llb.State, err error) {
// load the source
switch {
case src.HTTP != nil:
st, err = src.HTTP.AsState(name, opts...)
case src.Git != nil:
st, err = src.Git.AsState(opts...)
case src.Context != nil:
st, err = src.Context.AsState(src.Path, src.Includes, src.Excludes, sOpt, opts...)
case src.DockerImage != nil:
st, err = src.DockerImage.AsState(name, src.Path, sOpt, opts...)
case src.Build != nil:
st, err = src.Build.AsState(name, sOpt, opts...)
case src.Inline != nil:
st, err = src.Inline.AsState(name)
default:
st, err = llb.Scratch(), errNoSourceVariant
}
return
}
func (src *SourceInline) AsState(name string) (llb.State, error) {
if src.File != nil {
return llb.Scratch().With(src.File.PopulateAt(name)), nil
}
return llb.Scratch().With(src.Dir.PopulateAt("/")), nil
}
// withFollowPath similar to using [llb.IncludePatterns] except that it will
// follow symlinks at the provided path.
func withFollowPath(p string) localOptionFunc {
return func(li *llb.LocalInfo) {
if isRoot(p) {
return
}
paths := []string{p}
if li.FollowPaths != "" {
var ls []string
if err := json.Unmarshal([]byte(li.FollowPaths), &ls); err != nil {
panic(err)
}
paths = append(ls, paths...)
}
llb.FollowPaths(paths).SetLocalOption(li)
}
}
func (src *SourceContext) AsState(path string, includes []string, excludes []string, sOpt SourceOpts, opts ...llb.ConstraintsOpt) (llb.State, error) {
if !isRoot(path) {
excludes = append(excludeAllButPath(path), excludes...)
}
st, err := sOpt.GetContext(src.Name, localIncludeExcludeMerge(includes, excludes), withFollowPath(path), withConstraints(opts))
if err != nil {
return llb.Scratch(), err
}
if st == nil {
return llb.Scratch(), errors.Errorf("context %q not found", src.Name)
}
return *st, nil
}
func excludeAllButPath(p string) []string {
return []string{
"*",
"!" + filepath.ToSlash(filepath.Clean(p)),
}
}
func (src *SourceGit) AsState(opts ...llb.ConstraintsOpt) (llb.State, error) {
ref, err := gitutil.ParseGitRef(src.URL)
if err != nil {
return llb.Scratch(), fmt.Errorf("could not parse git ref: %w", err)
}
var gOpts []llb.GitOption
if src.KeepGitDir {
gOpts = append(gOpts, llb.KeepGitDir())
}
gOpts = append(gOpts, withConstraints(opts))
gOpts = append(gOpts, src.Auth.LLBOpt())
st := llb.Git(ref.Remote, src.Commit, gOpts...)
return st, nil
// TODO: Pass git secrets
}
func (src *SourceDockerImage) AsState(name string, path string, sOpt SourceOpts, opts ...llb.ConstraintsOpt) (llb.State, error) {
st := llb.Image(src.Ref, llb.WithMetaResolver(sOpt.Resolver), withConstraints(opts))
if src.Cmd == nil {
return st, nil
}
st, err := generateSourceFromImage(st, src.Cmd, sOpt, path, opts...)
if err != nil {
return llb.Scratch(), err
}
return st, nil
}
func (src *SourceHTTP) AsState(name string, opts ...llb.ConstraintsOpt) (llb.State, error) {
httpOpts := []llb.HTTPOption{withConstraints(opts)}
httpOpts = append(httpOpts, llb.Filename(name))
if src.Digest != "" {
httpOpts = append(httpOpts, llb.Checksum(src.Digest))
}
if src.Permissions != 0 {
httpOpts = append(httpOpts, llb.Chmod(src.Permissions))
}
st := llb.HTTP(src.URL, httpOpts...)
return st, nil
}
func (src *SourceHTTP) validate() error {
if src.URL == "" {
return errors.New("http source must have a URL")
}
if src.Digest != "" {
if err := src.Digest.Validate(); err != nil {
return errors.WithStack(err)
}
}
return nil
}
// InvalidSourceError is an error type returned when a source is invalid.
type InvalidSourceError struct {
Name string
Err error
}
func (s *InvalidSourceError) Error() string {
return fmt.Sprintf("invalid source %s: %v", s.Name, s.Err)
}
func (s *InvalidSourceError) Unwrap() error {
return s.Err
}
type InvalidPatchError struct {
Source string
PatchSpec *PatchSpec
Err error
}
func (s *InvalidPatchError) Error() string {
return fmt.Sprintf("invalid patch for source %q, patch source: %q: %v", s.Source, s.PatchSpec.Source, s.Err)
}
func (s *InvalidPatchError) Unwrap() error {
return s.Err
}
var (
sourceNamePathSeparatorError = errors.New("source name must not contain path separator")
errMissingSource = errors.New("source is missing from sources list")
errPatchRequiresSubpath = errors.New("patch source refers to a directory source without a subpath to the patch file to use")
errPatchFileNoSubpath = errors.New("patch source refers to a file source but patch spec specifies a subpath")
)
type LLBGetter func(sOpts SourceOpts, opts ...llb.ConstraintsOpt) (llb.State, error)
type ForwarderFunc func(llb.State, *SourceBuild) (llb.State, error)
type SourceOpts struct {
Resolver llb.ImageMetaResolver
Forward ForwarderFunc
GetContext func(string, ...llb.LocalOption) (*llb.State, error)
}
func (s *Source) asState(name string, forMount bool, sOpt SourceOpts, opts ...llb.ConstraintsOpt) (llb.State, error) {
st, err := getSource(*s, name, sOpt, opts...)
if err != nil {
return llb.Scratch(), err
}
return st.With(getFilter(*s, forMount)), nil
}
func (s *Source) AsState(name string, sOpt SourceOpts, opts ...llb.ConstraintsOpt) (llb.State, error) {
return s.asState(name, false, sOpt, opts...)
}
func (s *Source) AsMount(name string, sOpt SourceOpts, opts ...llb.ConstraintsOpt) (llb.State, error) {
return s.asState(name, true, sOpt, opts...)
}
var errInvalidMountConfig = errors.New("invalid mount config")
func pathHasPrefix(s string, prefix string) bool {
if s == prefix {
return true
}
s = filepath.Clean(s)
prefix = filepath.Clean(prefix)
if s == prefix {
return true
}
if strings.HasPrefix(s, prefix+"/") {
return true
}
return false
}
func (m *SourceMount) validate(root string) error {
if m.Dest == "/" {
return errors.Wrap(errInvalidMountConfig, "mount destination must not be \"/\"")
}
if root != "/" && pathHasPrefix(m.Dest, root) {
// We cannot support this as the base mount for subPath will shadow the mount being done here.
return errors.Wrapf(errInvalidMountConfig, "mount destination (%s) must not be a descendent of the target source path (%s)", m.Dest, root)
}
return nil
}
// must not be called with a nil cmd pointer
// subPath must be a valid non-empty path
func generateSourceFromImage(st llb.State, cmd *Command, sOpts SourceOpts, subPath string, opts ...llb.ConstraintsOpt) (llb.State, error) {
if len(cmd.Steps) == 0 {
return llb.Scratch(), fmt.Errorf("no steps defined for image source")
}
if subPath == "" {
// TODO: We should log a warning here since extracting an entire image while also running a command is
// probably not what the user really wanted to do here.
// The buildkit client provides functionality to do this we just need to wire it in.
subPath = "/"
}
for k, v := range cmd.Env {
st = st.AddEnv(k, v)
}
if cmd.Dir != "" {
st = st.Dir(cmd.Dir)
}
baseRunOpts := []llb.RunOption{CacheDirsToRunOpt(cmd.CacheDirs, "", "")}
for _, src := range cmd.Mounts {
if err := src.validate(subPath); err != nil {
return llb.Scratch(), err
}
srcSt, err := src.Spec.AsMount(internalMountSourceName, sOpts, opts...)
if err != nil {
return llb.Scratch(), err
}
var mountOpt []llb.MountOption
// This handles the case where we are mounting a source with a target extract path and
// no includes and excludes. In this case, we can extract the path here as a source mount
// if the source does not handle its own path extraction. This saves an extra llb.Copy operation
if src.Spec.Path != "" && len(src.Spec.Includes) == 0 && len(src.Spec.Excludes) == 0 &&
!src.Spec.handlesOwnPath() {
mountOpt = append(mountOpt, llb.SourcePath(src.Spec.Path))
}
if !SourceIsDir(src.Spec) {
mountOpt = append(mountOpt, llb.SourcePath(internalMountSourceName))
}
baseRunOpts = append(baseRunOpts, llb.AddMount(src.Dest, srcSt, mountOpt...))
}
out := llb.Scratch()
for i, step := range cmd.Steps {
rOpts := []llb.RunOption{llb.Args([]string{"/bin/sh", "-c", step.Command})}
rOpts = append(rOpts, baseRunOpts...)
for k, v := range step.Env {
rOpts = append(rOpts, llb.AddEnv(k, v))
}
rOpts = append(rOpts, withConstraints(opts))
cmdSt := st.Run(rOpts...)
// on first iteration with a root subpath
// do not use AddMount, as this will overwrite / with a
// scratch fs
if i == 0 && subPath == "/" {
out = cmdSt.Root()
} else {
out = cmdSt.AddMount(subPath, out)
}
// Update the base state so that changes to the rootfs propagate between
// steps.
st = cmdSt.Root()
}
return out, nil
}
func isRoot(extract string) bool {
return extract == "" || extract == "/" || extract == "."
}
func filterState(extract string, includes, excludes []string, opts ...llb.ConstraintsOpt) llb.StateOption {
return func(st llb.State) llb.State {
// if we have no includes, no excludes, and no non-root source path,
// then this is a no-op
if len(includes) == 0 && len(excludes) == 0 && isRoot(extract) {
return st
}
filtered := llb.Scratch().File(
llb.Copy(
st,
extract,
"/",
WithIncludes(includes),
WithExcludes(excludes),
WithDirContentsOnly(),
),
withConstraints(opts),
)
return filtered
}
}
func sharingMode(mode string) (llb.CacheMountSharingMode, error) {
switch mode {
case "shared", "":
return llb.CacheMountShared, nil
case "private":
return llb.CacheMountPrivate, nil
case "locked":
return llb.CacheMountLocked, nil
default:
return 0, fmt.Errorf("invalid sharing mode: %s", mode)
}
}
func WithCreateDestPath() llb.CopyOption {
return copyOptionFunc(func(i *llb.CopyInfo) {
i.CreateDestPath = true
})
}
func SourceIsDir(src Source) bool {
switch {
case src.DockerImage != nil,
src.Git != nil,
src.Build != nil,
src.Context != nil:
return true
case src.HTTP != nil:
return false
case src.Inline != nil:
return src.Inline.Dir != nil
default:
panic("unreachable")
}
}
func (src *Source) GetDisplayRef() (string, error) {
s := ""
switch {
case src.DockerImage != nil:
s = src.DockerImage.Ref
case src.Git != nil:
s = src.Git.URL
case src.HTTP != nil:
s = src.HTTP.URL
case src.Context != nil:
s = src.Context.Name
case src.Build != nil:
s = fmt.Sprintf("%v", src.Build.Source)
case src.Inline != nil:
s = "inline"
default:
return "", fmt.Errorf("no non-nil source provided")
}
return s, nil
}
// Doc returns the details of how the source was created.
// This should be included, where applicable, in build in build specs (such as RPM spec files)
// so that others can reproduce the build.
func (s Source) Doc(name string) (io.Reader, error) {
b := bytes.NewBuffer(nil)
switch {
case s.Context != nil:
fmt.Fprintln(b, "Generated from a local docker build context and is unreproducible.")
case s.Build != nil:
fmt.Fprintln(b, "Generated from a docker build:")
fmt.Fprintln(b, " Docker Build Target:", s.Build.Target)
sub, err := s.Build.Source.Doc(name)
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(sub)
for scanner.Scan() {
fmt.Fprintf(b, " %s\n", scanner.Text())
}
if scanner.Err() != nil {
return nil, scanner.Err()
}
if len(s.Build.Args) > 0 {
sorted := SortMapKeys(s.Build.Args)
fmt.Fprintln(b, " Build Args:")
for _, k := range sorted {
fmt.Fprintf(b, " %s=%s\n", k, s.Build.Args[k])
}
}
p := "Dockerfile"
if s.Build.DockerfilePath != "" {
p = s.Build.DockerfilePath
}
fmt.Fprintln(b, " Dockerfile path in context:", p)
case s.HTTP != nil:
fmt.Fprintln(b, "Generated from a http(s) source:")
fmt.Fprintln(b, " URL:", s.HTTP.URL)
case s.Git != nil:
git := s.Git
ref, err := gitutil.ParseGitRef(git.URL)
if err != nil {
return nil, err
}
fmt.Fprintln(b, "Generated from a git repository:")
fmt.Fprintln(b, " Remote:", ref.Remote)
fmt.Fprintln(b, " Ref:", git.Commit)
if s.Path != "" {
fmt.Fprintln(b, " Extracted path:", s.Path)
}
case s.DockerImage != nil:
img := s.DockerImage
if img.Cmd == nil {
fmt.Fprintln(b, "Generated from a docker image:")
fmt.Fprintln(b, " Image:", img.Ref)
if s.Path != "" {
fmt.Fprintln(b, " Extracted path:", s.Path)
}
} else {
fmt.Fprintln(b, "Generated from running a command(s) in a docker image:")
fmt.Fprintln(b, " Image:", img.Ref)
if s.Path != "" {
fmt.Fprintln(b, " Extracted path:", s.Path)
}
if len(img.Cmd.Env) > 0 {
fmt.Fprintln(b, " With the following environment variables set for all commands:")
sorted := SortMapKeys(img.Cmd.Env)
for _, k := range sorted {
fmt.Fprintf(b, " %s=%s\n", k, img.Cmd.Env[k])
}
}
if img.Cmd.Dir != "" {
fmt.Fprintln(b, " Working Directory:", img.Cmd.Dir)
}
fmt.Fprintln(b, " Command(s):")
for _, step := range img.Cmd.Steps {
fmt.Fprintf(b, " %s\n", step.Command)
if len(step.Env) > 0 {
fmt.Fprintln(b, " With the following environment variables set for this command:")
sorted := SortMapKeys(step.Env)
for _, k := range sorted {
fmt.Fprintf(b, " %s=%s\n", k, step.Env[k])
}
}
}
if len(img.Cmd.Mounts) > 0 {
fmt.Fprintln(b, " With the following items mounted:")
for _, src := range img.Cmd.Mounts {
sub, err := src.Spec.Doc(name)
if err != nil {
return nil, err
}
fmt.Fprintln(b, " Destination Path:", src.Dest)
scanner := bufio.NewScanner(sub)
for scanner.Scan() {
fmt.Fprintf(b, " %s\n", scanner.Text())
}
if scanner.Err() != nil {
return nil, scanner.Err()
}
}
}
return b, nil
}
case s.Inline != nil:
fmt.Fprintln(b, "Generated from an inline source:")
s.Inline.Doc(b, name)
default:
// This should be unrecable.
// We could panic here, but ultimately this is just a doc string and parsing user generated content.
fmt.Fprintln(b, "Generated from an unknown source type")
}
return b, nil
}
func patchSource(worker, sourceState llb.State, sourceToState map[string]llb.State, patchNames []PatchSpec, opts ...llb.ConstraintsOpt) llb.State {
for _, p := range patchNames {
patchState := sourceToState[p.Source]
// on each iteration, mount source state to /src to run `patch`, and
// set the state under /src to be the source state for the next iteration
subPath := p.Source
if p.Path != "" {
subPath = p.Path
}
sourceState = worker.Run(
llb.AddMount("/patch", patchState, llb.Readonly, llb.SourcePath(subPath)),
llb.Dir("src"),
ShArgs(fmt.Sprintf("patch -p%d < /patch", *p.Strip)),
WithConstraints(opts...),
).AddMount("/src", sourceState)
}
return sourceState
}
// PatchSources returns a new map containing the patched LLB state for each source in the source map.
// Sources that are not patched are also included in the result for convenience.
// `sourceToState` must be a complete map from source name -> llb state for each source in the dalec spec.
// `worker` must be an LLB state with a `patch` binary present.
func PatchSources(worker llb.State, spec *Spec, sourceToState map[string]llb.State, opts ...llb.ConstraintsOpt) map[string]llb.State {
// duplicate map to avoid possibly confusing behavior of mutating caller's map
states := DuplicateMap(sourceToState)
pgID := identity.NewID()
sorted := SortMapKeys(states)
for _, sourceName := range sorted {
sourceState := states[sourceName]
patches, patchesExist := spec.Patches[sourceName]
if !patchesExist {
continue
}
pg := llb.ProgressGroup(pgID, "Patch spec source: "+sourceName+" ", false)
states[sourceName] = patchSource(worker, sourceState, states, patches, pg, withConstraints(opts))
}
return states
}
func (s *Spec) getPatchedSources(sOpt SourceOpts, worker llb.State, filterFunc func(string) bool, opts ...llb.ConstraintsOpt) (map[string]llb.State, error) {
states := map[string]llb.State{}
for name, src := range s.Sources {
if !filterFunc(name) {
continue
}
st, err := src.AsState(name, sOpt, opts...)
if err != nil {
return nil, errors.Wrapf(err, "failed to get source state for %q", name)
}
states[name] = st
for _, p := range s.Patches[name] {
src, ok := s.Sources[p.Source]
if !ok {
return nil, errors.Errorf("patch source %q not found", p.Source)
}
states[p.Source], err = src.AsState(p.Source, sOpt, opts...)
if err != nil {
return nil, errors.Wrapf(err, "failed to get patch source state for %q", p.Source)
}
}
}
return PatchSources(worker, s, states, opts...), nil
}
// Tar creates a tar+gz from the provided state and puts it in the provided dest.
// The provided work state is used to perform the necessary operations to produce the tarball and requires the tar and gzip binaries.
func Tar(work llb.State, src llb.State, dest string, opts ...llb.ConstraintsOpt) llb.State {
// Put the output tar in a consistent location regardless of `dest`
// This way if `dest` changes we don't have to rebuild the tarball, which can be expensive.
outBase := "/tmp/out"
out := filepath.Join(outBase, filepath.Dir(dest))
worker := work.Run(
llb.AddMount("/src", src, llb.Readonly),
ShArgs("tar -C /src -cvzf /tmp/st ."),
WithConstraints(opts...),
).
Run(
llb.Args([]string{"/bin/sh", "-c", "mkdir -p " + out + " && mv /tmp/st " + filepath.Join(out, filepath.Base(dest))}),
WithConstraints(opts...),
)
return worker.AddMount(outBase, llb.Scratch())
}
func DefaultTarWorker(resolver llb.ImageMetaResolver, opts ...llb.ConstraintsOpt) llb.State {
return llb.Image("busybox:latest", llb.WithMetaResolver(resolver), withConstraints(opts))
}
// Sources gets all the source LLB states from the spec.
func Sources(spec *Spec, sOpt SourceOpts, opts ...llb.ConstraintsOpt) (map[string]llb.State, error) {
states := make(map[string]llb.State, len(spec.Sources))
for k, src := range spec.Sources {
pg := ProgressGroup("Prepare source: " + k)
opts := append(opts, pg)
st, err := src.AsState(k, sOpt, opts...)
if err != nil {
return nil, errors.Wrapf(err, "could not get source state for source: %s", k)
}
states[k] = st
}
return states, nil
}