diff --git a/cni/ipam/ipam.go b/cni/ipam/ipam.go index 32fc849c5a..5989d7e890 100644 --- a/cni/ipam/ipam.go +++ b/cni/ipam/ipam.go @@ -71,8 +71,8 @@ func (plugin *ipamPlugin) Start(config *common.PluginConfig) error { log.Printf("[cni-ipam] Plugin %v version %v.", plugin.Name, plugin.Version) log.Printf("[cni-ipam] Running on %v", platform.GetOSInfo()) - // Initialize address manager. - err = plugin.am.Initialize(config, plugin.Options) + // Initialize address manager. rehyrdration not required on reboot for cni ipam plugin + err = plugin.am.Initialize(config, false, plugin.Options) if err != nil { log.Printf("[cni-ipam] Failed to initialize address manager, err:%v.", err) return err diff --git a/cni/network/network.go b/cni/network/network.go index 1da16a9efd..00e79a96c6 100644 --- a/cni/network/network.go +++ b/cni/network/network.go @@ -144,8 +144,8 @@ func (plugin *netPlugin) Start(config *common.PluginConfig) error { platform.PrintDependencyPackageDetails() common.LogNetworkInterfaces() - // Initialize network manager. - err = plugin.nm.Initialize(config, rehydrateNetworkInfoOnReboot) + // Initialize network manager. rehyrdration not required on reboot for cni plugin + err = plugin.nm.Initialize(config, false) if err != nil { log.Printf("[cni-net] Failed to initialize network manager, err:%v.", err) return err diff --git a/cni/network/network_linux.go b/cni/network/network_linux.go index a8dfda1474..12ffa73d03 100644 --- a/cni/network/network_linux.go +++ b/cni/network/network_linux.go @@ -20,10 +20,7 @@ const ( infraInterface = "eth2" ) -const ( - snatConfigFileName = "/tmp/snatConfig" - rehydrateNetworkInfoOnReboot = false -) +const snatConfigFileName = "/tmp/snatConfig" // handleConsecutiveAdd is a dummy function for Linux platform. func handleConsecutiveAdd(args *cniSkel.CmdArgs, endpointId string, nwInfo network.NetworkInfo, epInfo *network.EndpointInfo, nwCfg *cni.NetworkConfig) (*cniTypesCurr.Result, error) { diff --git a/cni/network/network_windows.go b/cni/network/network_windows.go index f5d3afeeba..5de1b9d38b 100644 --- a/cni/network/network_windows.go +++ b/cni/network/network_windows.go @@ -29,9 +29,7 @@ var ( win1903Version = 18362 ) -const ( - rehydrateNetworkInfoOnReboot = true -) + /* handleConsecutiveAdd handles consecutive add calls for infrastructure containers on Windows platform. * This is a temporary work around for issue #57253 of Kubernetes. diff --git a/cnm/ipam/ipam.go b/cnm/ipam/ipam.go index 5aa7f00247..ec15b31aaf 100644 --- a/cnm/ipam/ipam.go +++ b/cnm/ipam/ipam.go @@ -64,8 +64,8 @@ func (plugin *ipamPlugin) Start(config *common.PluginConfig) error { return err } - // Initialize address manager. - err = plugin.am.Initialize(config, plugin.Options) + // Initialize address manager. rehyrdration required on reboot for cnm ipam plugin + err = plugin.am.Initialize(config, true, plugin.Options) if err != nil { log.Printf("[ipam] Failed to initialize address manager, err:%v.", err) return err diff --git a/cnm/network/network.go b/cnm/network/network.go index 077be98ca6..01a110b832 100644 --- a/cnm/network/network.go +++ b/cnm/network/network.go @@ -70,7 +70,7 @@ func (plugin *netPlugin) Start(config *common.PluginConfig) error { return err } - // Initialize network manager. + // Initialize network manager. rehyrdration required on reboot for cnm plugin err = plugin.nm.Initialize(config, true) if err != nil { log.Printf("[net] Failed to initialize network manager, err:%v.", err) diff --git a/ipam/manager.go b/ipam/manager.go index a823b30059..6a634594a8 100644 --- a/ipam/manager.go +++ b/ipam/manager.go @@ -31,7 +31,7 @@ type addressManager struct { // AddressManager API. type AddressManager interface { - Initialize(config *common.PluginConfig, options map[string]interface{}) error + Initialize(config *common.PluginConfig, rehydrateIpamInfoOnReboot bool, options map[string]interface{}) error Uninitialize() StartSource(options map[string]interface{}) error @@ -70,13 +70,13 @@ func NewAddressManager() (AddressManager, error) { } // Initialize configures address manager. -func (am *addressManager) Initialize(config *common.PluginConfig, options map[string]interface{}) error { +func (am *addressManager) Initialize(config *common.PluginConfig, rehydrateIpamInfoOnReboot bool, options map[string]interface{}) error { am.Version = config.Version am.store = config.Store am.netApi = config.NetApi // Restore persisted state. - err := am.restore() + err := am.restore(rehydrateIpamInfoOnReboot) if err != nil { return err } @@ -93,29 +93,15 @@ func (am *addressManager) Uninitialize() { } // Restore reads address manager state from persistent store. -func (am *addressManager) restore() error { +func (am *addressManager) restore(rehydrateIpamInfoOnReboot bool) error { // Skip if a store is not provided. if am.store == nil { log.Printf("[ipam] ipam store is nil") return nil } - rebooted := false - - // Check if the VM is rebooted. - modTime, err := am.store.GetModificationTime() - if err == nil { - rebootTime, err := platform.GetLastRebootTime() - log.Printf("[ipam] reboot time %v store mod time %v", rebootTime, modTime) - - if err == nil && rebootTime.After(modTime) { - log.Printf("[ipam] Detected Reboot") - rebooted = true - } - } - // Read any persisted state. - err = am.store.Read(storeKey, am) + err := am.store.Read(storeKey, am) if err != nil { if err == store.ErrKeyNotFound { log.Printf("[ipam] store key not found") @@ -144,15 +130,24 @@ func (am *addressManager) restore() error { } // if rebooted mark the ip as not in use. - if rebooted { - log.Printf("[ipam] Rehydrating ipam state from persistent store") - for _, as := range am.AddrSpaces { - for _, ap := range as.Pools { - ap.as = as - ap.RefCount = 0 - - for _, ar := range ap.Addresses { - ar.InUse = false + if rehydrateIpamInfoOnReboot { + // Check if the VM is rebooted. + modTime, err := am.store.GetModificationTime() + if err == nil { + rebootTime, err := platform.GetLastRebootTime() + log.Printf("[ipam] reboot time %v store mod time %v", rebootTime, modTime) + + if err == nil && rebootTime.After(modTime) { + log.Printf("[ipam] Rehydrating ipam state from persistent store") + for _, as := range am.AddrSpaces { + for _, ap := range as.Pools { + ap.as = as + ap.RefCount = 0 + + for _, ar := range ap.Addresses { + ar.InUse = false + } + } } } } diff --git a/ipam/manager_ipv6Ipam_test.go b/ipam/manager_ipv6Ipam_test.go index 13d060beed..0c33005781 100644 --- a/ipam/manager_ipv6Ipam_test.go +++ b/ipam/manager_ipv6Ipam_test.go @@ -36,7 +36,7 @@ func createTestIpv6AddressManager() (AddressManager, error) { return nil, err } - err = am.Initialize(&config, options) + err = am.Initialize(&config, false, options) if err != nil { return nil, err } diff --git a/ipam/manager_test.go b/ipam/manager_test.go index 0c1f0ec251..879a7180a1 100644 --- a/ipam/manager_test.go +++ b/ipam/manager_test.go @@ -49,7 +49,7 @@ func createAddressManager(options map[string]interface{}) (AddressManager, error return nil, err } - if err := am.Initialize(&config, options); err != nil { + if err := am.Initialize(&config, false, options); err != nil { return nil, err } @@ -164,7 +164,7 @@ var ( am, err := NewAddressManager() Expect(am).NotTo(BeNil()) Expect(err).NotTo(HaveOccurred()) - err = am.Initialize(&config, options) + err = am.Initialize(&config, false,options) Expect(err).To(BeNil()) }) }) @@ -180,7 +180,7 @@ var ( am, err := NewAddressManager() Expect(am).NotTo(BeNil()) Expect(err).NotTo(HaveOccurred()) - err = am.Initialize(&config, options) + err = am.Initialize(&config, false,options) Expect(err).To(BeNil()) }) }) @@ -196,7 +196,7 @@ var ( am, err := NewAddressManager() Expect(am).NotTo(BeNil()) Expect(err).NotTo(HaveOccurred()) - err = am.Initialize(&config, options) + err = am.Initialize(&config, false, options) Expect(err).To(HaveOccurred()) }) }) @@ -209,7 +209,7 @@ var ( am, err := NewAddressManager() Expect(am).NotTo(BeNil()) Expect(err).NotTo(HaveOccurred()) - err = am.Initialize(&config, options) + err = am.Initialize(&config, false,options) Expect(err).To(HaveOccurred()) }) }) @@ -221,7 +221,7 @@ var ( am := &addressManager{ AddrSpaces: make(map[string]*addressSpace), } - err := am.restore() + err := am.restore(false) Expect(err).To(BeNil()) }) }) @@ -250,7 +250,7 @@ var ( } as.Pools["ap-test"] = ap am.AddrSpaces["as-test"] = as - err := am.restore() + err := am.restore(false) Expect(err).To(BeNil()) as = am.AddrSpaces["as-test"] ap = as.Pools["ap-test"] @@ -284,7 +284,7 @@ var ( } as.Pools["ap-test"] = ap am.AddrSpaces["as-test"] = as - err := am.restore() + err := am.restore(false) Expect(err).To(BeNil()) as = am.AddrSpaces["as-test"] ap = as.Pools["ap-test"] @@ -294,38 +294,6 @@ var ( Expect(ar.InUse).To(BeTrue()) }) }) - - Context("When rebooted", func() { - It("Should clear the RefCount and InUse", func() { - am := &addressManager{ - AddrSpaces: make(map[string]*addressSpace), - } - am.store = &testutils.KeyValueStoreMock{} - ap := &addressPool{ - Id: "ap-test", - RefCount: 1, - Addresses: make(map[string]*addressRecord), - } - ap.Addresses["ar-test"] = &addressRecord{ - ID: "ar-test", - InUse: true, - } - as := &addressSpace{ - Id: "as-test", - Pools: make(map[string]*addressPool), - } - as.Pools["ap-test"] = ap - am.AddrSpaces["as-test"] = as - err := am.restore() - Expect(err).To(BeNil()) - as = am.AddrSpaces["as-test"] - ap = as.Pools["ap-test"] - ar := ap.addrsByID["ar-test"] - Expect(ar.ID).To(Equal("ar-test")) - Expect(ap.RefCount).To(Equal(0)) - Expect(ar.InUse).To(BeFalse()) - }) - }) }) Describe("Test save", func() {