@@ -5,6 +5,7 @@ package ipam
55
66import (
77 "sync"
8+ "time"
89
910 "github.com/Azure/azure-container-networking/common"
1011 "github.com/Azure/azure-container-networking/log"
@@ -19,6 +20,8 @@ const (
1920
2021// AddressManager manages the set of address spaces and pools allocated to containers.
2122type addressManager struct {
23+ Version string
24+ TimeStamp time.Time
2225 AddrSpaces map [string ]* addressSpace `json:"AddressSpaces"`
2326 store store.KeyValueStore
2427 source addressConfigSource
@@ -64,6 +67,7 @@ func NewAddressManager() (AddressManager, error) {
6467
6568// Initialize configures address manager.
6669func (am * addressManager ) Initialize (config * common.PluginConfig , options map [string ]interface {}) error {
70+ am .Version = config .Version
6771 am .store = config .Store
6872 am .netApi , _ = config .NetApi .(network.NetworkManager )
6973
@@ -91,8 +95,20 @@ func (am *addressManager) restore() error {
9195 return nil
9296 }
9397
98+ // After a reboot, all address resources are implicitly released.
99+ // Ignore the persisted state if it is older than the last reboot time.
100+ modTime , err := am .store .GetModificationTime ()
101+ if err == nil {
102+ rebootTime , err := common .GetLastRebootTime ()
103+ if err == nil && rebootTime .After (modTime ) {
104+ log .Printf ("[ipam] Ignoring stale state from store." )
105+ log .Printf ("[ipam] Store timestamp %v is older than boot timestamp %v." , modTime , rebootTime )
106+ return nil
107+ }
108+ }
109+
94110 // Read any persisted state.
95- err : = am .store .Read (storeKey , am )
111+ err = am .store .Read (storeKey , am )
96112 if err != nil {
97113 if err == store .ErrKeyNotFound {
98114 return nil
@@ -121,6 +137,9 @@ func (am *addressManager) save() error {
121137 return nil
122138 }
123139
140+ // Update time stamp.
141+ am .TimeStamp = time .Now ()
142+
124143 err := am .store .Write (storeKey , am )
125144 if err == nil {
126145 log .Printf ("[ipam] Save succeeded.\n " )
0 commit comments