executor_unix.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. )
  16. const networkName = "bridge"
  17. func newExecutor(root, cgroupParent string, net libnetwork.NetworkController) (executor.Executor, error) {
  18. networkProviders := map[pb.NetMode]network.Provider{
  19. pb.NetMode_UNSET: &bridgeProvider{NetworkController: net},
  20. pb.NetMode_HOST: network.NewHostProvider(),
  21. pb.NetMode_NONE: network.NewNoneProvider(),
  22. }
  23. return runcexecutor.New(runcexecutor.Opt{
  24. Root: filepath.Join(root, "executor"),
  25. CommandCandidates: []string{"runc"},
  26. DefaultCgroupParent: cgroupParent,
  27. }, networkProviders)
  28. }
  29. type bridgeProvider struct {
  30. libnetwork.NetworkController
  31. }
  32. func (p *bridgeProvider) New() (network.Namespace, error) {
  33. n, err := p.NetworkByName(networkName)
  34. if err != nil {
  35. return nil, err
  36. }
  37. iface := &lnInterface{ready: make(chan struct{}), provider: p}
  38. iface.Once.Do(func() {
  39. go iface.init(p.NetworkController, n)
  40. })
  41. return iface, nil
  42. }
  43. type lnInterface struct {
  44. ep libnetwork.Endpoint
  45. sbx libnetwork.Sandbox
  46. sync.Once
  47. err error
  48. ready chan struct{}
  49. provider *bridgeProvider
  50. }
  51. func (iface *lnInterface) init(c libnetwork.NetworkController, n libnetwork.Network) {
  52. defer close(iface.ready)
  53. id := identity.NewID()
  54. ep, err := n.CreateEndpoint(id)
  55. if err != nil {
  56. iface.err = err
  57. return
  58. }
  59. sbx, err := c.NewSandbox(id)
  60. if err != nil {
  61. iface.err = err
  62. return
  63. }
  64. if err := ep.Join(sbx); err != nil {
  65. iface.err = err
  66. return
  67. }
  68. iface.sbx = sbx
  69. iface.ep = ep
  70. }
  71. func (iface *lnInterface) Set(s *specs.Spec) {
  72. <-iface.ready
  73. if iface.err != nil {
  74. return
  75. }
  76. // attach netns to bridge within the container namespace, using reexec in a prestart hook
  77. s.Hooks = &specs.Hooks{
  78. Prestart: []specs.Hook{{
  79. Path: filepath.Join("/proc", strconv.Itoa(os.Getpid()), "exe"),
  80. Args: []string{"libnetwork-setkey", iface.sbx.ContainerID(), iface.provider.NetworkController.ID()},
  81. }},
  82. }
  83. }
  84. func (iface *lnInterface) Close() error {
  85. <-iface.ready
  86. err := iface.sbx.Delete()
  87. if iface.err != nil {
  88. // iface.err takes precedence over cleanup errors
  89. return iface.err
  90. }
  91. return err
  92. }