88
99 "github.com/golang/glog"
1010
11+ ktypes "k8s.io/apimachinery/pkg/types"
1112 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
1213 "k8s.io/apimachinery/pkg/util/sets"
1314 "k8s.io/apimachinery/pkg/watch"
@@ -63,6 +64,7 @@ type EgressIPTracker struct {
6364
6465 watcher EgressIPWatcher
6566
67+ nodes map [ktypes.UID ]* nodeEgress
6668 nodesByNodeIP map [string ]* nodeEgress
6769 namespacesByVNID map [uint32 ]* namespaceEgress
6870 egressIPs map [string ]* egressIPInfo
@@ -77,6 +79,7 @@ func NewEgressIPTracker(watcher EgressIPWatcher) *EgressIPTracker {
7779 return & EgressIPTracker {
7880 watcher : watcher ,
7981
82+ nodes : make (map [ktypes.UID ]* nodeEgress ),
8083 nodesByNodeIP : make (map [string ]* nodeEgress ),
8184 namespacesByVNID : make (map [uint32 ]* namespaceEgress ),
8285 egressIPs : make (map [string ]* egressIPInfo ),
@@ -167,9 +170,10 @@ func (eit *EgressIPTracker) handleDeleteHostSubnet(obj interface{}) {
167170 hs := obj .(* networkapi.HostSubnet )
168171 glog .V (5 ).Infof ("Watch %s event for HostSubnet %q" , watch .Deleted , hs .Name )
169172
170- eit .UpdateHostSubnetEgress (& networkapi.HostSubnet {
171- HostIP : hs .HostIP ,
172- })
173+ hs = hs .DeepCopy ()
174+ hs .EgressCIDRs = nil
175+ hs .EgressIPs = nil
176+ eit .UpdateHostSubnetEgress (hs )
173177}
174178
175179func (eit * EgressIPTracker ) UpdateHostSubnetEgress (hs * networkapi.HostSubnet ) {
@@ -185,7 +189,7 @@ func (eit *EgressIPTracker) UpdateHostSubnetEgress(hs *networkapi.HostSubnet) {
185189 sdnIP = netutils .GenerateDefaultGateway (cidr ).String ()
186190 }
187191
188- node := eit .nodesByNodeIP [hs .HostIP ]
192+ node := eit .nodes [hs .UID ]
189193 if node == nil {
190194 if len (hs .EgressIPs ) == 0 && len (hs .EgressCIDRs ) == 0 {
191195 return
@@ -196,9 +200,11 @@ func (eit *EgressIPTracker) UpdateHostSubnetEgress(hs *networkapi.HostSubnet) {
196200 sdnIP : sdnIP ,
197201 requestedIPs : sets .NewString (),
198202 }
203+ eit .nodes [hs .UID ] = node
199204 eit .nodesByNodeIP [hs .HostIP ] = node
200205 } else if len (hs .EgressIPs ) == 0 && len (hs .EgressCIDRs ) == 0 {
201- delete (eit .nodesByNodeIP , hs .HostIP )
206+ delete (eit .nodes , hs .UID )
207+ delete (eit .nodesByNodeIP , node .nodeIP )
202208 }
203209
204210 // Process EgressCIDRs
@@ -218,6 +224,28 @@ func (eit *EgressIPTracker) UpdateHostSubnetEgress(hs *networkapi.HostSubnet) {
218224 eit .updateEgressCIDRs = true
219225 }
220226
227+ if node .nodeIP != hs .HostIP {
228+ // We have to clean up the old egress IP mappings and call syncEgressIPs
229+ // before we can change node.nodeIP
230+ movedEgressIPs := make ([]string , 0 , node .requestedIPs .Len ())
231+ for _ , ip := range node .requestedIPs .UnsortedList () {
232+ eg := eit .egressIPs [ip ]
233+ if eg .assignedNodeIP == node .nodeIP {
234+ movedEgressIPs = append (movedEgressIPs , ip )
235+ eit .deleteNodeEgressIP (node , ip )
236+ }
237+ }
238+ eit .syncEgressIPs ()
239+
240+ delete (eit .nodesByNodeIP , node .nodeIP )
241+ node .nodeIP = hs .HostIP
242+ eit .nodesByNodeIP [node .nodeIP ] = node
243+
244+ for _ , ip := range movedEgressIPs {
245+ eit .addNodeEgressIP (node , ip )
246+ }
247+ }
248+
221249 // Process new and removed EgressIPs
222250 oldRequestedIPs := node .requestedIPs
223251 node .requestedIPs = sets .NewString (hs .EgressIPs ... )
@@ -448,7 +476,7 @@ func (eit *EgressIPTracker) findEgressIPAllocation(ip net.IP, allocation map[str
448476 bestNode := ""
449477 otherNodes := false
450478
451- for _ , node := range eit .nodesByNodeIP {
479+ for _ , node := range eit .nodes {
452480 egressIPs , exists := allocation [node .nodeName ]
453481 if ! exists {
454482 continue
@@ -478,7 +506,7 @@ func (eit *EgressIPTracker) ReallocateEgressIPs() map[string][]string {
478506 allocation := make (map [string ][]string )
479507 changed := make (map [string ]bool )
480508 alreadyAllocated := make (map [string ]bool )
481- for _ , node := range eit .nodesByNodeIP {
509+ for _ , node := range eit .nodes {
482510 if len (node .parsedCIDRs ) > 0 {
483511 allocation [node .nodeName ] = make ([]string , 0 , node .requestedIPs .Len ())
484512 }
@@ -534,7 +562,7 @@ func (eit *EgressIPTracker) ReallocateEgressIPs() map[string][]string {
534562 }
535563
536564 // Remove unchanged nodes from the return value
537- for _ , node := range eit .nodesByNodeIP {
565+ for _ , node := range eit .nodes {
538566 if ! changed [node .nodeName ] {
539567 delete (allocation , node .nodeName )
540568 }
0 commit comments