Skip to content

Commit edad963

Browse files
Merge pull request #20500 from danwinship/egress-ip-cleanup
Don't auto-assign egress IPs that are no longer in use
2 parents 875f246 + 9132d51 commit edad963

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

pkg/network/common/egressip.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func (eit *EgressIPTracker) UpdateHostSubnetEgress(hs *networkapi.HostSubnet) {
230230
movedEgressIPs := make([]string, 0, node.requestedIPs.Len())
231231
for _, ip := range node.requestedIPs.UnsortedList() {
232232
eg := eit.egressIPs[ip]
233-
if eg.assignedNodeIP == node.nodeIP {
233+
if eg != nil && eg.assignedNodeIP == node.nodeIP {
234234
movedEgressIPs = append(movedEgressIPs, ip)
235235
eit.deleteNodeEgressIP(node, ip)
236236
}
@@ -308,7 +308,9 @@ func (eit *EgressIPTracker) UpdateNetNamespaceEgress(netns *networkapi.NetNamesp
308308
// Even IPs that weren't added/removed need to be considered "changed", to
309309
// ensure we correctly process reorderings, duplicates added/removed, etc.
310310
for _, ip := range newRequestedIPs.Intersection(oldRequestedIPs).UnsortedList() {
311-
eit.egressIPChanged(eit.egressIPs[ip])
311+
if eg := eit.egressIPs[ip]; eg != nil {
312+
eit.egressIPChanged(eg)
313+
}
312314
}
313315

314316
eit.syncEgressIPs()
@@ -332,7 +334,7 @@ func (eit *EgressIPTracker) egressIPActive(eg *egressIPInfo) (bool, error) {
332334
}
333335
for _, ip := range eg.namespaces[0].requestedIPs {
334336
eg2 := eit.egressIPs[ip]
335-
if eg2 != eg && len(eg2.nodes) == 1 && eg2.nodes[0] == eg.nodes[0] {
337+
if eg2 != nil && eg2 != eg && len(eg2.nodes) == 1 && eg2.nodes[0] == eg.nodes[0] {
336338
return false, fmt.Errorf("Multiple EgressIPs (%s, %s) for VNID %d on node %s", eg.ip, eg2.ip, eg.namespaces[0].vnid, eg.nodes[0].nodeIP)
337339
}
338340
}
@@ -358,6 +360,12 @@ func (eit *EgressIPTracker) syncEgressIPs() {
358360
eit.syncEgressNamespaceState(ns)
359361
}
360362

363+
for eg := range changedEgressIPs {
364+
if len(eg.namespaces) == 0 && len(eg.nodes) == 0 {
365+
delete(eit.egressIPs, eg.ip)
366+
}
367+
}
368+
361369
if eit.updateEgressCIDRs {
362370
eit.updateEgressCIDRs = false
363371
if eit.nodesWithCIDRs > 0 {
@@ -539,7 +547,7 @@ func (eit *EgressIPTracker) ReallocateEgressIPs() map[string][]string {
539547

540548
// Allocate pending egress IPs that can only go to a single node
541549
for egressIP, eip := range eit.egressIPs {
542-
if alreadyAllocated[egressIP] {
550+
if alreadyAllocated[egressIP] || len(eip.namespaces) == 0 {
543551
continue
544552
}
545553
nodeName, otherNodes := eit.findEgressIPAllocation(eip.parsed, allocation)
@@ -551,7 +559,7 @@ func (eit *EgressIPTracker) ReallocateEgressIPs() map[string][]string {
551559
}
552560
// Allocate any other pending egress IPs that we can
553561
for egressIP, eip := range eit.egressIPs {
554-
if alreadyAllocated[egressIP] {
562+
if alreadyAllocated[egressIP] || len(eip.namespaces) == 0 {
555563
continue
556564
}
557565
nodeName, _ := eit.findEgressIPAllocation(eip.parsed, allocation)

pkg/network/common/egressip_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,36 @@ func TestEgressCIDRAllocation(t *testing.T) {
946946
if err != nil {
947947
t.Fatalf("%v", err)
948948
}
949+
950+
// Changing the EgressIPs of a namespace should drop the old allocation and create a new one
951+
updateNetNamespaceEgress(eit, &networkapi.NetNamespace{
952+
NetID: 46,
953+
EgressIPs: []string{"172.17.0.202"}, // was 172.17.0.200
954+
})
955+
err = w.assertChanges(
956+
"release 172.17.0.200 on 172.17.0.4",
957+
"namespace 46 dropped",
958+
"update egress CIDRs",
959+
)
960+
if err != nil {
961+
t.Fatalf("%v", err)
962+
}
963+
964+
allocation = eit.ReallocateEgressIPs()
965+
for _, ip := range allocation["node-4"] {
966+
if ip == "172.17.0.200" {
967+
t.Fatalf("reallocation failed to drop unused egress IP 172.17.0.200: %#v", allocation)
968+
}
969+
}
970+
updateAllocations(eit, allocation)
971+
err = w.assertChanges(
972+
"claim 172.17.0.202 on 172.17.0.4 for namespace 46",
973+
"namespace 46 via 172.17.0.202 on 172.17.0.4",
974+
"update egress CIDRs",
975+
)
976+
if err != nil {
977+
t.Fatalf("%v", err)
978+
}
949979
}
950980

951981
func TestEgressNodeRenumbering(t *testing.T) {

0 commit comments

Comments
 (0)