1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

builder: setup code for a bridge networking

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi 2018-08-02 14:24:34 -07:00 committed by Tibor Vass
parent bc67a78862
commit d6424a088d
6 changed files with 112 additions and 10 deletions

View file

@ -15,6 +15,7 @@ import (
"github.com/docker/docker/daemon/images" "github.com/docker/docker/daemon/images"
"github.com/docker/docker/pkg/streamformatter" "github.com/docker/docker/pkg/streamformatter"
"github.com/docker/docker/pkg/system" "github.com/docker/docker/pkg/system"
"github.com/docker/libnetwork"
controlapi "github.com/moby/buildkit/api/services/control" controlapi "github.com/moby/buildkit/api/services/control"
"github.com/moby/buildkit/control" "github.com/moby/buildkit/control"
"github.com/moby/buildkit/identity" "github.com/moby/buildkit/identity"
@ -27,9 +28,10 @@ import (
// Opt is option struct required for creating the builder // Opt is option struct required for creating the builder
type Opt struct { type Opt struct {
SessionManager *session.Manager SessionManager *session.Manager
Root string Root string
Dist images.DistributionServices Dist images.DistributionServices
NetworkController libnetwork.NetworkController
} }
// Builder can build using BuildKit backend // Builder can build using BuildKit backend

View file

@ -90,7 +90,7 @@ func newController(rt http.RoundTripper, opt Opt) (*control.Controller, error) {
return nil, err return nil, err
} }
exec, err := newExecutor(root) exec, err := newExecutor(root, opt.NetworkController)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -3,15 +3,108 @@
package buildkit package buildkit
import ( import (
"fmt"
"path/filepath" "path/filepath"
"sync"
"github.com/docker/libnetwork"
"github.com/moby/buildkit/executor" "github.com/moby/buildkit/executor"
"github.com/moby/buildkit/executor/runcexecutor" "github.com/moby/buildkit/executor/runcexecutor"
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/util/network"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
) )
func newExecutor(root string) (executor.Executor, error) { const networkName = "bridge"
func newExecutor(root string, net libnetwork.NetworkController) (executor.Executor, error) {
return runcexecutor.New(runcexecutor.Opt{ return runcexecutor.New(runcexecutor.Opt{
Root: filepath.Join(root, "executor"), Root: filepath.Join(root, "executor"),
CommandCandidates: []string{"docker-runc", "runc"}, CommandCandidates: []string{"docker-runc", "runc"},
}) }, &bridgeProvider{NetworkController: net})
}
type bridgeProvider struct {
libnetwork.NetworkController
}
func (p *bridgeProvider) NewInterface() (network.Interface, error) {
n, err := p.NetworkByName(networkName)
if err != nil {
return nil, err
}
iface := &lnInterface{ready: make(chan struct{})}
iface.Once.Do(func() {
go iface.init(p.NetworkController, n)
})
return iface, nil
}
func (p *bridgeProvider) Release(iface network.Interface) error {
go func() {
if err := p.release(iface); err != nil {
logrus.Errorf("%s", err)
}
}()
return nil
}
func (p *bridgeProvider) release(iface network.Interface) error {
li, ok := iface.(*lnInterface)
if !ok {
return errors.Errorf("invalid interface %T", iface)
}
err := li.sbx.Delete()
if err1 := li.ep.Delete(true); err1 != nil && err == nil {
err = err1
}
return err
}
type lnInterface struct {
ep libnetwork.Endpoint
sbx libnetwork.Sandbox
sync.Once
err error
ready chan struct{}
}
func (iface *lnInterface) init(c libnetwork.NetworkController, n libnetwork.Network) {
defer close(iface.ready)
id := identity.NewID()
ep, err := n.CreateEndpoint(id)
if err != nil {
iface.err = err
return
}
sbx, err := c.NewSandbox(id)
if err != nil {
iface.err = err
return
}
if err := ep.Join(sbx); err != nil {
iface.err = err
return
}
iface.sbx = sbx
iface.ep = ep
}
func (iface *lnInterface) Set(pid int) error {
<-iface.ready
if iface.err != nil {
return iface.err
}
return iface.sbx.SetKey(fmt.Sprintf("/proc/%d/ns/net", pid))
}
func (iface *lnInterface) Remove(pid int) error {
return nil
} }

View file

@ -5,11 +5,12 @@ import (
"errors" "errors"
"io" "io"
"github.com/docker/libnetwork"
"github.com/moby/buildkit/cache" "github.com/moby/buildkit/cache"
"github.com/moby/buildkit/executor" "github.com/moby/buildkit/executor"
) )
func newExecutor(_ string) (executor.Executor, error) { func newExecutor(_ string, _ libnetwork.NetworkController) (executor.Executor, error) {
return &winExecutor{}, nil return &winExecutor{}, nil
} }

View file

@ -286,9 +286,10 @@ func newRouterOptions(config *config.Config, daemon *daemon.Daemon) (routerOptio
return opts, err return opts, err
} }
bk, err := buildkit.New(buildkit.Opt{ bk, err := buildkit.New(buildkit.Opt{
SessionManager: sm, SessionManager: sm,
Root: filepath.Join(config.Root, "buildkit"), Root: filepath.Join(config.Root, "buildkit"),
Dist: daemon.DistributionServices(), Dist: daemon.DistributionServices(),
NetworkController: daemon.NetworkController(),
}) })
if err != nil { if err != nil {
return opts, err return opts, err

View file

@ -49,6 +49,11 @@ func (daemon *Daemon) NetworkControllerEnabled() bool {
return daemon.netController != nil return daemon.netController != nil
} }
// NetworkController returns the network controller created by the daemon.
func (daemon *Daemon) NetworkController() libnetwork.NetworkController {
return daemon.netController
}
// FindNetwork returns a network based on: // FindNetwork returns a network based on:
// 1. Full ID // 1. Full ID
// 2. Full Name // 2. Full Name