Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cni/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (plugin *netPlugin) Start(config *common.PluginConfig) error {
common.LogNetworkInterfaces()

// Initialize network manager.
err = plugin.nm.Initialize(config)
err = plugin.nm.Initialize(config, false)
if err != nil {
log.Printf("[cni-net] Failed to initialize network manager, err:%v.", err)
return err
Expand Down
14 changes: 1 addition & 13 deletions cni/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,7 @@ func (plugin *Plugin) InitializeKeyValueStore(config *common.PluginConfig) error
}

// Force unlock the json store if the lock file is left on the node after reboot
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")
}
}
}
removeLockFileAfterReboot(plugin)
}

// Acquire store lock.
Expand Down
11 changes: 11 additions & 0 deletions cni/plugin_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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)
}
22 changes: 22 additions & 0 deletions cni/plugin_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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")
}
}
}
}
2 changes: 1 addition & 1 deletion cnm/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (plugin *netPlugin) Start(config *common.PluginConfig) error {
}

// Initialize network manager.
err = plugin.nm.Initialize(config)
err = plugin.nm.Initialize(config, true)
if err != nil {
log.Printf("[net] Failed to initialize network manager, err:%v.", err)
return err
Expand Down
2 changes: 1 addition & 1 deletion cnms/service/networkmonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func main() {
return
}

if err := nm.Initialize(&config); err != nil {
if err := nm.Initialize(&config, false); err != nil {
log.Printf("[monitor] Failed while initializing network manager %+v", err)
}

Expand Down
1 change: 1 addition & 0 deletions ipam/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ func (am *addressManager) RequestAddress(asId, poolId, address string, options m

err = am.save()
if err != nil {
ap.releaseAddress(addr, options)
return "", err
}

Expand Down
62 changes: 30 additions & 32 deletions network/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type networkManager struct {

// NetworkManager API.
type NetworkManager interface {
Initialize(config *common.PluginConfig) error
Initialize(config *common.PluginConfig, isRehydrationRequired bool) error
Uninitialize()

AddExternalInterface(ifName string, subnet string) error
Expand Down Expand Up @@ -81,12 +81,12 @@ func NewNetworkManager() (NetworkManager, error) {
}

// Initialize configures network manager.
func (nm *networkManager) Initialize(config *common.PluginConfig) error {
func (nm *networkManager) Initialize(config *common.PluginConfig, isRehydrationRequired bool) error {
nm.Version = config.Version
nm.store = config.Store

// Restore persisted state.
err := nm.restore()
err := nm.restore(isRehydrationRequired)
return err
}

Expand All @@ -95,7 +95,7 @@ func (nm *networkManager) Uninitialize() {
}

// Restore reads network manager state from persistent store.
func (nm *networkManager) restore() error {
func (nm *networkManager) restore(isRehydrationRequired bool) error {
// Skip if a store is not provided.
if nm.store == nil {
log.Printf("[net] network store is nil")
Expand All @@ -107,9 +107,6 @@ func (nm *networkManager) restore() error {
// Ignore the persisted state if it is older than the last reboot time.

// Read any persisted state.
nm.Lock()
defer nm.Unlock()

err := nm.store.Read(storeKey, nm)
if err != nil {
if err == store.ErrKeyNotFound {
Expand All @@ -122,38 +119,39 @@ func (nm *networkManager) restore() error {
}
}

modTime, err := nm.store.GetModificationTime()
if err == nil {
rebootTime, err := platform.GetLastRebootTime()
log.Printf("[net] reboot time %v store mod time %v", rebootTime, modTime)
if err == nil && rebootTime.After(modTime) {
log.Printf("[net] Detected Reboot")
rebooted = true
if clearNwConfig, err := platform.ClearNetworkConfiguration(); clearNwConfig {
if err != nil {
log.Printf("[net] Failed to clear network configuration, err:%v\n", err)
return err
}
if isRehydrationRequired {
modTime, err := nm.store.GetModificationTime()
if err == nil {
rebootTime, err := platform.GetLastRebootTime()
log.Printf("[net] reboot time %v store mod time %v", rebootTime, modTime)
if err == nil && rebootTime.After(modTime) {
log.Printf("[net] Detected Reboot")
rebooted = true
if clearNwConfig, err := platform.ClearNetworkConfiguration(); clearNwConfig {
if err != nil {
log.Printf("[net] Failed to clear network configuration, err:%v\n", err)
return err
}

// Delete the networks left behind after reboot
for _, extIf := range nm.ExternalInterfaces {
for _, nw := range extIf.Networks {
log.Printf("[net] Deleting the network %s on reboot\n", nw.Id)
nm.deleteNetwork(nw.Id)
// Delete the networks left behind after reboot
for _, extIf := range nm.ExternalInterfaces {
for _, nw := range extIf.Networks {
log.Printf("[net] Deleting the network %s on reboot\n", nw.Id)
nm.deleteNetwork(nw.Id)
}
}
}

// Clear networkManager contents
nm.TimeStamp = time.Time{}
for extIfName := range nm.ExternalInterfaces {
delete(nm.ExternalInterfaces, extIfName)
}
// Clear networkManager contents
nm.TimeStamp = time.Time{}
for extIfName := range nm.ExternalInterfaces {
delete(nm.ExternalInterfaces, extIfName)
}

return nil
return nil
}
}
}
}

// Populate pointers.
for _, extIf := range nm.ExternalInterfaces {
for _, nw := range extIf.Networks {
Expand Down
29 changes: 15 additions & 14 deletions network/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var (
Context("When restore is nil", func() {
It("Should return nil", func() {
nm := &networkManager{}
err := nm.restore()
err := nm.restore(false)
Expect(err).NotTo(HaveOccurred())
})
})
Expand All @@ -63,7 +63,7 @@ var (
ReadError: store.ErrKeyNotFound,
},
}
err := nm.restore()
err := nm.restore(false)
Expect(err).NotTo(HaveOccurred())
})
})
Expand All @@ -75,7 +75,7 @@ var (
ReadError: errors.New("error for test"),
},
}
err := nm.restore()
err := nm.restore(false)
Expect(err).To(HaveOccurred())
})
})
Expand All @@ -91,11 +91,11 @@ var (
ExternalInterfaces: map[string]*externalInterface{},
}
nm.ExternalInterfaces[extIfName] = &externalInterface{
Name: extIfName,
Name: extIfName,
Networks: map[string]*network{},
}
nm.ExternalInterfaces[extIfName].Networks[nwId] = &network{}
err := nm.restore()
err := nm.restore(false)
Expect(err).NotTo(HaveOccurred())
Expect(nm.ExternalInterfaces[extIfName].Networks[nwId].extIf.Name).To(Equal(extIfName))
})
Expand All @@ -104,23 +104,24 @@ var (

Describe("Test save", func() {
Context("When store is nil", func() {
It("Should return nil", func(){
It("Should return nil", func() {
nm := &networkManager{}
err := nm.save()
Expect(err).NotTo(HaveOccurred())
Expect(nm.TimeStamp).To(Equal(time.Time{}))
})
})
Context("When store.Write return error", func() {
It("Should raise error", func(){
It("Should raise error", func() {
nm := &networkManager{
store: &testutils.KeyValueStoreMock{
WriteError: errors.New("error for test"),
},
}
err := nm.save()
Expect(err).To(HaveOccurred())
Expect(nm.TimeStamp).NotTo(Equal(time.Time{}))})
Expect(nm.TimeStamp).NotTo(Equal(time.Time{}))
})
})
})

Expand Down Expand Up @@ -198,9 +199,9 @@ var (
}
nm.ExternalInterfaces[ifName].Networks[nwId] = &network{
Endpoints: map[string]*endpoint{
"ep1":&endpoint{},
"ep2":&endpoint{},
"ep3":&endpoint{},
"ep1": &endpoint{},
"ep2": &endpoint{},
"ep3": &endpoint{},
},
}
num := nm.GetNumberOfEndpoints(ifName, nwId)
Expand All @@ -220,9 +221,9 @@ var (
}
nm.ExternalInterfaces[ifName].Networks[nwId] = &network{
Endpoints: map[string]*endpoint{
"ep1":&endpoint{},
"ep2":&endpoint{},
"ep3":&endpoint{},
"ep1": &endpoint{},
"ep2": &endpoint{},
"ep3": &endpoint{},
},
}
num := nm.GetNumberOfEndpoints("", nwId)
Expand Down