Skip to content

Commit a0c7979

Browse files
committed
LinuxContainer: Add new constructor
Add a constructor that allows just passing in a configuration without doing the closure dance.
1 parent 0177505 commit a0c7979

File tree

3 files changed

+88
-4
lines changed

3 files changed

+88
-4
lines changed

Sources/Containerization/LinuxContainer.swift

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,34 @@ public final class LinuxContainer: Container, Sendable {
6464
public var bootlog: URL?
6565

6666
public init() {}
67+
68+
public init(
69+
process: LinuxProcessConfiguration,
70+
cpus: Int = 4,
71+
memoryInBytes: UInt64 = 1024.mib(),
72+
hostname: String = "",
73+
sysctl: [String: String] = [:],
74+
interfaces: [any Interface] = [],
75+
sockets: [UnixSocketConfiguration] = [],
76+
mounts: [Mount] = LinuxContainer.defaultMounts(),
77+
dns: DNS? = nil,
78+
hosts: Hosts? = nil,
79+
virtualization: Bool = false,
80+
bootlog: URL? = nil
81+
) {
82+
self.process = process
83+
self.cpus = cpus
84+
self.memoryInBytes = memoryInBytes
85+
self.hostname = hostname
86+
self.sysctl = sysctl
87+
self.interfaces = interfaces
88+
self.sockets = sockets
89+
self.mounts = mounts
90+
self.dns = dns
91+
self.hosts = hosts
92+
self.virtualization = virtualization
93+
self.bootlog = bootlog
94+
}
6795
}
6896

6997
private let state: AsyncMutex<State>
@@ -189,10 +217,14 @@ public final class LinuxContainer: Container, Sendable {
189217
private let vmm: VirtualMachineManager
190218
private let logger: Logger?
191219

192-
/// Create a new `LinuxContainer`. A `Mount` that contains the contents
193-
/// of the container image must be provided, as well as a `VirtualMachineManager`
194-
/// instance that will handle launching the virtual machine the container will
195-
/// execute inside of.
220+
/// Create a new `LinuxContainer`.
221+
///
222+
/// - Parameters:
223+
/// - id: The identifier for the container.
224+
/// - rootfs: The root filesystem mount containing the container image contents.
225+
/// - vmm: The virtual machine manager that will handle launching the VM for the container.
226+
/// - logger: Optional logger for container operations.
227+
/// - configuration: A closure that configures the container by modifying the Configuration instance.
196228
public init(
197229
_ id: String,
198230
rootfs: Mount,
@@ -214,6 +246,32 @@ public final class LinuxContainer: Container, Sendable {
214246
self.state = AsyncMutex(.initialized)
215247
}
216248

249+
/// Create a new `LinuxContainer`.
250+
///
251+
/// - Parameters:
252+
/// - id: The identifier for the container.
253+
/// - rootfs: The root filesystem mount containing the container image contents.
254+
/// - vmm: The virtual machine manager that will handle launching the VM for the container.
255+
/// - configuration: The container configuration specifying process, resources, networking, and other settings.
256+
/// - logger: Optional logger for container operations.
257+
public init(
258+
_ id: String,
259+
rootfs: Mount,
260+
vmm: VirtualMachineManager,
261+
configuration: LinuxContainer.Configuration,
262+
logger: Logger? = nil
263+
) {
264+
self.id = id
265+
self.vmm = vmm
266+
self.hostVsockPorts = Atomic<UInt32>(0x1000_0000)
267+
self.guestVsockPorts = Atomic<UInt32>(0x1000_0000)
268+
self.rootfs = rootfs
269+
self.logger = logger
270+
271+
self.config = configuration
272+
self.state = AsyncMutex(.initialized)
273+
}
274+
217275
private static func createDefaultRuntimeSpec(_ id: String) -> Spec {
218276
.init(
219277
process: .init(),

Sources/Integration/ContainerTests.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,31 @@ extension IntegrationSuite {
987987
}
988988
}
989989

990+
func testNonClosureConstructor() async throws {
991+
let id = "test-container-different-constructor"
992+
993+
let bs = try await bootstrap(id)
994+
let config = LinuxContainer.Configuration(
995+
process: LinuxProcessConfiguration(arguments: ["/bin/true"])
996+
)
997+
let container = LinuxContainer(
998+
id,
999+
rootfs: bs.rootfs,
1000+
vmm: bs.vmm,
1001+
configuration: config
1002+
)
1003+
1004+
try await container.create()
1005+
try await container.start()
1006+
1007+
let status = try await container.wait()
1008+
try await container.stop()
1009+
1010+
guard status.exitCode == 0 else {
1011+
throw IntegrationError.assert(msg: "process status \(status) != 0")
1012+
}
1013+
}
1014+
9901015
private func createHostUnixSocket() throws -> String {
9911016
let dir = FileManager.default.uniqueTemporaryDirectory(create: true)
9921017
let socketPath = dir.appendingPathComponent("test.sock").path

Sources/Integration/Suite.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ struct IntegrationSuite: AsyncParsableCommand {
294294
Test("container cgroup limits", testCgroupLimits),
295295
Test("container no serial console", testNoSerialConsole),
296296
Test("unix socket into guest", testUnixSocketIntoGuest),
297+
Test("container non-closure constructor", testNonClosureConstructor),
297298

298299
// Pods
299300
Test("pod single container", testPodSingleContainer),

0 commit comments

Comments
 (0)