-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add CNI bridge network provider #4352
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
package cniprovider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
|
||
cni "github.com/containerd/go-cni" | ||
"github.com/moby/buildkit/util/bklog" | ||
"github.com/moby/buildkit/util/network" | ||
"github.com/pkg/errors" | ||
"github.com/vishvananda/netlink" | ||
) | ||
|
||
func NewBridge(opt Opt) (network.Provider, error) { | ||
cniOptions := []cni.Opt{cni.WithInterfacePrefix("eth")} | ||
bridgeBinName := "bridge" | ||
loopbackBinName := "loopback" | ||
hostLocalBinName := "host-local" | ||
firewallBinName := "firewall" | ||
var setup bool | ||
// binaries shipping with buildkit | ||
for { | ||
var dirs []string | ||
|
||
bridgePath, err := exec.LookPath("buildkit-cni-bridge") | ||
if err != nil { | ||
break | ||
} | ||
var bridgeDir string | ||
bridgeDir, bridgeBinName = filepath.Split(bridgePath) | ||
dirs = append(dirs, bridgeDir) | ||
|
||
loopbackPath, err := exec.LookPath("buildkit-cni-loopback") | ||
if err != nil { | ||
break | ||
} | ||
var loopbackDir string | ||
loopbackDir, loopbackBinName = filepath.Split(loopbackPath) | ||
if loopbackDir != bridgeDir { | ||
dirs = append(dirs, loopbackDir) | ||
} | ||
|
||
hostLocalPath, err := exec.LookPath("buildkit-cni-host-local") | ||
if err != nil { | ||
break | ||
} | ||
var hostLocalDir string | ||
hostLocalDir, hostLocalBinName = filepath.Split(hostLocalPath) | ||
if hostLocalDir != bridgeDir && hostLocalDir != loopbackDir { | ||
dirs = append(dirs, hostLocalDir) | ||
} | ||
|
||
firewallPath, err := exec.LookPath("buildkit-cni-firewall") | ||
if err != nil { | ||
break | ||
} | ||
var firewallDir string | ||
firewallDir, firewallBinName = filepath.Split(firewallPath) | ||
if firewallDir != bridgeDir && firewallDir != loopbackDir && firewallDir != hostLocalDir { | ||
dirs = append(dirs, firewallDir) | ||
} | ||
|
||
cniOptions = append(cniOptions, cni.WithPluginDir(dirs)) | ||
setup = true | ||
break //nolint: staticcheck | ||
} | ||
|
||
if !setup { | ||
fn := filepath.Join(opt.BinaryDir, "bridge") | ||
if _, err := os.Stat(fn); err != nil { | ||
return nil, errors.Wrapf(err, "failed to find CNI bridge %q or buildkit-cni-bridge", fn) | ||
} | ||
|
||
cniOptions = append(cniOptions, cni.WithPluginDir([]string{opt.BinaryDir})) | ||
} | ||
|
||
cniOptions = append(cniOptions, cni.WithConfListBytes([]byte(fmt.Sprintf(`{ | ||
"cniVersion": "1.0.0", | ||
"name": "buildkit", | ||
"plugins": [ | ||
{ | ||
"type": "%s" | ||
}, | ||
{ | ||
"type": "%s", | ||
"bridge": "%s", | ||
"isDefaultGateway": true, | ||
"ipMasq": true, | ||
"ipam": { | ||
"type": "%s", | ||
"ranges": [ | ||
[ | ||
{ "subnet": "%s" } | ||
] | ||
] | ||
} | ||
}, | ||
{ | ||
"type": "%s", | ||
"ingressPolicy": "same-bridge" | ||
} | ||
] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this would be another plugin we would need to include? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes ( |
||
}`, loopbackBinName, bridgeBinName, opt.BridgeName, hostLocalBinName, opt.BridgeSubnet, firewallBinName)))) | ||
|
||
unlock, err := initLock() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer unlock() | ||
|
||
createBridge := true | ||
if _, err := bridgeByName(opt.BridgeName); err == nil { | ||
createBridge = false | ||
} | ||
|
||
cniHandle, err := cni.New(cniOptions...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cp := &cniProvider{ | ||
CNI: cniHandle, | ||
root: opt.Root, | ||
} | ||
|
||
if createBridge { | ||
cp.release = func() error { | ||
if err := removeBridge(opt.BridgeName); err != nil { | ||
bklog.L.Errorf("failed to remove bridge %q: %v", opt.BridgeName, err) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
cleanOldNamespaces(cp) | ||
|
||
cp.nsPool = &cniPool{targetSize: opt.PoolSize, provider: cp} | ||
if err := cp.initNetwork(false); err != nil { | ||
return nil, err | ||
} | ||
go cp.nsPool.fillPool(context.TODO()) | ||
return cp, nil | ||
} | ||
|
||
func bridgeByName(name string) (*netlink.Bridge, error) { | ||
l, err := netlink.LinkByName(name) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "could not lookup %q", name) | ||
} | ||
br, ok := l.(*netlink.Bridge) | ||
if !ok { | ||
return nil, errors.Errorf("%q already exists but is not a bridge", name) | ||
} | ||
return br, nil | ||
} | ||
|
||
func removeBridge(name string) error { | ||
br, err := bridgeByName(name) | ||
if err != nil { | ||
return err | ||
} | ||
return netlink.LinkDel(br) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a follow up PR, could you add a documentation to explain this default CIDR and other configuration?