8
8
9
9
"github.com/golang/glog"
10
10
11
+ ktypes "k8s.io/apimachinery/pkg/types"
11
12
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
12
13
"k8s.io/apimachinery/pkg/util/sets"
13
14
"k8s.io/apimachinery/pkg/watch"
@@ -63,6 +64,7 @@ type EgressIPTracker struct {
63
64
64
65
watcher EgressIPWatcher
65
66
67
+ nodes map [ktypes.UID ]* nodeEgress
66
68
nodesByNodeIP map [string ]* nodeEgress
67
69
namespacesByVNID map [uint32 ]* namespaceEgress
68
70
egressIPs map [string ]* egressIPInfo
@@ -77,6 +79,7 @@ func NewEgressIPTracker(watcher EgressIPWatcher) *EgressIPTracker {
77
79
return & EgressIPTracker {
78
80
watcher : watcher ,
79
81
82
+ nodes : make (map [ktypes.UID ]* nodeEgress ),
80
83
nodesByNodeIP : make (map [string ]* nodeEgress ),
81
84
namespacesByVNID : make (map [uint32 ]* namespaceEgress ),
82
85
egressIPs : make (map [string ]* egressIPInfo ),
@@ -167,9 +170,10 @@ func (eit *EgressIPTracker) handleDeleteHostSubnet(obj interface{}) {
167
170
hs := obj .(* networkapi.HostSubnet )
168
171
glog .V (5 ).Infof ("Watch %s event for HostSubnet %q" , watch .Deleted , hs .Name )
169
172
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 )
173
177
}
174
178
175
179
func (eit * EgressIPTracker ) UpdateHostSubnetEgress (hs * networkapi.HostSubnet ) {
@@ -185,7 +189,7 @@ func (eit *EgressIPTracker) UpdateHostSubnetEgress(hs *networkapi.HostSubnet) {
185
189
sdnIP = netutils .GenerateDefaultGateway (cidr ).String ()
186
190
}
187
191
188
- node := eit .nodesByNodeIP [hs .HostIP ]
192
+ node := eit .nodes [hs .UID ]
189
193
if node == nil {
190
194
if len (hs .EgressIPs ) == 0 && len (hs .EgressCIDRs ) == 0 {
191
195
return
@@ -196,9 +200,11 @@ func (eit *EgressIPTracker) UpdateHostSubnetEgress(hs *networkapi.HostSubnet) {
196
200
sdnIP : sdnIP ,
197
201
requestedIPs : sets .NewString (),
198
202
}
203
+ eit .nodes [hs .UID ] = node
199
204
eit .nodesByNodeIP [hs .HostIP ] = node
200
205
} 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 )
202
208
}
203
209
204
210
// Process EgressCIDRs
@@ -218,6 +224,28 @@ func (eit *EgressIPTracker) UpdateHostSubnetEgress(hs *networkapi.HostSubnet) {
218
224
eit .updateEgressCIDRs = true
219
225
}
220
226
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
+
221
249
// Process new and removed EgressIPs
222
250
oldRequestedIPs := node .requestedIPs
223
251
node .requestedIPs = sets .NewString (hs .EgressIPs ... )
@@ -448,7 +476,7 @@ func (eit *EgressIPTracker) findEgressIPAllocation(ip net.IP, allocation map[str
448
476
bestNode := ""
449
477
otherNodes := false
450
478
451
- for _ , node := range eit .nodesByNodeIP {
479
+ for _ , node := range eit .nodes {
452
480
egressIPs , exists := allocation [node .nodeName ]
453
481
if ! exists {
454
482
continue
@@ -478,7 +506,7 @@ func (eit *EgressIPTracker) ReallocateEgressIPs() map[string][]string {
478
506
allocation := make (map [string ][]string )
479
507
changed := make (map [string ]bool )
480
508
alreadyAllocated := make (map [string ]bool )
481
- for _ , node := range eit .nodesByNodeIP {
509
+ for _ , node := range eit .nodes {
482
510
if len (node .parsedCIDRs ) > 0 {
483
511
allocation [node .nodeName ] = make ([]string , 0 , node .requestedIPs .Len ())
484
512
}
@@ -534,7 +562,7 @@ func (eit *EgressIPTracker) ReallocateEgressIPs() map[string][]string {
534
562
}
535
563
536
564
// Remove unchanged nodes from the return value
537
- for _ , node := range eit .nodesByNodeIP {
565
+ for _ , node := range eit .nodes {
538
566
if ! changed [node .nodeName ] {
539
567
delete (allocation , node .nodeName )
540
568
}
0 commit comments