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:
J Logan
2025-08-13 18:18:19 -07:00
committed by GitHub
parent d2eb350393
commit a8dbf294f6
11 changed files with 171 additions and 180 deletions
@@ -14,6 +14,7 @@
// limitations under the License.
//===----------------------------------------------------------------------===//
import ContainerNetworkService
import ContainerizationOCI
public struct ContainerConfiguration: Sendable, Codable {
@@ -32,13 +33,11 @@ public struct ContainerConfiguration: Sendable, Codable {
/// System controls for the container.
public var sysctls: [String: String] = [:]
/// The networks the container will be added to.
public var networks: [String] = []
public var networks: [AttachmentConfiguration] = []
/// The DNS configuration for the container.
public var dns: DNSConfiguration? = nil
/// Whether to enable rosetta x86-64 translation for the container.
public var rosetta: Bool = false
/// The hostname for the container.
public var hostname: String? = nil
/// Initial or main process of the container.
public var initProcess: ProcessConfiguration
/// Platform for the container
@@ -61,7 +60,6 @@ public struct ContainerConfiguration: Sendable, Codable {
case networks
case dns
case rosetta
case hostname
case initProcess
case platform
case resources
@@ -81,10 +79,21 @@ public struct ContainerConfiguration: Sendable, Codable {
publishedSockets = try container.decodeIfPresent([PublishSocket].self, forKey: .publishedSockets) ?? []
labels = try container.decodeIfPresent([String: String].self, forKey: .labels) ?? [:]
sysctls = try container.decodeIfPresent([String: String].self, forKey: .sysctls) ?? [:]
networks = try container.decodeIfPresent([String].self, forKey: .networks) ?? []
// NOTE: migrates [String] to [AttachmentConfiguration]; remove [String] support in a later release
if container.contains(.networks) {
do {
networks = try container.decode([AttachmentConfiguration].self, forKey: .networks)
} catch {
let networkIds = try container.decode([String].self, forKey: .networks)
networks = try Utility.getAttachmentConfigurations(containerId: id, networkIds: networkIds)
}
} else {
networks = []
}
dns = try container.decodeIfPresent(DNSConfiguration.self, forKey: .dns)
rosetta = try container.decodeIfPresent(Bool.self, forKey: .rosetta) ?? false
hostname = try container.decodeIfPresent(String.self, forKey: .hostname)
initProcess = try container.decode(ProcessConfiguration.self, forKey: .initProcess)
platform = try container.decodeIfPresent(ContainerizationOCI.Platform.self, forKey: .platform) ?? .current
resources = try container.decodeIfPresent(Resources.self, forKey: .resources) ?? .init()