Skip to content

Commit 5050027

Browse files
authored
Update allocations in Allocator (#264)
The `allocations` property in the nested `Allocator` structure wasn't updated. This PR resolves the problem by using mutating methods and keeping the original types as structs.
1 parent 985dac8 commit 5050027

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

Sources/Containerization/ContainerManager.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,22 @@ import vmnet
2929
public struct ContainerManager: Sendable {
3030
public let imageStore: ImageStore
3131
private let vmm: VirtualMachineManager
32-
private let network: Network?
32+
private var network: Network?
3333

3434
private var containerRoot: URL {
3535
self.imageStore.path.appendingPathComponent("containers")
3636
}
3737

3838
/// A network that can allocate and release interfaces for use with containers.
3939
public protocol Network: Sendable {
40-
func create(_ id: String) throws -> Interface?
41-
func release(_ id: String) throws
40+
mutating func create(_ id: String) throws -> Interface?
41+
mutating func release(_ id: String) throws
4242
}
4343

4444
/// A network backed by vmnet on macOS.
4545
@available(macOS 26.0, *)
4646
public struct VmnetNetwork: Network {
47-
private let allocator: Allocator
47+
private var allocator: Allocator
4848
nonisolated(unsafe) private let reference: vmnet_network_ref
4949

5050
/// The IPv4 subnet of this network.
@@ -70,18 +70,20 @@ public struct ContainerManager: Sendable {
7070
)
7171
}
7272

73-
func allocate(_ id: String) throws -> String {
73+
mutating func allocate(_ id: String) throws -> String {
7474
if allocations[id] != nil {
7575
throw ContainerizationError(.exists, message: "allocation with id \(id) already exists")
7676
}
7777
let index = try addressAllocator.allocate()
78+
allocations[id] = index
7879
let ip = IPv4Address(fromValue: index)
7980
return try CIDRAddress(ip, prefixLength: cidr.prefixLength).description
8081
}
8182

82-
func release(_ id: String) throws {
83+
mutating func release(_ id: String) throws {
8384
if let index = self.allocations[id] {
8485
try addressAllocator.release(index)
86+
allocations.removeValue(forKey: id)
8587
}
8688
}
8789
}
@@ -147,7 +149,7 @@ public struct ContainerManager: Sendable {
147149

148150
/// Returns a new interface for use with a container.
149151
/// - Parameter id: The container ID.
150-
public func create(_ id: String) throws -> Containerization.Interface? {
152+
public mutating func create(_ id: String) throws -> Containerization.Interface? {
151153
let address = try allocator.allocate(id)
152154
return Self.Interface(
153155
reference: self.reference,
@@ -158,7 +160,7 @@ public struct ContainerManager: Sendable {
158160

159161
/// Performs cleanup of an interface.
160162
/// - Parameter id: The container ID.
161-
public func release(_ id: String) throws {
163+
public mutating func release(_ id: String) throws {
162164
try allocator.release(id)
163165
}
164166

@@ -332,7 +334,7 @@ public struct ContainerManager: Sendable {
332334
/// - id: The container ID.
333335
/// - reference: The image reference.
334336
/// - rootfsSizeInBytes: The size of the root filesystem in bytes. Defaults to 8 GiB.
335-
public func create(
337+
public mutating func create(
336338
_ id: String,
337339
reference: String,
338340
rootfsSizeInBytes: UInt64 = 8.gib(),
@@ -352,7 +354,7 @@ public struct ContainerManager: Sendable {
352354
/// - id: The container ID.
353355
/// - image: The image.
354356
/// - rootfsSizeInBytes: The size of the root filesystem in bytes. Defaults to 8 GiB.
355-
public func create(
357+
public mutating func create(
356358
_ id: String,
357359
image: Image,
358360
rootfsSizeInBytes: UInt64 = 8.gib(),
@@ -378,7 +380,7 @@ public struct ContainerManager: Sendable {
378380
/// - id: The container ID.
379381
/// - image: The image.
380382
/// - rootfs: The root filesystem mount pointing to an existing block file.
381-
public func create(
383+
public mutating func create(
382384
_ id: String,
383385
image: Image,
384386
rootfs: Mount,
@@ -405,7 +407,7 @@ public struct ContainerManager: Sendable {
405407
/// - Parameters:
406408
/// - id: The container ID.
407409
/// - image: The image.
408-
public func get(
410+
public mutating func get(
409411
_ id: String,
410412
image: Image,
411413
) async throws -> LinuxContainer {
@@ -439,7 +441,7 @@ public struct ContainerManager: Sendable {
439441

440442
/// Performs the cleanup of a container.
441443
/// - Parameter id: The container ID.
442-
public func delete(_ id: String) throws {
444+
public mutating func delete(_ id: String) throws {
443445
try self.network?.release(id)
444446
let path = containerRoot.appendingPathComponent(id)
445447
try FileManager.default.removeItem(at: path)

Sources/Integration/VMTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ extension IntegrationSuite {
172172
let bs = try await bootstrap()
173173

174174
// Create ContainerManager with kernel and initfs reference
175-
let manager = try ContainerManager(vmm: bs.vmm)
175+
var manager = try ContainerManager(vmm: bs.vmm)
176176
defer {
177177
try? manager.delete(id)
178178
}
@@ -213,7 +213,7 @@ extension IntegrationSuite {
213213
let bs = try await bootstrap()
214214

215215
// Create ContainerManager with kernel and initfs reference
216-
let manager = try ContainerManager(vmm: bs.vmm)
216+
var manager = try ContainerManager(vmm: bs.vmm)
217217
defer {
218218
try? manager.delete(id)
219219
}
@@ -262,7 +262,7 @@ extension IntegrationSuite {
262262

263263
let bs = try await bootstrap()
264264

265-
let manager = try ContainerManager(vmm: bs.vmm)
265+
var manager = try ContainerManager(vmm: bs.vmm)
266266
defer {
267267
try? manager.delete(id)
268268
}

Sources/cctl/RunCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ extension Application {
7676
path: URL(fileURLWithPath: kernel),
7777
platform: .linuxArm
7878
)
79-
let manager = try await ContainerManager(
79+
var manager = try await ContainerManager(
8080
kernel: kernel,
8181
initfsReference: "vminit:latest",
8282
)

0 commit comments

Comments
 (0)