@@ -11,13 +11,12 @@ import (
1111 "reflect"
1212 "time"
1313
14- "github.com/Azure/azure-container-networking/nns"
15-
1614 "github.com/Azure/azure-container-networking/cni"
1715 "github.com/Azure/azure-container-networking/cni/network"
1816 "github.com/Azure/azure-container-networking/common"
1917 acn "github.com/Azure/azure-container-networking/common"
2018 "github.com/Azure/azure-container-networking/log"
19+ "github.com/Azure/azure-container-networking/nns"
2120 "github.com/Azure/azure-container-networking/platform"
2221 "github.com/Azure/azure-container-networking/telemetry"
2322 "github.com/containernetworking/cni/pkg/skel"
@@ -127,7 +126,6 @@ func handleIfCniUpdate(update func(*skel.CmdArgs) error) (bool, error) {
127126
128127// Main is the entry point for CNI network plugin.
129128func main () {
130-
131129 startTime := time .Now ()
132130
133131 // Initialize and parse command line arguments.
@@ -143,6 +141,7 @@ func main() {
143141 config common.PluginConfig
144142 err error
145143 logDirectory string // This sets empty string i.e. current location
144+ tb * telemetry.TelemetryBuffer
146145 )
147146
148147 log .SetName (name )
@@ -168,84 +167,87 @@ func main() {
168167
169168 cniReport := reportManager .Report .(* telemetry.CNIReport )
170169
171- upTime , err := platform .GetLastRebootTime ()
172- if err == nil {
173- cniReport .VMUptime = upTime .Format ("2006-01-02 15:04:05" )
174- }
175-
176- cniReport .GetReport (pluginName , version , ipamQueryURL )
177-
178170 netPlugin , err := network .NewPlugin (name , & config , & nns.GrpcClient {})
179171 if err != nil {
180172 log .Printf ("Failed to create network plugin, err:%v.\n " , err )
181173 return
182174 }
183175
184- // CNI Acquires lock
185- if err = netPlugin .Plugin .InitializeKeyValueStore (& config ); err != nil {
186- log .Errorf ("Failed to initialize key-value store of network plugin, err:%v.\n " , err )
187- tb := telemetry .NewTelemetryBuffer ()
188- if tberr := tb .Connect (); tberr == nil {
189- reportPluginError (reportManager , tb , err )
190- tb .Close ()
176+ // Check CNI_COMMAND value
177+ cniCmd := os .Getenv (cni .Cmd )
178+
179+ if cniCmd != cni .CmdVersion {
180+ log .Printf ("CNI_COMMAND environment variable set to %s" , cniCmd )
181+
182+ cniReport .GetReport (pluginName , version , ipamQueryURL )
183+
184+ upTime , err := platform .GetLastRebootTime ()
185+ if err == nil {
186+ cniReport .VMUptime = upTime .Format ("2006-01-02 15:04:05" )
191187 }
192188
193- if isSafe , _ := netPlugin .Plugin .IsSafeToRemoveLock (name ); isSafe {
194- log .Printf ("[CNI] Removing lock file as process holding lock exited" )
195- if errUninit := netPlugin .Plugin .UninitializeKeyValueStore (true ); errUninit != nil {
196- log .Errorf ("Failed to uninitialize key-value store of network plugin, err:%v.\n " , errUninit )
189+ // CNI Acquires lock
190+ if err = netPlugin .Plugin .InitializeKeyValueStore (& config ); err != nil {
191+ log .Errorf ("Failed to initialize key-value store of network plugin, err:%v.\n " , err )
192+ tb := telemetry .NewTelemetryBuffer ()
193+ if tberr := tb .Connect (); tberr == nil {
194+ reportPluginError (reportManager , tb , err )
195+ tb .Close ()
197196 }
198- }
199197
200- return
201- }
198+ if isSafe , _ := netPlugin .Plugin .IsSafeToRemoveLock (name ); isSafe {
199+ log .Printf ("[CNI] Removing lock file as process holding lock exited" )
200+ if errUninit := netPlugin .Plugin .UninitializeKeyValueStore (true ); errUninit != nil {
201+ log .Errorf ("Failed to uninitialize key-value store of network plugin, err:%v.\n " , errUninit )
202+ }
203+ }
202204
203- defer func () {
204- if errUninit := netPlugin .Plugin .UninitializeKeyValueStore (false ); errUninit != nil {
205- log .Errorf ("Failed to uninitialize key-value store of network plugin, err:%v.\n " , errUninit )
205+ return
206206 }
207207
208- if recover () != nil {
209- os . Exit ( 1 )
210- }
211- }()
208+ defer func () {
209+ if errUninit := netPlugin . Plugin . UninitializeKeyValueStore ( false ); errUninit != nil {
210+ log . Errorf ( "Failed to uninitialize key-value store of network plugin, err:%v. \n " , errUninit )
211+ }
212212
213- // Start telemetry process if not already started. This should be done inside lock, otherwise multiple process
214- // end up creating/killing telemetry process results in undesired state.
215- tb := telemetry .NewTelemetryBuffer ()
216- tb .ConnectToTelemetryService (telemetryNumRetries , telemetryWaitTimeInMilliseconds )
217- defer tb .Close ()
213+ if recover () != nil {
214+ os .Exit (1 )
215+ }
216+ }()
218217
219- netPlugin .SetCNIReport (cniReport , tb )
218+ // Start telemetry process if not already started. This should be done inside lock, otherwise multiple process
219+ // end up creating/killing telemetry process results in undesired state.
220+ tb = telemetry .NewTelemetryBuffer ()
221+ tb .ConnectToTelemetryService (telemetryNumRetries , telemetryWaitTimeInMilliseconds )
222+ defer tb .Close ()
220223
221- t := time .Now ()
222- cniReport .Timestamp = t .Format ("2006-01-02 15:04:05" )
224+ netPlugin .SetCNIReport (cniReport , tb )
223225
224- if err = netPlugin .Start (& config ); err != nil {
225- log .Errorf ("Failed to start network plugin, err:%v.\n " , err )
226- reportPluginError (reportManager , tb , err )
227- panic ("network plugin start fatal error" )
228- }
226+ t := time .Now ()
227+ cniReport .Timestamp = t .Format ("2006-01-02 15:04:05" )
229228
230- // Check CNI_COMMAND value for logging purposes.
231- cniCmd := os .Getenv (cni .Cmd )
232- log .Printf ("CNI_COMMAND environment variable set to %s" , cniCmd )
233-
234- // used to dump state
235- if cniCmd == cni .CmdGetEndpointsState {
236- log .Printf ("Retrieving state" )
237- simpleState , err := netPlugin .GetAllEndpointState ("azure" )
238- if err != nil {
239- log .Errorf ("Failed to get Azure CNI state, err:%v.\n " , err )
240- return
229+ if err = netPlugin .Start (& config ); err != nil {
230+ log .Errorf ("Failed to start network plugin, err:%v.\n " , err )
231+ reportPluginError (reportManager , tb , err )
232+ panic ("network plugin start fatal error" )
241233 }
242234
243- err = simpleState .PrintResult ()
244- if err != nil {
245- log .Errorf ("Failed to print state result to stdout with err %v\n " , err )
246- }
235+ // used to dump state
236+ if cniCmd == cni .CmdGetEndpointsState {
237+ log .Printf ("Retrieving state" )
238+ simpleState , err := netPlugin .GetAllEndpointState ("azure" )
239+ if err != nil {
240+ log .Errorf ("Failed to get Azure CNI state, err:%v.\n " , err )
241+ return
242+ }
247243
248- return
244+ err = simpleState .PrintResult ()
245+ if err != nil {
246+ log .Errorf ("Failed to print state result to stdout with err %v\n " , err )
247+ }
248+
249+ return
250+ }
249251 }
250252
251253 handled , err := handleIfCniUpdate (netPlugin .Update )
@@ -255,6 +257,10 @@ func main() {
255257 log .Errorf ("Failed to execute network plugin, err:%v.\n " , err )
256258 }
257259
260+ if cniCmd == cni .CmdVersion {
261+ return
262+ }
263+
258264 netPlugin .Stop ()
259265
260266 // release cni lock
0 commit comments