Skip to content

Commit 106f527

Browse files
committed
fix locks
1 parent ce20b7e commit 106f527

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

cni.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ func (c *libcni) Load(opts ...Opt) error {
137137
func (c *libcni) Status() error {
138138
c.RLock()
139139
defer c.RUnlock()
140-
if err := c.ready(); err != nil {
141-
return err
140+
if len(c.networks) < c.networkCount {
141+
return ErrCNINotInitialized
142142
}
143143

144144
// STATUS is only called for CNI Version 1.1.0 or greater. It is ignored for previous versions.
@@ -163,8 +163,10 @@ func (c *libcni) Networks() []*Network {
163163

164164
// Setup setups the network in the namespace and returns a Result
165165
func (c *libcni) Setup(ctx context.Context, id string, path string, opts ...NamespaceOpts) (*Result, error) {
166-
if err := c.ready(); err != nil {
167-
return nil, err
166+
c.Lock()
167+
defer c.Unlock()
168+
if len(c.networks) < c.networkCount {
169+
return nil, ErrCNINotInitialized
168170
}
169171
ns, err := newNamespace(id, path, opts...)
170172
if err != nil {
@@ -179,8 +181,10 @@ func (c *libcni) Setup(ctx context.Context, id string, path string, opts ...Name
179181

180182
// SetupSerially setups the network in the namespace and returns a Result
181183
func (c *libcni) SetupSerially(ctx context.Context, id string, path string, opts ...NamespaceOpts) (*Result, error) {
182-
if err := c.ready(); err != nil {
183-
return nil, err
184+
c.Lock()
185+
defer c.Unlock()
186+
if len(c.networks) < c.networkCount {
187+
return nil, ErrCNINotInitialized
184188
}
185189
ns, err := newNamespace(id, path, opts...)
186190
if err != nil {
@@ -242,8 +246,10 @@ func (c *libcni) attachNetworks(ctx context.Context, ns *Namespace) ([]*types100
242246

243247
// Remove removes the network config from the namespace
244248
func (c *libcni) Remove(ctx context.Context, id string, path string, opts ...NamespaceOpts) error {
245-
if err := c.ready(); err != nil {
246-
return err
249+
c.Lock()
250+
defer c.Unlock()
251+
if len(c.networks) < c.networkCount {
252+
return ErrCNINotInitialized
247253
}
248254
ns, err := newNamespace(id, path, opts...)
249255
if err != nil {
@@ -270,8 +276,10 @@ func (c *libcni) Remove(ctx context.Context, id string, path string, opts ...Nam
270276

271277
// Check checks if the network is still in desired state
272278
func (c *libcni) Check(ctx context.Context, id string, path string, opts ...NamespaceOpts) error {
273-
if err := c.ready(); err != nil {
274-
return err
279+
c.RLock()
280+
defer c.RUnlock()
281+
if len(c.networks) < c.networkCount {
282+
return ErrCNINotInitialized
275283
}
276284
ns, err := newNamespace(id, path, opts...)
277285
if err != nil {
@@ -320,13 +328,3 @@ func (c *libcni) GetConfig() *ConfigResult {
320328
func (c *libcni) reset() {
321329
c.networks = nil
322330
}
323-
324-
func (c *libcni) ready() error {
325-
c.RLock()
326-
defer c.RUnlock()
327-
if len(c.networks) < c.networkCount {
328-
return ErrCNINotInitialized
329-
}
330-
331-
return nil
332-
}

0 commit comments

Comments
 (0)