Skip to content

Commit 8eb8cca

Browse files
jglogansaehejkang
authored andcommitted
Use new IP/CIDR types from Containerization. (apple#957)
- Part of work for apple#460. - With CZ release 0.17.0, the IP and CIDR address types changed from String to IPv4Address and CIDRv4, respectively. This PR applies the corresponding adaptations to container.
1 parent 3eb54b1 commit 8eb8cca

File tree

24 files changed

+176
-117
lines changed

24 files changed

+176
-117
lines changed

Package.resolved

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import PackageDescription
2323
let releaseVersion = ProcessInfo.processInfo.environment["RELEASE_VERSION"] ?? "0.0.0"
2424
let gitCommit = ProcessInfo.processInfo.environment["GIT_COMMIT"] ?? "unspecified"
2525
let builderShimVersion = "0.7.0"
26-
let scVersion = "0.16.2"
26+
let scVersion = "0.17.0"
2727

2828
let package = Package(
2929
name: "container",
@@ -146,6 +146,7 @@ let package = Package(
146146
dependencies: [
147147
.product(name: "Logging", package: "swift-log"),
148148
.product(name: "Containerization", package: "containerization"),
149+
.product(name: "ContainerizationExtras", package: "containerization"),
149150
.product(name: "ContainerizationOS", package: "containerization"),
150151
.product(name: "ArgumentParser", package: "swift-argument-parser"),
151152
"ContainerClient",

Sources/ContainerCommands/Builder/BuilderStart.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ extension Application {
230230
throw ContainerizationError(.invalidState, message: "default network is not running")
231231
}
232232
config.networks = [AttachmentConfiguration(network: network.id, options: AttachmentOptions(hostname: id))]
233-
let subnet = try CIDRAddress(networkStatus.address)
234-
let nameserver = IPv4Address(fromValue: subnet.lower.value + 1).description
233+
let subnet = networkStatus.ipv4Subnet
234+
let nameserver = IPv4Address(subnet.lower.value + 1).description
235235
let nameservers = [nameserver]
236236
config.dns = ContainerConfiguration.DNSConfiguration(nameservers: nameservers)
237237

Sources/ContainerCommands/Builder/BuilderStatus.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ extension ClientContainer {
9494
self.id,
9595
self.configuration.image.reference,
9696
self.status.rawValue,
97-
self.networks.compactMap { try? CIDRAddress($0.address).address.description }.joined(separator: ","),
97+
self.networks.compactMap { $0.ipv4Address.description }.joined(separator: ","),
9898
"\(self.configuration.resources.cpus)",
9999
"\(self.configuration.resources.memoryInBytes / (1024 * 1024)) MB",
100100
]

Sources/ContainerCommands/Container/ContainerList.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ extension ClientContainer {
9494
self.configuration.platform.os,
9595
self.configuration.platform.architecture,
9696
self.status.rawValue,
97-
self.networks.compactMap { try? CIDRAddress($0.address).address.description }.joined(separator: ","),
97+
self.networks.compactMap { $0.ipv4Address.description }.joined(separator: ","),
9898
"\(self.configuration.resources.cpus)",
9999
"\(self.configuration.resources.memoryInBytes / (1024 * 1024)) MB",
100100
]

Sources/ContainerCommands/Network/NetworkCreate.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import ArgumentParser
1818
import ContainerClient
1919
import ContainerNetworkService
2020
import ContainerizationError
21+
import ContainerizationExtras
2122
import Foundation
2223
import TerminalProgress
2324

@@ -43,7 +44,8 @@ extension Application {
4344

4445
public func run() async throws {
4546
let parsedLabels = Utility.parseKeyValuePairs(labels)
46-
let config = try NetworkConfiguration(id: self.name, mode: .nat, subnet: subnet, labels: parsedLabels)
47+
let ipv4Subnet = try subnet.map { try CIDRv4($0) }
48+
let config = try NetworkConfiguration(id: self.name, mode: .nat, ipv4Subnet: ipv4Subnet, labels: parsedLabels)
4749
let state = try await ClientNetwork.create(configuration: config)
4850
print(state.id)
4951
}

Sources/ContainerCommands/Network/NetworkList.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ extension NetworkState {
8383
case .created(_):
8484
return [self.id, self.state, "none"]
8585
case .running(_, let status):
86-
return [self.id, self.state, status.address]
86+
return [self.id, self.state, status.ipv4Subnet.description]
8787
}
8888
}
8989
}

Sources/ContainerCommands/System/Property/PropertySet.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ extension Application {
7070
DefaultsStore.set(value: value, key: key)
7171
return
7272
case .defaultSubnet:
73-
guard (try? CIDRAddress(value)) != nil else {
73+
guard (try? CIDRv4(value)) != nil else {
7474
throw ContainerizationError(.invalidArgument, message: "invalid CIDRv4 address: \(value)")
7575
}
7676
DefaultsStore.set(value: value, key: key)

Sources/Helpers/APIServer/ContainerDNSHandler.swift

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,9 @@ struct ContainerDNSHandler: DNSHandler {
9494
guard let ipAllocation = try await networkService.lookup(hostname: question.name) else {
9595
return nil
9696
}
97-
98-
let components = ipAllocation.address.split(separator: "/")
99-
guard !components.isEmpty else {
100-
throw DNSResolverError.serverError("invalid IP format: empty address")
101-
}
102-
103-
let ipString = String(components[0])
104-
guard let ip = IPv4(ipString) else {
105-
throw DNSResolverError.serverError("failed to parse IP address: \(ipString)")
97+
let ipv4 = ipAllocation.ipv4Address.address.description
98+
guard let ip = IPv4(ipv4) else {
99+
throw DNSResolverError.serverError("failed to parse IP address: \(ipv4)")
106100
}
107101

108102
return HostRecord<IPv4>(name: question.name, ttl: ttl, ip: ip)

Sources/Helpers/NetworkVmnet/NetworkVmnetHelper+Start.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ extension NetworkVmnetHelper {
5050

5151
do {
5252
log.info("configuring XPC server")
53-
let subnet = try self.subnet.map { try CIDRAddress($0) }
54-
let configuration = try NetworkConfiguration(id: id, mode: .nat, subnet: subnet?.description)
53+
let ipv4Subnet = try self.subnet.map { try CIDRv4($0) }
54+
let configuration = try NetworkConfiguration(id: id, mode: .nat, ipv4Subnet: ipv4Subnet)
5555
let network = try Self.createNetwork(configuration: configuration, log: log)
5656
try await network.start()
5757
let server = try await NetworkService(network: network, log: log)

0 commit comments

Comments
 (0)