diff --git a/cni/plugin.go b/cni/plugin.go index d811155234..a0664ad25d 100644 --- a/cni/plugin.go +++ b/cni/plugin.go @@ -158,9 +158,6 @@ func (plugin *Plugin) InitializeKeyValueStore(config *common.PluginConfig) error log.Printf("[cni] Failed to create store: %v.", err) return err } - - // Force unlock the json store if the lock file is left on the node after reboot - removeLockFileAfterReboot(plugin) } // Acquire store lock. diff --git a/cni/plugin_linux.go b/cni/plugin_linux.go deleted file mode 100644 index 1a950712c2..0000000000 --- a/cni/plugin_linux.go +++ /dev/null @@ -1,11 +0,0 @@ -package cni - -import ( - "github.com/Azure/azure-container-networking/log" - "github.com/Azure/azure-container-networking/platform" -) - -func removeLockFileAfterReboot(plugin *Plugin) { - rebootTime, _ := platform.GetLastRebootTime() - log.Printf("[cni] reboot time %v", rebootTime) -} diff --git a/cni/plugin_windows.go b/cni/plugin_windows.go deleted file mode 100644 index 34a96403a4..0000000000 --- a/cni/plugin_windows.go +++ /dev/null @@ -1,22 +0,0 @@ -package cni - -import ( - "github.com/Azure/azure-container-networking/log" - "github.com/Azure/azure-container-networking/platform" -) - -func removeLockFileAfterReboot(plugin *Plugin) { - if lockFileModTime, err := plugin.Store.GetLockFileModificationTime(); err == nil { - rebootTime, err := platform.GetLastRebootTime() - log.Printf("[cni] reboot time %v storeLockFile mod time %v", rebootTime, lockFileModTime) - if err == nil && rebootTime.After(lockFileModTime) { - log.Printf("[cni] Detected Reboot") - - if err := plugin.Store.Unlock(true); err != nil { - log.Printf("[cni] Failed to force unlock store due to error %v", err) - } else { - log.Printf("[cni] Force unlocked the store successfully") - } - } - } -} diff --git a/store/json.go b/store/json.go index 3181f371a9..84cdff5c0a 100644 --- a/store/json.go +++ b/store/json.go @@ -176,6 +176,11 @@ func (kvs *jsonFileStore) flush() error { // Lock locks the store for exclusive access. func (kvs *jsonFileStore) Lock(block bool) error { + var ( + lockFile *os.File + err error + ) + kvs.Mutex.Lock() defer kvs.Mutex.Unlock() @@ -183,9 +188,8 @@ func (kvs *jsonFileStore) Lock(block bool) error { return ErrStoreLocked } - var lockFile *os.File - var err error - lockPerm := os.FileMode(0o664) + os.FileMode(os.ModeExclusive) + //nolint:gomnd // 0o664 - read write mode constant + lockPerm := os.FileMode(0o644) + os.FileMode(os.ModeExclusive) // Try to acquire the lock file. var lockRetryCount uint @@ -270,14 +274,6 @@ func (kvs *jsonFileStore) GetLockFileModificationTime() (time.Time, error) { kvs.Mutex.Lock() defer kvs.Mutex.Unlock() - // Check if the file exists. - file, err := os.Open(kvs.lockFileName) - if err != nil { - return time.Time{}.UTC(), err - } - - defer file.Close() - info, err := os.Stat(kvs.lockFileName) if err != nil { log.Printf("os.stat() for file %v failed: %v", kvs.lockFileName, err)