executor_unix.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //go:build !windows
  2. package buildkit
  3. import (
  4. "context"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. "sync"
  9. "github.com/docker/docker/daemon/config"
  10. "github.com/docker/docker/libnetwork"
  11. "github.com/docker/docker/pkg/idtools"
  12. "github.com/docker/docker/pkg/stringid"
  13. "github.com/moby/buildkit/executor"
  14. "github.com/moby/buildkit/executor/oci"
  15. "github.com/moby/buildkit/executor/runcexecutor"
  16. "github.com/moby/buildkit/identity"
  17. "github.com/moby/buildkit/solver/pb"
  18. "github.com/moby/buildkit/util/network"
  19. specs "github.com/opencontainers/runtime-spec/specs-go"
  20. "github.com/sirupsen/logrus"
  21. )
  22. const networkName = "bridge"
  23. func newExecutor(root, cgroupParent string, net *libnetwork.Controller, dnsConfig *oci.DNSConfig, rootless bool, idmap idtools.IdentityMapping, apparmorProfile string) (executor.Executor, error) {
  24. netRoot := filepath.Join(root, "net")
  25. networkProviders := map[pb.NetMode]network.Provider{
  26. pb.NetMode_UNSET: &bridgeProvider{Controller: net, Root: netRoot},
  27. pb.NetMode_HOST: network.NewHostProvider(),
  28. pb.NetMode_NONE: network.NewNoneProvider(),
  29. }
  30. // make sure net state directory is cleared from previous state
  31. fis, err := os.ReadDir(netRoot)
  32. if err == nil {
  33. for _, fi := range fis {
  34. fp := filepath.Join(netRoot, fi.Name())
  35. if err := os.RemoveAll(fp); err != nil {
  36. logrus.WithError(err).Errorf("failed to delete old network state: %v", fp)
  37. }
  38. }
  39. }
  40. // Returning a non-nil but empty *IdentityMapping breaks BuildKit:
  41. // https://github.com/moby/moby/pull/39444
  42. pidmap := &idmap
  43. if idmap.Empty() {
  44. pidmap = nil
  45. }
  46. return runcexecutor.New(runcexecutor.Opt{
  47. Root: filepath.Join(root, "executor"),
  48. CommandCandidates: []string{"runc"},
  49. DefaultCgroupParent: cgroupParent,
  50. Rootless: rootless,
  51. NoPivot: os.Getenv("DOCKER_RAMDISK") != "",
  52. IdentityMapping: pidmap,
  53. DNS: dnsConfig,
  54. ApparmorProfile: apparmorProfile,
  55. }, networkProviders)
  56. }
  57. type bridgeProvider struct {
  58. *libnetwork.Controller
  59. Root string
  60. }
  61. func (p *bridgeProvider) New(ctx context.Context, hostname string) (network.Namespace, error) {
  62. n, err := p.NetworkByName(networkName)
  63. if err != nil {
  64. return nil, err
  65. }
  66. iface := &lnInterface{ready: make(chan struct{}), provider: p}
  67. iface.Once.Do(func() {
  68. go iface.init(p.Controller, n)
  69. })
  70. return iface, nil
  71. }
  72. func (p *bridgeProvider) Close() error {
  73. return nil
  74. }
  75. type lnInterface struct {
  76. ep *libnetwork.Endpoint
  77. sbx *libnetwork.Sandbox
  78. sync.Once
  79. err error
  80. ready chan struct{}
  81. provider *bridgeProvider
  82. }
  83. func (iface *lnInterface) init(c *libnetwork.Controller, n libnetwork.Network) {
  84. defer close(iface.ready)
  85. id := identity.NewID()
  86. ep, err := n.CreateEndpoint(id, libnetwork.CreateOptionDisableResolution())
  87. if err != nil {
  88. iface.err = err
  89. return
  90. }
  91. sbx, err := c.NewSandbox(id, libnetwork.OptionUseExternalKey(), libnetwork.OptionHostsPath(filepath.Join(iface.provider.Root, id, "hosts")),
  92. libnetwork.OptionResolvConfPath(filepath.Join(iface.provider.Root, id, "resolv.conf")))
  93. if err != nil {
  94. iface.err = err
  95. return
  96. }
  97. if err := ep.Join(sbx); err != nil {
  98. iface.err = err
  99. return
  100. }
  101. iface.sbx = sbx
  102. iface.ep = ep
  103. }
  104. func (iface *lnInterface) Set(s *specs.Spec) error {
  105. <-iface.ready
  106. if iface.err != nil {
  107. logrus.WithError(iface.err).Error("failed to set networking spec")
  108. return iface.err
  109. }
  110. shortNetCtlrID := stringid.TruncateID(iface.provider.Controller.ID())
  111. // attach netns to bridge within the container namespace, using reexec in a prestart hook
  112. s.Hooks = &specs.Hooks{
  113. Prestart: []specs.Hook{{
  114. Path: filepath.Join("/proc", strconv.Itoa(os.Getpid()), "exe"),
  115. Args: []string{"libnetwork-setkey", "-exec-root=" + iface.provider.Config().ExecRoot, iface.sbx.ContainerID(), shortNetCtlrID},
  116. }},
  117. }
  118. return nil
  119. }
  120. func (iface *lnInterface) Close() error {
  121. <-iface.ready
  122. if iface.sbx != nil {
  123. go func() {
  124. if err := iface.sbx.Delete(); err != nil {
  125. logrus.WithError(err).Errorf("failed to delete builder network sandbox")
  126. }
  127. if err := os.RemoveAll(filepath.Join(iface.provider.Root, iface.sbx.ContainerID())); err != nil {
  128. logrus.WithError(err).Errorf("failed to delete builder sandbox directory")
  129. }
  130. }()
  131. }
  132. return iface.err
  133. }
  134. func getDNSConfig(cfg config.DNSConfig) *oci.DNSConfig {
  135. if cfg.DNS != nil || cfg.DNSSearch != nil || cfg.DNSOptions != nil {
  136. return &oci.DNSConfig{
  137. Nameservers: cfg.DNS,
  138. SearchDomains: cfg.DNSSearch,
  139. Options: cfg.DNSOptions,
  140. }
  141. }
  142. return nil
  143. }