Update go-cni to 891c2a41e18144b2d7921f971d6c9789a68046b2.

Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
Lantao Liu 2019-04-09 17:07:38 -07:00
parent ebb0928057
commit e425bd019a
7 changed files with 115 additions and 19 deletions

View File

@ -50,3 +50,8 @@ func (f *FakeCNIPlugin) Status() error {
func (f *FakeCNIPlugin) Load(opts ...cni.CNIOpt) error { func (f *FakeCNIPlugin) Load(opts ...cni.CNIOpt) error {
return f.LoadErr return f.LoadErr
} }
// GetConfig returns a copy of the CNI plugin configurations as parsed by CNI
func (f *FakeCNIPlugin) GetConfig() *cni.ConfigResult {
return nil
}

View File

@ -5,7 +5,7 @@ github.com/containerd/console c12b1e7919c14469339a5d38f2f8ed9b64a9de23
github.com/containerd/containerd 591e52c504b23a89096bd7d3091a32fcfa2e92fc github.com/containerd/containerd 591e52c504b23a89096bd7d3091a32fcfa2e92fc
github.com/containerd/continuity bd77b46c8352f74eb12c85bdc01f4b90f69d66b4 github.com/containerd/continuity bd77b46c8352f74eb12c85bdc01f4b90f69d66b4
github.com/containerd/fifo 3d5202aec260678c48179c56f40e6f38a095738c 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/go-runc 5a6d9f37cfa36b15efba46dc7ea349fa9b7143c3
github.com/containerd/ttrpc f02858b1457c5ca3aaec3a0803eb0d59f96e41d6 github.com/containerd/ttrpc f02858b1457c5ca3aaec3a0803eb0d59f96e41d6
github.com/containerd/typeurl a93fcdb778cd272c6e9b3028b2f42d813e785d40 github.com/containerd/typeurl a93fcdb778cd272c6e9b3028b2f42d813e785d40

View File

@ -47,3 +47,14 @@ func main() {
fmt.Printf("IP of the default interface %s:%s", defaultIfName, IP) 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.

View File

@ -22,6 +22,7 @@ import (
"sync" "sync"
cnilibrary "github.com/containernetworking/cni/libcni" cnilibrary "github.com/containernetworking/cni/libcni"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current" "github.com/containernetworking/cni/pkg/types/current"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -35,6 +36,34 @@ type CNI interface {
Load(opts ...CNIOpt) error Load(opts ...CNIOpt) error
// Status checks the status of the cni initialization // Status checks the status of the cni initialization
Status() error 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 { type libcni struct {
@ -60,6 +89,7 @@ func defaultCNIConfig() *libcni {
} }
} }
// New creates a new libcni instance.
func New(config ...CNIOpt) (CNI, error) { func New(config ...CNIOpt) (CNI, error) {
cni := defaultCNIConfig() cni := defaultCNIConfig()
var err error var err error
@ -71,6 +101,7 @@ func New(config ...CNIOpt) (CNI, error) {
return cni, nil return cni, nil
} }
// Load loads the latest config from cni config files.
func (c *libcni) Load(opts ...CNIOpt) error { func (c *libcni) Load(opts ...CNIOpt) error {
var err error var err error
c.Lock() c.Lock()
@ -87,17 +118,27 @@ func (c *libcni) Load(opts ...CNIOpt) error {
return nil return nil
} }
// Status returns the status of CNI initialization.
func (c *libcni) Status() error { func (c *libcni) Status() error {
c.RLock() c.RLock()
defer c.RUnlock() 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 // Setup setups the network in the namespace
func (c *libcni) Setup(id string, path string, opts ...NamespaceOpts) (*CNIResult, error) { func (c *libcni) Setup(id string, path string, opts ...NamespaceOpts) (*CNIResult, error) {
c.RLock() if err := c.Status(); err != nil {
defer c.RUnlock()
if err := c.status(); err != nil {
return nil, err return nil, err
} }
ns, err := newNamespace(id, path, opts...) ns, err := newNamespace(id, path, opts...)
@ -105,7 +146,7 @@ func (c *libcni) Setup(id string, path string, opts ...NamespaceOpts) (*CNIResul
return nil, err return nil, err
} }
var results []*current.Result var results []*current.Result
for _, network := range c.networks { for _, network := range c.Networks() {
r, err := network.Attach(ns) r, err := network.Attach(ns)
if err != nil { if err != nil {
return nil, err 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 // Remove removes the network config from the namespace
func (c *libcni) Remove(id string, path string, opts ...NamespaceOpts) error { func (c *libcni) Remove(id string, path string, opts ...NamespaceOpts) error {
c.RLock() if err := c.Status(); err != nil {
defer c.RUnlock()
if err := c.status(); err != nil {
return err return err
} }
ns, err := newNamespace(id, path, opts...) ns, err := newNamespace(id, path, opts...)
if err != nil { if err != nil {
return err return err
} }
for _, network := range c.networks { for _, network := range c.Networks() {
if err := network.Remove(ns); err != nil { if err := network.Remove(ns); err != nil {
// Based on CNI spec v0.7.0, empty network namespace is allowed to // Based on CNI spec v0.7.0, empty network namespace is allowed to
// do best effort cleanup. However, it is not handled consistently // 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 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() { func (c *libcni) reset() {
c.networks = nil c.networks = nil
} }
func (c *libcni) status() error {
if len(c.networks) < c.networkCount {
return ErrCNINotInitialized
}
return nil
}

View File

@ -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 package cni
import ( import (
@ -13,7 +29,7 @@ var (
ErrLoad = errors.New("failed to load cni config") 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 { func IsCNINotInitialized(err error) bool {
return errors.Cause(err) == ErrCNINotInitialized return errors.Cause(err) == ErrCNINotInitialized
} }

View File

@ -55,7 +55,7 @@ func WithPluginConfDir(dir string) CNIOpt {
} }
// WithMinNetworkCount can be used to configure the // 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. // for the status to report success. By default its 1.
func WithMinNetworkCount(count int) CNIOpt { func WithMinNetworkCount(count int) CNIOpt {
return func(c *libcni) error { return func(c *libcni) error {

View File

@ -54,6 +54,9 @@ type Config struct {
// c) DNS information. Dictionary that includes DNS information for nameservers, // c) DNS information. Dictionary that includes DNS information for nameservers,
// domain, search domains and options. // domain, search domains and options.
func (c *libcni) GetCNIResultFromResults(results []*current.Result) (*CNIResult, error) { func (c *libcni) GetCNIResultFromResults(results []*current.Result) (*CNIResult, error) {
c.RLock()
defer c.RUnlock()
r := &CNIResult{ r := &CNIResult{
Interfaces: make(map[string]*Config), Interfaces: make(map[string]*Config),
} }