-
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 <tonistiigi@gmail.com>
- Loading branch information
1 parent
8dfc926
commit 16c853b
Showing
98 changed files
with
23,523 additions
and
36 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,150 @@ | ||
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" | ||
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) | ||
} | ||
|
||
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": "10.10.0.0/16" } | ||
] | ||
] | ||
} | ||
} | ||
] | ||
}`, loopbackBinName, bridgeBinName, opt.BridgeName, hostLocalBinName)))) | ||
|
||
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) | ||
} |
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
Oops, something went wrong.