Skip to content

Commit f7a1f8f

Browse files
committed
Move to TOML configuration for defaults
This change migrates away from using `UserDefaults`, instead providing a TOML configuration mechanism for user configurable settings. Users will have to migrate any custom defaults configured via `UserDefaults` into a toml configuration file in `~/.config/container/`. - Addresses discussion #1336
1 parent 49f1a56 commit f7a1f8f

57 files changed

Lines changed: 1110 additions & 981 deletions

File tree

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+
[image]
87+
initImage = "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 `[image]` 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+
[image]
144+
builder = "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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ let package = Package(
7777
.product(name: "ContainerizationArchive", package: "containerization"),
7878
.product(name: "ContainerizationExtras", package: "containerization"),
7979
.product(name: "ContainerizationOS", package: "containerization"),
80+
.product(name: "TOML", package: "swift-toml"),
8081
"ContainerBuild",
8182
"ContainerLog",
83+
"ContainerPersistence",
8284
"ContainerResource",
8385
],
8486
path: "Tests/CLITests"
@@ -89,6 +91,7 @@ let package = Package(
8991
.product(name: "ArgumentParser", package: "swift-argument-parser"),
9092
.product(name: "Logging", package: "swift-log"),
9193
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
94+
.product(name: "TOML", package: "swift-toml"),
9295
.product(name: "Containerization", package: "containerization"),
9396
.product(name: "ContainerizationOCI", package: "containerization"),
9497
.product(name: "ContainerizationOS", package: "containerization"),
@@ -215,6 +218,7 @@ let package = Package(
215218
.product(name: "SystemPackage", package: "swift-system"),
216219
"ContainerImagesService",
217220
"ContainerLog",
221+
"ContainerPersistence",
218222
"ContainerPlugin",
219223
"ContainerVersion",
220224
"ContainerXPC",
@@ -263,6 +267,7 @@ let package = Package(
263267
"ContainerLog",
264268
"ContainerNetworkService",
265269
"ContainerNetworkServiceClient",
270+
"ContainerPersistence",
266271
"ContainerPlugin",
267272
"ContainerResource",
268273
"ContainerVersion",
@@ -379,6 +384,8 @@ let package = Package(
379384
dependencies: [
380385
.product(name: "Logging", package: "swift-log"),
381386
.product(name: "Containerization", package: "containerization"),
387+
.product(name: "SystemPackage", package: "swift-system"),
388+
.product(name: "TOML", package: "swift-toml"),
382389
"CVersion",
383390
"ContainerVersion",
384391
]

Sources/APIServer/APIServer+Start.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import ContainerAPIClient
1919
import ContainerAPIService
2020
import ContainerLog
2121
import ContainerNetworkService
22+
import ContainerPersistence
2223
import ContainerPlugin
2324
import ContainerResource
2425
import ContainerXPC
@@ -37,6 +38,9 @@ extension APIServer {
3738
static let listenAddress = "127.0.0.1"
3839
static let localhostDNSPort = 1053
3940
static let dnsPort = 2053
41+
private static let containerSystemConfig: ContainerSystemConfig = try! SystemRuntimeOptions.loadConfig(
42+
configFile: SystemRuntimeOptions.configFileFromAppRoot(ApplicationRoot.url)
43+
)
4044

4145
@Flag(name: .long, help: "Enable debug logging")
4246
var debug = false
@@ -60,6 +64,7 @@ extension APIServer {
6064
log.info("configuring XPC server")
6165
var routes = [XPCRoute: XPCServer.RouteHandler]()
6266
let pluginLoader = try initializePluginLoader(log: log)
67+
6368
try await initializePlugins(pluginLoader: pluginLoader, log: log, routes: &routes)
6469
let containersService = try initializeContainersService(
6570
pluginLoader: pluginLoader,
@@ -261,12 +266,17 @@ extension APIServer {
261266
routes[XPCRoute.getDefaultKernel] = harness.getDefaultKernel
262267
}
263268

264-
private func initializeContainersService(pluginLoader: PluginLoader, log: Logger, routes: inout [XPCRoute: XPCServer.RouteHandler]) throws -> ContainersService {
269+
private func initializeContainersService(
270+
pluginLoader: PluginLoader,
271+
log: Logger,
272+
routes: inout [XPCRoute: XPCServer.RouteHandler]
273+
) throws -> ContainersService {
265274
log.info("initializing containers service")
266275

267276
let service = try ContainersService(
268277
appRoot: appRoot,
269278
pluginLoader: pluginLoader,
279+
containerSystemConfig: Self.containerSystemConfig,
270280
log: log,
271281
debugHelpers: debug
272282
)

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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ 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, quiet: config.quiet, output: config.terminal?.handle ?? FileHandle.standardError, pull: config.pull,
35+
containerSystemConfig: config.containerSystemConfig),
3436
try BuildStdio(quiet: config.quiet, output: config.terminal?.handle ?? FileHandle.standardError),
3537
]
3638
}

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
@@ -254,6 +255,7 @@ public struct Builder: Sendable {
254255
public let cacheIn: [String]
255256
public let cacheOut: [String]
256257
public let pull: Bool
258+
public let containerSystemConfig: ContainerSystemConfig
257259

258260
public init(
259261
buildID: String,
@@ -273,7 +275,8 @@ public struct Builder: Sendable {
273275
exports: [BuildExport],
274276
cacheIn: [String],
275277
cacheOut: [String],
276-
pull: Bool
278+
pull: Bool,
279+
containerSystemConfig: ContainerSystemConfig
277280
) {
278281
self.buildID = buildID
279282
self.contentStore = contentStore
@@ -293,6 +296,7 @@ public struct Builder: Sendable {
293296
self.cacheIn = cacheIn
294297
self.cacheOut = cacheOut
295298
self.pull = pull
299+
self.containerSystemConfig = containerSystemConfig
296300
}
297301
}
298302
}

Sources/ContainerCommands/BuildCommand.swift

Lines changed: 13 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
@@ -40,6 +42,10 @@ extension Application {
4042
return config
4143
}
4244

45+
private static let containerSystemConfig: ContainerSystemConfig = try! SystemRuntimeOptions.loadConfig(
46+
configFile: SystemRuntimeOptions.configFileFromAppRoot(ApplicationRoot.url)
47+
)
48+
4349
enum ProgressType: String, ExpressibleByArgument {
4450
case auto
4551
case plain
@@ -192,7 +198,8 @@ extension Application {
192198
memory: memory,
193199
log: log,
194200
dnsNameservers: dnsNameservers,
195-
progressUpdate: progress.handler
201+
progressUpdate: progress.handler,
202+
containerSystemConfig: Self.containerSystemConfig,
196203
)
197204

198205
// wait (seconds) for builder to start listening on vsock
@@ -351,7 +358,9 @@ extension Application {
351358
return results
352359
}()
353360
group.addTask {
354-
[terminal, buildArg, secretsData, contextDir, hiddenDockerDir, label, noCache, target, quiet, cacheIn, cacheOut, pull, exports, imageNames, tempURL, log] in
361+
[
362+
terminal, buildArg, secretsData, contextDir, hiddenDockerDir, label, noCache, target, quiet, cacheIn, cacheOut, pull, exports, imageNames, tempURL, log,
363+
] in
355364
let config = Builder.BuildConfig(
356365
buildID: buildID,
357366
contentStore: RemoteContentStoreClient(),
@@ -370,7 +379,8 @@ extension Application {
370379
exports: exports,
371380
cacheIn: cacheIn,
372381
cacheOut: cacheOut,
373-
pull: pull
382+
pull: pull,
383+
containerSystemConfig: Self.containerSystemConfig,
374384
)
375385
progress.finish()
376386

0 commit comments

Comments
 (0)