Merge pull request #591 from Random-Liu/update-ocicni

Update ocicni to my fork.
This commit is contained in:
Mike Brown 2018-02-02 14:30:17 -06:00 committed by GitHub
commit bec05e31df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 63 additions and 76 deletions

View File

@ -124,7 +124,7 @@ func (c *criContainerdService) RunPodSandbox(ctx context.Context, r *runtime.Run
NetNS: sandbox.NetNSPath,
PortMappings: toCNIPortMappings(config.GetPortMappings()),
}
if err = c.netPlugin.SetUpPod(podNetwork); err != nil {
if _, err = c.netPlugin.SetUpPod(podNetwork); err != nil {
return nil, fmt.Errorf("failed to setup network for sandbox %q: %v", id, err)
}
defer func() {

View File

@ -23,6 +23,7 @@ import (
"sync"
"time"
"github.com/containernetworking/cni/pkg/types"
"github.com/cri-o/ocicni/pkg/ocicni"
)
@ -120,15 +121,16 @@ func (f *FakeCNIPlugin) Name() string {
}
// SetUpPod setup the network of PodSandbox.
func (f *FakeCNIPlugin) SetUpPod(podNetwork ocicni.PodNetwork) error {
func (f *FakeCNIPlugin) SetUpPod(podNetwork ocicni.PodNetwork) (types.Result, error) {
f.Lock()
defer f.Unlock()
f.appendCalled("SetUpPod", podNetwork)
if err := f.getError("SetUpPod"); err != nil {
return err
return nil, err
}
f.IPMap[podNetwork.NetNS] = generateIP()
return nil
// types.Result is unused.
return nil, nil
}
// TearDownPod teardown the network of PodSandbox.

View File

@ -8,7 +8,7 @@ github.com/containerd/typeurl f6943554a7e7e88b3c14aad190bf05932da84788
github.com/containernetworking/cni v0.6.0
github.com/containernetworking/plugins v0.6.0
github.com/coreos/go-systemd 48702e0da86bd25e76cfef347e2adeb434a0d0a6
github.com/cri-o/ocicni fc9c77cc55795c09bf1f74b2afd92078a793a69e
github.com/cri-o/ocicni 72ee66ecd10d0d37678bfd2384889582364c8197 https://github.com/Random-Liu/ocicni.git
github.com/davecgh/go-spew v1.1.0
github.com/docker/distribution b38e5838b7b2f2ad48e06ec4b500011976080621
github.com/docker/docker 86f080cff0914e9694068ed78d503701667c4c00

View File

@ -1,24 +0,0 @@
package ocicni
type cniNoOp struct {
}
func (noop *cniNoOp) Name() string {
return "CNINoOp"
}
func (noop *cniNoOp) SetUpPod(network PodNetwork) error {
return nil
}
func (noop *cniNoOp) TearDownPod(network PodNetwork) error {
return nil
}
func (noop *cniNoOp) GetPodNetworkStatus(network PodNetwork) (string, error) {
return "", nil
}
func (noop *cniNoOp) Status() error {
return nil
}

View File

@ -8,6 +8,7 @@ import (
"sort"
"strings"
"sync"
"time"
"github.com/containernetworking/cni/libcni"
cnitypes "github.com/containernetworking/cni/pkg/types"
@ -15,6 +16,8 @@ import (
"github.com/sirupsen/logrus"
)
const monitorNetDirPeriod = 1 * time.Second
type cniNetworkPlugin struct {
loNetwork *cniNetwork
@ -100,6 +103,13 @@ func (plugin *cniNetworkPlugin) podUnlock(podNetwork PodNetwork) {
}
func (plugin *cniNetworkPlugin) monitorNetDir() {
if _, err := os.Stat(plugin.pluginDir); err != nil {
if !os.IsNotExist(err) {
logrus.Errorf("failed to stat %q: %v", plugin.pluginDir, err)
}
return
}
logrus.Infof("Found CNI config directory %q", plugin.pluginDir)
watcher, err := fsnotify.NewWatcher()
if err != nil {
logrus.Errorf("could not create new watcher %v", err)
@ -107,6 +117,23 @@ func (plugin *cniNetworkPlugin) monitorNetDir() {
}
defer watcher.Close()
if err = watcher.Add(plugin.pluginDir); err != nil {
logrus.Errorf("Failed to add watch on %q: %v", plugin.pluginDir, err)
return
}
// Now that `watcher` is running and watching the `pluginDir`
// gather the initial configuration, before starting the
// goroutine which will actually process events. It has to be
// done in this order to avoid missing any updates which might
// otherwise occur between gathering the initial configuration
// and starting the watcher.
if err := plugin.syncNetworkConfig(); err != nil {
logrus.Infof("Initial CNI setting failed, continue monitoring: %v", err)
} else {
logrus.Infof("Initial CNI setting succeeded")
}
go func() {
for {
select {
@ -132,46 +159,14 @@ func (plugin *cniNetworkPlugin) monitorNetDir() {
}
}()
if err = watcher.Add(plugin.pluginDir); err != nil {
logrus.Error(err)
return
}
<-plugin.monitorNetDirChan
}
// InitCNI takes the plugin directory and cni directories where the cni files should be searched for
// Returns a valid plugin object and any error
// InitCNI takes the plugin directory and CNI directories where the CNI config
// files should be searched for. If no valid CNI configs exist, network requests
// will fail until valid CNI config files are present in the config directory.
func InitCNI(pluginDir string, cniDirs ...string) (CNIPlugin, error) {
plugin := probeNetworkPluginsWithVendorCNIDirPrefix(pluginDir, cniDirs, "")
var err error
plugin.nsenterPath, err = exec.LookPath("nsenter")
if err != nil {
return nil, err
}
// check if a default network exists, otherwise dump the CNI search and return a noop plugin
_, err = getDefaultCNINetwork(plugin.pluginDir, plugin.cniDirs, plugin.vendorCNIDirPrefix)
if err != nil {
if err != errMissingDefaultNetwork {
logrus.Warningf("Error in finding usable CNI plugin - %v", err)
// create a noop plugin instead
return &cniNoOp{}, nil
}
// Fail loudly if plugin directory doesn't exist, because fsnotify watcher
// won't be able to watch it.
if _, err := os.Stat(pluginDir); err != nil {
return nil, err
}
// We do not have a default network, we start the monitoring thread.
go plugin.monitorNetDir()
}
return plugin, nil
}
func probeNetworkPluginsWithVendorCNIDirPrefix(pluginDir string, cniDirs []string, vendorCNIDirPrefix string) *cniNetworkPlugin {
vendorCNIDirPrefix := ""
plugin := &cniNetworkPlugin{
defaultNetwork: nil,
loNetwork: getLoNetwork(cniDirs, vendorCNIDirPrefix),
@ -182,11 +177,18 @@ func probeNetworkPluginsWithVendorCNIDirPrefix(pluginDir string, cniDirs []strin
pods: make(map[string]*podLock),
}
// sync NetworkConfig in best effort during probing.
if err := plugin.syncNetworkConfig(); err != nil {
logrus.Error(err)
var err error
plugin.nsenterPath, err = exec.LookPath("nsenter")
if err != nil {
return nil, err
}
return plugin
go func() {
plugin.monitorNetDir()
time.Sleep(monitorNetDirPeriod)
}()
return plugin, nil
}
func getDefaultCNINetwork(pluginDir string, cniDirs []string, vendorCNIDirPrefix string) (*cniNetwork, error) {
@ -314,9 +316,9 @@ func (plugin *cniNetworkPlugin) Name() string {
return CNIPluginName
}
func (plugin *cniNetworkPlugin) SetUpPod(podNetwork PodNetwork) error {
func (plugin *cniNetworkPlugin) SetUpPod(podNetwork PodNetwork) (cnitypes.Result, error) {
if err := plugin.checkInitialized(); err != nil {
return err
return nil, err
}
plugin.podLock(podNetwork).Lock()
@ -325,16 +327,16 @@ func (plugin *cniNetworkPlugin) SetUpPod(podNetwork PodNetwork) error {
_, err := plugin.loNetwork.addToNetwork(podNetwork)
if err != nil {
logrus.Errorf("Error while adding to cni lo network: %s", err)
return err
return nil, err
}
_, err = plugin.getDefaultNetwork().addToNetwork(podNetwork)
result, err := plugin.getDefaultNetwork().addToNetwork(podNetwork)
if err != nil {
logrus.Errorf("Error while adding to cni network: %s", err)
return err
return nil, err
}
return err
return result, err
}
func (plugin *cniNetworkPlugin) TearDownPod(podNetwork PodNetwork) error {
@ -355,6 +357,9 @@ func (plugin *cniNetworkPlugin) GetPodNetworkStatus(podNetwork PodNetwork) (stri
defer plugin.podUnlock(podNetwork)
ip, err := getContainerIP(plugin.nsenterPath, podNetwork.NetNS, DefaultInterfaceName, "-4")
if err != nil {
ip, err = getContainerIP(plugin.nsenterPath, podNetwork.NetNS, DefaultInterfaceName, "-6")
}
if err != nil {
return "", err
}

View File

@ -1,5 +1,9 @@
package ocicni
import (
"github.com/containernetworking/cni/pkg/types"
)
const (
// DefaultInterfaceName is the string to be used for the interface name inside the net namespace
DefaultInterfaceName = "eth0"
@ -49,7 +53,7 @@ type CNIPlugin interface {
// SetUpPod is the method called after the sandbox container of
// the pod has been created but before the other containers of the
// pod are launched.
SetUpPod(network PodNetwork) error
SetUpPod(network PodNetwork) (types.Result, error)
// TearDownPod is the method called before a pod's sandbox container will be deleted
TearDownPod(network PodNetwork) error