Skip to content

Commit 5882530

Browse files
authored
Merge pull request #26 from abhi/master
Minor cleanup with concurrency and locking
2 parents 47457ea + 1695039 commit 5882530

2 files changed

Lines changed: 17 additions & 23 deletions

File tree

cni.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ func New(config ...CNIOpt) (CNI, error) {
7272

7373
func (c *libcni) Load(opts ...CNIOpt) error {
7474
var err error
75+
c.Lock()
76+
defer c.Unlock()
7577
// Reset the networks on a load operation to ensure
7678
// config happens on a clean slate
7779
c.reset()
@@ -81,30 +83,27 @@ func (c *libcni) Load(opts ...CNIOpt) error {
8183
return errors.Wrapf(ErrLoad, fmt.Sprintf("cni config load failed: %v", err))
8284
}
8385
}
84-
return c.Status()
86+
return nil
8587
}
8688

8789
func (c *libcni) Status() error {
8890
c.RLock()
8991
defer c.RUnlock()
90-
if len(c.networks) < c.networkCount {
91-
return ErrCNINotInitialized
92-
}
93-
return nil
92+
return c.status()
9493
}
9594

9695
// Setup setups the network in the namespace
9796
func (c *libcni) Setup(id string, path string, opts ...NamespaceOpts) (*CNIResult, error) {
98-
if err := c.Status(); err != nil {
97+
c.RLock()
98+
defer c.RUnlock()
99+
if err := c.status(); err != nil {
99100
return nil, err
100101
}
101102
ns, err := newNamespace(id, path, opts...)
102103
if err != nil {
103104
return nil, err
104105
}
105106
var results []*current.Result
106-
c.RLock()
107-
defer c.RUnlock()
108107
for _, network := range c.networks {
109108
r, err := network.Attach(ns)
110109
if err != nil {
@@ -117,15 +116,15 @@ func (c *libcni) Setup(id string, path string, opts ...NamespaceOpts) (*CNIResul
117116

118117
// Remove removes the network config from the namespace
119118
func (c *libcni) Remove(id string, path string, opts ...NamespaceOpts) error {
120-
if err := c.Status(); err != nil {
119+
c.RLock()
120+
defer c.RUnlock()
121+
if err := c.status(); err != nil {
121122
return err
122123
}
123124
ns, err := newNamespace(id, path, opts...)
124125
if err != nil {
125126
return err
126127
}
127-
c.RLock()
128-
defer c.RUnlock()
129128
for _, network := range c.networks {
130129
if err := network.Remove(ns); err != nil {
131130
return err
@@ -135,7 +134,12 @@ func (c *libcni) Remove(id string, path string, opts ...NamespaceOpts) error {
135134
}
136135

137136
func (c *libcni) reset() {
138-
c.Lock()
139-
defer c.Unlock()
140137
c.networks = nil
141138
}
139+
140+
func (c *libcni) status() error {
141+
if len(c.networks) < c.networkCount {
142+
return ErrCNINotInitialized
143+
}
144+
return nil
145+
}

opts.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ func WithLoNetwork(c *libcni) error {
7575
}]
7676
}`))
7777

78-
c.Lock()
79-
defer c.Unlock()
8078
c.networks = append(c.networks, &Network{
8179
cni: c.cniConfig,
8280
config: loConfig,
@@ -97,8 +95,6 @@ func WithConf(bytes []byte) CNIOpt {
9795
if err != nil {
9896
return err
9997
}
100-
c.Lock()
101-
defer c.Unlock()
10298
c.networks = append(c.networks, &Network{
10399
cni: c.cniConfig,
104100
config: confList,
@@ -122,8 +118,6 @@ func WithConfFile(fileName string) CNIOpt {
122118
if err != nil {
123119
return err
124120
}
125-
c.Lock()
126-
defer c.Unlock()
127121
c.networks = append(c.networks, &Network{
128122
cni: c.cniConfig,
129123
config: confList,
@@ -142,8 +136,6 @@ func WithConfListFile(fileName string) CNIOpt {
142136
if err != nil {
143137
return err
144138
}
145-
c.Lock()
146-
defer c.Unlock()
147139
c.networks = append(c.networks, &Network{
148140
cni: c.cniConfig,
149141
config: confList,
@@ -174,8 +166,6 @@ func WithDefaultConf(c *libcni) error {
174166
// interface provided during init as the network interface for this default
175167
// network. For every other network use a generated interface id.
176168
i := 0
177-
c.Lock()
178-
defer c.Unlock()
179169
for _, confFile := range files {
180170
var confList *cnilibrary.NetworkConfigList
181171
if strings.HasSuffix(confFile, ".conflist") {

0 commit comments

Comments
 (0)