Skip to content

Commit e98936c

Browse files
ashvindeodharYongli Chen
authored andcommitted
Cleanup old networks and lock files after detecting reboot (#285)
1 parent b7f6742 commit e98936c

File tree

7 files changed

+75
-15
lines changed

7 files changed

+75
-15
lines changed

cni/plugin.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ func NewPlugin(name, version string) (*Plugin, error) {
3434
return nil, err
3535
}
3636

37+
// Initialize logging.
38+
log.SetName(plugin.Name)
39+
log.SetLevel(log.LevelInfo)
40+
err = log.SetTarget(log.TargetLogfile)
41+
if err != nil {
42+
log.Printf("[cni] Failed to configure logging, err:%v.\n", err)
43+
return &Plugin{
44+
Plugin: plugin,
45+
version: version,
46+
}, err
47+
}
48+
3749
return &Plugin{
3850
Plugin: plugin,
3951
version: version,
@@ -45,14 +57,6 @@ func (plugin *Plugin) Initialize(config *common.PluginConfig) error {
4557
// Initialize the base plugin.
4658
plugin.Plugin.Initialize(config)
4759

48-
// Initialize logging.
49-
log.SetName(plugin.Name)
50-
log.SetLevel(log.LevelInfo)
51-
err := log.SetTarget(log.TargetLogfile)
52-
if err != nil {
53-
log.Printf("[cni] Failed to configure logging, err:%v.\n", err)
54-
return err
55-
}
5660
return nil
5761
}
5862

@@ -164,6 +168,21 @@ func (plugin *Plugin) InitializeKeyValueStore(config *common.PluginConfig) error
164168
log.Printf("[cni] Failed to create store: %v.", err)
165169
return err
166170
}
171+
172+
// Force unlock the json store if the lock file is left on the node after reboot
173+
if lockFileModTime, err := plugin.Store.GetLockFileModificationTime(); err == nil {
174+
rebootTime, err := platform.GetLastRebootTime()
175+
log.Printf("[cni] reboot time %v storeLockFile mod time %v", rebootTime, lockFileModTime)
176+
if err == nil && rebootTime.After(lockFileModTime) {
177+
log.Printf("[cni] Detected Reboot")
178+
179+
if err := plugin.Store.Unlock(true); err != nil {
180+
log.Printf("[cni] Failed to force unlock store due to error %v", err)
181+
} else {
182+
log.Printf("[cni] Force unlocked the store successfully")
183+
}
184+
}
185+
}
167186
}
168187

169188
// Acquire store lock.
@@ -180,7 +199,7 @@ func (plugin *Plugin) InitializeKeyValueStore(config *common.PluginConfig) error
180199
// Uninitialize key-value store
181200
func (plugin *Plugin) UninitializeKeyValueStore() error {
182201
if plugin.Store != nil {
183-
err := plugin.Store.Unlock()
202+
err := plugin.Store.Unlock(false)
184203
if err != nil {
185204
log.Printf("[cni] Failed to unlock store: %v.", err)
186205
return err

ipam/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ func (am *addressManager) restore() error {
105105
// Check if the VM is rebooted.
106106
modTime, err := am.store.GetModificationTime()
107107
if err == nil {
108-
109108
rebootTime, err := platform.GetLastRebootTime()
110109
log.Printf("[ipam] reboot time %v store mod time %v", rebootTime, modTime)
111110

@@ -147,6 +146,7 @@ func (am *addressManager) restore() error {
147146
for _, as := range am.AddrSpaces {
148147
for _, ap := range as.Pools {
149148
ap.as = as
149+
ap.RefCount = 0
150150

151151
for _, ar := range ap.Addresses {
152152
ar.InUse = false

network/manager.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ func (nm *networkManager) restore() error {
129129
return err
130130
}
131131

132+
// Delete the networks left behind after reboot
133+
for _, extIf := range nm.ExternalInterfaces {
134+
for _, nw := range extIf.Networks {
135+
log.Printf("[net] Deleting the network %s on reboot\n", nw.Id)
136+
nm.deleteNetwork(nw.Id)
137+
}
138+
}
139+
132140
// Clear networkManager contents
133141
nm.TimeStamp = time.Time{}
134142
for extIfName := range nm.ExternalInterfaces {

platform/os_windows.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,15 @@ func GetLastRebootTime() (time.Time, error) {
6565
bootPM := strings.Contains(strings.Split(systemBootTime, " ")[2], "PM")
6666

6767
month := strings.Split(bootDate, "/")[0]
68+
if len(month) < 2 {
69+
month = "0" + month
70+
}
71+
6872
day := strings.Split(bootDate, "/")[1]
73+
if len(day) < 2 {
74+
day = "0" + day
75+
}
76+
6977
year := strings.Split(bootDate, "/")[2]
7078
year = strings.Trim(year, ",")
7179
hour := strings.Split(bootTime, ":")[0]

store/json.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ func (kvs *jsonFileStore) Lock(block bool) error {
170170
}
171171

172172
// Unlock unlocks the store.
173-
func (kvs *jsonFileStore) Unlock() error {
173+
func (kvs *jsonFileStore) Unlock(forceUnlock bool) error {
174174
kvs.Mutex.Lock()
175175
defer kvs.Mutex.Unlock()
176176

177-
if !kvs.locked {
177+
if !forceUnlock && !kvs.locked {
178178
return ErrStoreNotLocked
179179
}
180180

@@ -202,3 +202,27 @@ func (kvs *jsonFileStore) GetModificationTime() (time.Time, error) {
202202

203203
return info.ModTime().UTC(), nil
204204
}
205+
206+
// GetLockFileModificationTime returns the modification time of the lock file of the persistent store.
207+
func (kvs *jsonFileStore) GetLockFileModificationTime() (time.Time, error) {
208+
kvs.Mutex.Lock()
209+
defer kvs.Mutex.Unlock()
210+
211+
lockFileName := kvs.fileName + lockExtension
212+
213+
// Check if the file exists.
214+
file, err := os.Open(lockFileName)
215+
if err != nil {
216+
return time.Time{}.UTC(), err
217+
}
218+
219+
defer file.Close()
220+
221+
info, err := os.Stat(lockFileName)
222+
if err != nil {
223+
log.Printf("os.stat() for file %v failed: %v", lockFileName, err)
224+
return time.Time{}.UTC(), err
225+
}
226+
227+
return info.ModTime().UTC(), nil
228+
}

store/json_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func TestLockingStoreGivesExclusiveAccess(t *testing.T) {
185185
}
186186

187187
// Unlock the first store.
188-
err = kvs.Unlock()
188+
err = kvs.Unlock(false)
189189
if err != nil {
190190
t.Errorf("Failed to unlock first store: %v", err)
191191
}
@@ -198,7 +198,7 @@ func TestLockingStoreGivesExclusiveAccess(t *testing.T) {
198198
}
199199

200200
// Unlock the second store.
201-
err = kvs2.Unlock()
201+
err = kvs2.Unlock(false)
202202
if err != nil {
203203
t.Errorf("Failed to unlock second store: %v", err)
204204
}

store/store.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ type KeyValueStore interface {
1414
Write(key string, value interface{}) error
1515
Flush() error
1616
Lock(block bool) error
17-
Unlock() error
17+
Unlock(forceUnlock bool) error
1818
GetModificationTime() (time.Time, error)
19+
GetLockFileModificationTime() (time.Time, error)
1920
}
2021

2122
var (

0 commit comments

Comments
 (0)