diff --git a/cni.go b/cni.go index c99df43..21fab91 100644 --- a/cni.go +++ b/cni.go @@ -137,8 +137,8 @@ func (c *libcni) Load(opts ...Opt) error { func (c *libcni) Status() error { c.RLock() defer c.RUnlock() - if len(c.networks) < c.networkCount { - return ErrCNINotInitialized + if err := c.ready(); err != nil { + return err } // STATUS is only called for CNI Version 1.1.0 or greater. It is ignored for previous versions. @@ -163,10 +163,8 @@ func (c *libcni) Networks() []*Network { // Setup setups the network in the namespace and returns a Result func (c *libcni) Setup(ctx context.Context, id string, path string, opts ...NamespaceOpts) (*Result, error) { - c.Lock() - defer c.Unlock() - if len(c.networks) < c.networkCount { - return nil, ErrCNINotInitialized + if err := c.ready(); err != nil { + return nil, err } ns, err := newNamespace(id, path, opts...) if err != nil { @@ -181,10 +179,8 @@ func (c *libcni) Setup(ctx context.Context, id string, path string, opts ...Name // SetupSerially setups the network in the namespace and returns a Result func (c *libcni) SetupSerially(ctx context.Context, id string, path string, opts ...NamespaceOpts) (*Result, error) { - c.Lock() - defer c.Unlock() - if len(c.networks) < c.networkCount { - return nil, ErrCNINotInitialized + if err := c.ready(); err != nil { + return nil, err } ns, err := newNamespace(id, path, opts...) if err != nil { @@ -246,10 +242,8 @@ func (c *libcni) attachNetworks(ctx context.Context, ns *Namespace) ([]*types100 // Remove removes the network config from the namespace func (c *libcni) Remove(ctx context.Context, id string, path string, opts ...NamespaceOpts) error { - c.Lock() - defer c.Unlock() - if len(c.networks) < c.networkCount { - return ErrCNINotInitialized + if err := c.ready(); err != nil { + return err } ns, err := newNamespace(id, path, opts...) if err != nil { @@ -276,10 +270,8 @@ func (c *libcni) Remove(ctx context.Context, id string, path string, opts ...Nam // Check checks if the network is still in desired state func (c *libcni) Check(ctx context.Context, id string, path string, opts ...NamespaceOpts) error { - c.RLock() - defer c.RUnlock() - if len(c.networks) < c.networkCount { - return ErrCNINotInitialized + if err := c.ready(); err != nil { + return err } ns, err := newNamespace(id, path, opts...) if err != nil { @@ -328,3 +320,13 @@ func (c *libcni) GetConfig() *ConfigResult { func (c *libcni) reset() { c.networks = nil } + +func (c *libcni) ready() error { + c.RLock() + defer c.RUnlock() + if len(c.networks) < c.networkCount { + return ErrCNINotInitialized + } + + return nil +}