executor_unix.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. }, networkProviders)
  30. }
  31. type bridgeProvider struct {
  32. libnetwork.NetworkController
  33. Root string
  34. }
  35. func (p *bridgeProvider) New() (network.Namespace, error) {
  36. n, err := p.NetworkByName(networkName)
  37. if err != nil {
  38. return nil, err
  39. }
  40. iface := &lnInterface{ready: make(chan struct{}), provider: p}
  41. iface.Once.Do(func() {
  42. go iface.init(p.NetworkController, n)
  43. })
  44. return iface, nil
  45. }
  46. type lnInterface struct {
  47. ep libnetwork.Endpoint
  48. sbx libnetwork.Sandbox
  49. sync.Once
  50. err error
  51. ready chan struct{}
  52. provider *bridgeProvider
  53. }
  54. func (iface *lnInterface) init(c libnetwork.NetworkController, n libnetwork.Network) {
  55. defer close(iface.ready)
  56. id := identity.NewID()
  57. ep, err := n.CreateEndpoint(id, libnetwork.CreateOptionDisableResolution())
  58. if err != nil {
  59. iface.err = err
  60. return
  61. }
  62. sbx, err := c.NewSandbox(id, libnetwork.OptionUseExternalKey(), libnetwork.OptionHostsPath(filepath.Join(iface.provider.Root, id, "hosts")),
  63. libnetwork.OptionResolvConfPath(filepath.Join(iface.provider.Root, id, "resolv.conf")))
  64. if err != nil {
  65. iface.err = err
  66. return
  67. }
  68. if err := ep.Join(sbx); err != nil {
  69. iface.err = err
  70. return
  71. }
  72. iface.sbx = sbx
  73. iface.ep = ep
  74. }
  75. func (iface *lnInterface) Set(s *specs.Spec) {
  76. <-iface.ready
  77. if iface.err != nil {
  78. logrus.WithError(iface.err).Error("failed to set networking spec")
  79. return
  80. }
  81. // attach netns to bridge within the container namespace, using reexec in a prestart hook
  82. s.Hooks = &specs.Hooks{
  83. Prestart: []specs.Hook{{
  84. Path: filepath.Join("/proc", strconv.Itoa(os.Getpid()), "exe"),
  85. Args: []string{"libnetwork-setkey", "-exec-root=" + iface.provider.Config().Daemon.ExecRoot, iface.sbx.ContainerID(), iface.provider.NetworkController.ID()},
  86. }},
  87. }
  88. }
  89. func (iface *lnInterface) Close() error {
  90. <-iface.ready
  91. if iface.sbx != nil {
  92. go func() {
  93. if err := iface.sbx.Delete(); err != nil {
  94. logrus.Errorf("failed to delete builder network sandbox: %v", err)
  95. }
  96. }()
  97. }
  98. return iface.err
  99. }