@@ -29,22 +29,22 @@ import vmnet
29
29
public struct ContainerManager : Sendable {
30
30
public let imageStore : ImageStore
31
31
private let vmm : VirtualMachineManager
32
- private let network : Network ?
32
+ private var network : Network ?
33
33
34
34
private var containerRoot : URL {
35
35
self . imageStore. path. appendingPathComponent ( " containers " )
36
36
}
37
37
38
38
/// A network that can allocate and release interfaces for use with containers.
39
39
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
42
42
}
43
43
44
44
/// A network backed by vmnet on macOS.
45
45
@available ( macOS 26 . 0 , * )
46
46
public struct VmnetNetwork : Network {
47
- private let allocator : Allocator
47
+ private var allocator : Allocator
48
48
nonisolated ( unsafe) private let reference : vmnet_network_ref
49
49
50
50
/// The IPv4 subnet of this network.
@@ -70,18 +70,20 @@ public struct ContainerManager: Sendable {
70
70
)
71
71
}
72
72
73
- func allocate( _ id: String ) throws -> String {
73
+ mutating func allocate( _ id: String ) throws -> String {
74
74
if allocations [ id] != nil {
75
75
throw ContainerizationError ( . exists, message: " allocation with id \( id) already exists " )
76
76
}
77
77
let index = try addressAllocator. allocate ( )
78
+ allocations [ id] = index
78
79
let ip = IPv4Address ( fromValue: index)
79
80
return try CIDRAddress ( ip, prefixLength: cidr. prefixLength) . description
80
81
}
81
82
82
- func release( _ id: String ) throws {
83
+ mutating func release( _ id: String ) throws {
83
84
if let index = self . allocations [ id] {
84
85
try addressAllocator. release ( index)
86
+ allocations. removeValue ( forKey: id)
85
87
}
86
88
}
87
89
}
@@ -147,7 +149,7 @@ public struct ContainerManager: Sendable {
147
149
148
150
/// Returns a new interface for use with a container.
149
151
/// - Parameter id: The container ID.
150
- public func create( _ id: String ) throws -> Containerization . Interface ? {
152
+ public mutating func create( _ id: String ) throws -> Containerization . Interface ? {
151
153
let address = try allocator. allocate ( id)
152
154
return Self . Interface (
153
155
reference: self . reference,
@@ -158,7 +160,7 @@ public struct ContainerManager: Sendable {
158
160
159
161
/// Performs cleanup of an interface.
160
162
/// - Parameter id: The container ID.
161
- public func release( _ id: String ) throws {
163
+ public mutating func release( _ id: String ) throws {
162
164
try allocator. release ( id)
163
165
}
164
166
@@ -332,7 +334,7 @@ public struct ContainerManager: Sendable {
332
334
/// - id: The container ID.
333
335
/// - reference: The image reference.
334
336
/// - rootfsSizeInBytes: The size of the root filesystem in bytes. Defaults to 8 GiB.
335
- public func create(
337
+ public mutating func create(
336
338
_ id: String ,
337
339
reference: String ,
338
340
rootfsSizeInBytes: UInt64 = 8 . gib ( ) ,
@@ -352,7 +354,7 @@ public struct ContainerManager: Sendable {
352
354
/// - id: The container ID.
353
355
/// - image: The image.
354
356
/// - rootfsSizeInBytes: The size of the root filesystem in bytes. Defaults to 8 GiB.
355
- public func create(
357
+ public mutating func create(
356
358
_ id: String ,
357
359
image: Image ,
358
360
rootfsSizeInBytes: UInt64 = 8 . gib ( ) ,
@@ -378,7 +380,7 @@ public struct ContainerManager: Sendable {
378
380
/// - id: The container ID.
379
381
/// - image: The image.
380
382
/// - rootfs: The root filesystem mount pointing to an existing block file.
381
- public func create(
383
+ public mutating func create(
382
384
_ id: String ,
383
385
image: Image ,
384
386
rootfs: Mount ,
@@ -405,7 +407,7 @@ public struct ContainerManager: Sendable {
405
407
/// - Parameters:
406
408
/// - id: The container ID.
407
409
/// - image: The image.
408
- public func get(
410
+ public mutating func get(
409
411
_ id: String ,
410
412
image: Image ,
411
413
) async throws -> LinuxContainer {
@@ -439,7 +441,7 @@ public struct ContainerManager: Sendable {
439
441
440
442
/// Performs the cleanup of a container.
441
443
/// - Parameter id: The container ID.
442
- public func delete( _ id: String ) throws {
444
+ public mutating func delete( _ id: String ) throws {
443
445
try self . network? . release ( id)
444
446
let path = containerRoot. appendingPathComponent ( id)
445
447
try FileManager . default. removeItem ( at: path)
0 commit comments