executor_unix.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //go:build !windows
  2. // +build !windows
  3. package buildkit
  4. import (
  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() (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. type lnInterface struct {
  73. ep *libnetwork.Endpoint
  74. sbx *libnetwork.Sandbox
  75. sync.Once
  76. err error
  77. ready chan struct{}
  78. provider *bridgeProvider
  79. }
  80. func (iface *lnInterface) init(c *libnetwork.Controller, n libnetwork.Network) {
  81. defer close(iface.ready)
  82. id := identity.NewID()
  83. ep, err := n.CreateEndpoint(id, libnetwork.CreateOptionDisableResolution())
  84. if err != nil {
  85. iface.err = err
  86. return
  87. }
  88. sbx, err := c.NewSandbox(id, libnetwork.OptionUseExternalKey(), libnetwork.OptionHostsPath(filepath.Join(iface.provider.Root, id, "hosts")),
  89. libnetwork.OptionResolvConfPath(filepath.Join(iface.provider.Root, id, "resolv.conf")))
  90. if err != nil {
  91. iface.err = err
  92. return
  93. }
  94. if err := ep.Join(sbx); err != nil {
  95. iface.err = err
  96. return
  97. }
  98. iface.sbx = sbx
  99. iface.ep = ep
  100. }
  101. func (iface *lnInterface) Set(s *specs.Spec) error {
  102. <-iface.ready
  103. if iface.err != nil {
  104. logrus.WithError(iface.err).Error("failed to set networking spec")
  105. return iface.err
  106. }
  107. shortNetCtlrID := stringid.TruncateID(iface.provider.Controller.ID())
  108. // attach netns to bridge within the container namespace, using reexec in a prestart hook
  109. s.Hooks = &specs.Hooks{
  110. Prestart: []specs.Hook{{
  111. Path: filepath.Join("/proc", strconv.Itoa(os.Getpid()), "exe"),
  112. Args: []string{"libnetwork-setkey", "-exec-root=" + iface.provider.Config().ExecRoot, iface.sbx.ContainerID(), shortNetCtlrID},
  113. }},
  114. }
  115. return nil
  116. }
  117. func (iface *lnInterface) Close() error {
  118. <-iface.ready
  119. if iface.sbx != nil {
  120. go func() {
  121. if err := iface.sbx.Delete(); err != nil {
  122. logrus.WithError(err).Errorf("failed to delete builder network sandbox")
  123. }
  124. if err := os.RemoveAll(filepath.Join(iface.provider.Root, iface.sbx.ContainerID())); err != nil {
  125. logrus.WithError(err).Errorf("failed to delete builder sandbox directory")
  126. }
  127. }()
  128. }
  129. return iface.err
  130. }
  131. func getDNSConfig(cfg config.DNSConfig) *oci.DNSConfig {
  132. if cfg.DNS != nil || cfg.DNSSearch != nil || cfg.DNSOptions != nil {
  133. return &oci.DNSConfig{
  134. Nameservers: cfg.DNS,
  135. SearchDomains: cfg.DNSSearch,
  136. Options: cfg.DNSOptions,
  137. }
  138. }
  139. return nil
  140. }