executor_unix.go 3.0 KB

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