Skip to content

Commit

Permalink
Avoid Status lockup during setup and remove.
Browse files Browse the repository at this point in the history
Signed-off-by: Lantao Liu <lantaol@google.com>
  • Loading branch information
Random-Liu committed Apr 9, 2019
1 parent 0683513 commit e7a8c33
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
35 changes: 19 additions & 16 deletions cni.go
Expand Up @@ -89,6 +89,7 @@ func defaultCNIConfig() *libcni {
}
}

// New creates a new libcni instance.
func New(config ...CNIOpt) (CNI, error) {
cni := defaultCNIConfig()
var err error
Expand All @@ -100,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()
Expand All @@ -116,25 +118,35 @@ 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
}

// Setup setups the network in the namespace
func (c *libcni) Setup(id string, path string, opts ...NamespaceOpts) (*CNIResult, error) {
// 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()
if err := c.status(); err != nil {
return append([]*Network{}, c.networks...)
}

// Setup setups the network in the namespace
func (c *libcni) Setup(id string, path string, opts ...NamespaceOpts) (*CNIResult, error) {
if err := c.Status(); err != nil {
return nil, err
}
ns, err := newNamespace(id, path, opts...)
if err != nil {
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
Expand All @@ -146,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
Expand Down Expand Up @@ -204,10 +214,3 @@ func (c *libcni) GetConfig() *ConfigResult {
func (c *libcni) reset() {
c.networks = nil
}

func (c *libcni) status() error {
if len(c.networks) < c.networkCount {
return ErrCNINotInitialized
}
return nil
}
3 changes: 3 additions & 0 deletions result.go
Expand Up @@ -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),
}
Expand Down

0 comments on commit e7a8c33

Please sign in to comment.