From e425bd019a95d1ff308fd783790771cd3d836497 Mon Sep 17 00:00:00 2001 From: Lantao Liu Date: Tue, 9 Apr 2019 17:07:38 -0700 Subject: [PATCH] Update go-cni to 891c2a41e18144b2d7921f971d6c9789a68046b2. Signed-off-by: Lantao Liu --- pkg/server/testing/fake_cni_plugin.go | 5 + vendor.conf | 2 +- vendor/github.com/containerd/go-cni/README.md | 11 +++ vendor/github.com/containerd/go-cni/cni.go | 93 +++++++++++++++---- vendor/github.com/containerd/go-cni/errors.go | 18 +++- vendor/github.com/containerd/go-cni/opts.go | 2 +- vendor/github.com/containerd/go-cni/result.go | 3 + 7 files changed, 115 insertions(+), 19 deletions(-) diff --git a/pkg/server/testing/fake_cni_plugin.go b/pkg/server/testing/fake_cni_plugin.go index 4a7e8d602..22250b2eb 100644 --- a/pkg/server/testing/fake_cni_plugin.go +++ b/pkg/server/testing/fake_cni_plugin.go @@ -50,3 +50,8 @@ func (f *FakeCNIPlugin) Status() error { func (f *FakeCNIPlugin) Load(opts ...cni.CNIOpt) error { return f.LoadErr } + +// GetConfig returns a copy of the CNI plugin configurations as parsed by CNI +func (f *FakeCNIPlugin) GetConfig() *cni.ConfigResult { + return nil +} diff --git a/vendor.conf b/vendor.conf index 7d482fdeb..22e8374a7 100644 --- a/vendor.conf +++ b/vendor.conf @@ -5,7 +5,7 @@ github.com/containerd/console c12b1e7919c14469339a5d38f2f8ed9b64a9de23 github.com/containerd/containerd 591e52c504b23a89096bd7d3091a32fcfa2e92fc github.com/containerd/continuity bd77b46c8352f74eb12c85bdc01f4b90f69d66b4 github.com/containerd/fifo 3d5202aec260678c48179c56f40e6f38a095738c -github.com/containerd/go-cni 40bcf8ec8acd7372be1d77031d585d5d8e561c90 +github.com/containerd/go-cni 891c2a41e18144b2d7921f971d6c9789a68046b2 github.com/containerd/go-runc 5a6d9f37cfa36b15efba46dc7ea349fa9b7143c3 github.com/containerd/ttrpc f02858b1457c5ca3aaec3a0803eb0d59f96e41d6 github.com/containerd/typeurl a93fcdb778cd272c6e9b3028b2f42d813e785d40 diff --git a/vendor/github.com/containerd/go-cni/README.md b/vendor/github.com/containerd/go-cni/README.md index c0856aebc..fea3ce29f 100644 --- a/vendor/github.com/containerd/go-cni/README.md +++ b/vendor/github.com/containerd/go-cni/README.md @@ -47,3 +47,14 @@ func main() { fmt.Printf("IP of the default interface %s:%s", defaultIfName, IP) } ``` + +## Project details + +The go-cni is a containerd sub-project, licensed under the [Apache 2.0 license](./LICENSE). +As a containerd sub-project, you will find the: + + * [Project governance](https://github.com/containerd/project/blob/master/GOVERNANCE.md), + * [Maintainers](https://github.com/containerd/project/blob/master/MAINTAINERS), + * and [Contributing guidelines](https://github.com/containerd/project/blob/master/CONTRIBUTING.md) + +information in our [`containerd/project`](https://github.com/containerd/project) repository. diff --git a/vendor/github.com/containerd/go-cni/cni.go b/vendor/github.com/containerd/go-cni/cni.go index 31350e304..c706793ed 100644 --- a/vendor/github.com/containerd/go-cni/cni.go +++ b/vendor/github.com/containerd/go-cni/cni.go @@ -22,6 +22,7 @@ import ( "sync" cnilibrary "github.com/containernetworking/cni/libcni" + "github.com/containernetworking/cni/pkg/types" "github.com/containernetworking/cni/pkg/types/current" "github.com/pkg/errors" ) @@ -35,6 +36,34 @@ type CNI interface { Load(opts ...CNIOpt) error // Status checks the status of the cni initialization Status() error + // GetConfig returns a copy of the CNI plugin configurations as parsed by CNI + GetConfig() *ConfigResult +} + +type ConfigResult struct { + PluginDirs []string + PluginConfDir string + Prefix string + Networks []*ConfNetwork +} + +type ConfNetwork struct { + Config *NetworkConfList + IFName string +} + +// NetworkConfList is a source bytes to string version of cnilibrary.NetworkConfigList +type NetworkConfList struct { + Name string + CNIVersion string + Plugins []*NetworkConf + Source string +} + +// NetworkConf is a source bytes to string conversion of cnilibrary.NetworkConfig +type NetworkConf struct { + Network *types.NetConf + Source string } type libcni struct { @@ -60,6 +89,7 @@ func defaultCNIConfig() *libcni { } } +// New creates a new libcni instance. func New(config ...CNIOpt) (CNI, error) { cni := defaultCNIConfig() var err error @@ -71,6 +101,7 @@ func New(config ...CNIOpt) (CNI, error) { return cni, nil } +// Load loads the latest config from cni config files. func (c *libcni) Load(opts ...CNIOpt) error { var err error c.Lock() @@ -87,17 +118,27 @@ func (c *libcni) Load(opts ...CNIOpt) error { return nil } +// Status returns the status of CNI initialization. func (c *libcni) Status() error { c.RLock() defer c.RUnlock() - return c.status() + if len(c.networks) < c.networkCount { + return ErrCNINotInitialized + } + return nil +} + +// Networks returns all the configured networks. +// NOTE: Caller MUST NOT modify anything in the returned array. +func (c *libcni) Networks() []*Network { + c.RLock() + defer c.RUnlock() + return append([]*Network{}, c.networks...) } // Setup setups the network in the namespace func (c *libcni) Setup(id string, path string, opts ...NamespaceOpts) (*CNIResult, error) { - c.RLock() - defer c.RUnlock() - if err := c.status(); err != nil { + if err := c.Status(); err != nil { return nil, err } ns, err := newNamespace(id, path, opts...) @@ -105,7 +146,7 @@ func (c *libcni) Setup(id string, path string, opts ...NamespaceOpts) (*CNIResul return nil, err } var results []*current.Result - for _, network := range c.networks { + for _, network := range c.Networks() { r, err := network.Attach(ns) if err != nil { return nil, err @@ -117,16 +158,14 @@ func (c *libcni) Setup(id string, path string, opts ...NamespaceOpts) (*CNIResul // Remove removes the network config from the namespace func (c *libcni) Remove(id string, path string, opts ...NamespaceOpts) error { - c.RLock() - defer c.RUnlock() - if err := c.status(); err != nil { + if err := c.Status(); err != nil { return err } ns, err := newNamespace(id, path, opts...) if err != nil { return err } - for _, network := range c.networks { + for _, network := range c.Networks() { if err := network.Remove(ns); err != nil { // Based on CNI spec v0.7.0, empty network namespace is allowed to // do best effort cleanup. However, it is not handled consistently @@ -143,13 +182,35 @@ func (c *libcni) Remove(id string, path string, opts ...NamespaceOpts) error { return nil } +// GetConfig returns a copy of the CNI plugin configurations as parsed by CNI +func (c *libcni) GetConfig() *ConfigResult { + c.RLock() + defer c.RUnlock() + r := &ConfigResult{ + PluginDirs: c.config.pluginDirs, + PluginConfDir: c.config.pluginConfDir, + Prefix: c.config.prefix, + } + for _, network := range c.networks { + conf := &NetworkConfList{ + Name: network.config.Name, + CNIVersion: network.config.CNIVersion, + Source: string(network.config.Bytes), + } + for _, plugin := range network.config.Plugins { + conf.Plugins = append(conf.Plugins, &NetworkConf{ + Network: plugin.Network, + Source: string(plugin.Bytes), + }) + } + r.Networks = append(r.Networks, &ConfNetwork{ + Config: conf, + IFName: network.ifName, + }) + } + return r +} + func (c *libcni) reset() { c.networks = nil } - -func (c *libcni) status() error { - if len(c.networks) < c.networkCount { - return ErrCNINotInitialized - } - return nil -} diff --git a/vendor/github.com/containerd/go-cni/errors.go b/vendor/github.com/containerd/go-cni/errors.go index c6f468924..28761711e 100644 --- a/vendor/github.com/containerd/go-cni/errors.go +++ b/vendor/github.com/containerd/go-cni/errors.go @@ -1,3 +1,19 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + package cni import ( @@ -13,7 +29,7 @@ var ( ErrLoad = errors.New("failed to load cni config") ) -// IsCNINotInitialized returns true if the error is due cni config not being intialized +// IsCNINotInitialized returns true if the error is due to cni config not being initialized func IsCNINotInitialized(err error) bool { return errors.Cause(err) == ErrCNINotInitialized } diff --git a/vendor/github.com/containerd/go-cni/opts.go b/vendor/github.com/containerd/go-cni/opts.go index be533f16e..c82483617 100644 --- a/vendor/github.com/containerd/go-cni/opts.go +++ b/vendor/github.com/containerd/go-cni/opts.go @@ -55,7 +55,7 @@ func WithPluginConfDir(dir string) CNIOpt { } // WithMinNetworkCount can be used to configure the -// minimum networks to be configured and initalized +// minimum networks to be configured and initialized // for the status to report success. By default its 1. func WithMinNetworkCount(count int) CNIOpt { return func(c *libcni) error { diff --git a/vendor/github.com/containerd/go-cni/result.go b/vendor/github.com/containerd/go-cni/result.go index d79967682..1e958dc76 100644 --- a/vendor/github.com/containerd/go-cni/result.go +++ b/vendor/github.com/containerd/go-cni/result.go @@ -54,6 +54,9 @@ type Config struct { // c) DNS information. Dictionary that includes DNS information for nameservers, // domain, search domains and options. func (c *libcni) GetCNIResultFromResults(results []*current.Result) (*CNIResult, error) { + c.RLock() + defer c.RUnlock() + r := &CNIResult{ Interfaces: make(map[string]*Config), }