@@ -72,6 +72,8 @@ func New(config ...CNIOpt) (CNI, error) {
7272
7373func (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
8789func (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
9796func (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
119118func (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
137136func (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+ }
0 commit comments