Skip to content

Commit 0f71a62

Browse files
committed
Removing metadataPath parameter from SandboxService
1 parent 93dd451 commit 0f71a62

4 files changed

Lines changed: 23 additions & 29 deletions

File tree

Sources/ContainerResource/Container/Bundle.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import ContainerizationError
1919
import Foundation
2020

2121
public struct BundleMetadata: Codable, Sendable {
22+
static let bundleMetadataFileName = "bundle-metadata.json"
23+
2224
public let path: URL
2325
public let initialFilesystem: Filesystem
2426
public let kernel: Kernel
@@ -41,6 +43,10 @@ public struct BundleMetadata: Codable, Sendable {
4143
self.containerRootFilesystem = containerRootFilesystem
4244
self.options = options
4345
}
46+
47+
public var bundleMetadataPath: URL {
48+
self.path.appendingPathComponent(Self.bundleMetadataFileName)
49+
}
4450
}
4551

4652
public struct Bundle: Sendable {
@@ -156,18 +162,18 @@ extension Bundle {
156162
}
157163

158164
public static func writeMetadata(
159-
_ metadata: BundleMetadata,
160-
to metadataPath: URL
165+
_ metadata: BundleMetadata
161166
) throws {
162167
// Ensure the parent directory exists
163-
let directory = metadataPath.deletingLastPathComponent()
168+
let directory = metadata.bundleMetadataPath.deletingLastPathComponent()
164169
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
165170

166171
let data = try JSONEncoder().encode(metadata)
167-
try data.write(to: metadataPath)
172+
try data.write(to: metadata.bundleMetadataPath)
168173
}
169174

170-
public static func readMetadata(from metadataPath: URL) throws -> BundleMetadata {
175+
public static func readMetadata(from bundlePath: URL) throws -> BundleMetadata {
176+
let metadataPath = bundlePath.appendingPathComponent(BundleMetadata.bundleMetadataFileName)
171177
guard FileManager.default.fileExists(atPath: metadataPath.path) else {
172178
throw ContainerizationError(
173179
.notFound,

Sources/Services/ContainerAPIService/Server/Containers/ContainersService.swift

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ public actor ContainersService {
232232
}
233233

234234
let path = self.containerRoot.appendingPathComponent(configuration.id)
235-
let metadataPath = self.getMetadataPath(for: configuration.id, containerRoot: self.containerRoot)
236235
let systemPlatform = kernel.platform
237236
let initFs = try await self.getInitBlock(for: systemPlatform.ociPlatform())
238237

@@ -249,7 +248,7 @@ public actor ContainersService {
249248
options: options
250249
)
251250

252-
try ContainerResource.Bundle.writeMetadata(completeMetadata, to: metadataPath)
251+
try ContainerResource.Bundle.writeMetadata(completeMetadata)
253252

254253
let snapshot = ContainerSnapshot(
255254
configuration: configuration,
@@ -279,7 +278,7 @@ public actor ContainersService {
279278
}
280279

281280
let path = self.containerRoot.appendingPathComponent(id)
282-
let config = try await self.getContainerConfiguration(for: id, at: path)
281+
let config = try await self.getContainerConfiguration(at: path)
283282

284283
do {
285284
try Self.registerService(
@@ -705,18 +704,11 @@ public actor ContainersService {
705704
}
706705
}
707706

708-
/// Get metadata file path for the given container ID
709-
private nonisolated func getMetadataPath(for containerID: String, containerRoot: URL) -> URL {
710-
containerRoot.appendingPathComponent(containerID)
711-
.appendingPathComponent("bundle-metadata.json")
712-
}
713-
714707
/// Get container configuration, either from existing bundle or from metadata
715-
private func getContainerConfiguration(for id: String, at path: URL) async throws -> ContainerConfiguration {
708+
private func getContainerConfiguration(at path: URL) async throws -> ContainerConfiguration {
716709
guard await bundleExists(at: path) else {
717710
// Bundle doesn't exist, get config from metadata
718-
let metadataPath = self.getMetadataPath(for: id, containerRoot: self.containerRoot)
719-
let metadata = try ContainerResource.Bundle.readMetadata(from: metadataPath)
711+
let metadata = try ContainerResource.Bundle.readMetadata(from: path)
720712
guard let config = metadata.containerConfiguration else {
721713
throw ContainerizationError(.internalError, message: "Metadata missing container configuration")
722714
}

Sources/Services/ContainerSandboxService/Server/SandboxService.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import struct ContainerizationOCI.Process
3939
public actor SandboxService {
4040
private let connection: xpc_connection_t
4141
private let root: URL
42-
private let metadataPath: URL?
4342
private let interfaceStrategy: InterfaceStrategy
4443
private var container: ContainerInfo?
4544
private let monitor: ExitMonitor
@@ -63,14 +62,12 @@ public actor SandboxService {
6362

6463
public init(
6564
root: URL,
66-
metadataPath: URL? = nil,
6765
interfaceStrategy: InterfaceStrategy,
6866
eventLoopGroup: any EventLoopGroup,
6967
connection: xpc_connection_t,
7068
log: Logger
7169
) {
7270
self.root = root
73-
self.metadataPath = metadataPath ?? root.appendingPathComponent("bundle-metadata.json")
7471
self.interfaceStrategy = interfaceStrategy
7572
self.log = log
7673
self.monitor = ExitMonitor(log: log)
@@ -1320,12 +1317,8 @@ extension SandboxService {
13201317

13211318
/// Create bundle from metadata
13221319
private func createBundle() throws {
1323-
guard let metadataPath = self.metadataPath else {
1324-
throw ContainerizationError(.internalError, message: "No metadata path provided for bundle creation")
1325-
}
1326-
13271320
do {
1328-
let metadata = try ContainerResource.Bundle.readMetadata(from: metadataPath)
1321+
let metadata = try ContainerResource.Bundle.readMetadata(from: self.root)
13291322
_ = try ContainerResource.Bundle.createFromMetadata(metadata)
13301323
self.log.info("Created bundle from metadata at \(metadata.path)")
13311324
// Could remove the metadata file at this point, but will be

Tests/ContainerResourceTests/BundleMetadataTests.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,9 @@ struct BundleMetadataTests {
4343
func testMetadataReadWrite() throws {
4444
let tempDir = FileManager.default.temporaryDirectory
4545
let bundlePath = tempDir.appendingPathComponent("test-bundle-\(UUID())")
46-
let metadataPath = tempDir.appendingPathComponent("test-metadata-\(UUID()).json")
4746

4847
defer {
4948
try? FileManager.default.removeItem(at: bundlePath)
50-
try? FileManager.default.removeItem(at: metadataPath)
5149
}
5250

5351
let initFs = Filesystem.virtiofs(
@@ -70,8 +68,13 @@ struct BundleMetadataTests {
7068
options: nil
7169
)
7270

73-
try ContainerResource.Bundle.writeMetadata(metadata, to: metadataPath)
74-
let readMetadata = try ContainerResource.Bundle.readMetadata(from: metadataPath)
71+
try ContainerResource.Bundle.writeMetadata(metadata)
72+
73+
defer {
74+
try? FileManager.default.removeItem(at: metadata.bundleMetadataPath)
75+
}
76+
77+
let readMetadata = try ContainerResource.Bundle.readMetadata(from: bundlePath)
7578

7679
#expect(
7780
readMetadata.path == bundlePath,

0 commit comments

Comments
 (0)