executor_unix.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // +build !windows
  2. package buildkit
  3. import (
  4. "os"
  5. "path/filepath"
  6. "strconv"
  7. "sync"
  8. "github.com/docker/docker/pkg/idtools"
  9. "github.com/docker/libnetwork"
  10. "github.com/moby/buildkit/executor"
  11. "github.com/moby/buildkit/executor/runcexecutor"
  12. "github.com/moby/buildkit/identity"
  13. "github.com/moby/buildkit/solver/pb"
  14. "github.com/moby/buildkit/util/network"
  15. specs "github.com/opencontainers/runtime-spec/specs-go"
  16. "github.com/sirupsen/logrus"
  17. )
  18. const networkName = "bridge"
  19. func newExecutor(root, cgroupParent string, net libnetwork.NetworkController, rootless bool, idmap *idtools.IdentityMapping) (executor.Executor, error) {
  20. networkProviders := map[pb.NetMode]network.Provider{
  21. pb.NetMode_UNSET: &bridgeProvider{NetworkController: net, Root: filepath.Join(root, "net")},
  22. pb.NetMode_HOST: network.NewHostProvider(),
  23. pb.NetMode_NONE: network.NewNoneProvider(),
  24. }
  25. return runcexecutor.New(runcexecutor.Opt{
  26. Root: filepath.Join(root, "executor"),
  27. CommandCandidates: []string{"runc"},
  28. DefaultCgroupParent: cgroupParent,
  29. Rootless: rootless,
  30. NoPivot: os.Getenv("DOCKER_RAMDISK") != "",
  31. IdentityMapping: idmap,
  32. }, networkProviders)
  33. }
  34. type bridgeProvider struct {
  35. libnetwork.NetworkController
  36. Root string
  37. }
  38. func (p *bridgeProvider) New() (network.Namespace, error) {
  39. n, err := p.NetworkByName(networkName)
  40. if err != nil {
  41. return nil, err
  42. }
  43. iface := &lnInterface{ready: make(chan struct{}), provider: p}
  44. iface.Once.Do(func() {
  45. go iface.init(p.NetworkController, n)
  46. })
  47. return iface, nil
  48. }
  49. type lnInterface struct {
  50. ep libnetwork.Endpoint
  51. sbx libnetwork.Sandbox
  52. sync.Once
  53. err error
  54. ready chan struct{}
  55. provider *bridgeProvider
  56. }
  57. func (iface *lnInterface) init(c libnetwork.NetworkController, n libnetwork.Network) {
  58. defer close(iface.ready)
  59. id := identity.NewID()
  60. ep, err := n.CreateEndpoint(id, libnetwork.CreateOptionDisableResolution())
  61. if err != nil {
  62. iface.err = err
  63. return
  64. }
  65. sbx, err := c.NewSandbox(id, libnetwork.OptionUseExternalKey(), libnetwork.OptionHostsPath(filepath.Join(iface.provider.Root, id, "hosts")),
  66. libnetwork.OptionResolvConfPath(filepath.Join(iface.provider.Root, id, "resolv.conf")))
  67. if err != nil {
  68. iface.err = err
  69. return
  70. }
  71. if err := ep.Join(sbx); err != nil {
  72. iface.err = err
  73. return
  74. }
  75. iface.sbx = sbx
  76. iface.ep = ep
  77. }
  78. func (iface *lnInterface) Set(s *specs.Spec) {
  79. <-iface.ready
  80. if iface.err != nil {
  81. logrus.WithError(iface.err).Error("failed to set networking spec")
  82. return
  83. }
  84. // attach netns to bridge within the container namespace, using reexec in a prestart hook
  85. s.Hooks = &specs.Hooks{
  86. Prestart: []specs.Hook{{
  87. Path: filepath.Join("/proc", strconv.Itoa(os.Getpid()), "exe"),
  88. Args: []string{"libnetwork-setkey", "-exec-root=" + iface.provider.Config().Daemon.ExecRoot, iface.sbx.ContainerID(), iface.provider.NetworkController.ID()},
  89. }},
  90. }
  91. }
  92. func (iface *lnInterface) Close() error {
  93. <-iface.ready
  94. if iface.sbx != nil {
  95. go func() {
  96. if err := iface.sbx.Delete(); err != nil {
  97. logrus.Errorf("failed to delete builder network sandbox: %v", err)
  98. }
  99. }()
  100. }
  101. return iface.err
  102. }