Skip to content

Commit fc4a124

Browse files
authored
Continue documenting public surface (#25)
Signed-off-by: Danny Canter <danny_canter@apple.com>
1 parent 46260fd commit fc4a124

32 files changed

Lines changed: 215 additions & 33 deletions

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ Applications built using the package will run on macOS Sequoia or later, but the
3838

3939
- Non-isolated container networking - with macOS Sequoia, containers on the same vmnet network cannot communicate with each other
4040

41+
## Example Usage
42+
43+
For examples of how to use some of the libraries surface, the cctl executable is a good start. This tools primary job is as a playground to trial out the API. It contains commands that exercise some of the core functionality of the various products, such as:
44+
45+
1. [Manipulating OCI images](./Sources/cctl/ImageCommand.swift)
46+
2. [Logging in to container registries](./Sources/cctl/LoginCommand.swift)
47+
3. [Creating root filesystem blocks](./Sources/cctl/RootfsCommand.swift)
48+
4. [Running simple Linux containers](./Sources/cctl/RunCommand.swift)
49+
4150
## Linux kernel
4251

4352
A Linux kernel is required for spawning light weight virtual machines on macOS.

Sources/Containerization/Agent/Vminitd.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@ public struct Vminitd: Sendable {
4242
self.client = .init(connection: connection, group: group)
4343
}
4444

45+
/// Close the connection to the guest agent.
4546
public func close() async throws {
4647
try await client.close()
4748
}
4849
}
4950

