-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a new network configuration based on CNI bridge. Unlike the existing CNI provider this does not require user to provide CNI configuration and plugins externally. The minimal set of plugins required to use the network mode is provided together with buildkit. Currently, bridge mode is opt-in but intention is to make it default after a testing period of one release cycle. Signed-off-by: Tonis Tiigi <[email protected]>
- Loading branch information
1 parent
06c971f
commit feaf181
Showing
100 changed files
with
23,588 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
}`, 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) | ||
} |
Oops, something went wrong.