Skip to content

Commit ed52a64

Browse files
Remove lock for version command (#970)
* remove lock for version command. * linter error fix * sort imports * addressed issues * linter fix
1 parent 4b38974 commit ed52a64

File tree

2 files changed

+76
-64
lines changed

2 files changed

+76
-64
lines changed

cni/cni.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@ import (
99

1010
const (
1111
// CNI commands.
12-
Cmd = "CNI_COMMAND"
13-
CmdAdd = "ADD"
14-
CmdGet = "GET"
15-
CmdDel = "DEL"
12+
Cmd = "CNI_COMMAND"
13+
// CmdAdd - CNI ADD command.
14+
CmdAdd = "ADD"
15+
// CmdGet - CNI GET command.
16+
CmdGet = "GET"
17+
// CmdDel - CNI DEL command.
18+
CmdDel = "DEL"
19+
// CmdUpdate - CNI UPDATE command.
1620
CmdUpdate = "UPDATE"
21+
// CmdVersion - CNI VERSION command.
22+
CmdVersion = "VERSION"
1723

1824
// nonstandard CNI spec command, used to dump CNI state to stdout
1925
CmdGetEndpointsState = "GET_ENDPOINT_STATE"

cni/network/plugin/main.go

Lines changed: 66 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
129128
func 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

Comments
 (0)