Skip to content

Commit 2dfabeb

Browse files
authored
Vminitd: Rework ManagedContainer (#168)
This gets rid of some design decisions that I was not fond of. This changes the process start for both execs and the init process to happen internally in the object, as well as makes it so that all exec functionalities can happen on the container object itself. This allows us to get rid of the `if request.id == containerID` branches in the rpcs, and handle this entirely in the object itself using its internal state. The existing integration tests should stress this just fine I believe.
1 parent 4bacf4c commit 2dfabeb

File tree

2 files changed

+36
-52
lines changed

2 files changed

+36
-52
lines changed

vminitd/Sources/vminitd/ManagedContainer.swift

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,34 +91,29 @@ extension ManagedContainer {
9191
self._execs[id] = process
9292
}
9393

94-
func getExec(id: String) throws -> ManagedProcess {
95-
guard let exec = self._execs[id] else {
96-
throw ContainerizationError(
97-
.invalidState,
98-
message: "exec \(id) does not exist in container \(self.id)"
99-
)
100-
}
101-
return exec
102-
}
103-
104-
func start() throws -> Int32 {
105-
try self.initProcess.start()
94+
func start(execID: String) async throws -> Int32 {
95+
let proc = try self.getExecOrInit(execID: execID)
96+
return try await ProcessSupervisor.default.start(process: proc)
10697
}
10798

108-
func wait() async -> Int32 {
109-
await self.initProcess.wait()
99+
func wait(execID: String) async throws -> Int32 {
100+
let proc = try self.getExecOrInit(execID: execID)
101+
return await proc.wait()
110102
}
111103

112-
func kill(_ signal: Int32) throws {
113-
try self.initProcess.kill(signal)
104+
func kill(execID: String, _ signal: Int32) throws {
105+
let proc = try self.getExecOrInit(execID: execID)
106+
try proc.kill(signal)
114107
}
115108

116-
func resize(size: Terminal.Size) throws {
117-
try self.initProcess.resize(size: size)
109+
func resize(execID: String, size: Terminal.Size) throws {
110+
let proc = try self.getExecOrInit(execID: execID)
111+
try proc.resize(size: size)
118112
}
119113

120-
func close() throws {
121-
try self.initProcess.close()
114+
func close(execID: String) throws {
115+
let proc = try self.getExecOrInit(execID: execID)
116+
try proc.close()
122117
}
123118

124119
func deleteExec(id: String) throws {
@@ -134,6 +129,19 @@ extension ManagedContainer {
134129
func delete() throws {
135130
try self._bundle.delete()
136131
}
132+
133+
func getExecOrInit(execID: String) throws -> ManagedProcess {
134+
if execID == self.id {
135+
return self.initProcess
136+
}
137+
guard let proc = self._execs[execID] else {
138+
throw ContainerizationError(
139+
.invalidState,
140+
message: "exec \(execID) does not exist in container \(self.id)"
141+
)
142+
}
143+
return proc
144+
}
137145
}
138146

139147
extension ContainerizationOCI.Bundle {

vminitd/Sources/vminitd/Server+GRPC.swift

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -465,13 +465,7 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContextAsyncProvid
465465
}
466466

467467
let ctr = try await self.state.get(container: request.containerID)
468-
469-
if request.id == request.containerID {
470-
try await ctr.kill(request.signal)
471-
} else {
472-
let proc = try await ctr.getExec(id: request.id)
473-
try proc.kill(request.signal)
474-
}
468+
try await ctr.kill(execID: request.id, request.signal)
475469

476470
return .init()
477471
}
@@ -520,16 +514,7 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContextAsyncProvid
520514

521515
do {
522516
let ctr = try await self.state.get(container: request.containerID)
523-
524-
// FIXME: This should just happen inside of ManagedContainer.
525-
let pid: Int32
526-
if request.id == request.containerID {
527-
let process = ctr.initProcess
528-
pid = try await ProcessSupervisor.default.start(process: process)
529-
} else {
530-
let process = try await ctr.getExec(id: request.id)
531-
pid = try await ProcessSupervisor.default.start(process: process)
532-
}
517+
let pid = try await ctr.start(execID: request.id)
533518

534519
return .with {
535520
$0.pid = pid
@@ -565,14 +550,11 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContextAsyncProvid
565550

566551
do {
567552
let ctr = try await self.state.get(container: request.containerID)
568-
569-
let size = Terminal.Size(width: UInt16(request.columns), height: UInt16(request.rows))
570-
if request.id == request.containerID {
571-
try await ctr.resize(size: size)
572-
} else {
573-
let proc = try await ctr.getExec(id: request.id)
574-
try proc.resize(size: size)
575-
}
553+
let size = Terminal.Size(
554+
width: UInt16(request.columns),
555+
height: UInt16(request.rows)
556+
)
557+
try await ctr.resize(execID: request.id, size: size)
576558
} catch {
577559
log.error(
578560
"resizeProcess",
@@ -607,13 +589,7 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContextAsyncProvid
607589
do {
608590
let ctr = try await self.state.get(container: request.containerID)
609591

610-
let exitCode: Int32
611-
if request.id == request.containerID {
612-
exitCode = await ctr.wait()
613-
} else {
614-
let proc = try await ctr.getExec(id: request.id)
615-
exitCode = await proc.wait()
616-
}
592+
let exitCode = try await ctr.wait(execID: request.id)
617593

618594
return .with {
619595
$0.exitCode = exitCode

0 commit comments

Comments
 (0)