2019-08-06 05:23:23 +00:00
|
|
|
package cniprovider
|
2019-07-10 21:42:30 +00:00
|
|
|
|
|
|
|
import (
|
2019-08-22 01:01:04 +00:00
|
|
|
"context"
|
2019-07-10 21:42:30 +00:00
|
|
|
"os"
|
2020-07-20 13:28:57 +00:00
|
|
|
"runtime"
|
2019-07-10 21:42:30 +00:00
|
|
|
|
2020-05-13 15:37:27 +00:00
|
|
|
cni "github.com/containerd/go-cni"
|
2019-07-11 23:47:45 +00:00
|
|
|
"github.com/gofrs/flock"
|
2019-07-10 21:42:30 +00:00
|
|
|
"github.com/moby/buildkit/identity"
|
2019-08-06 05:23:23 +00:00
|
|
|
"github.com/moby/buildkit/util/network"
|
2019-07-10 21:42:30 +00:00
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2019-08-06 05:23:23 +00:00
|
|
|
type Opt struct {
|
|
|
|
Root string
|
|
|
|
ConfigPath string
|
|
|
|
BinaryDir string
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(opt Opt) (network.Provider, error) {
|
|
|
|
if _, err := os.Stat(opt.ConfigPath); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to read cni config %q", opt.ConfigPath)
|
2019-07-10 21:42:30 +00:00
|
|
|
}
|
2019-08-06 05:23:23 +00:00
|
|
|
if _, err := os.Stat(opt.BinaryDir); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to read cni binary dir %q", opt.BinaryDir)
|
2019-07-10 21:42:30 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 07:43:58 +00:00
|
|
|
cniOptions := []cni.Opt{cni.WithPluginDir([]string{opt.BinaryDir}), cni.WithInterfacePrefix("eth")}
|
2020-07-20 13:28:57 +00:00
|
|
|
|
|
|
|
// Windows doesn't use CNI for loopback.
|
|
|
|
if runtime.GOOS != "windows" {
|
2021-04-19 07:43:58 +00:00
|
|
|
cniOptions = append([]cni.Opt{cni.WithMinNetworkCount(2)}, cniOptions...)
|
2020-07-20 13:28:57 +00:00
|
|
|
cniOptions = append(cniOptions, cni.WithLoNetwork)
|
|
|
|
}
|
|
|
|
|
|
|
|
cniOptions = append(cniOptions, cni.WithConfFile(opt.ConfigPath))
|
|
|
|
|
|
|
|
cniHandle, err := cni.New(cniOptions...)
|
2019-07-10 21:42:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-11 23:47:45 +00:00
|
|
|
cp := &cniProvider{CNI: cniHandle, root: opt.Root}
|
|
|
|
if err := cp.initNetwork(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cp, nil
|
2019-07-10 21:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type cniProvider struct {
|
|
|
|
cni.CNI
|
|
|
|
root string
|
|
|
|
}
|
|
|
|
|
2019-07-11 23:47:45 +00:00
|
|
|
func (c *cniProvider) initNetwork() error {
|
|
|
|
if v := os.Getenv("BUILDKIT_CNI_INIT_LOCK_PATH"); v != "" {
|
|
|
|
l := flock.New(v)
|
|
|
|
if err := l.Lock(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer l.Unlock()
|
|
|
|
}
|
|
|
|
ns, err := c.New()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ns.Close()
|
|
|
|
}
|
|
|
|
|
2019-08-06 05:23:23 +00:00
|
|
|
func (c *cniProvider) New() (network.Namespace, error) {
|
2019-07-10 21:42:30 +00:00
|
|
|
id := identity.NewID()
|
2020-07-20 10:26:05 +00:00
|
|
|
nativeID, err := createNetNS(c, id)
|
|
|
|
if err != nil {
|
2019-07-10 21:42:30 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-20 10:26:05 +00:00
|
|
|
if _, err := c.CNI.Setup(context.TODO(), id, nativeID); err != nil {
|
|
|
|
deleteNetNS(nativeID)
|
2019-07-10 21:42:30 +00:00
|
|
|
return nil, errors.Wrap(err, "CNI setup error")
|
|
|
|
}
|
|
|
|
|
2020-07-20 10:26:05 +00:00
|
|
|
return &cniNS{nativeID: nativeID, id: id, handle: c.CNI}, nil
|
2019-07-10 21:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type cniNS struct {
|
2020-07-20 10:26:05 +00:00
|
|
|
handle cni.CNI
|
|
|
|
id string
|
|
|
|
nativeID string
|
2019-07-10 21:42:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 22:51:00 +00:00
|
|
|
func (ns *cniNS) Set(s *specs.Spec) error {
|
2020-07-20 10:26:05 +00:00
|
|
|
return setNetNS(s, ns.nativeID)
|
2019-07-10 21:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ns *cniNS) Close() error {
|
2020-07-20 10:26:05 +00:00
|
|
|
err := ns.handle.Remove(context.TODO(), ns.id, ns.nativeID)
|
|
|
|
if err1 := unmountNetNS(ns.nativeID); err1 != nil && err == nil {
|
|
|
|
err = err1
|
2019-07-10 21:42:30 +00:00
|
|
|
}
|
2020-07-20 10:26:05 +00:00
|
|
|
if err1 := deleteNetNS(ns.nativeID); err1 != nil && err == nil {
|
|
|
|
err = err1
|
2019-07-10 21:42:30 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|