Skip to content

Commit 41ffd07

Browse files
authored
Add a default route to the first interface only (#697)
Adds a default route to the first interface only.
1 parent bbea928 commit 41ffd07

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

Sources/Containerization/LinuxContainer.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,12 +590,16 @@ extension LinuxContainer {
590590
// For every interface asked for:
591591
// 1. Add the address requested
592592
// 2. Online the adapter
593-
// 3. If a gateway IP address is present, add the default route.
593+
// 3. For the first interface, add the default route
594+
var defaultRouteSet = false
594595
for (index, i) in self.interfaces.enumerated() {
595596
let name = "eth\(index)"
596597
self.logger?.debug("setting up interface \(name) with address \(i.ipv4Address)")
597598
try await agent.addressAdd(name: name, ipv4Address: i.ipv4Address)
598599
try await agent.up(name: name, mtu: i.mtu)
600+
if defaultRouteSet {
601+
continue
602+
}
599603
if let ipv4Gateway = i.ipv4Gateway {
600604
if !i.ipv4Address.contains(ipv4Gateway) {
601605
self.logger?.debug("gateway \(ipv4Gateway) is outside subnet \(i.ipv4Address), adding a route first")
@@ -606,6 +610,7 @@ extension LinuxContainer {
606610
self.logger?.debug("no gateway for \(name)")
607611
try await agent.routeAddDefault(name: name, ipv4Gateway: nil)
608612
}
613+
defaultRouteSet = true
609614
}
610615

611616
// Setup /etc/resolv.conf and /etc/hosts if asked for.

Sources/Containerization/LinuxPod.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,12 +444,16 @@ extension LinuxPod {
444444
// For every interface asked for:
445445
// 1. Add the address requested
446446
// 2. Online the adapter
447-
// 3. If a gateway IP address is present, add the default route.
447+
// 3. For the first interface, add the default route
448+
var defaultRouteSet = false
448449
for (index, i) in self.interfaces.enumerated() {
449450
let name = "eth\(index)"
450451
self.logger?.debug("setting up interface \(name) with address \(i.ipv4Address)")
451452
try await agent.addressAdd(name: name, ipv4Address: i.ipv4Address)
452453
try await agent.up(name: name, mtu: i.mtu)
454+
if defaultRouteSet {
455+
continue
456+
}
453457
if let ipv4Gateway = i.ipv4Gateway {
454458
if !i.ipv4Address.contains(ipv4Gateway) {
455459
self.logger?.debug("gateway \(ipv4Gateway) is outside subnet \(i.ipv4Address), adding a route first")
@@ -460,6 +464,7 @@ extension LinuxPod {
460464
self.logger?.debug("no gateway for \(name)")
461465
try await agent.routeAddDefault(name: name, ipv4Gateway: nil)
462466
}
467+
defaultRouteSet = true
463468
}
464469

465470
// Setup /etc/resolv.conf and /etc/hosts for each container.

0 commit comments

Comments
 (0)