convert_runmount.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // +build dfrunmount dfextall
  2. package dockerfile2llb
  3. import (
  4. "path"
  5. "path/filepath"
  6. "github.com/moby/buildkit/client/llb"
  7. "github.com/moby/buildkit/frontend/dockerfile/instructions"
  8. "github.com/pkg/errors"
  9. )
  10. func detectRunMount(cmd *command, allDispatchStates *dispatchStates) bool {
  11. if c, ok := cmd.Command.(*instructions.RunCommand); ok {
  12. mounts := instructions.GetMounts(c)
  13. sources := make([]*dispatchState, len(mounts))
  14. for i, mount := range mounts {
  15. if mount.From == "" && mount.Type == instructions.MountTypeCache {
  16. mount.From = emptyImageName
  17. }
  18. from := mount.From
  19. if from == "" || mount.Type == instructions.MountTypeTmpfs {
  20. continue
  21. }
  22. stn, ok := allDispatchStates.findStateByName(from)
  23. if !ok {
  24. stn = &dispatchState{
  25. stage: instructions.Stage{BaseName: from},
  26. deps: make(map[*dispatchState]struct{}),
  27. unregistered: true,
  28. }
  29. }
  30. sources[i] = stn
  31. }
  32. cmd.sources = sources
  33. return true
  34. }
  35. return false
  36. }
  37. func dispatchRunMounts(d *dispatchState, c *instructions.RunCommand, sources []*dispatchState, opt dispatchOpt) ([]llb.RunOption, error) {
  38. var out []llb.RunOption
  39. mounts := instructions.GetMounts(c)
  40. for i, mount := range mounts {
  41. if mount.From == "" && mount.Type == instructions.MountTypeCache {
  42. mount.From = emptyImageName
  43. }
  44. st := opt.buildContext
  45. if mount.From != "" {
  46. st = sources[i].state
  47. }
  48. var mountOpts []llb.MountOption
  49. if mount.Type == instructions.MountTypeTmpfs {
  50. st = llb.Scratch()
  51. mountOpts = append(mountOpts, llb.Tmpfs())
  52. }
  53. if mount.Type == instructions.MountTypeSecret {
  54. secret, err := dispatchSecret(mount)
  55. if err != nil {
  56. return nil, err
  57. }
  58. out = append(out, secret)
  59. continue
  60. }
  61. if mount.ReadOnly {
  62. mountOpts = append(mountOpts, llb.Readonly)
  63. }
  64. if mount.Type == instructions.MountTypeCache {
  65. sharing := llb.CacheMountShared
  66. if mount.CacheSharing == instructions.MountSharingPrivate {
  67. sharing = llb.CacheMountPrivate
  68. }
  69. if mount.CacheSharing == instructions.MountSharingLocked {
  70. sharing = llb.CacheMountLocked
  71. }
  72. mountOpts = append(mountOpts, llb.AsPersistentCacheDir(opt.cacheIDNamespace+"/"+mount.CacheID, sharing))
  73. }
  74. target := mount.Target
  75. if !filepath.IsAbs(filepath.Clean(mount.Target)) {
  76. target = filepath.Join("/", d.state.GetDir(), mount.Target)
  77. }
  78. if target == "/" {
  79. return nil, errors.Errorf("invalid mount target %q", target)
  80. }
  81. if src := path.Join("/", mount.Source); src != "/" {
  82. mountOpts = append(mountOpts, llb.SourcePath(src))
  83. }
  84. out = append(out, llb.AddMount(target, st, mountOpts...))
  85. d.ctxPaths[path.Join("/", filepath.ToSlash(mount.Source))] = struct{}{}
  86. }
  87. return out, nil
  88. }