mirror of
https://github.com/apple/container.git
synced 2026-09-21 15:15:36 +00:00
Ensure two containers cannot use the same DNS hostname. (#490)
- Closes #150, #394. - Introduces `AttachmentConfiguration` type so that we can add key-value options to `--network` in the future. - Eliminates redundant `ContainersService.Item` type. - Since we now ensure at ContainersService that hostnames will not conflict, the network helper IP allocator now simply provides the existing IP if for an allocation on an existing hostname, which should handle (in an eventually consistent way) the case where a container fails to deallocate an IP on shutdown.
This commit is contained in:
@@ -55,7 +55,6 @@ public actor AllocationOnlyVmnetNetwork: Network {
|
||||
guard case .created(let configuration) = _state else {
|
||||
throw ContainerizationError(.invalidState, message: "cannot start network \(_state.id) in \(_state.state) state")
|
||||
}
|
||||
var defaultSubnet = "192.168.64.1/24"
|
||||
|
||||
log.info(
|
||||
"starting allocation-only network",
|
||||
@@ -65,21 +64,16 @@ public actor AllocationOnlyVmnetNetwork: Network {
|
||||
]
|
||||
)
|
||||
|
||||
if let suite = UserDefaults.init(suiteName: UserDefaults.appSuiteName) {
|
||||
// TODO: Make the suiteName a constant defined in DefaultsStore and use that.
|
||||
// This will need some re-working of dependencies between NetworkService and Client
|
||||
defaultSubnet = suite.string(forKey: "network.subnet") ?? defaultSubnet
|
||||
}
|
||||
|
||||
let subnet = try CIDRAddress(defaultSubnet)
|
||||
let gateway = IPv4Address(fromValue: subnet.lower.value + 1)
|
||||
self._state = .running(configuration, NetworkStatus(address: subnet.description, gateway: gateway.description))
|
||||
let subnet = DefaultsStore.get(key: .defaultSubnet)
|
||||
let subnetCIDR = try CIDRAddress(subnet)
|
||||
let gateway = IPv4Address(fromValue: subnetCIDR.lower.value + 1)
|
||||
self._state = .running(configuration, NetworkStatus(address: subnetCIDR.description, gateway: gateway.description))
|
||||
log.info(
|
||||
"started allocation-only network",
|
||||
metadata: [
|
||||
"id": "\(configuration.id)",
|
||||
"mode": "\(configuration.mode)",
|
||||
"cidr": "\(defaultSubnet)",
|
||||
"cidr": "\(subnet)",
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
@@ -30,9 +30,11 @@ actor AttachmentAllocator {
|
||||
|
||||
/// Allocate a network address for a host.
|
||||
func allocate(hostname: String) async throws -> UInt32 {
|
||||
guard hostnames[hostname] == nil else {
|
||||
throw ContainerizationError(.exists, message: "Hostname \(hostname) already exists on the network")
|
||||
// Client is responsible for ensuring two containers don't use same hostname, so provide existing IP if hostname exists
|
||||
if let index = hostnames[hostname] {
|
||||
return index
|
||||
}
|
||||
|
||||
let index = try allocator.allocate()
|
||||
hostnames[hostname] = index
|
||||
|
||||
|
||||
+21
-3
@@ -14,8 +14,26 @@
|
||||
// limitations under the License.
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import Foundation
|
||||
/// Configuration information for attaching a container network interface to a network.
|
||||
public struct AttachmentConfiguration: Codable, Sendable {
|
||||
/// The network ID associated with the attachment.
|
||||
public let network: String
|
||||
|
||||
extension UserDefaults {
|
||||
public static let appSuiteName = "com.apple.container.defaults"
|
||||
/// The option information for the attachment
|
||||
public let options: AttachmentOptions
|
||||
|
||||
public init(network: String, options: AttachmentOptions) {
|
||||
self.network = network
|
||||
self.options = options
|
||||
}
|
||||
}
|
||||
|
||||
// Option information for a network attachment.
|
||||
public struct AttachmentOptions: Codable, Sendable {
|
||||
/// The hostname associated with the attachment.
|
||||
public let hostname: String
|
||||
|
||||
public init(hostname: String) {
|
||||
self.hostname = hostname
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@
|
||||
// limitations under the License.
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import ContainerPersistence
|
||||
import ContainerXPC
|
||||
import Containerization
|
||||
import ContainerizationError
|
||||
@@ -100,8 +101,7 @@ public final class ReservedVmnetNetwork: Network {
|
||||
"mode": "\(configuration.mode)",
|
||||
]
|
||||
)
|
||||
let suite = UserDefaults.init(suiteName: UserDefaults.appSuiteName)
|
||||
let subnetText = configuration.subnet ?? suite?.string(forKey: "network.subnet")
|
||||
let subnetText = configuration.subnet ?? DefaultsStore.getOptional(key: .defaultSubnet)
|
||||
|
||||
// with the reservation API, subnet priority is CLI argument, UserDefault, auto
|
||||
let subnet = try subnetText.map { try CIDRAddress($0) }
|
||||
|
||||
Reference in New Issue
Block a user