executor_unix.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // +build !windows
  2. package buildkit
  3. import (
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. "sync"
  9. "github.com/docker/docker/daemon/config"
  10. "github.com/docker/docker/pkg/idtools"
  11. "github.com/docker/docker/pkg/stringid"
  12. "github.com/docker/libnetwork"
  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.NetworkController, dnsConfig *oci.DNSConfig, rootless bool, idmap *idtools.IdentityMapping) (executor.Executor, error) {
  24. netRoot := filepath.Join(root, "net")
  25. networkProviders := map[pb.NetMode]network.Provider{
  26. pb.NetMode_UNSET: &bridgeProvider{NetworkController: 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 := ioutil.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. return runcexecutor.New(runcexecutor.Opt{
  41. Root: filepath.Join(root, "executor"),
  42. CommandCandidates: []string{"runc"},
  43. DefaultCgroupParent: cgroupParent,
  44. Rootless: rootless,
  45. NoPivot: os.Getenv("DOCKER_RAMDISK") != "",
  46. IdentityMapping: idmap,
  47. DNS: dnsConfig,
  48. }, networkProviders)
  49. }
  50. type bridgeProvider struct {
  51. libnetwork.NetworkController
  52. Root string
  53. }
  54. func (p *bridgeProvider) New() (network.Namespace, error) {
  55. n, err := p.NetworkByName(networkName)
  56. if err != nil {
  57. return nil, err
  58. }
  59. iface := &lnInterface{ready: make(chan struct{}), provider: p}
  60. iface.Once.Do(func() {
  61. go iface.init(p.NetworkController, n)
  62. })
  63. return iface, nil
  64. }
  65. type lnInterface struct {
  66. ep libnetwork.Endpoint
  67. sbx libnetwork.Sandbox
  68. sync.Once
  69. err error
  70. ready chan struct{}
  71. provider *bridgeProvider
  72. }
  73. func (iface *lnInterface) init(c libnetwork.NetworkController, n libnetwork.Network) {
  74. defer close(iface.ready)
  75. id := identity.NewID()
  76. ep, err := n.CreateEndpoint(id, libnetwork.CreateOptionDisableResolution())
  77. if err != nil {
  78. iface.err = err
  79. return
  80. }
  81. sbx, err := c.NewSandbox(id, libnetwork.OptionUseExternalKey(), libnetwork.OptionHostsPath(filepath.Join(iface.provider.Root, id, "hosts")),
  82. libnetwork.OptionResolvConfPath(filepath.Join(iface.provider.Root, id, "resolv.conf")))
  83. if err != nil {
  84. iface.err = err
  85. return
  86. }
  87. if err := ep.Join(sbx); err != nil {
  88. iface.err = err
  89. return
  90. }
  91. iface.sbx = sbx
  92. iface.ep = ep
  93. }
  94. func (iface *lnInterface) Set(s *specs.Spec) error {
  95. <-iface.ready
  96. if iface.err != nil {
  97. logrus.WithError(iface.err).Error("failed to set networking spec")
  98. return iface.err
  99. }
  100. shortNetCtlrID := stringid.TruncateID(iface.provider.NetworkController.ID())
  101. // attach netns to bridge within the container namespace, using reexec in a prestart hook
  102. s.Hooks = &specs.Hooks{
  103. Prestart: []specs.Hook{{
  104. Path: filepath.Join("/proc", strconv.Itoa(os.Getpid()), "exe"),
  105. Args: []string{"libnetwork-setkey", "-exec-root=" + iface.provider.Config().Daemon.ExecRoot, iface.sbx.ContainerID(), shortNetCtlrID},
  106. }},
  107. }
  108. return nil
  109. }
  110. func (iface *lnInterface) Close() error {
  111. <-iface.ready
  112. if iface.sbx != nil {
  113. go func() {
  114. if err := iface.sbx.Delete(); err != nil {
  115. logrus.WithError(err).Errorf("failed to delete builder network sandbox")
  116. }
  117. if err := os.RemoveAll(filepath.Join(iface.provider.Root, iface.sbx.ContainerID())); err != nil {
  118. logrus.WithError(err).Errorf("failed to delete builder sandbox directory")
  119. }
  120. }()
  121. }
  122. return iface.err
  123. }
  124. func getDNSConfig(cfg config.DNSConfig) *oci.DNSConfig {
  125. if cfg.DNS != nil || cfg.DNSSearch != nil || cfg.DNSOptions != nil {
  126. return &oci.DNSConfig{
  127. Nameservers: cfg.DNS,
  128. SearchDomains: cfg.DNSSearch,
  129. Options: cfg.DNSOptions,
  130. }
  131. }
  132. return nil
  133. }