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
4 changes: 2 additions & 2 deletions cni/ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cni/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions cni/network/network_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 1 addition & 3 deletions cni/network/network_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions cnm/ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cnm/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
51 changes: 23 additions & 28 deletions ipam/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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")
Expand Down Expand Up @@ -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
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ipam/manager_ipv6Ipam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
48 changes: 8 additions & 40 deletions ipam/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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())
})
})
Expand All @@ -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())
})
})
Expand All @@ -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())
})
})
Expand All @@ -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())
})
})
Expand All @@ -221,7 +221,7 @@ var (
am := &addressManager{
AddrSpaces: make(map[string]*addressSpace),
}
err := am.restore()
err := am.restore(false)
Expect(err).To(BeNil())
})
})
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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"]
Expand All @@ -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() {
Expand Down