Skip to content

Commit cec124f

Browse files
authored
refactor: extract K8s logic into ContainerK8s library target (#2079)
- Closes #2078. - Move all K8s sources from the container-k8s executable into a new ContainerK8s library target. Sources/Plugins/K8s/ becomes a thin entry point (K8sMain.swift) that calls K8sCommand.main().
1 parent abff418 commit cec124f

19 files changed

Lines changed: 92 additions & 39 deletions

Package.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ let package = Package(
5252
.library(name: "TerminalProgress", targets: ["TerminalProgress"]),
5353
.library(name: "MachineAPIClient", targets: ["MachineAPIClient"]),
5454
.library(name: "MachineAPIService", targets: ["MachineAPIService"]),
55+
.library(name: "ContainerK8s", targets: ["ContainerK8s"]),
5556
],
5657
dependencies: [
5758
.package(url: "https://github.com/apple/containerization.git", exact: Version(stringLiteral: scVersion)),
@@ -167,19 +168,21 @@ let package = Package(
167168
.testTarget(
168169
name: "K8sTests",
169170
dependencies: [
170-
"k8s",
171+
"ContainerK8s",
171172
"ContainerResource",
172173
"Yams",
173174
],
174175
path: "Tests/K8sPluginTests"
175176
),
176-
.executableTarget(
177-
name: "k8s",
177+
.target(
178+
name: "ContainerK8s",
178179
dependencies: [
179180
.product(name: "ArgumentParser", package: "swift-argument-parser"),
180181
.product(name: "Logging", package: "swift-log"),
181182
.product(name: "Containerization", package: "containerization"),
182183
.product(name: "ContainerizationOCI", package: "containerization"),
184+
.product(name: "ContainerizationOS", package: "containerization"),
185+
.product(name: "SystemPackage", package: "swift-system"),
183186
"ContainerAPIClient",
184187
"ContainerLog",
185188
"ContainerPersistence",
@@ -188,10 +191,14 @@ let package = Package(
188191
"TerminalProgress",
189192
"Yams",
190193
],
191-
path: "Sources/Plugins/K8s",
192-
exclude: ["config.toml"],
193194
resources: [.process("Resources/kindnet.yaml")]
194195
),
196+
.executableTarget(
197+
name: "k8s",
198+
dependencies: ["ContainerK8s"],
199+
path: "Sources/Plugins/K8s",
200+
exclude: ["config.toml"]
201+
),
195202
.executableTarget(
196203
name: "container-apiserver",
197204
dependencies: [

Sources/Plugins/K8s/K8sCreate.swift renamed to Sources/ContainerK8s/Commands/K8sCreate.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ import Foundation
2525
import Logging
2626
import TerminalProgress
2727

28-
struct K8sCreate: AsyncParsableCommand {
29-
static let configuration = CommandConfiguration(
28+
public struct K8sCreate: AsyncParsableCommand {
29+
public init() {}
30+
31+
public static let configuration = CommandConfiguration(
3032
commandName: "create",
3133
abstract: "Create and start a local Kubernetes cluster"
3234
)
@@ -49,7 +51,7 @@ struct K8sCreate: AsyncParsableCommand {
4951
@Option(help: "Node image reference (default: \(K8sHelper.nodeImage))")
5052
var nodeImage: String = K8sHelper.nodeImage
5153

52-
func run() async throws {
54+
public func run() async throws {
5355
LoggingSystem.bootstrap { _ in StderrLogHandler() }
5456
let log = Logger(label: K8sHelper.pluginName)
5557

Sources/Plugins/K8s/K8sDelete.swift renamed to Sources/ContainerK8s/Commands/K8sDelete.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ import ContainerResource
2121
import ContainerizationError
2222
import Logging
2323

24-
struct K8sDelete: AsyncParsableCommand {
25-
static let configuration = CommandConfiguration(
24+
public struct K8sDelete: AsyncParsableCommand {
25+
public init() {}
26+
27+
public static let configuration = CommandConfiguration(
2628
commandName: "delete",
2729
abstract: "Delete a Kubernetes cluster",
2830
aliases: ["rm"]
@@ -31,7 +33,7 @@ struct K8sDelete: AsyncParsableCommand {
3133
@Option(name: .long, help: "Cluster name (default: \(K8sHelper.defaultName))")
3234
var name: String = K8sHelper.defaultName
3335

34-
func run() async throws {
36+
public func run() async throws {
3537
LoggingSystem.bootstrap { _ in StderrLogHandler() }
3638
let log = Logger(label: K8sHelper.pluginName)
3739

Sources/Plugins/K8s/K8sList.swift renamed to Sources/ContainerK8s/Commands/K8sList.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ import ContainerLog
2020
import ContainerResource
2121
import Logging
2222

23-
struct K8sList: AsyncParsableCommand {
24-
static let configuration = CommandConfiguration(
23+
public struct K8sList: AsyncParsableCommand {
24+
public init() {}
25+
26+
public static let configuration = CommandConfiguration(
2527
commandName: "list",
2628
abstract: "List clusters and their nodes",
2729
aliases: ["ls"]
2830
)
2931

30-
func run() async throws {
32+
public func run() async throws {
3133
LoggingSystem.bootstrap { _ in StderrLogHandler() }
3234

3335
let snapshots = try await ContainerClient().list(

Sources/Plugins/K8s/K8sLoadImage.swift renamed to Sources/ContainerK8s/Commands/K8sLoadImage.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ import Foundation
2525
import Logging
2626
import SystemPackage
2727

28-
struct K8sLoadImage: AsyncParsableCommand {
28+
public struct K8sLoadImage: AsyncParsableCommand {
29+
public init() {}
30+
2931
private static let ctrPath = "/usr/local/bin/ctr"
3032

31-
static let configuration = CommandConfiguration(
33+
public static let configuration = CommandConfiguration(
3234
commandName: "load-image",
3335
abstract: "Load a container image into a cluster's containerd"
3436
)
@@ -44,7 +46,7 @@ struct K8sLoadImage: AsyncParsableCommand {
4446
)
4547
var platform: String?
4648

47-
func run() async throws {
49+
public func run() async throws {
4850
LoggingSystem.bootstrap { _ in StderrLogHandler() }
4951
let log = Logger(label: K8sHelper.pluginName)
5052

Sources/Plugins/K8s/K8sStart.swift renamed to Sources/ContainerK8s/Commands/K8sStart.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@ import ContainerizationError
2222
import Foundation
2323
import Logging
2424

25-
struct K8sStart: AsyncParsableCommand {
26-
static let configuration = CommandConfiguration(
25+
public struct K8sStart: AsyncParsableCommand {
26+
public init() {}
27+
28+
public static let configuration = CommandConfiguration(
2729
commandName: "start",
2830
abstract: "Start a stopped Kubernetes cluster"
2931
)
3032

3133
@Option(name: .long, help: "Cluster name (default: \(K8sHelper.defaultName))")
3234
var name: String = K8sHelper.defaultName
3335

34-
func run() async throws {
36+
public func run() async throws {
3537
LoggingSystem.bootstrap { _ in StderrLogHandler() }
3638
let log = Logger(label: K8sHelper.pluginName)
3739

Sources/Plugins/K8s/K8sWriteConfig.swift renamed to Sources/ContainerK8s/Commands/K8sWriteConfig.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ import Foundation
2121
import Logging
2222
import SystemPackage
2323

24-
struct K8sWriteConfig: AsyncParsableCommand {
25-
static let configuration = CommandConfiguration(
24+
public struct K8sWriteConfig: AsyncParsableCommand {
25+
public init() {}
26+
27+
public static let configuration = CommandConfiguration(
2628
commandName: "write-config",
2729
abstract: "Write the cluster context to a Kubernetes configuration file"
2830
)
@@ -33,7 +35,7 @@ struct K8sWriteConfig: AsyncParsableCommand {
3335
@Option(name: .long, help: "Path to the kubeconfig file to write or append to (default: ~/.kube/config)")
3436
var kubeconfig: String?
3537

36-
func run() async throws {
38+
public func run() async throws {
3739
LoggingSystem.bootstrap { _ in StderrLogHandler() }
3840
let log = Logger(label: K8sHelper.pluginName)
3941

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ struct K8sHelper {
199199
executable: executable, arguments: arguments, environment: [], terminal: false)
200200
let proc = try await client.createProcess(
201201
containerId: containerId, processId: UUID().uuidString.lowercased(),
202-
configuration: config, stdio: [nil, pipe.fileHandleForWriting, nil])
202+
configuration: config, stdio: [nil, pipe.fileHandleForWriting, pipe.fileHandleForWriting])
203203
try await proc.start()
204204
pipe.fileHandleForWriting.closeFile()
205205
let data = pipe.fileHandleForReading.readDataToEndOfFile()
@@ -291,8 +291,8 @@ struct K8sHelper {
291291
sysctl -w net.bridge.bridge-nf-call-ip6tables=1 2>/dev/null || true
292292
systemctl restart containerd
293293
ctr -n k8s.io images tag registry.k8s.io/pause:3.10 registry.k8s.io/pause:3.10.1 2>/dev/null || true
294-
iptables -t mangle -A OUTPUT -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1220
295-
iptables -t mangle -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1220
294+
/usr/sbin/iptables-nft -t mangle -A OUTPUT -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1220
295+
/usr/sbin/iptables-nft -t mangle -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1220
296296
"""
297297
}()
298298

@@ -538,7 +538,7 @@ struct K8sHelper {
538538
existing.clusters.append(contentsOf: config.clusters)
539539
existing.contexts.append(contentsOf: config.contexts)
540540
existing.users.append(contentsOf: config.users)
541-
if setCurrentContext && existing.currentContext == nil {
541+
if setCurrentContext {
542542
existing.currentContext = containerId
543543
}
544544

File renamed without changes.

0 commit comments

Comments
 (0)