@@ -41,6 +41,7 @@ const (
4141 ipVersion = "4"
4242 ipamV6 = "azure-vnet-ipamv6"
4343 defaultRequestTimeout = 15 * time .Second
44+ azureCniName = "azure-vnet"
4445)
4546
4647// CNI Operation Types
@@ -145,12 +146,6 @@ func (plugin *netPlugin) Start(config *common.PluginConfig) error {
145146 common .LogNetworkInterfaces ()
146147
147148 // Initialize network manager.
148- err = plugin .nm .Initialize (config , rehydrateNetworkInfoOnReboot )
149- if err != nil {
150- log .Printf ("[cni-net] Failed to initialize network manager, err:%v." , err )
151- return err
152- }
153-
154149 log .Printf ("[cni-net] Plugin started." )
155150
156151 return nil
@@ -282,7 +277,6 @@ func (plugin *netPlugin) setCNIReportDetails(nwCfg *cni.NetworkConfig, opType st
282277 plugin .report .SubContext = fmt .Sprintf ("%+v" , nwCfg )
283278 plugin .report .EventMessage = msg
284279 plugin .report .BridgeDetails .NetworkMode = nwCfg .Mode
285- plugin .report .InterfaceDetails .SecondaryCAUsedCount = plugin .nm .GetNumberOfEndpoints ("" , nwCfg .Name )
286280}
287281
288282func addNatIPV6SubnetInfo (nwCfg * cni.NetworkConfig ,
@@ -301,6 +295,33 @@ func addNatIPV6SubnetInfo(nwCfg *cni.NetworkConfig,
301295 }
302296}
303297
298+ func acquireLockForStore (config * common.PluginConfig , plugin * netPlugin ) error {
299+ var err error
300+ if err = plugin .Store .Lock (true ); err != nil {
301+ log .Printf ("[CNI] Failed to lock store: %v. check if process running" , err )
302+ if isSafe , _ := plugin .IsSafeToRemoveLock (azureCniName ); isSafe {
303+ log .Printf ("[CNI] Removing lock file as process holding lock exited" )
304+ if err = releaseLockForStore (plugin ); err != nil {
305+ log .Errorf ("Failed to release lock file, err:%v.\n " , err )
306+ }
307+ }
308+ }
309+
310+ return err
311+ }
312+
313+ func releaseLockForStore (plugin * netPlugin ) error {
314+ if plugin .Store != nil {
315+ err := plugin .Store .Unlock (false )
316+ if err != nil {
317+ log .Printf ("[cni] Failed to unlock store: %v." , err )
318+ return err
319+ }
320+ }
321+
322+ log .Printf ("Released lock file" )
323+ return nil
324+ }
304325//
305326// CNI implementation
306327// https://github.com/containernetworking/cni/blob/master/SPEC.md
@@ -349,6 +370,22 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error {
349370 iptables .DisableIPTableLock = nwCfg .DisableIPTableLock
350371 plugin .setCNIReportDetails (nwCfg , CNI_ADD , "" )
351372
373+ // acquire cni lock file
374+ // TODO: check if we need pluginconfig and if not remove it
375+ config := & common.PluginConfig {Store : plugin .Store }
376+ if err := acquireLockForStore (config , plugin ); err != nil {
377+ log .Errorf ("Couldn't acquire lock: %+v" , err )
378+ return err
379+ }
380+
381+ defer releaseLockForStore (plugin )
382+
383+ // restore network state
384+ if err = plugin .nm .Initialize (config , rehydrateNetworkInfoOnReboot ); err != nil {
385+ log .Printf ("[cni-net] Failed to initialize network manager, err:%+v." , err )
386+ return err
387+ }
388+
352389 defer func () {
353390 operationTimeMs := time .Since (startTime ).Milliseconds ()
354391 cniMetric .Metric = aitelemetry.Metric {
@@ -774,6 +811,20 @@ func (plugin *netPlugin) Get(args *cniSkel.CmdArgs) error {
774811
775812 iptables .DisableIPTableLock = nwCfg .DisableIPTableLock
776813
814+ config := & common.PluginConfig {Store : plugin .Store }
815+ if err := acquireLockForStore (config , plugin ); err != nil {
816+ log .Errorf ("Couldn't acquire lock: %+v" , err )
817+ return err
818+ }
819+
820+ defer releaseLockForStore (plugin )
821+
822+ // restore network state
823+ if err = plugin .nm .Initialize (config , rehydrateNetworkInfoOnReboot ); err != nil {
824+ log .Printf ("[cni-net] Failed to initialize network manager, err:%+v." , err )
825+ return err
826+ }
827+
777828 // Parse Pod arguments.
778829 if k8sPodName , k8sNamespace , err = plugin .getPodInfo (args .Args ); err != nil {
779830 return err
@@ -859,6 +910,21 @@ func (plugin *netPlugin) Delete(args *cniSkel.CmdArgs) error {
859910
860911 log .Printf ("[cni-net] Read network configuration %+v." , nwCfg )
861912
913+ // acquire cni lock file
914+ config := & common.PluginConfig {Store : plugin .Store }
915+ if err := acquireLockForStore (config , plugin ); err != nil {
916+ log .Errorf ("Couldn't acquire lock: %+v" , err )
917+ return err
918+ }
919+
920+ defer releaseLockForStore (plugin )
921+
922+ // restore network state
923+ if err = plugin .nm .Initialize (config , rehydrateNetworkInfoOnReboot ); err != nil {
924+ log .Printf ("[cni-net] Failed to initialize network manager, err:%+v." , err )
925+ return err
926+ }
927+
862928 // Parse Pod arguments.
863929 if k8sPodName , k8sNamespace , err = plugin .getPodInfo (args .Args ); err != nil {
864930 log .Printf ("[cni-net] Failed to get POD info due to error: %v" , err )
@@ -1021,6 +1087,21 @@ func (plugin *netPlugin) Update(args *cniSkel.CmdArgs) error {
10211087 iptables .DisableIPTableLock = nwCfg .DisableIPTableLock
10221088 plugin .setCNIReportDetails (nwCfg , CNI_UPDATE , "" )
10231089
1090+ // acquire cni lock file
1091+ config := & common.PluginConfig {Store : plugin .Store }
1092+ if err := acquireLockForStore (config , plugin ); err != nil {
1093+ log .Errorf ("Couldn't acquire lock: %+v" , err )
1094+ return err
1095+ }
1096+
1097+ defer releaseLockForStore (plugin )
1098+
1099+ // restore network state
1100+ if err = plugin .nm .Initialize (config , rehydrateNetworkInfoOnReboot ); err != nil {
1101+ log .Printf ("[cni-net] Failed to initialize network manager, err:%+v." , err )
1102+ return err
1103+ }
1104+
10241105 defer func () {
10251106 operationTimeMs := time .Since (startTime ).Milliseconds ()
10261107 cniMetric .Metric = aitelemetry.Metric {
0 commit comments