Makes interface gateway configuration optional. (#156)

Here we implement the mechanism for ensuring that `container` doesn't
set up two default routes on containers that connect to multiple
networks. We'll implement the policy in apple/container#243.
This commit is contained in:
J Logan
2025-06-27 15:16:15 -04:00
committed by GitHub
parent 885db858a5
commit 1a59de82b0
4 changed files with 17 additions and 9 deletions
+7 -1
View File
@@ -16,7 +16,13 @@
/// A network interface.
public protocol Interface: Sendable {
/// The interface IPv4 address and subnet prefix length, as a CIDR address.
/// Example: `192.168.64.3/24`
var address: String { get }
var gateway: String { get }
/// The IP address for the default route, or nil for no default route.
var gateway: String? { get }
/// The interface MAC address, or nil to auto-configure the address.
var macAddress: String? { get }
}
@@ -500,12 +500,14 @@ extension LinuxContainer {
// For every interface asked for:
// 1. Add the address requested
// 2. Online the adapter
// 3. Add the gateway address
// 3. If a gateway IP address is present, add the default route.
for (index, i) in self.interfaces.enumerated() {
let name = "eth\(index)"
try await agent.addressAdd(name: name, address: i.address)
try await agent.up(name: name)
try await agent.routeAddDefault(name: name, gateway: i.gateway)
if let gateway = i.gateway {
try await agent.routeAddDefault(name: name, gateway: gateway)
}
}
if let dns = self.dns {
try await agent.configureDNS(config: dns, location: rootfs.destination)
+2 -2
View File
@@ -16,10 +16,10 @@
public struct NATInterface: Interface {
public var address: String
public var gateway: String
public var gateway: String?
public var macAddress: String?
public init(address: String, gateway: String, macAddress: String? = nil) {
public init(address: String, gateway: String?, macAddress: String? = nil) {
self.address = address
self.gateway = gateway
self.macAddress = macAddress
@@ -32,7 +32,7 @@ public final class NATNetworkInterface: Interface, Sendable {
}
public var gateway: String {
public var gateway: String? {
get { state.gateway }
set { state.gateway = newValue }
}
@@ -49,7 +49,7 @@ public final class NATNetworkInterface: Interface, Sendable {
private struct State {
fileprivate var address: String
fileprivate var gateway: String
fileprivate var gateway: String?
fileprivate var reference: vmnet_network_ref!
fileprivate var macAddress: String?
}
@@ -60,7 +60,7 @@ public final class NATNetworkInterface: Interface, Sendable {
@available(macOS 26, *)
public init(
address: String,
gateway: String,
gateway: String?,
reference: sending vmnet_network_ref,
macAddress: String? = nil
) {
@@ -75,7 +75,7 @@ public final class NATNetworkInterface: Interface, Sendable {
@available(macOS, obsoleted: 26, message: "Use init(address:gateway:reference:macAddress:) instead")
public init(
address: String,
gateway: String,
gateway: String?,
macAddress: String? = nil
) {
self.state = .init(