Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Sources/ContainerK8s/Commands/K8sCreate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public struct K8sCreate: AsyncParsableCommand {
@Option(name: .long, help: "Cluster name (default: \(K8sHelper.defaultName))")
var name: String = K8sHelper.defaultName

@Option(name: .customLong("mount"), help: "Add a mount to the container (format: type=<>,source=<>,target=<>,readonly)")
var mounts: [String] = []

@Flag(name: [.customLong("rm"), .long], help: "Remove the cluster container after it stops")
var remove: Bool = false

Expand All @@ -51,6 +54,7 @@ public struct K8sCreate: AsyncParsableCommand {
@Option(help: "Node image reference (default: \(K8sHelper.nodeImage))")
var nodeImage: String = K8sHelper.nodeImage


public func run() async throws {
LoggingSystem.bootstrap { _ in StderrLogHandler() }
let log = Logger(label: K8sHelper.pluginName)
Expand Down Expand Up @@ -86,7 +90,8 @@ public struct K8sCreate: AsyncParsableCommand {
registryScheme: registryFlags.scheme,
maxConcurrentDownloads: imageFetchFlags.maxConcurrentDownloads,
remove: remove,
fqdn: fqdn
fqdn: fqdn,
mounts: mounts
)

progress.set(description: "Starting cluster")
Expand Down
7 changes: 5 additions & 2 deletions Sources/ContainerK8s/Provisioners/LinuxNodeProvisioner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public struct LinuxNodeProvisioner: NodeProvisioner {
private let maxConcurrentDownloads: Int
private let remove: Bool
private let fqdn: String?
private let mounts: [String]

public init(
clusterName: String,
Expand All @@ -42,7 +43,8 @@ public struct LinuxNodeProvisioner: NodeProvisioner {
registryScheme: String = "https",
maxConcurrentDownloads: Int = 3,
remove: Bool = false,
fqdn: String? = nil
fqdn: String? = nil,
mounts: [String] = []
) throws {
guard !roles.isEmpty else {
throw ContainerizationError(.invalidArgument, message: "LinuxNode roles must not be empty")
Expand All @@ -63,6 +65,7 @@ public struct LinuxNodeProvisioner: NodeProvisioner {
self.maxConcurrentDownloads = maxConcurrentDownloads
self.remove = remove
self.fqdn = fqdn
self.mounts = mounts
}

public func provision(name: String, log: Logger) async throws {
Expand Down Expand Up @@ -90,7 +93,7 @@ public struct LinuxNodeProvisioner: NodeProvisioner {
"\(ResourceLabelKeys.role)=\(roles.joined(separator: ","))",
],
maskedPaths: [],
mounts: [],
mounts: mounts,
name: name,
networks: [],
os: "linux",
Expand Down
29 changes: 29 additions & 0 deletions Tests/IntegrationTests/K8s/TestK8sRunSerial.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,33 @@ struct TestK8sRunSerial {
#expect(server1 != server2)
}
}

@Test func testCreateWithMount() async throws {
try await ContainerFixture.with { f in
let name = "k8s-\(f.testID)"
let testData = "hello k8s mount"
let hostFile = f.testDir.appending("testfile.txt")
try testData.write(toFile: hostFile.string, atomically: true, encoding: .utf8)

f.addCleanup { _ = try? f.run(["k8s", "delete", "--name", name]) }

try f.restoreWarmupImage(.kindestNodeV1_35_5)
print("[k8s-run] k8s create --name \(name) --mount type=virtiofs,source=\(f.testDir.string),target=/tmp/testmount,readonly")
let result = try f.run([
"k8s", "create",
"--name", name,
"--mount", "type=virtiofs,source=\(f.testDir.string),target=/tmp/testmount,readonly",
])
print("[k8s-run] k8s create exit=\(result.status)")
if result.status != 0 {
print("[k8s-run] k8s create stderr: \(result.error)")
f.dumpNodeDiagnostics(node: name)
}
try result.check()

let output = try f.doExec(name, cmd: ["cat", "/tmp/testmount/testfile.txt"])
.trimmingCharacters(in: .whitespacesAndNewlines)
#expect(output == testData)
}
}
}
7 changes: 4 additions & 3 deletions docs/command-reference.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Container CLI Command Reference

> [!IMPORTANT]
> This file contains documentation for the CURRENT BRANCH. To find documentation for official releases, find the target release on the [Release Page](https://github.com/apple/container/releases) and click the tag corresponding to your release version.
> This file contains documentation for the CURRENT BRANCH. To find documentation for official releases, find the target release on the [Release Page](https://github.com/apple/container/releases) and click the tag corresponding to your release version.
>
> Example: [release 0.4.1 tag](https://github.com/apple/container/tree/0.4.1)

Expand Down Expand Up @@ -246,7 +246,7 @@ container create [<options>] <image> [<arguments> ...]
* `--read-only-path <path>`: **Experimental.** Mark a path inside the container read-only, in addition to the runtime defaults (or `NONE` to clear prior values and the defaults)
* `--rm, --remove`: Remove the container after it stops
* `--rosetta`: Enable Rosetta in the container
* `--runtime`: Set the runtime handler for the container (default: container-runtime-linux)
* `--runtime`: Set the runtime handler for the container (default: container-runtime-linux)
* `--ssh`: Forward SSH agent socket to container
* `--shm-size <shm-size>`: Size of `/dev/shm` (e.g. 64M, 1G)
* `--tmpfs <tmpfs>`: Add a tmpfs mount to the container at the given path
Expand Down Expand Up @@ -1586,11 +1586,12 @@ Creates and starts a local Kubernetes cluster. Pulls the node image if needed, r
**Usage**

```bash
container k8s create [--name <name>] [--node-image <image>] [--rm] [<resource options>] [--debug]
container k8s create [--name <name>] [--node-image <image>] [--mount <mount>] [--rm] [<resource options>] [--debug]
```

**Options**

* `--mount <mount>`: Add a mount to the container (format: type=<>,source=<>,target=<>,readonly)
* `--name <name>`: Cluster name (default: `k8s-dev`)
* `--node-image <image>`: Node image reference (default: `docker.io/kindest/node:v1.35.5`)
* `--rm`: Remove the cluster container after it stops
Expand Down