executor_unix.go 2.4 KB

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