Skip to content

Commit e3c4980

Browse files
authored
Move to TOML configuration for defaults (apple#1425)
- Discussion topic apple#1336. - This change migrates away from using `UserDefaults`, instead providing a TOML configuration mechanism for user configurable settings. All existing system property settings keys are supported in the new configuration file. However, users will have to migrate any settings they have configured in the `UserDefaults` into TOML for these settings to take effect. - Breaking changes: * `container system property get` is removed in favor of users directly utilizing `container system property list --format toml | jq<>`. * `container system property set` is removed since the TOML configuration is effectively immutable during the lifetime of the `container` daemon. Uses can edit the TOML they have in their home directory, however no changes will take effect until the daemon is restarted via `container system stop && container system start` * `container system property list --format table` is removed as generating tabular format is non-trivial and the new TOML format is intended to be human readable
1 parent 8a25213 commit e3c4980

56 files changed

Lines changed: 1168 additions & 971 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

BUILDING.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ to prepare your build environment.
8080
>
8181
> **Note:** If you have already run `swift package edit`, whether intentionally or by accident, follow the steps in the next section to restore the normal `containerization` dependency. Otherwise, the modified `Package.swift` file will not work, and the project may fail to build.
8282

83-
5. If you want `container` to use any changes you made in the `vminit` subproject of Containerization, update the system property to use the locally built init filesystem image:
83+
5. If you want `container` to use any changes you made in the `vminit` subproject of Containerization, set the init image in your runtime configuration file at `~/.config/container/runtime-config.toml`:
8484

85-
```bash
86-
container system property set image.init vminit:latest
85+
```toml
86+
[vminit]
87+
image = "vminit:latest"
8788
```
8889

8990
6. Build `container`.
@@ -101,11 +102,7 @@ to prepare your build environment.
101102

102103
To revert to using the Containerization dependency from your `Package.swift`:
103104

104-
1. If you were using the local init filesystem, revert the system property to its default value:
105-
106-
```bash
107-
container system property clear image.init
108-
```
105+
1. If you were using the local init filesystem, remove the `init` override from your `~/.config/container/runtime-config.toml` (or delete the `[vminit]` section if no other image settings are present).
109106

110107
2. Use the Swift package manager to restore the normal `containerization` dependency and update your `Package.resolved` file. If you are using Xcode, revert your `Package.swift` change instead of using `swift package unedit`.
111108

@@ -133,14 +130,20 @@ To test changes that require the `container-builder-shim` project:
133130

134131
1. Clone the [container-builder-shim](https://github.com/apple/container-builder-shim) repository and navigate to its directory.
135132

136-
2. After making the necessary changes, build the custom builder image, set it as the active builder image, and remove the existing `buildkit` container so the new image will be used:
133+
2. After making the necessary changes, build the custom builder image, set it as the active builder image in `~/.config/container/runtime-config.toml`, and remove the existing `buildkit` container so the new image will be used:
137134

138135
```bash
139136
container build -t builder .
140-
container system property set image.builder builder:latest
141137
container rm -f buildkit
142138
```
143139

140+
Add the following to your `~/.config/container/runtime-config.toml`:
141+
142+
```toml
143+
[build]
144+
image = "builder:latest"
145+
```
146+
144147
3. Run the `container` build as usual:
145148

146149
```bash

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ let package = Package(
8080
.product(name: "ContainerizationArchive", package: "containerization"),
8181
.product(name: "ContainerizationExtras", package: "containerization"),
8282
.product(name: "ContainerizationOS", package: "containerization"),
83+
.product(name: "TOML", package: "swift-toml"),
8384
"ContainerBuild",
8485
"ContainerLog",
86+
"ContainerPersistence",
8587
"ContainerResource",
8688
"Yams",
8789
],
@@ -93,6 +95,7 @@ let package = Package(
9395
.product(name: "ArgumentParser", package: "swift-argument-parser"),
9496
.product(name: "Logging", package: "swift-log"),
9597
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
98+
.product(name: "TOML", package: "swift-toml"),
9699
.product(name: "Containerization", package: "containerization"),
97100
.product(name: "ContainerizationOCI", package: "containerization"),
98101
.product(name: "ContainerizationOS", package: "containerization"),
@@ -237,6 +240,7 @@ let package = Package(
237240
.product(name: "SystemPackage", package: "swift-system"),
238241
"ContainerImagesService",
239242
"ContainerLog",
243+
"ContainerPersistence",
240244
"ContainerPlugin",
241245
"ContainerVersion",
242246
"ContainerXPC",
@@ -285,6 +289,7 @@ let package = Package(
285289
"ContainerLog",
286290
"ContainerNetworkService",
287291
"ContainerNetworkServiceClient",
292+
"ContainerPersistence",
288293
"ContainerPlugin",
289294
"ContainerResource",
290295
"ContainerVersion",
@@ -401,6 +406,7 @@ let package = Package(
401406
.product(name: "Logging", package: "swift-log"),
402407
.product(name: "Containerization", package: "containerization"),
403408
.product(name: "SystemPackage", package: "swift-system"),
409+
.product(name: "TOML", package: "swift-toml"),
404410
"CVersion",
405411
"ContainerVersion",
406412
]

Sources/APIServer/APIServer+Start.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ extension APIServer {
5050
var logRoot = LogRoot.path
5151

5252
func run() async throws {
53+
let containerSystemConfig: ContainerSystemConfig = try SystemRuntimeOptions.loadConfig(
54+
configFile: SystemRuntimeOptions.configFileFromAppRoot(ApplicationRoot.url)
55+
)
5356
let commandName = APIServer._commandName
5457
let logPath = logRoot.map { $0.appending("\(commandName).log") }
5558
let log = ServiceLogger.bootstrap(category: "APIServer", debug: debug, logPath: logPath)
@@ -62,15 +65,18 @@ extension APIServer {
6265
log.info("configuring XPC server")
6366
var routes = [XPCRoute: XPCServer.RouteHandler]()
6467
let pluginLoader = try initializePluginLoader(log: log)
68+
6569
try await initializePlugins(pluginLoader: pluginLoader, log: log, routes: &routes)
6670
let containersService = try initializeContainersService(
6771
pluginLoader: pluginLoader,
72+
containerSystemConfig: containerSystemConfig,
6873
log: log,
6974
routes: &routes
7075
)
7176
let networkService = try await initializeNetworksService(
7277
pluginLoader: pluginLoader,
7378
containersService: containersService,
79+
containerSystemConfig: containerSystemConfig,
7480
log: log,
7581
routes: &routes
7682
)
@@ -259,12 +265,18 @@ extension APIServer {
259265
routes[XPCRoute.getDefaultKernel] = harness.getDefaultKernel
260266
}
261267

262-
private func initializeContainersService(pluginLoader: PluginLoader, log: Logger, routes: inout [XPCRoute: XPCServer.RouteHandler]) throws -> ContainersService {
268+
private func initializeContainersService(
269+
pluginLoader: PluginLoader,
270+
containerSystemConfig: ContainerSystemConfig,
271+
log: Logger,
272+
routes: inout [XPCRoute: XPCServer.RouteHandler]
273+
) throws -> ContainersService {
263274
log.info("initializing containers service")
264275

265276
let service = try ContainersService(
266277
appRoot: appRoot,
267278
pluginLoader: pluginLoader,
279+
containerSystemConfig: containerSystemConfig,
268280
log: log,
269281
debugHelpers: debug
270282
)
@@ -292,6 +304,7 @@ extension APIServer {
292304
private func initializeNetworksService(
293305
pluginLoader: PluginLoader,
294306
containersService: ContainersService,
307+
containerSystemConfig: ContainerSystemConfig,
295308
log: Logger,
296309
routes: inout [XPCRoute: XPCServer.RouteHandler]
297310
) async throws -> NetworksService {
@@ -316,8 +329,8 @@ extension APIServer {
316329
let config = try NetworkConfiguration(
317330
id: NetworkClient.defaultNetworkName,
318331
mode: .nat,
319-
ipv4Subnet: try? DefaultsStore.getOptional(key: .defaultSubnet).map { try CIDRv4($0) },
320-
ipv6Subnet: try? DefaultsStore.getOptional(key: .defaultIPv6Subnet).map { try CIDRv6($0) },
332+
ipv4Subnet: containerSystemConfig.network.subnet,
333+
ipv6Subnet: containerSystemConfig.network.subnetv6,
321334
labels: try .init([ResourceLabelKeys.role: ResourceRoleValues.builtin]),
322335
pluginInfo: NetworkPluginInfo(plugin: "container-network-vmnet")
323336
)

Sources/ContainerBuild/BuildImageResolver.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
import ContainerAPIClient
18+
import ContainerPersistence
1819
import Containerization
1920
import ContainerizationOCI
2021
import Foundation
@@ -27,12 +28,16 @@ struct BuildImageResolver: BuildPipelineHandler {
2728
let quiet: Bool
2829
let output: FileHandle
2930
let pull: Bool
31+
let containerSystemConfig: ContainerSystemConfig
3032

31-
public init(_ contentStore: ContentStore, quiet: Bool = false, output: FileHandle = FileHandle.standardError, pull: Bool = false) throws {
33+
public init(_ contentStore: ContentStore, quiet: Bool = false, output: FileHandle = FileHandle.standardError, pull: Bool = false, containerSystemConfig: ContainerSystemConfig)
34+
throws
35+
{
3236
self.contentStore = contentStore
3337
self.quiet = quiet
3438
self.output = output
3539
self.pull = pull
40+
self.containerSystemConfig = containerSystemConfig
3641
}
3742

3843
func accept(_ packet: ServerStream) throws -> Bool {
@@ -75,10 +80,10 @@ struct BuildImageResolver: BuildPipelineHandler {
7580
progress.start()
7681

7782
if self.pull {
78-
return try await ClientImage.pull(reference: ref, platform: platform, progressUpdate: progress.handler)
83+
return try await ClientImage.pull(reference: ref, platform: platform, containerSystemConfig: containerSystemConfig, progressUpdate: progress.handler)
7984
}
8085
// Use fetch() which checks cache first, then pulls if needed
81-
return try await ClientImage.fetch(reference: ref, platform: platform, progressUpdate: progress.handler)
86+
return try await ClientImage.fetch(reference: ref, platform: platform, containerSystemConfig: containerSystemConfig, progressUpdate: progress.handler)
8287
}()
8388

8489
let index: Index = try await img.index()

Sources/ContainerBuild/BuildPipelineHandler.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ public actor BuildPipeline {
3030
[
3131
try BuildFSSync(URL(filePath: config.contextDir)),
3232
try BuildRemoteContentProxy(config.contentStore),
33-
try BuildImageResolver(config.contentStore, quiet: config.quiet, output: config.terminal?.handle ?? FileHandle.standardError, pull: config.pull),
33+
try BuildImageResolver(
34+
config.contentStore,
35+
quiet: config.quiet,
36+
output: config.terminal?.handle ?? FileHandle.standardError,
37+
pull: config.pull,
38+
containerSystemConfig: config.containerSystemConfig
39+
),
3440
try BuildStdio(quiet: config.quiet, output: config.terminal?.handle ?? FileHandle.standardError),
3541
]
3642
}

Sources/ContainerBuild/Builder.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
import ContainerAPIClient
18+
import ContainerPersistence
1819
import Containerization
1920
import ContainerizationOCI
2021
import ContainerizationOS
@@ -279,6 +280,7 @@ public struct Builder: Sendable {
279280
public let cacheIn: [String]
280281
public let cacheOut: [String]
281282
public let pull: Bool
283+
public let containerSystemConfig: ContainerSystemConfig
282284

283285
public init(
284286
buildID: String,
@@ -298,7 +300,8 @@ public struct Builder: Sendable {
298300
exports: [BuildExport],
299301
cacheIn: [String],
300302
cacheOut: [String],
301-
pull: Bool
303+
pull: Bool,
304+
containerSystemConfig: ContainerSystemConfig
302305
) {
303306
self.buildID = buildID
304307
self.contentStore = contentStore
@@ -318,6 +321,7 @@ public struct Builder: Sendable {
318321
self.cacheIn = cacheIn
319322
self.cacheOut = cacheOut
320323
self.pull = pull
324+
self.containerSystemConfig = containerSystemConfig
321325
}
322326
}
323327

Sources/ContainerCommands/BuildCommand.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import ArgumentParser
1818
import ContainerAPIClient
1919
import ContainerBuild
2020
import ContainerImagesServiceClient
21+
import ContainerPersistence
22+
import ContainerPlugin
2123
import Containerization
2224
import ContainerizationError
2325
import ContainerizationOCI
@@ -147,6 +149,9 @@ extension Application {
147149
var pull: Bool = false
148150

149151
public func run() async throws {
152+
let containerSystemConfig: ContainerSystemConfig = try SystemRuntimeOptions.loadConfig(
153+
configFile: SystemRuntimeOptions.configFileFromAppRoot(ApplicationRoot.url)
154+
)
150155
do {
151156
let timeout: Duration = .seconds(300)
152157
let progressConfig = try ProgressConfig(
@@ -190,7 +195,8 @@ extension Application {
190195
memory: memory,
191196
log: log,
192197
dnsNameservers: dnsNameservers,
193-
progressUpdate: progress.handler
198+
progressUpdate: progress.handler,
199+
containerSystemConfig: containerSystemConfig,
194200
)
195201

196202
// wait (seconds) for builder to start listening on vsock
@@ -330,7 +336,9 @@ extension Application {
330336
return results
331337
}()
332338
group.addTask {
333-
[terminal, buildArg, secretsData, contextDir, ignoreFileData, label, noCache, target, quiet, cacheIn, cacheOut, pull, exports, imageNames, tempURL, log] in
339+
[
340+
terminal, buildArg, secretsData, contextDir, ignoreFileData, label, noCache, target, quiet, cacheIn, cacheOut, pull, exports, imageNames, tempURL, log,
341+
] in
334342
let config = Builder.BuildConfig(
335343
buildID: buildID,
336344
contentStore: RemoteContentStoreClient(),
@@ -349,7 +357,8 @@ extension Application {
349357
exports: exports,
350358
cacheIn: cacheIn,
351359
cacheOut: cacheOut,
352-
pull: pull
360+
pull: pull,
361+
containerSystemConfig: containerSystemConfig,
353362
)
354363
progress.finish()
355364

0 commit comments

Comments
 (0)