Skip to content

Commit

Permalink
create ready
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeZappa87 committed Nov 24, 2024
1 parent 91c449d commit 733a0f5
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -163,7 +163,7 @@ 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) {
if err := c.Status(); err != nil {
if err := c.ready(); err != nil {
return nil, err
}
ns, err := newNamespace(id, path, opts...)
Expand All @@ -179,7 +179,7 @@ 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) {
if err := c.Status(); err != nil {
if err := c.ready(); err != nil {
return nil, err
}
ns, err := newNamespace(id, path, opts...)
Expand Down Expand Up @@ -242,7 +242,7 @@ 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 {
if err := c.Status(); err != nil {
if err := c.ready(); err != nil {
return err
}
ns, err := newNamespace(id, path, opts...)
Expand Down Expand Up @@ -270,7 +270,7 @@ 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 {
if err := c.Status(); err != nil {
if err := c.ready(); err != nil {
return err
}
ns, err := newNamespace(id, path, opts...)
Expand Down Expand Up @@ -320,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
}

0 comments on commit 733a0f5

Please sign in to comment.