executor_unix.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/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, apparmorProfile string) (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 := 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. 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. ApparmorProfile: apparmorProfile,
  49. }, networkProviders)
  50. }
  51. type bridgeProvider struct {
  52. libnetwork.NetworkController
  53. Root string
  54. }
  55. func (p *bridgeProvider) New() (network.Namespace, error) {
  56. n, err := p.NetworkByName(networkName)
  57. if err != nil {
  58. return nil, err
  59. }
  60. iface := &lnInterface{ready: make(chan struct{}), provider: p}
  61. iface.Once.Do(func() {
  62. go iface.init(p.NetworkController, n)
  63. })
  64. return iface, nil
  65. }
  66. type lnInterface struct {
  67. ep libnetwork.Endpoint
  68. sbx libnetwork.Sandbox
  69. sync.Once
  70. err error
  71. ready chan struct{}
  72. provider *bridgeProvider
  73. }
  74. func (iface *lnInterface) init(c libnetwork.NetworkController, n libnetwork.Network) {
  75. defer close(iface.ready)
  76. id := identity.NewID()
  77. ep, err := n.CreateEndpoint(id, libnetwork.CreateOptionDisableResolution())
  78. if err != nil {
  79. iface.err = err
  80. return
  81. }
  82. sbx, err := c.NewSandbox(id, libnetwork.OptionUseExternalKey(), libnetwork.OptionHostsPath(filepath.Join(iface.provider.Root, id, "hosts")),
  83. libnetwork.OptionResolvConfPath(filepath.Join(iface.provider.Root, id, "resolv.conf")))
  84. if err != nil {
  85. iface.err = err
  86. return
  87. }
  88. if err := ep.Join(sbx); err != nil {
  89. iface.err = err
  90. return
  91. }
  92. iface.sbx = sbx
  93. iface.ep = ep
  94. }
  95. func (iface *lnInterface) Set(s *specs.Spec) error {
  96. <-iface.ready
  97. if iface.err != nil {
  98. logrus.WithError(iface.err).Error("failed to set networking spec")
  99. return iface.err
  100. }
  101. shortNetCtlrID := stringid.TruncateID(iface.provider.NetworkController.ID())
  102. // attach netns to bridge within the container namespace, using reexec in a prestart hook
  103. s.Hooks = &specs.Hooks{
  104. Prestart: []specs.Hook{{
  105. Path: filepath.Join("/proc", strconv.Itoa(os.Getpid()), "exe"),
  106. Args: []string{"libnetwork-setkey", "-exec-root=" + iface.provider.Config().Daemon.ExecRoot, iface.sbx.ContainerID(), shortNetCtlrID},
  107. }},
  108. }
  109. return nil
  110. }
  111. func (iface *lnInterface) Close() error {
  112. <-iface.ready
  113. if iface.sbx != nil {
  114. go func() {
  115. if err := iface.sbx.Delete(); err != nil {
  116. logrus.WithError(err).Errorf("failed to delete builder network sandbox")
  117. }
  118. if err := os.RemoveAll(filepath.Join(iface.provider.Root, iface.sbx.ContainerID())); err != nil {
  119. logrus.WithError(err).Errorf("failed to delete builder sandbox directory")
  120. }
  121. }()
  122. }
  123. return iface.err
  124. }
  125. func getDNSConfig(cfg config.DNSConfig) *oci.DNSConfig {
  126. if cfg.DNS != nil || cfg.DNSSearch != nil || cfg.DNSOptions != nil {
  127. return &oci.DNSConfig{
  128. Nameservers: cfg.DNS,
  129. SearchDomains: cfg.DNSSearch,
  130. Options: cfg.DNSOptions,
  131. }
  132. }
  133. return nil
  134. }