5051
extension Vminitd: VirtualMachineAgent {
52+
/// Perform the standard guest setup necessary for vminitd to be able to
53+
/// run containers.
5154
public func standardSetup() async throws {
5255
try await up(name: "lo")
5356

Sources/Containerization/Container.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717

1818
/// The core protocol container implementations must implement.
1919
public protocol Container {
20+
/// ID for the container.
2021
var id: String { get }
21-
22+
/// The amount of cpus assigned to the container.
2223
var cpus: Int { get }
24+
/// The memory in bytes assigned to the container.
2325
var memoryInBytes: UInt64 { get }
24-
26+
/// The network interfaces assigned to the container.
2527
var interfaces: [any Interface] { get }
2628
}

Sources/Containerization/DNSConfiguration.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@
1818
/// DNS configuration for a container. The values will be used to
1919
/// construct /etc/resolv.conf for a given container.
2020
public struct DNS: Sendable {
21+
/// The set of default nameservers to use if none are provided
22+
/// in the constructor.
2123
public static let defaultNameservers = ["1.1.1.1"]
2224

25+
/// The nameservers a container should use.
2326
public var nameservers: [String]
27+
/// The DNS domain to use.
2428
public var domain: String?
29+
/// The DNS search domains to use.
2530
public var searchDomains: [String]
31+
/// The DNS options to use.
2632
public var options: [String]
2733

2834
public init(

Sources/Containerization/Image/Image.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,19 @@ import ContainerizationExtras
2929

3030
/// Type representing an OCI container image.
3131
public struct Image: Sendable {
32-
3332
private let contentStore: ContentStore
3433
/// The description for the image that comprises of its name and a reference to its root descriptor.
3534
public let description: Description
3635

36+
/// A description of the OCI image.
3737
public struct Description: Sendable {
38+
/// The string reference of the image.
3839
public let reference: String
40+
/// The descriptor identifying the image.
3941
public let descriptor: Descriptor
42+
/// The digest for the image.
4043
public var digest: String { descriptor.digest }
44+
/// The media type of the image.
4145
public var mediaType: String { descriptor.mediaType }
4246

4347
public init(reference: String, descriptor: Descriptor) {
@@ -46,9 +50,13 @@ public struct Image: Sendable {
4650
}
4751
}
4852

53+
/// The descriptor for the image.
4954
public var descriptor: Descriptor { description.descriptor }
55+
/// The digest of the image.
5056
public var digest: String { description.digest }
57+
/// The media type of the image.
5158
public var mediaType: String { description.mediaType }
59+
/// The string reference for the image.
5260
public var reference: String { description.reference }
5361

5462
public init(description: Description, contentStore: ContentStore) {
@@ -79,6 +87,8 @@ public struct Image: Sendable {
7987
return try content.decode()
8088
}
8189

90+
/// Returns the descriptor for the given platform. If it does not exist
91+
/// will throw a ContainerizationError with the code set to .invalidArgument.
8292
public func descriptor(for platform: Platform) async throws -> Descriptor {
8393
let index = try await self.index()
8494
let desc = index.manifests.first { $0.platform == platform }

Sources/ContainerizationEXT4/EXT4.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ public enum EXT4 {
297297
}
298298

299299
extension EXT4 {
300+
// `EXT4` errors.
300301
public enum Error: Swift.Error, CustomStringConvertible, Sendable, Equatable {
301302
case notFound(_ path: String)
302303
case couldNotReadSuperBlock(_ path: String, _ offset: UInt64, _ size: Int)

Sources/ContainerizationError/ContainerizationError.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
/// Most API surfaces for the core container/process/agent types will
2121
/// return a ContainerizationError.
2222
public struct ContainerizationError: Swift.Error, Sendable {
23+
/// A code describing the error encountered.
2324
public var code: Code
25+
/// A description of the error.
2426
public var message: String
27+
/// The original error which led to this error being thrown.
2528
public var cause: (any Error)?
2629

2730
/// Creates a new error.
@@ -48,21 +51,25 @@ public struct ContainerizationError: Swift.Error, Sendable {
4851
self.cause = cause
4952
}
5053

54+
/// Provides a unique hash of the error.
5155
public func hash(into hasher: inout Hasher) {
5256
hasher.combine(self.code)
5357
hasher.combine(self.message)
5458
}
5559

60+
/// Equality operator for the error. Uses the code and message.
5661
public static func == (lhs: Self, rhs: Self) -> Bool {
5762
lhs.code == rhs.code && lhs.message == rhs.message
5863
}
5964

65+
/// Checks if the given error has the provided code.
6066
public func isCode(_ code: Code) -> Bool {
6167
self.code == code
6268
}
6369
}
6470

6571
extension ContainerizationError: CustomStringConvertible {
72+
/// Description of the error.
6673
public var description: String {
6774
guard let cause = self.cause else {
6875
return "\(self.code): \"\(self.message)\""
@@ -72,6 +79,7 @@ extension ContainerizationError: CustomStringConvertible {
7279
}
7380

7481
extension ContainerizationError {
82+
/// Codes for a `ContainerizationError`.
7583
public struct Code: Sendable, Hashable {
7684
private enum Value: Hashable, Sendable, CaseIterable {
7785
case unknown

Sources/ContainerizationExtras/AddressAllocator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public protocol AddressAllocator<AddressType>: Sendable {
3232
func disableAllocator() -> Bool
3333
}
3434

35+
/// Errors that a type implementing AddressAllocator should throw.
3536
public enum AllocatorError: Swift.Error, CustomStringConvertible, Equatable {
3637
case allocatorDisabled
3738
case allocatorFull

Sources/ContainerizationExtras/AsyncLock.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public actor AsyncLock {
3131

3232
public init() {}
3333

34+
/// withLock provides a scoped locking API to run a function while holding the lock.
3435
public func withLock<T: Sendable>(_ body: @Sendable @escaping (Context) async throws -> T) async rethrows -> T {
3536
while self.busy {
3637
await withCheckedContinuation { cc in

Sources/ContainerizationExtras/FileManager+Temporary.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import Foundation
1919

2020
extension FileManager {
21+
/// Returns a unique temporary directory to use.
2122
public func uniqueTemporaryDirectory(create: Bool = true) -> URL {
2223
let tempDirectoryURL = temporaryDirectory
2324
let uniqueDirectoryURL = tempDirectoryURL.appendingPathComponent(UUID().uuidString)

0 commit comments

Comments
 (0)