executor_linux.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package buildkit
  2. import (
  3. "context"
  4. "net"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. "sync"
  9. "github.com/containerd/log"
  10. "github.com/docker/docker/daemon/config"
  11. "github.com/docker/docker/libnetwork"
  12. "github.com/docker/docker/pkg/idtools"
  13. "github.com/docker/docker/pkg/stringid"
  14. "github.com/moby/buildkit/executor"
  15. "github.com/moby/buildkit/executor/oci"
  16. "github.com/moby/buildkit/executor/resources"
  17. "github.com/moby/buildkit/executor/runcexecutor"
  18. "github.com/moby/buildkit/identity"
  19. "github.com/moby/buildkit/solver/pb"
  20. "github.com/moby/buildkit/util/network"
  21. specs "github.com/opencontainers/runtime-spec/specs-go"
  22. )
  23. const networkName = "bridge"
  24. func newExecutor(root, cgroupParent string, net *libnetwork.Controller, dnsConfig *oci.DNSConfig, rootless bool, idmap idtools.IdentityMapping, apparmorProfile string) (executor.Executor, error) {
  25. netRoot := filepath.Join(root, "net")
  26. networkProviders := map[pb.NetMode]network.Provider{
  27. pb.NetMode_UNSET: &bridgeProvider{Controller: net, Root: netRoot},
  28. pb.NetMode_HOST: network.NewHostProvider(),
  29. pb.NetMode_NONE: network.NewNoneProvider(),
  30. }
  31. // make sure net state directory is cleared from previous state
  32. fis, err := os.ReadDir(netRoot)
  33. if err == nil {
  34. for _, fi := range fis {
  35. fp := filepath.Join(netRoot, fi.Name())
  36. if err := os.RemoveAll(fp); err != nil {
  37. log.G(context.TODO()).WithError(err).Errorf("failed to delete old network state: %v", fp)
  38. }
  39. }
  40. }
  41. // Returning a non-nil but empty *IdentityMapping breaks BuildKit:
  42. // https://github.com/moby/moby/pull/39444
  43. pidmap := &idmap
  44. if idmap.Empty() {
  45. pidmap = nil
  46. }
  47. rm, err := resources.NewMonitor()
  48. if err != nil {
  49. return nil, err
  50. }
  51. return runcexecutor.New(runcexecutor.Opt{
  52. Root: filepath.Join(root, "executor"),
  53. CommandCandidates: []string{"runc"},
  54. DefaultCgroupParent: cgroupParent,
  55. Rootless: rootless,
  56. NoPivot: os.Getenv("DOCKER_RAMDISK") != "",
  57. IdentityMapping: pidmap,
  58. DNS: dnsConfig,
  59. ApparmorProfile: apparmorProfile,
  60. ResourceMonitor: rm,
  61. }, networkProviders)
  62. }
  63. type bridgeProvider struct {
  64. *libnetwork.Controller
  65. Root string
  66. }
  67. func (p *bridgeProvider) New(ctx context.Context, hostname string) (network.Namespace, error) {
  68. n, err := p.NetworkByName(networkName)
  69. if err != nil {
  70. return nil, err
  71. }
  72. iface := &lnInterface{ready: make(chan struct{}), provider: p}
  73. iface.Once.Do(func() {
  74. go iface.init(p.Controller, n)
  75. })
  76. return iface, nil
  77. }
  78. func (p *bridgeProvider) Close() error {
  79. return nil
  80. }
  81. type lnInterface struct {
  82. ep *libnetwork.Endpoint
  83. sbx *libnetwork.Sandbox
  84. sync.Once
  85. err error
  86. ready chan struct{}
  87. provider *bridgeProvider
  88. }
  89. func (iface *lnInterface) init(c *libnetwork.Controller, n *libnetwork.Network) {
  90. defer close(iface.ready)
  91. id := identity.NewID()
  92. ep, err := n.CreateEndpoint(id, libnetwork.CreateOptionDisableResolution())
  93. if err != nil {
  94. iface.err = err
  95. return
  96. }
  97. sbx, err := c.NewSandbox(id, libnetwork.OptionUseExternalKey(), libnetwork.OptionHostsPath(filepath.Join(iface.provider.Root, id, "hosts")),
  98. libnetwork.OptionResolvConfPath(filepath.Join(iface.provider.Root, id, "resolv.conf")))
  99. if err != nil {
  100. iface.err = err
  101. return
  102. }
  103. if err := ep.Join(sbx); err != nil {
  104. iface.err = err
  105. return
  106. }
  107. iface.sbx = sbx
  108. iface.ep = ep
  109. }
  110. // TODO(neersighted): Unstub Sample(), and collect data from the libnetwork Endpoint.
  111. func (iface *lnInterface) Sample() (*network.Sample, error) {
  112. return &network.Sample{}, nil
  113. }
  114. func (iface *lnInterface) Set(s *specs.Spec) error {
  115. <-iface.ready
  116. if iface.err != nil {
  117. log.G(context.TODO()).WithError(iface.err).Error("failed to set networking spec")
  118. return iface.err
  119. }
  120. shortNetCtlrID := stringid.TruncateID(iface.provider.Controller.ID())
  121. // attach netns to bridge within the container namespace, using reexec in a prestart hook
  122. s.Hooks = &specs.Hooks{
  123. Prestart: []specs.Hook{{
  124. Path: filepath.Join("/proc", strconv.Itoa(os.Getpid()), "exe"),
  125. Args: []string{"libnetwork-setkey", "-exec-root=" + iface.provider.Config().ExecRoot, iface.sbx.ContainerID(), shortNetCtlrID},
  126. }},
  127. }
  128. return nil
  129. }
  130. func (iface *lnInterface) Close() error {
  131. <-iface.ready
  132. if iface.sbx != nil {
  133. go func() {
  134. if err := iface.sbx.Delete(); err != nil {
  135. log.G(context.TODO()).WithError(err).Errorf("failed to delete builder network sandbox")
  136. }
  137. if err := os.RemoveAll(filepath.Join(iface.provider.Root, iface.sbx.ContainerID())); err != nil {
  138. log.G(context.TODO()).WithError(err).Errorf("failed to delete builder sandbox directory")
  139. }
  140. }()
  141. }
  142. return iface.err
  143. }
  144. func getDNSConfig(cfg config.DNSConfig) *oci.DNSConfig {
  145. if cfg.DNS != nil || cfg.DNSSearch != nil || cfg.DNSOptions != nil {
  146. return &oci.DNSConfig{
  147. Nameservers: ipAddresses(cfg.DNS),
  148. SearchDomains: cfg.DNSSearch,
  149. Options: cfg.DNSOptions,
  150. }
  151. }
  152. return nil
  153. }
  154. func ipAddresses(ips []net.IP) []string {
  155. var addrs []string
  156. for _, ip := range ips {
  157. addrs = append(addrs, ip.String())
  158. }
  159. return addrs
  160. }